static char* dupString(const char* s) { char* d; if ( ! s ) s = ""; d = (char*)comdCalloc((strlen(s)+1),sizeof(char)); strcpy(d, s); return d; }
static MyOption* myOptionAlloc( const char* longOption, const char shortOption, int has_arg, const char type, void* dataPtr, int dataSize, const char* help) { static int iBase=129; MyOption* o = (MyOption*)comdCalloc(1, sizeof(MyOption)); o->help = dupString(help); o->longArg = dupString(longOption); if(shortOption) o->shortArg[0] = (unsigned char)shortOption; else { o->shortArg[0] = iBase; iBase++; } o->argFlag = has_arg; o->type = type; o->ptr = dataPtr; o->sz = dataSize; if(longOption) longest = (longest>strlen(longOption)?longest:strlen(longOption)); return o; }
/// Builds a structure to store interpolation data for a tabular /// function. Interpolation must be supported on the range /// \f$[x_0, x_n]\f$, where \f$x_n = n*dx\f$. /// /// \see interpolate /// \see bcastInterpolationObject /// \see destroyInterpolationObject /// /// \param [in] n number of values in the table. /// \param [in] x0 minimum ordinate value of the table. /// \param [in] dx spacing of the ordinate values. /// \param [in] data abscissa values. An array of size n. InterpolationObject* initInterpolationObject( int n, real_t x0, real_t dx, real_t* data) { InterpolationObject* table = (InterpolationObject *)comdMalloc(sizeof(InterpolationObject)) ; assert(table); table->values = (real_t*)comdCalloc(1, (n+3)*sizeof(real_t)); assert(table->values); table->values++; table->n = n; table->invDx = 1.0/dx; table->x0 = x0; for (int ii=0; ii<n; ++ii) table->values[ii] = data[ii]; table->values[-1] = table->values[0]; table->values[n+1] = table->values[n] = table->values[n-1]; return table; }
void processArgs(int argc, char** argv) { MyOption* o; int n=0; int i; struct option* opts; char* sArgs; int c; if ( ! myargs) return; o = myargs; while(o) {n++,o=nextOption(o);} o = myargs; sArgs= (char*)comdCalloc(2*(n+2),sizeof(char)); opts = (struct option*)comdCalloc(n,sizeof(struct option)); for (i=0; i<n; i++) { opts[i].name = o->longArg; opts[i].has_arg = o->argFlag; opts[i].flag = 0; opts[i].val = o->shortArg[0]; strcat(sArgs,(char*) o->shortArg); if(o->argFlag) strcat(sArgs,":"); o = nextOption(o); } while(1) { int option_index = 0; c = getopt_long (argc, argv, sArgs, opts, &option_index); if ( c == -1) break; o = findOption(myargs,c); if ( ! o ) { fprintf(screenOut,"\n\n" " invalid switch : -%c in getopt()\n" "\n\n", c); break; } if(! o->argFlag) { int* i = (int*)o->ptr; *i = 1; } else { switch(o->type) { case 'i': sscanf(optarg,"%d",(int*)o->ptr); break; case 'f': sscanf(optarg,"%f",(float*)o->ptr); break; case 'd': sscanf(optarg,"%lf",(double*)o->ptr); break; case 's': strncpy((char*)o->ptr,(char*)optarg,o->sz); ((char*)o->ptr)[o->sz-1] = '\0'; break; case 'c': sscanf(optarg,"%c",(char*)o->ptr); break; default: fprintf(screenOut,"\n\n" " invalid type : %c in getopt()\n" " valid values are 'e', 'z'. 'i','d','f','s', and 'c'\n" "\n\n", c); } } } free(opts); free(sArgs); return; }