Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | mjames | 1 | /* |
2 | * Miscellaneous support functions for nec2c.c |
||
3 | */ |
||
4 | |||
5 | #include "nec2c.h" |
||
6 | //#include "windows.h" |
||
7 | |||
8 | /* pointers to input/output files */ |
||
9 | extern FILE *input_fp, *output_fp, *plot_fp; |
||
10 | |||
11 | /*------------------------------------------------------------------------*/ |
||
12 | |||
13 | /* usage() |
||
14 | * |
||
15 | * prints usage information |
||
16 | */ |
||
17 | |||
18 | void usage (void) |
||
19 | { |
||
20 | fprintf ( |
||
21 | stderr, |
||
22 | "usage: nec2c [-i<input-file-name>] [-o<output-file-name>]" |
||
23 | "\n -h: print this usage information and exit." |
||
24 | "\n -v: print nec2c version number and exit.\n"); |
||
25 | |||
26 | } /* end of usage() */ |
||
27 | |||
28 | /*------------------------------------------------------------------------*/ |
||
29 | |||
30 | /* abort_on_error() |
||
31 | * |
||
32 | * prints an error message and exits |
||
33 | */ |
||
34 | |||
35 | void abort_on_error (int why) |
||
36 | { |
||
37 | switch (why) |
||
38 | { |
||
39 | case -1: /* abort if input file name too long */ |
||
40 | fprintf (stderr, "%s\n", "nec2c: Input file name too long - aborting"); |
||
41 | break; |
||
42 | |||
43 | case -2: /* abort if output file name too long */ |
||
44 | fprintf (stderr, "%s\n", "nec2c: Output file name too long - aborting"); |
||
45 | break; |
||
46 | |||
47 | case -3: /* abort on input file read error */ |
||
48 | fprintf (stderr, "%s\n", "nec2c: Error reading input file - aborting"); |
||
49 | break; |
||
50 | |||
51 | case -4: /* Abort on malloc failure */ |
||
52 | fprintf ( |
||
53 | stderr, |
||
54 | "%s\n", |
||
55 | "nec2c: A memory allocation request has failed - aborting"); |
||
56 | break; |
||
57 | |||
58 | case -5: /* Abort if a GF card is read */ |
||
59 | fprintf ( |
||
60 | stderr, "%s\n", "nec2c: NGF solution option not supported - aborting"); |
||
61 | break; |
||
62 | |||
63 | case -6: /* No convergence in gshank() */ |
||
64 | fprintf (stderr, "%s\n", "nec2c: No convergence in gshank() - aborting"); |
||
65 | break; |
||
66 | |||
67 | case -7: /* Error in hankel() */ |
||
68 | fprintf (stderr, "%s\n", "nec2c: Hankel not valid for z=0. - aborting"); |
||
69 | |||
70 | } /* switch( why ) */ |
||
71 | |||
72 | /* clean up and quit */ |
||
73 | stop (why); |
||
74 | |||
75 | } /* end of abort_on_error() */ |
||
76 | |||
77 | /*------------------------------------------------------------------------*/ |
||
78 | |||
79 | /* Returns process time (user+system) BUT in _msec_ */ |
||
80 | void secnds (double *x) |
||
81 | { |
||
82 | #if 0 |
||
83 | struct tms buffer; |
||
84 | |||
85 | times(&buffer); |
||
86 | *x = 1000. * ( (double)(buffer.tms_utime + buffer.tms_stime) ) / |
||
87 | ( (double) sysconf(_SC_CLK_TCK) ); |
||
88 | #else |
||
89 | SYSTEMTIME time; |
||
90 | GetSystemTime (&time); |
||
91 | *x = 1000.0 * time.wSecond + time.wMilliseconds; |
||
92 | |||
93 | #endif |
||
94 | |||
95 | return; |
||
96 | } |
||
97 | |||
98 | /*------------------------------------------------------------------------*/ |
||
99 | |||
100 | /* Does the STOP function of fortran but with return value */ |
||
101 | int stop (int flag) |
||
102 | { |
||
103 | if (input_fp != NULL) |
||
104 | fclose (input_fp); |
||
105 | if (output_fp != NULL) |
||
106 | fclose (output_fp); |
||
107 | if (plot_fp != NULL) |
||
108 | fclose (plot_fp); |
||
109 | |||
110 | exit (flag); |
||
111 | } |
||
112 | |||
113 | /*------------------------------------------------------------------*/ |
||
114 | |||
115 | /* load_line() |
||
116 | * |
||
117 | * loads a line from a file, aborts on failure. lines beginning |
||
118 | * with a '#' are ignored as comments. at the end of file EOF is |
||
119 | * returned. |
||
120 | */ |
||
121 | |||
122 | int load_line (char *buff, FILE *pfile) |
||
123 | { |
||
124 | int num_chr, /* number of characters read, excluding lf/cr */ |
||
125 | eof = 0, /* EOF flag */ |
||
126 | chr; /* character read by getc */ |
||
127 | |||
128 | num_chr = 0; |
||
129 | |||
130 | /* clear buffer at start */ |
||
131 | buff[0] = '\0'; |
||
132 | |||
133 | /* ignore commented lines, white spaces and eol/cr */ |
||
134 | if ((chr = fgetc (pfile)) == EOF) |
||
135 | return (EOF); |
||
136 | |||
137 | while ((chr == '#') || (chr == ' ') || (chr == CR) || (chr == LF)) |
||
138 | { |
||
139 | /* go to the end of line (look for lf or cr) */ |
||
140 | while ((chr != CR) && (chr != LF)) |
||
141 | if ((chr = fgetc (pfile)) == EOF) |
||
142 | return (EOF); |
||
143 | |||
144 | /* dump any cr/lf remaining */ |
||
145 | while ((chr == CR) || (chr == LF)) |
||
146 | if ((chr = fgetc (pfile)) == EOF) |
||
147 | return (EOF); |
||
148 | |||
149 | } /* end of while( (chr == '#') || ... */ |
||
150 | |||
151 | while (num_chr < LINE_LEN) |
||
152 | { |
||
153 | /* if lf/cr reached before filling buffer, return */ |
||
154 | if ((chr == CR) || (chr == LF)) |
||
155 | break; |
||
156 | |||
157 | /* enter new char to buffer */ |
||
158 | buff[num_chr++] = chr; |
||
159 | |||
160 | /* terminate buffer as a string on EOF */ |
||
161 | if ((chr = fgetc (pfile)) == EOF) |
||
162 | { |
||
163 | buff[num_chr] = '\0'; |
||
164 | eof = EOF; |
||
165 | } |
||
166 | |||
167 | } /* end of while( num_chr < max_chr ) */ |
||
168 | |||
169 | /* Capitalize first two characters (mnemonics) */ |
||
170 | if ((buff[0] > 0x60) && (buff[0] < 0x79)) |
||
171 | buff[0] -= 0x20; |
||
172 | if ((buff[1] > 0x60) && (buff[1] < 0x79)) |
||
173 | buff[1] -= 0x20; |
||
174 | |||
175 | /* terminate buffer as a string */ |
||
176 | buff[num_chr] = '\0'; |
||
177 | |||
178 | return (eof); |
||
179 | |||
180 | } /* end of load_line() */ |
||
181 | |||
182 | /*------------------------------------------------------------------------*/ |
||
183 | |||
184 | /*** Memory allocation/freeing utils ***/ |
||
185 | |||
186 | void mem_alloc (void **ptr, long int req) |
||
187 | { |
||
188 | free_ptr (ptr); |
||
189 | *ptr = malloc (req); |
||
190 | if (*ptr == NULL) |
||
191 | abort_on_error (-4); |
||
192 | |||
193 | } /* End of void mem_alloc() */ |
||
194 | |||
195 | /*------------------------------------------------------------------------*/ |
||
196 | |||
197 | void mem_realloc (void **ptr, long int req) |
||
198 | { |
||
199 | *ptr = realloc (*ptr, req); |
||
200 | if (*ptr == NULL) |
||
201 | abort_on_error (-4); |
||
202 | |||
203 | } /* End of void mem_realloc() */ |
||
204 | |||
205 | /*------------------------------------------------------------------------*/ |
||
206 | |||
207 | void free_ptr (void **ptr) |
||
208 | { |
||
209 | if (*ptr != NULL) |
||
210 | free (*ptr); |
||
211 | *ptr = NULL; |
||
212 | |||
213 | } /* End of void free_ptr() */ |
||
214 | |||
215 | /*------------------------------------------------------------------------*/ |