int main(int argc, char *argv[]) { time_t rawtime; struct tm *brokentime; FILE *fileptr; char buffer[20]; int i; if (argc == 3) { if (strlen(argv[1]) < 3) fatal("Usage: almanac [Mon date]"); *(argv[1] + 3) = '\0'; sprintf(buffer, "%3s %s ", argv[1], argv[2]); } else if (argc == 1) { /* build the time string */ rawtime = time(NULL); brokentime = localtime(&rawtime); sprintf(buffer, "%s %d ", months[brokentime->tm_mon], brokentime->tm_mday); } else fatal("Usage: almanac [Mon date]"); /* open almanac text files; call string printing routine */ for (i = 0; i < 3; i++) { if ((fileptr = fopen(filenames[i], "r")) == NULL) fatal("Cannot open almanac file."); printf("%s\n", slugs[i]); findstring(buffer, fileptr); fclose(fileptr); } exit(EXIT_SUCCESS); }
void TestDicomSampleImage(const char* name) { std::vector<BYTE> data; bool success = ReadFile(name, &data, 9); Assert::IsTrue(success); BYTE pixeldataStart[] = { 0x00, 0x00, 0x01, 0x00, 0xFF, 0xD8, 0xFF, 0xF7 }; int offset = findstring(data, pixeldataStart, COUNT(pixeldataStart)); data.erase(data.begin(), data.begin() + offset - 4); // remove the dicom fragment headers (in the concerned images they occur every 64k) for (unsigned int i = 0; i < data.size(); i+= 64 * 1024) { data.erase(data.begin() + i, data.begin() + i + 8); } JlsParameters info; auto error = JpegLsReadHeader(&data[0], data.size(), &info, nullptr); // 0xFE, 0xFF, 0x00, 0xE0, 0x00, 0x00, 0x01, 0x00 std::vector<BYTE> dataUnc; dataUnc.resize(info.bytesperline * info.height); error = JpegLsDecode(&dataUnc[0], dataUnc.size(), &data[0], data.size(), nullptr, nullptr); Assert::IsTrue(error == charls::ApiResult::OK); std::cout << "."; }
void optionreadstring(char *ptr,char *str,int size) { if (findstring(str)) getstring(ptr,size); parser.textloc=0; }
void optionreadint(int *ptr,char *str) { if (findstring(str)) *ptr=getint(); parser.textloc=0; }
static void count_malloc_strings(char *s) { malloced_strings++; malloced_string_size+=strlen(s)+1; if(level>0) { if(findstring(s)) { malloced_strings_shared++; malloced_strings_shared_size+=strlen(s)+1; } } }
int main(void) { const char text1[] = "Well, here goes; let's hope.."; const char pattern[] = "goes"; int location = findstring(text1, pattern); if (location >= 0) printf("<%s> starts at %d in <%s>\n", pattern, location, text1); else printf("<%s> does not exist in <%s>\n", pattern, text1); return 0; }
/* * Search for a line which contains the specified string. * If the string is NULL, then the previously searched for string * is used. The currently searched for string is saved for future use. * Returns the line number which matches, or 0 if there was no match * with an error printed. */ static NUM searchlines(char *str, NUM num1, NUM num2) { LINE *lp; int len; if ((num1 < 1) || (num2 > lastnum) || (num1 > num2)) { fprintf(stderr, "Bad line numbers for search\n"); return 0; } if (*str == '\0') { if (searchstring[0] == '\0') { fprintf(stderr, "No previous search string\n"); return 0; } str = searchstring; } if (str != searchstring) strcpy(searchstring, str); len = strlen(str); lp = findline(num1); if (lp == NULL) return 0; while (num1 <= num2) { if (findstring(lp, str, len, 0) >= 0) return num1; num1++; lp = lp->next; } fprintf(stderr, "Cannot find string \"%s\"\n", str); return 0; }
/* * Add an opcode to the current function being compiled. * Note: This can change the curfunc global variable when the * function needs expanding. */ void addop(long op) { register FUNC *fp; /* current function */ NUMBER *q, *q1, *q2; unsigned long count; BOOL cut; int diff; fp = curfunc; count = fp->f_opcodecount; cut = TRUE; diff = 2; q = NULL; if ((count + 5) >= maxopcodes) { maxopcodes += OPCODEALLOCSIZE; fp = (FUNC *) malloc(funcsize(maxopcodes)); if (fp == NULL) { math_error("cannot malloc function"); /*NOTREACHED*/ } memcpy((char *) fp, (char *) curfunc, funcsize(curfunc->f_opcodecount)); if (curfunc != functemplate) free(curfunc); curfunc = fp; } /* * Check the current opcode against the previous opcode and try to * slightly optimize the code depending on the various combinations. */ switch (op) { case OP_GETVALUE: switch (oldop) { case OP_NUMBER: case OP_ZERO: case OP_ONE: case OP_IMAGINARY: case OP_GETEPSILON: case OP_SETEPSILON: case OP_STRING: case OP_UNDEF: case OP_GETCONFIG: case OP_SETCONFIG: return; case OP_DUPLICATE: diff = 1; oldop = OP_DUPVALUE; break; case OP_FIADDR: diff = 1; oldop = OP_FIVALUE; break; case OP_GLOBALADDR: diff = 1 + PTR_SIZE; oldop = OP_GLOBALVALUE; break; case OP_LOCALADDR: oldop = OP_LOCALVALUE; break; case OP_PARAMADDR: oldop = OP_PARAMVALUE; break; case OP_ELEMADDR: oldop = OP_ELEMVALUE; break; default: cut = FALSE; } if (cut) { fp->f_opcodes[count - diff] = oldop; return; } break; case OP_POP: switch (oldop) { case OP_ASSIGN: fp->f_opcodes[count-1] = OP_ASSIGNPOP; oldop = OP_ASSIGNPOP; return; case OP_NUMBER: case OP_IMAGINARY: q = constvalue(fp->f_opcodes[count-1]); qfree(q); break; case OP_STRING: sfree(findstring((long)fp->f_opcodes[count-1])); break; case OP_LOCALADDR: case OP_PARAMADDR: break; case OP_GLOBALADDR: diff = 1 + PTR_SIZE; break; case OP_UNDEF: fp->f_opcodecount -= 1; oldop = OP_NOP; oldoldop = OP_NOP; return; default: cut = FALSE; } if (cut) { fp->f_opcodecount -= diff; oldop = OP_NOP; oldoldop = OP_NOP; fprintf(stderr, "Line %ld: unused value ignored\n", linenumber()); return; } break; case OP_NEGATE: if (oldop == OP_NUMBER) { q = constvalue(fp->f_opcodes[count-1]); fp->f_opcodes[count-1] = addqconstant(qneg(q)); qfree(q); return; } } if (oldop == OP_NUMBER) { if (oldoldop == OP_NUMBER) { q1 = constvalue(fp->f_opcodes[count - 3]); q2 = constvalue(fp->f_opcodes[count - 1]); switch (op) { case OP_DIV: if (qiszero(q2)) { cut = FALSE; break; } q = qqdiv(q1,q2); break; case OP_MUL: q = qmul(q1,q2); break; case OP_ADD: q = qqadd(q1,q2); break; case OP_SUB: q = qsub(q1,q2); break; case OP_POWER: if (qisfrac(q2) || qisneg(q2)) cut = FALSE; else q = qpowi(q1,q2); break; default: cut = FALSE; } if (cut) { qfree(q1); qfree(q2); fp->f_opcodes[count - 3] = addqconstant(q); fp->f_opcodecount -= 2; oldoldop = OP_NOP; return; } } else if (op != OP_NUMBER) { q = constvalue(fp->f_opcodes[count - 1]); if (op == OP_POWER) { if (qcmpi(q, 2L) == 0) { fp->f_opcodecount--; fp->f_opcodes[count - 2] = OP_SQUARE; qfree(q); oldop = OP_SQUARE; return; } if (qcmpi(q, 4L) == 0) { fp->f_opcodes[count - 2] = OP_SQUARE; fp->f_opcodes[count - 1] = OP_SQUARE; qfree(q); oldop = OP_SQUARE; return; } } if (qiszero(q)) { qfree(q); fp->f_opcodes[count - 2] = OP_ZERO; fp->f_opcodecount--; } else if (qisone(q)) { qfree(q); fp->f_opcodes[count - 2] = OP_ONE; fp->f_opcodecount--; } } } /* * No optimization possible, so store the opcode. */ fp->f_opcodes[fp->f_opcodecount] = op; fp->f_opcodecount++; oldoldop = oldop; oldop = op; }
static int user_parser (char * buff) { char verb_buff[MAX_VERB_BUFF]; sentence_t *s; char *p; int length; char *user_verb = 0; int where; int save_illegal_sentence_action; debug(d_flag, ("cmd [/%s]: %s\n", command_giver->obname, buff)); /* strip trailing spaces. */ for (p = buff + strlen(buff) - 1; p >= buff; p--) { if (*p != ' ') break; *p = '\0'; } if (buff[0] == '\0') return 0; length = p - buff + 1; p = strchr(buff, ' '); if (p == 0) { user_verb = findstring(buff); } else { *p = '\0'; user_verb = findstring(buff); *p = ' '; length = p - buff; } if (!user_verb) { /* either an xverb or a verb without a specific add_action */ user_verb = buff; } /* * copy user_verb into a static character buffer to be pointed to by * last_verb. */ strncpy(verb_buff, user_verb, MAX_VERB_BUFF - 1); if (p) { int pos; pos = p - buff; if (pos < MAX_VERB_BUFF) { verb_buff[pos] = '\0'; } } save_illegal_sentence_action = illegal_sentence_action; illegal_sentence_action = 0; for (s = command_giver->sent; s; s = s->next) { svalue_t *ret; object_t *command_object; if (s->flags & (V_NOSPACE | V_SHORT)) { if (strncmp(buff, s->verb, strlen(s->verb)) != 0) continue; } else { /* note: if was add_action(blah, "") then accept it */ if (s->verb[0] && (user_verb != s->verb)) continue; } /* * Now we have found a special sentence ! */ if (!(s->flags & V_FUNCTION)) debug(d_flag, ("Local command %s on /%s", s->function.s, s->ob->obname)); if (s->flags & V_NOSPACE) { int l1 = strlen(s->verb); int l2 = strlen(verb_buff); if (l1 < l2) last_verb = verb_buff + l1; else last_verb = ""; } else { if (!s->verb[0] || (s->flags & V_SHORT)) last_verb = verb_buff; else last_verb = s->verb; } /* * If the function is static and not defined by current object, then * it will fail. If this is called directly from user input, then * the origin is the driver and it will be allowed. */ where = (current_object ? ORIGIN_EFUN : ORIGIN_DRIVER); /* * Remember the object, to update moves. */ command_object = s->ob; save_command_giver(command_giver); if (s->flags & V_NOSPACE) { copy_and_push_string(&buff[strlen(s->verb)]); } else if (buff[length] == ' ') { copy_and_push_string(&buff[length + 1]); } else { push_undefined(); } if (s->flags & V_FUNCTION) { ret = call_function_pointer(s->function.f, 1); } else { if (s->function.s[0] == APPLY___INIT_SPECIAL_CHAR) error("Illegal function name.\n"); ret = apply(s->function.s, s->ob, 1, where); } /* s may be dangling at this point */ restore_command_giver(); last_verb = 0; /* was this the right verb? */ if (ret == 0) { /* is it still around? Otherwise, ignore this ... it moved somewhere or dested itself */ if (s == command_giver->sent) { char buf[256]; if (s->flags & V_FUNCTION) { sprintf(buf, "Verb '%s' bound to uncallable function pointer.\n", s->verb); error(buf); } else { sprintf(buf, "Function for verb '%s' not found.\n", s->verb); error(buf); } } } if (ret && (ret->type != T_NUMBER || ret->u.number != 0)) { #ifdef PACKAGE_MUDLIB_STATS if (command_giver && command_giver->interactive #ifndef NO_WIZARDS && !(command_giver->flags & O_IS_WIZARD) #endif ) add_moves(&command_object->stats, 1); #endif if (!illegal_sentence_action) illegal_sentence_action = save_illegal_sentence_action; return 1; } if (illegal_sentence_action) { switch (illegal_sentence_action) { case 1: error("Illegal to call remove_action() [caller was /%s] from a verb returning zero.\n", illegal_sentence_ob->obname); case 2: error("Illegal to move or destruct an object (/%s) defining actions from a verb function which returns zero.\n", illegal_sentence_ob->obname); } } } notify_no_command(); illegal_sentence_action = save_illegal_sentence_action; return 0; }
void getmass(struct atm_data *at, struct molecules *mol){ int nmol = at->n_aiso; /* FINDME: De-hardcode filename, put it in tr.ds.at: */ char *filename = "../inputs/molecules.dat"; FILE *elist; /* Atomic masses, names, alias names, alias molecules, and sizes: */ double *amass, /* Atomic masses form list */ *radius; /* Molecular radii from list */ char **aname, /* Atomic symbol names */ **rname, /* Molecules names for listed radii */ **alias, /* Alias of names given in atmfile */ **amol, /* Corresponding molecule for alias */ **elements; /* Elements in a molecule */ int natoms = 92, /* Number of listed atoms */ nalias = 2, /* Number of listed alias names */ nradii = 14, /* Number of listed radii */ namelen = 3, /* Atomic symbol name length */ maxlinelen = 501, molnamelen, /* Length of a molecule name */ elen, /* Element name length */ iatom, /* Atom's index from list */ ielement, /* Element counter in a molecule */ i, j; /* Auxiliary for-loop index */ int *nelements; /* Number of elements in a molecule */ char line[maxlinelen], *lp, molecule[MAXNAMELEN]; /* Current molecule's name */ /* Alias names, and corresponding molecules: */ amass = (double *)calloc(natoms, sizeof(double)); aname = (char **)calloc(natoms, sizeof(char *)); aname[0] = (char *)calloc(natoms*namelen, sizeof(char)); for (i=1; i<natoms; i++) aname[i] = aname[0] + i*namelen; /* Open Molecules file: */ if((elist=verbfileopen(filename, "Molecular info ")) == NULL) exit(EXIT_FAILURE); do{ /* Read lines, skipping comments and blank lines: */ lp = fgets(line, maxlinelen, elist); }while (lp[0] == '\0' || lp[0] == '\n' || lp[0] == '#'); /* Fill atoms and mass array with info from element list: */ for (i=0; i<natoms; i++){ lp += 19; /* The element's symbol starts at the 19th character */ getname(lp, aname[i]); lp = nextfield(lp); amass[i] = strtod(lp, NULL); lp = fgets(line, maxlinelen, elist); } /* Allocate alias names and corresponding molecules: */ alias = (char **)calloc(nalias, sizeof(char *)); amol = (char **)calloc(nalias, sizeof(char *)); alias[0] = (char *)calloc(nalias*MAXNAMELEN, sizeof(char)); amol[0] = (char *)calloc(nalias*MAXNAMELEN, sizeof(char)); for (i=1; i<nalias; i++){ alias[i] = alias[0] + i*MAXNAMELEN; amol[i] = amol[0] + i*MAXNAMELEN; } /* Continue reading the file to get the alias names: */ do{ /* Skip blank and comment lines: */ lp = fgets(line, maxlinelen, elist); }while (lp[0] == '\0' || lp[0] == '\n' || lp[0] == '#'); /* Get aliases from file: */ for (i=0; i<nalias; i++){ /* Get alias and molecule name: */ getname(lp, alias[i]); lp = nextfield(lp); getname(lp, amol[i]); lp = fgets(line, maxlinelen, elist); } /* Allocate names and radii: */ radius = (double *)calloc(nradii, sizeof(double)); rname = (char **)calloc(nradii, sizeof(char *)); rname[0] = (char *)calloc(nradii*MAXNAMELEN, sizeof(char)); for (i=1; i<nradii; i++) rname[i] = rname[0] + i*MAXNAMELEN; /* Go to next block */ do{ lp = fgets(line, maxlinelen, elist); }while (lp[0] == '\0' || lp[0] == '\n' || lp[0] == '#'); /* Get radii from file: */ for (i=0; i<nradii; i++){ /* Get molecules' name and radius: */ getname(lp, rname[i]); lp = nextfield(lp); radius[i] = strtod(lp, NULL)/2.0; lp = fgets(line, maxlinelen, elist); } /* Allocate max number of molecules and max len of molecule name: */ nelements = (int *)calloc(MAXNAMELEN, sizeof(int)); elements = (char **)calloc(MAXNAMELEN, sizeof(char *)); elements[0] = (char *)calloc((MAXNAMELEN+1)*MAXNAMELEN, sizeof(char)); for (j=0; j<MAXNAMELEN; j++) elements[j] = elements[0] + j*(MAXNAMELEN+1); /* For each molecule: */ for (i=0; i<nmol; i++){ /* Check if molecule name is an alias: */ if ((j=findstring(mol->name[i], alias, nalias)) >= 0) strcpy(molecule, amol[j]); else strcpy(molecule, mol->name[i]); /* Allocate elements in a molecule: */ molnamelen = (int)strlen(molecule); /* Break down molecule into its elements: */ elen = 0; /* Element name length */ ielement = 0; /* Elements in a molecule counter */ for (j=0; j<molnamelen; j++){ if (isalpha(molecule[j])){ if (isupper(molecule[j])){ /* Uppercase letter: */ if (elen > 0){ /* End last element, advance j, store new letter */ if (elen <= 2){ /* If name is longer, it's an alias name */ elen = 0; ielement++; /* Count 1 more element */ nelements[ielement] = 1; } elements[ielement][elen++] = molecule[j]; } else{ /* New Atom (elen==0) */ elements[ielement][elen++] = molecule[j]; nelements[ielement] = 1; } } else{ /* Lowercase: */ elements[ielement][elen++] = molecule[j]; } } else{ /* A numeric value: */ nelements[ielement] = (int)strtol(&molecule[j], NULL, 10); elements[ielement][elen] = '\0'; j += (int)log10((double)nelements[ielement++]); elen = 0; } } if (elen != 0) ielement++; /* Calculate molecule's mass: */ for (j=0; j<ielement; j++){ /* Find index of atom in list: */ iatom = findstring(elements[j], aname, natoms); transitprint(30, verblevel, "Found %d %2s[%2d] atom(s) with mass " "%9.6f u.\n", nelements[j], aname[iatom], iatom, amass[iatom]); /* Get mass and multiply by the number of atoms in molecule: */ mol->mass[i] += amass[iatom] * nelements[j]; } /* Set the radius: */ j = findstring(molecule, rname, nradii); mol->radius[i] = radius[j] * ANGSTROM; transitprint(30, verblevel, "Molecule '%s' has radius %4.2f A and mass " "%4.2f u.\n", mol->name[i], mol->radius[i]/ANGSTROM, mol->mass[i]); } }
/* * Do the substitute command. * The current line is set to the last substitution done. */ static void subcommand(char *cp, NUM num1, NUM num2) { int delim; char *oldstr; char *newstr; LEN oldlen; LEN newlen; LEN deltalen; LEN offset; LINE *lp; LINE *nlp; BOOL globalflag; BOOL printflag; BOOL didsub; BOOL needprint; if ((num1 < 1) || (num2 > lastnum) || (num1 > num2)) { fprintf(stderr, "Bad line range for substitute\n"); return; } globalflag = FALSE; printflag = FALSE; didsub = FALSE; needprint = FALSE; if (isblank(*cp) || (*cp == '\0')) { fprintf(stderr, "Bad delimiter for substitute\n"); return; } delim = *cp++; oldstr = cp; cp = strchr(cp, delim); if (cp == NULL) { fprintf(stderr, "Missing 2nd delimiter for substitute\n"); return; } *cp++ = '\0'; newstr = cp; cp = strchr(cp, delim); if (cp) *cp++ = '\0'; else cp = ""; while (*cp) switch (*cp++) { case 'g': globalflag = TRUE; break; case 'p': printflag = TRUE; break; default: fprintf(stderr, "Unknown option for substitute\n"); return; } if (*oldstr == '\0') { if (searchstring[0] == '\0') { fprintf(stderr, "No previous search string\n"); return; } oldstr = searchstring; } if (oldstr != searchstring) strcpy(searchstring, oldstr); lp = findline(num1); if (lp == NULL) return; oldlen = strlen(oldstr); newlen = strlen(newstr); deltalen = newlen - oldlen; offset = 0; while (num1 <= num2) { offset = findstring(lp, oldstr, oldlen, offset); if (offset < 0) { if (needprint) { printlines(num1, num1, FALSE); needprint = FALSE; } offset = 0; lp = lp->next; num1++; continue; } needprint = printflag; didsub = TRUE; dirty = TRUE; /* * If the replacement string is the same size or shorter * than the old string, then the substitution is easy. */ if (deltalen <= 0) { memcpy(&lp->data[offset], newstr, newlen); if (deltalen) { memcpy(&lp->data[offset + newlen], &lp->data[offset + oldlen], lp->len - offset - oldlen); lp->len += deltalen; } offset += newlen; if (globalflag) continue; if (needprint) { printlines(num1, num1, FALSE); needprint = FALSE; } lp = nlp->next; num1++; continue; } /* * The new string is larger, so allocate a new line * structure and use that. Link it in in place of * the old line structure. */ nlp = (LINE *) malloc(sizeof(LINE) + lp->len + deltalen); if (nlp == NULL) { fprintf(stderr, "Cannot get memory for line\n"); return; } nlp->len = lp->len + deltalen; memcpy(nlp->data, lp->data, offset); memcpy(&nlp->data[offset], newstr, newlen); memcpy(&nlp->data[offset + newlen], &lp->data[offset + oldlen], lp->len - offset - oldlen); nlp->next = lp->next; nlp->prev = lp->prev; nlp->prev->next = nlp; nlp->next->prev = nlp; if (curline == lp) curline = nlp; free(lp); lp = nlp; offset += newlen; if (globalflag) continue; if (needprint) { printlines(num1, num1, FALSE); needprint = FALSE; } lp = lp->next; num1++; } if (!didsub) fprintf(stderr, "No substitutions found for \"%s\"\n", oldstr); }