void max_heapify(void *array, int i, int32_t nelem, size_t elem_size, int32_t offset, int i_type) { void *largest = array + i * elem_size; // if it's STRUCT,THEN largest + offset is the CMP-VALUE! void *pswap = malloc(elem_size); int new_index; printf("\nARGUMENT CHECK\n array = %08x\ni = %d\nnelem = %d\nelem_size = %d\noffset = %d\ni_type = %d\n", (uint32_t)array,i,nelem,elem_size,offset,i_type); if(pswap == NULL) printf("%s Out Of Memory!\n",__func__); if((LEFT(i) < nelem) && ((valcmp(i_type, largest + offset, array + (LEFT(i)) * elem_size + offset)) < 0)) { largest = array + LEFT(i) * elem_size; new_index = LEFT(i); } if((RIGHT(i) < nelem) && ((valcmp(i_type, largest + offset, array + (RIGHT(i)) * elem_size + offset)) < 0)) { largest = array + RIGHT(i) * elem_size; new_index = RIGHT(i); } if(largest != array + i * elem_size) { memmove(pswap, largest, elem_size); memmove(largest, array + i * elem_size, elem_size); memmove(array + i * elem_size, pswap, elem_size); free(pswap); max_heapify(array, new_index, nelem, elem_size, offset, i_type); } }
/* Display non-null header field values */ void printheader(segy *tp) { int i; /* index over header fields */ int j; /* index over non-null header fields */ Value val; /* value in header field */ String type; /* ... its data type */ String key; /* ... the name of the header field */ Value zeroval; /* zero value to compare with */ zeroval.l = 0; j = 0; for (i = 0; i < SU_NKEYS; i++) { gethval(tp, i, &val); key = getkey(i); type = hdtype(key); if (valcmp(type, val, zeroval)) { /* not equal to zero */ (void) printf(" %s=", key); printfval(type, val); if ((++j % 6) == 0) putchar('\n'); } } putchar('\n'); return; }
char cmp ( val * other ) { // lexicographic if ( LST != other->type() ) return valcmp( LST, other->type() ); lstval * that = other->aslst(); val * s1 = this->head; val * s2 = that->head; while ( s1 && s2 ) { if ( char r = s1->cmp( s2 ) ) { return r; } s1 = s1->right; s2 = s2->right; } if ( !( s1 || s2 ) ) return 0; if ( s1 ) return 1; return -1; }
int script_process_line(char *buf) { int ret = -ENOTSUP; struct timespec ts = { .tv_sec = 0, .tv_nsec = 0 }; struct timespec tse; if (_perf_measure) ts = utils_get_time( TIME_CURRENT ); /* Skip if it's a one line comment (more to be implemented) */ if (is_comment(buf) == 1) { DPRINTF("%s: Found comment, skipping\n", __FUNCTION__); ret = 0; goto cleanup; } else if (strcmp(buf, "else {") == 0) { /* Reverse the condition */ if ((_script_in_condition_and_met == 1) || (_script_in_condition_and_met == -10)) _script_in_condition_and_met = 0; else if ((_script_in_condition_and_met == 0) || (_script_in_condition_and_met == -20)) _script_in_condition_and_met = 1; else if (_script_in_condition_and_met != -1) { DPRINTF("%s: Invalid state for else statement\n", __FUNCTION__); ret = -EINVAL; goto cleanup; } ret = 0; goto cleanup; } else if (strcmp(buf, "}") == 0) { _script_in_condition_and_met = (_script_in_condition_and_met == 1) ? -10 : -20; ret = 0; goto cleanup; } if (_script_in_condition_and_met < -1) _script_in_condition_and_met = 1; if (_script_in_condition_and_met == 0) { ret = 0; goto cleanup; } /* Comparison with no ternary operator support... yet */ if (regex_match("if \\(([^(]*)([^)]*)\\)", buf)) { if (regex_match("if \\(([^(]*) == ([^)]*)\\) {", buf)) { char **matches = NULL; int i, num_matches; matches = (char **)utils_alloc( "scripting.script_process_line.matches", sizeof(char *) ); _regex_match("if \\(([^(]*) == ([^)]*)\\) {", buf, matches, &num_matches); if (num_matches >= 2) _script_in_condition_and_met = (valcmp(matches[0], matches[1]) == 0) ? 1 : 0; for (i = 0; i < num_matches; i++) matches[i] = utils_free("scripting.condition.matches[]", matches[i]); matches = utils_free("scripting.condition.matches", matches); } } else /* Definition */ if (strncmp(buf, "define ", 7) == 0) { tTokenizer t; t = tokenize(buf + 7, " "); if (t.numTokens != 2) { ret = -EINVAL; goto cleanup; } if (variable_create(trim(t.tokens[0]), trim(t.tokens[1])) != 1) { ret = -EIO; goto cleanup; } } else /* Operators */ if ((strstr(buf, "+=") != NULL) || (strstr(buf, "-=") != NULL) || (strstr(buf, "%=") != NULL) || (strstr(buf, "*=") != NULL) || (strstr(buf, "/=") != NULL)) { tTokenizer t; char *var; char *val; int op, vtype; t = tokenize(buf, "="); if (t.numTokens != 2) { ret = -EINVAL; goto cleanup; } var = trim(strdup(t.tokens[0])); val = trim(strdup(t.tokens[1])); op = var[ strlen(var) - 1]; var[ strlen(var) - 1] = 0; var = trim(var); if (val[strlen(val) - 1] == ';') val[strlen(val) - 1] = 0; val = trim(val); vtype = variable_get_type(var, NULL); if ((vtype == TYPE_INT) || (vtype == TYPE_LONG)) { char tmp[32] = { 0 }; long value = atol(variable_get_element_as_string(var, NULL)); if (op == '+') value += atol(val); else if (op == '-') value -= atol(val); else if (op == '*') value *= atol(val); else if (op == '%') value %= atol(val); else if (op == '/') value /= atol(val); snprintf(tmp, sizeof(tmp), "%ld", value); variable_set_deleted(var, 1); variable_add(var, tmp, TYPE_QSCRIPT, -1, vtype); ret = 0; } else ret = -EINVAL; var = utils_free("scripting.operators.var", var); val = utils_free("scripting.operators.val", val); free_tokens(t); return ret; } else /* Assignment */ if (is_assignment(buf)) { tTokenizer t; t = tokenize(buf, "="); char *val = strdup( trim(t.tokens[1]) ); if (val[strlen(val) - 1] == ';') { val[strlen(val) - 1] = 0; if (is_numeric(val) || is_string(val)) { if (is_string(val)) { *val++; val[strlen(val) - 1] = 0; } if (variable_add(trim(t.tokens[0]), val, TYPE_QSCRIPT, -1, gettype(val)) < 0) { desc_printf(gIO, gFd, "Cannot set new value to variable %s\n", trim(t.tokens[0])); ret = -EEXIST; } else ret = 0; } else if (regex_match("([^(]*)([^)]*)", val)) { tTokenizer t2; char *args = NULL; char *fn; t2 = tokenize(val, "("); if (t2.tokens[t2.numTokens - 1][strlen(t2.tokens[t2.numTokens - 1]) - 1] != ')') { ret = -EINVAL; goto cleanup; } t2.tokens[t2.numTokens - 1][strlen(t2.tokens[t2.numTokens - 1]) - 1] = 0; fn = strdup(t2.tokens[0]); /* We need to make sure parenthesis won't break script line */ if (t2.numTokens > 1) { int i; char argstmp[8192] = { 0 }; for (i = 1; i < t2.numTokens; i++) { strcat(argstmp, t2.tokens[i]); if (i < t2.numTokens - 1) strcat(argstmp, "("); } args = strdup(argstmp); } if (args != NULL) { char args2[1024] = { 0 }; snprintf(args2, sizeof(args2), "%s", args + 1); args2[ strlen(args2) - 1] = 0; args = utils_free("scripting.function-call.args", args); args = strdup( args2 ); } free_tokens(t2); if (_script_builtin_function(trim(t.tokens[0]), fn, args) != 0) DPRINTF("Function %s doesn't seem to be builtin, we should try user-defined function\n", fn); DPRINTF("%s: Should be a function with return value\n", __FUNCTION__); args = utils_free("scripting.function-call.args", args); fn = utils_free("scripting.function-call.fn", fn); ret = 0; } else ret = -EINVAL; } free_tokens(t); } else if (regex_match("([^(]*)([^)]*)", buf)) { tTokenizer t2; char *args = NULL; char *fn; t2 = tokenize(buf, "("); if (t2.tokens[t2.numTokens - 1][strlen(t2.tokens[t2.numTokens - 1]) - 1] != ';') { ret = -EINVAL; goto cleanup; } t2.tokens[t2.numTokens - 1][strlen(t2.tokens[t2.numTokens - 1]) - 1] = 0; fn = strdup(t2.tokens[0]); /* We need to make sure parenthesis won't break script line */ if (t2.numTokens > 1) { int i; char argstmp[8192] = { 0 }; for (i = 1; i < t2.numTokens; i++) { strcat(argstmp, t2.tokens[i]); if (i < t2.numTokens - 1) strcat(argstmp, "("); } args = strdup(argstmp); } if (args != NULL) { if (args[strlen(args) - 1] == ')') args[strlen(args) - 1] = 0; } free_tokens(t2); if (_script_builtin_function(NULL, fn, args) != 0) DPRINTF("Function %s doesn't seem to be builtin, we should try user-defined function\n", fn); args = utils_free("scripting.function-call.args", args); fn = utils_free("scripting.function-call.fn", fn); ret = 0; } else DPRINTF("%s: Not implemented yet\n", __FUNCTION__); cleanup: if ((_perf_measure) && ((ts.tv_nsec > 0) && (ts.tv_sec > 0)) && (_script_in_condition_and_met > 0)) { tse = utils_get_time( TIME_CURRENT ); desc_printf(gIO, gFd, "PERF: Line \"%s\" was being processed for %.3f microseconds (%.3f ms)\n\n", buf, get_time_float_us( tse, ts ), get_time_float_us(tse, ts) / 1000.); } return ret; } int run_script(char *filename) { FILE *fp; int opened = 0; char buf[4096] = { 0 }; struct timespec ts = utils_get_time( TIME_CURRENT ); struct timespec tse; if (access(filename, R_OK) != 0) return -ENOENT; _script_in_condition_and_met = -1; fp = fopen(filename, "r"); if (fp == NULL) return -EPERM; if (gHttpHandler) http_host_header(gIO, gFd, HTTP_CODE_OK, gHost, "text/html", NULL, myRealm, 0); while (!feof(fp)) { memset(buf, 0, sizeof(buf)); fgets(buf, sizeof(buf), fp); if ((strlen(buf) > 1) && (buf[strlen(buf) - 1] == '\n')) buf[strlen(buf) - 1] = 0; if ((strlen(buf) > 1) && (buf[0] != '\n')) { if (strcmp(buf, "<$") == 0) opened = 1; if (strcmp(buf, "$>") == 0) opened = 0; if ((opened) && (strcmp(buf, "<$") != 0)) script_process_line(trim(buf)); } } fclose(fp); idb_free_last_select_data(); if (_perf_measure) { tse = utils_get_time( TIME_CURRENT ); desc_printf(gIO, gFd, "PERF: File \"%s\" has been processed in %.3f microseconds (%.3f ms)\n\n", basename(filename), get_time_float_us( tse, ts ), get_time_float_us(tse, ts) / 1000.); } variable_dump(); variable_free_all(); return 0; }
int main(int argc, char **argv) { int cdpindex; /* index of cdp header word */ int offindex; /* index of offset header word */ int nt; /* number of data points on trace */ int nsegy; /* number of bytes in the segy */ Value cdpval; /* value of cdp in current gather */ Value offval; /* ... same for offset */ Value cdpvalnew;/* value of cdp in trace being treated */ Value offvalnew;/* ... same for offset */ int newtracl; /* tracl for stacked traces */ int fold; /* number of traces with same offset */ float ffold; /* ... cast to float */ int norm; /* norm=1 => divide by fold */ int itmp; /* temporary for swap of sx, gx keys */ /* Initialize */ initargs(argc, argv); requestdoc(1); /* Set parameter */ if (!getparint("norm", &norm)) norm = 1; /* Get indices */ cdpindex = getindex("cdp"); offindex = getindex("offset"); /* Set up for first trace */ nsegy = gettr(&intrace); nt = intrace.ns; gethval(&intrace, cdpindex, &cdpval); gethval(&intrace, offindex, &offval); memcpy( (void *) &outtrace, (const void *) &intrace, nsegy); newtracl = 1; /* Global initialization */ fold = 1; /* Will be re-initialized for each gather */ /* Loop over traces */ while (nsegy) { /* While previous trace non-empty */ nsegy = gettr(&intrace); gethval(&intrace, cdpindex, &cdpvalnew); gethval(&intrace, offindex, &offvalnew); if (valcmp("l", cdpval, cdpvalnew) || !nsegy) { /* Either cdpval and cdpvalnew differ, */ /* indicating a new gather or nsegy is zero, */ /* indicating the end of the traces. */ /* Add header info and output leftover stack */ outtrace.nhs = fold; outtrace.tracl = newtracl++; if (outtrace.sx > outtrace.gx) { itmp = outtrace.sx; outtrace.sx = outtrace.gx; outtrace.gx = itmp; } if (norm) { ffold = (float) fold; if (fold != 1) { register int i; for (i = 0; i < nt; ++i) outtrace.data[i] /= ffold; } } puttr(&outtrace); /* Set up for next gather */ memcpy( (void *) &outtrace, (const void *) &intrace, nsegy); fold = 1; cdpval = cdpvalnew; offval = offvalnew; } else { /* still in same cdp gather */ if (valcmp("l", offval, offvalnew)) { /* offval and offvalnew differ */ /* Add header info and output stack */ outtrace.nhs = fold; outtrace.tracl = newtracl++; if (outtrace.sx > outtrace.gx) { itmp = outtrace.sx; outtrace.sx = outtrace.gx; outtrace.gx = itmp; } if (norm) { ffold = (float) fold; if (fold != 1) { register int i; for (i = 0; i < nt; ++i) outtrace.data[i] /= ffold; } } puttr(&outtrace); /* Set up for next offset */ memcpy( (void *) &outtrace, (const void *) &intrace, nsegy); fold = 1; offval = offvalnew; } else { /* same offset within this cdp */ register int i; for (i = 0; i < nt; ++i) outtrace.data[i] += intrace.data[i]; fold++; } } } return(CWP_Exit()); }
int main(int argc, char **argv) { cwp_String key; /* header key word from segy.h */ cwp_String type;/* ... its type */ int index; /* ... its index */ int nsegy; /* number of bytes in the segy */ Value val; /* value of key in current gather */ Value valnew; /* value of key in trace being treated */ int verbose; /* verbose flag */ int val_i; /* key value as an integer */ int ntr=0; /* count of number of traces in an ensemble */ int numlength; /* length of numerical part of filenames */ FILE *tmpfp=NULL; /* file pointer */ FILE *outfp=NULL; /* file pointer */ cwp_String dir; /* directory name */ cwp_String suffix; /* suffix of output files */ char directory[BUFSIZ]; /* storage for directory name */ char tempfilename[BUFSIZ]; /* ...temporary filename */ char middle[BUFSIZ]; /* ...middle of final filename */ char stem[BUFSIZ]; /* ...stem of final filename */ char format[BUFSIZ]; /* ...format of numeric part */ char outfile[BUFSIZ]; /* ...final filename */ /* Initialize */ initargs(argc, argv); requestdoc(1); /* Get parameters */ MUSTGETPARSTRING("dir", &dir); if (!getparint ("verbose", &verbose)) verbose = 0; if (!getparstring("key", &key)) key = "ep"; if (!getparstring("suffix", &suffix)) suffix = ".hsu"; if (!getparint("numlength", &numlength)) numlength=7; /* Initialize */ /* tempfilename = ealloc1(strlen(tmplt)+3,sizeof(char)); */ type = hdtype(key); index = getindex(key); /* Set up for first trace (must compare new key field each time) */ nsegy = gettr(&intrace); /* Create temporary filename for the first gather */ strcpy(directory, dir); strcpy(tempfilename, temporary_filename(directory)); if((tmpfp = fopen(tempfilename,"w+")) == NULL) err("Cannot open file %s\n",tempfilename); if (verbose) warn(" temporary filename = %s", tempfilename); /* get key header value */ gethval(&intrace, index, &val); ntr=0; do { ++ntr; /* Get header value */ gethval(&intrace, index, &valnew); /* compare past header value to current value */ if (valcmp(type, val, valnew) || !nsegy) { /* Either val and valnew differ, indicating a */ /* new gather or nsegy is zero, indicating the */ /* end of the traces. */ /* capture key field value */ /* and build output filename */ val_i=vtoi(type,val); /* zero out name parts */ strcpy(outfile,""); strcpy(middle,""); strcpy(stem,""); /* build output name parts */ strcat(middle,directory); strcat(middle,"/"); strcat(stem,key); strcat(stem,"="); /* format for numeric part */ (void)sprintf(format, "%%s%%s%%0%dd%%s",numlength); /* build name of output file */ (void)sprintf(outfile, format,middle,stem, val_i, suffix); if (verbose) warn(" outfile = %s", outfile); /* rewind file */ rewind(tmpfp); /* open the new file */ if((outfp = fopen(outfile,"w+")) == NULL) err("Cannot open file %s\n",outfile); /* loop over traces setting ntr field */ /* and write to finalfile */ while(fgettr(tmpfp,&tmptr)) { tmptr.ntr = ntr; fputtr(outfp,&tmptr); } /* Close files */ fclose(tmpfp); fclose(outfp); remove(tempfilename); if (verbose) warn("val= %i", val_i); /* Set up for next gather */ /* create new tempfname first */ strcpy(tempfilename, temporary_filename(directory)); /* open filename */ if((tmpfp = fopen(tempfilename,"w+")) == NULL) err("Cannot open file %s\n",tempfilename); val = valnew; ntr=0; } fputtr(tmpfp,&intrace); } while(gettr(&intrace)); /* Close file */ rewind(tmpfp); val_i=vtoi(type,val); /* Move last gather into new location */ /* capture key field value */ /* and build output filename */ /* null out name parts */ strcpy(outfile,""); strcpy(middle,""); strcpy(stem,""); /* build name parts */ strcat(middle,directory); strcat(middle,"/"); strcat(stem,key); strcat(stem,"="); /* write format of numeric part of output filename */ (void)sprintf(format, "%%s%%s%%0%dd%%s",numlength); /* write output filename */ (void)sprintf(outfile, format,middle,stem, val_i, suffix); /* open the output file */ if((outfp = fopen(outfile,"w+")) == NULL) err("Cannot open file %s\n",outfile); /* loop over traces setting ntr field */ while(fgettr(tmpfp,&tmptr)) { tmptr.ntr = ntr; fputtr(outfp,&tmptr); } /* Close file */ fclose(tmpfp); fclose(outfp); remove(tempfilename); if (verbose) warn(" outfile = %s", outfile); if (verbose) warn("val= %i",val_i); return(CWP_Exit()); }