char *do_get_word(double n, char *string, char *delm) { char *temp, *token, *word; int i; NEWSTR(string, temp); token = strtok(temp,delm); if( n == 1 ) { NEWSTR(token,word); free(temp); return(word); } for(i=1; i<n; i++) { if( (token = strtok(NULL,delm)) == NULL ) { free(temp); return(NULL); } } NEWSTR(token,word); free(temp); return(word); }
char* kogmo_rtdb_obj_dumpbase_str (kogmo_rtdb_handle_t *db_h, void *data_p) { kogmo_rtdb_subobj_base_t *objbase; kogmo_timestamp_string_t tstr, data_tstr; kogmo_rtdb_obj_c3_process_info_t pi; NEWSTR(buf); objbase = (kogmo_rtdb_subobj_base_t*) data_p; if ( objbase->size < (int)(sizeof(kogmo_rtdb_subobj_base_t)) ) { STRPRINTF(buf,"* NO DATA\n"); return buf; } kogmo_timestamp_to_string(objbase->committed_ts, tstr); kogmo_timestamp_to_string(objbase->data_ts, data_tstr); kogmo_rtdb_obj_c3_process_getprocessinfo (db_h, objbase->committed_proc, objbase->committed_ts, pi); STRPRINTF(buf,"* DATA:\n"); STRPRINTF(buf,"Committed: %s by %s, %lli bytes\n", tstr, pi, (long long int)objbase->size); STRPRINTF(buf,"Data created: %s\n", data_tstr); return buf; }
char *do_include_path(char *new_path) { if (ap_options.include_path != NULL) { free(ap_options.include_path); } NEWSTR(new_path, ap_options.include_path); return (NULL); }
char *do_exodus_info(char *filename) { int exoid; int size = 0; int count; char **info; int i; char *lines; char *ret_string = NULL; /* * Open the specified exodusII file, read the info records * then parse them as input to aprepro. */ exoid = open_exodus_file(filename); if (exoid < 0) return ""; count = ex_inquire_int(exoid, EX_INQ_INFO); if (count > 0) { info = (char**)malloc(count * sizeof(char*)); for (i=0; i < count; i++) { info[i] = (char*)malloc((MAX_LINE_LENGTH+1) * sizeof(char)); memset(info[i], '\0', MAX_LINE_LENGTH+1); } ex_get_info(exoid, info); /* Count total size of info records.. */ for (i=0; i<count; i++) { size += strlen(info[i]); } size += count-1; /* newlines */ lines = malloc(size * sizeof(char) + 1); if (lines) { lines[0] = '\0'; for (i=0; i<count; i++) { strcat(lines, info[i]); strcat(lines, "\n"); } NEWSTR(lines, ret_string); free(lines); } if (count > 0) { for (i=0; i < count; i++) { free(info[i]); } free(info); } ex_close(exoid); return ret_string; } else { ex_close(exoid); return ""; } }
char *do_tostring(double x) { char *tmp; static char tmpstr[128]; if (x == 0.0) { NEWSTR("0", tmp); return (tmp); } else { symrec *format; format = getsym("_FORMAT"); (void) sprintf(tmpstr, format->value.svar, x); NEWSTR(tmpstr, tmp); return (tmp); } }
char *do_get_time(void) { char *tmp; const size_t bufsize = 32; static char tmpstr[32]; time_t timer = time(NULL); struct tm *timeptr = localtime(&timer); /* Now the time in the form HH:MM:SS where 0 <= HH < 24 */ strftime(tmpstr, bufsize, "%H:%M:%S", timeptr); NEWSTR(tmpstr, tmp); return(tmp); }
char *do_get_iso_date(void) { char *tmp; const size_t bufsize = 32; static char tmpstr[32]; time_t timer = time(NULL); struct tm *timeptr = localtime(&timer); /* First the date in the form CCYY/MM/DD */ strftime(tmpstr, bufsize, "%Y%m%d", timeptr); NEWSTR(tmpstr, tmp); return(tmp); }
char* kogmo_rtdb_obj_dumphex_str (kogmo_rtdb_handle_t *db_h, void *data_p) { kogmo_rtdb_objsize_t size; int i; NEWSTR(buf); size = ( (kogmo_rtdb_subobj_base_t*) data_p )->size; STRPRINTF(buf, "* BINARY DATA:\n"); for(i=0;i<size;i++) { STRPRINTF(buf,"%02x ", ((unsigned char*)data_p)[i]); } STRPRINTF(buf,"\n"); return buf; }
char *do_intout(double intval) { /* convert 'intval' to a string using an integer format * This can be used if you need to set the default output * format to force the decimal point. In that case, integers * also have a decimal point which is usually not wanted. * Using 'intout(val)', val will be converted to a string * using an integer format */ char *tmp; static char tmpstr[128]; if (intval == 0.0) { NEWSTR("0", tmp); return (tmp); } else { (void) sprintf(tmpstr, "%d", (int)intval); NEWSTR(tmpstr, tmp); return (tmp); } }
char *do_getenv(char *env) { char *tmp; char *ret_string; if (env == NULL) return (NULL); tmp = (char *)getenv(env); if (tmp != NULL) { NEWSTR(tmp, ret_string); return (ret_string); } else { return (NULL); } }
char *do_file_to_string(char *filename) { FILE * fp; int size = 0; char *line = NULL; char *ret_string = NULL; size_t len = 0; int error = 0; struct stat st; char *lines = NULL; errno = 0; error = stat(filename, &st); if (error < 0) { char tmpstr[128]; sprintf(tmpstr, "Aprepro: ERR: Can't open '%s'",filename); perror(tmpstr); exit(EXIT_FAILURE); } /* Add extra characters to size in case file does not end in newline * getline add the newline at the end which results in overrunning * the memory. */ size = st.st_size+2; lines = malloc(size * sizeof(char)+1); if (lines == NULL) { perror("Aprepro: ERR: Out of memory in file_to_string function.\n"); exit(EXIT_FAILURE); } lines[0] = '\0'; fp = open_file(filename, "r"); while (getline(&line, &len, fp) != -1) { strcat ( lines, line ); assert(strlen(lines) <= size); } assert(strlen(lines) <= size); NEWSTR(lines, ret_string); fclose(fp); if (line) free(line); if (lines) free(lines); return ret_string; }
double do_word_count(char *string, char *delm) { char *temp ; double i = 0; NEWSTR(string, temp); if( strtok(temp,delm)) { i++; while( strtok(NULL,delm) ) { i++; } } free(temp); return (i) ; }
char *do_print_array(array *my_array_data) { if (my_array_data != NULL) { char *lines = NULL; char *ret_string = NULL; int ir, ic; int rows = my_array_data->rows; int cols = my_array_data->cols; int idx=0; symrec *format = getsym("_FORMAT"); /* Assume a maximum of 32 characters per array entry. * Total space for the array data is then 32*rows*cols */ int size = 32 * rows * cols; lines = malloc(size * sizeof(char) + 1); if (lines == NULL) { perror("Aprepro: ERR: Out of memory in print_array function.\n"); exit(EXIT_FAILURE); } lines[0] = '\0'; for (ir=0; ir < rows; ir++) { if (ir > 0) strcat(lines, "\n"); strcat(lines, "\t"); for (ic=0; ic < cols; ic++) { assert(strlen(lines) <= size); sprintf(&lines[strlen(lines)], format->value.svar, my_array_data->data[idx++]); if (ic < cols-1) strcat(lines, "\t"); } } assert(strlen(lines) <= size); NEWSTR(lines, ret_string); if (lines) free(lines); return ret_string; } else { return ""; } }
char *do_extract(char *string, char *begin, char *end) { /* From 'string' return a substring delimited by 'begin' and 'end'. * 'begin' is included in the string, but 'end' is not. If * 'begin' does not appear in the string, return NULL; If 'end' * does not appear, then return the remainder of the string. If * 'begin' == "", then start at beginning; if 'end' == "", then * return remainder of the string. */ char *start = string; char *tmp; int len = 0; if (strlen(begin) > 0) { start = strstr(string, begin); if (start == NULL) return ""; } len = strlen(start); if (strlen(end) > 0) { char *finish = strstr(start, end); if (finish != NULL) { len = finish-start; } } { char *tmpstr = malloc(len+1); strncpy(tmpstr, start, len); tmpstr[len] = '\0'; NEWSTR(tmpstr, tmp); free(tmpstr); } return tmp; }
int main (int argc, char *argv[]) { char *version_string = "Algebraic Preprocessor (Aprepro)"; int c; time_t time_val; struct tm *time_structure; char *asc_time = NULL; char *include_file = NULL; #define NO_ARG 0 #define IS_ARG 1 #define OP_ARG 2 static struct option long_options[] = { {"debug", NO_ARG, 0, 'd'}, {"statistics", NO_ARG, 0, 's'}, {"copyright", NO_ARG, 0, 'C'}, {"comment", IS_ARG, 0, 'c'}, {"version", NO_ARG, 0, 'v'}, {"interactive", NO_ARG, 0, 'i'}, {"include", IS_ARG, 0, 'I'}, {"exit_on", NO_ARG, 0, 'e'}, {"help", NO_ARG, 0, 'h'}, {"nowarning", NO_ARG, 0, 'W'}, {"messages", NO_ARG, 0, 'M'}, {"quiet", NO_ARG, 0, 'q'}, {"immutable", NO_ARG, 0, 'X'}, {"one_based_index", NO_ARG, 0, '1'}, {NULL, NO_ARG, NULL, 0} }; int option_index = 0; extern int optind; extern char *optarg; myname = strrchr (argv[0], '/'); if (myname == NULL) myname = argv[0]; else myname++; /* Process command line options */ initialize_options(&ap_options); ap_options.end_on_exit = False; while ((c = getopt_long (argc, argv, "c:dDsSvViI:eEwWmMhHCqX1", long_options, &option_index)) != EOF) { switch (c) { case 'c': NEWSTR(optarg, ap_options.comment); break; case 'd': case 'D': ap_options.debugging = True; ap_options.info_msg = True; ap_options.warning_msg = True; break; case 's': case 'S': /* Print hash statistics */ ap_options.statistics = True; break; case 'C': /* Print copyright message */ ap_options.copyright = True; break; case 'v': case 'V': fprintf (stderr, "%s: (%s) %s\n", version_string, qainfo[2], qainfo[1]); break; case 'i': ap_options.interactive = True; break; case 'I': /* * Check whether optarg specifies a file or a directory * If a file, it is an include file, * If a directory, it is an include_path */ if (is_directory(optarg)) { NEWSTR(optarg, ap_options.include_path); } else { NEWSTR(optarg, include_file); } break; case 'e': case 'E': ap_options.end_on_exit = True; break; case 'W': ap_options.warning_msg = False; break; case 'q': ap_options.quiet = True; break; case 'M': ap_options.info_msg = True; break; case 'X': ap_options.immutable = True; break; case '1': ap_options.one_based_index = True; break; case 'h': case 'H': usage(); exit(EXIT_SUCCESS); break; case '?': default: /* getopt will print a message for us */ usage (); exit(EXIT_FAILURE); break; } } /* Process remaining options. If '=' in word, then it is of the form * var=value. Set the value. If '=' not found, process remaining * options as input and output files */ while (optind < argc && strchr(argv[optind], '=') && !strchr(argv[optind], '/')) { char *var, *val; double value; symrec *s; var = argv[optind++]; val = strchr (var, '='); if (val == NULL) { fprintf(stderr, "ERROR: '%s' is not a valid form for assiging a variable; it will not be defined\n", var); } else { *val++ = '\0'; if (!check_valid_var(var)) { fprintf(stderr, "ERROR: '%s' is not a valid form for a variable; it will not be defined\n", var); } else { if (strchr(val, '"') != NULL) { /* Should be a string variable */ char *pt = strrchr(val, '"'); if (pt != NULL) { val++; *pt = '\0'; if (var[0] == '_') s = putsym(var, SVAR, 0); else s = putsym(var, IMMSVAR, 0); NEWSTR(val, s->value.svar); } else { fprintf(stderr, "ERROR: Missing trailing \" in definition of variable '%s'; it will not be defined\n", var); } } else { int err = sscanf (val, "%lf", &value); if (err <= 0) { fprintf(stderr, "ERROR: Could not parse value in assignment of variable '%s'; it will not be defined\n", var); } else { if (var[0] == '_') s = putsym (var, VAR, 0); else s = putsym (var, IMMVAR, 0); s->value.var = value; } } } } } if (ap_options.copyright == True) copyright_output(); /* Assume stdin, recopy if and when it is changed */ yyin = stdin; yyout = stdout; if (argc > optind) { add_input_file(argv[optind]); } else { NEWSTR ("stdin", ap_file_list[nfile].name); SET_FILE_LIST (nfile, 0, False, 1); } if (argc > ++optind) { yyout = open_file(argv[optind], "w"); } else { /* Writing to stdout */ if (ap_options.interactive) setbuf (yyout, (char *) NULL); } state_immutable = ap_options.immutable; time_val = time ((time_t*)NULL); time_structure = localtime (&time_val); asc_time = asctime (time_structure); /* NOTE: asc_time includes \n at end of string */ if (!ap_options.quiet) { if (state_immutable) { fprintf (yyout, "%s Aprepro (%s) [immutable mode] %s", ap_options.comment, qainfo[2], asc_time); } else { fprintf (yyout, "%s Aprepro (%s) %s", ap_options.comment, qainfo[2], asc_time); } } if (include_file) { nfile++; add_input_file(include_file); /* Include file specified on command line is processed in immutable * state. Reverts back to global immutable state at end of file. */ state_immutable = True; echo = False; } srand((unsigned)time_val); init_table (ap_options.comment); yyparse (); if (ap_options.debugging > 0) dumpsym (VAR, 0); if (ap_options.statistics > 0) pstats (); add_to_log(myname, 0); return (EXIT_SUCCESS); } /* NOTREACHED */
char *do_exodus_meta(char *filename) { int exoid; int ndim, nnodes, nelems, nblks, nnsets, nssets; char *title; symrec *ptr; int *ids = NULL; /* * Open the specified exodusII file, read the metadata and set * variables for each item. * Examples include "node_count", "element_count", ... */ exoid = open_exodus_file(filename); if (exoid < 0) return ""; /* read database paramters */ title = (char *)calloc ((MAX_LINE_LENGTH+1),sizeof(char *)); ex_get_init(exoid,title,&ndim,&nnodes,&nelems,&nblks,&nnsets,&nssets); ptr = putsym("ex_title", SVAR, 0); ptr->value.svar = title; ptr = putsym("ex_dimension", VAR, 0); ptr->value.var = ndim; ptr = putsym("ex_node_count", VAR, 0); ptr->value.var = nnodes; ptr = putsym("ex_element_count", VAR, 0); ptr->value.var = nelems; ptr = putsym("ex_block_count", VAR, 0); ptr->value.var = nblks; ptr = putsym("ex_nset_count", VAR, 0); ptr->value.var = nnsets; ptr = putsym("ex_sset_count", VAR, 0); ptr->value.var = nssets; { /* Nemesis Information */ int proc_count; int proc_in_file; char file_type[MAX_STR_LENGTH+1]; int global_nodes; int global_elements; int global_blocks; int global_nsets; int global_ssets; int error; error = ex_get_init_info(exoid, &proc_count, &proc_in_file, file_type); if (error >= 0) { ptr = putsym("ex_processor_count", VAR, 0); ptr->value.var = proc_count; ex_get_init_global(exoid, &global_nodes, &global_elements, &global_blocks, &global_nsets, &global_ssets); ptr = putsym("ex_node_count_global", VAR, 0); ptr->value.var = global_nodes; ptr = putsym("ex_element_count_global", VAR, 0); ptr->value.var = global_elements; } } /* * Read The Element Blocks And Set Variables For Those Also. * The Scheme Is: * -- 'Ex_Block_Ids' Is A List Of Ids. Due To Aprepro Limitations, * This List Is A String, Not An Integer List... * -- Each Block Is Named 'Ex_Block_X' Where X Is Replaced By The * Blocks Position In The List. For Example, The First Block Will * Be Named 'Ex_Block_1' * * -- Each Block Will Have The Following Symbols: * -- Ex_Block_X_Id = Id Of This Element Block * -- Ex_Block_X_Name = Composed Name "Block_" + Id * -- Ex_Block_X_Element_Count = Number Of Elements In Block * -- Ex_Block_X_Nodes_Per_Element = Number Of Nodes Per Element * -- Ex_Block_X_Topology = Type Of Elements In Block * (Lowercased) * -- Ex_Block_X_Attribute_Count = Number Of Attributes. */ ids = malloc(nblks * sizeof(int)); ex_get_elem_blk_ids (exoid, ids); { int i; char *buffer = NULL; char cid[33]; /* arbitrary size, large enough for INT_MAX */ int size = 2048; char *tmp = NULL; buffer = calloc(size, sizeof(char)); if (buffer != NULL) { for (i=0; i < nblks; i++) { sprintf(cid, "%d ", ids[i]); if (strlen(buffer) + strlen(cid) +1 > size) { if (realloc(buffer, size *=2) == NULL) { free(buffer); yyerror("Error allocating memory."); } memset(&buffer[size/2], 0, size/2); } strcat(buffer, cid); } NEWSTR(buffer, tmp); ptr = putsym("ex_block_ids", SVAR, 0); ptr->value.svar = tmp; free(buffer); } } { int i; char var[128]; char type[MAX_STR_LENGTH+1]; char *tmp = NULL; int nel; int nnel; int natr; for (i=0; i < nblks; i++) { ex_get_elem_block(exoid, ids[i], type, &nel, &nnel, &natr); sprintf(var, "ex_block_seq_%d_id", i+1); ptr = putsym(var, VAR, 0); ptr->value.var = ids[i]; sprintf(var, "ex_block_%d_name", ids[i]); ptr = putsym(var, SVAR, 0); sprintf(var, "block_%d", ids[i]); NEWSTR(var, tmp); ptr->value.svar = tmp; sprintf(var, "ex_block_%d_element_count", ids[i]); ptr = putsym(var, VAR, 0); ptr->value.var = nel; sprintf(var, "ex_block_%d_nodes_per_element", ids[i]); ptr = putsym(var, VAR, 0); ptr->value.var = nnel; sprintf(var, "ex_block_%d_topology", ids[i]); ptr = putsym(var, SVAR, 0); NEWSTR(type, tmp); /* lowercase the string */ LowerCaseTrim(tmp); ptr->value.svar = tmp; sprintf(var, "ex_block_%d_attribute_count", ids[i]); ptr = putsym(var, VAR, 0); ptr->value.var = natr; } } if (ids != NULL) free(ids); { /* Get timestep count */ int ts_count = ex_inquire_int(exoid, EX_INQ_TIME); ptr = putsym("ex_timestep_count", VAR, 0); ptr->value.var = ts_count; if (ts_count > 0) { int i; symrec *format = getsym("_FORMAT"); char *buffer = NULL; char cid[33]; /* arbitrary size, large enough for double... */ int size = 2048; char *tmp = NULL; double *timesteps = malloc(ts_count * sizeof(double)); ex_get_all_times(exoid, timesteps); buffer = calloc(size, sizeof(char)); if (buffer != NULL) { for (i=0; i < ts_count; i++) { sprintf(cid, format->value.svar, timesteps[i]); if (strlen(buffer) + strlen(cid) +2 > size) { if (realloc(buffer, size *=2) == NULL) { free(buffer); yyerror("Error allocating memory."); } memset(&buffer[size/2], 0, size/2); } strcat(buffer, cid); strcat(buffer, " "); } NEWSTR(buffer, tmp); ptr = putsym("ex_timestep_times", SVAR, 0); ptr->value.svar = tmp; free(buffer); } } } ex_close(exoid); return ""; }