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