main(int argc, string argv[]) { int i; long lstr[32], *lcop; stream opt, ipt; for (i = 0; i < 32; i++) lstr[i] = (i < 21 ? 12345 + 512 * i : (int) NULL); printf("xstrlen(lstr, %d) == %d\n", sizeof(long), xstrlen(lstr, sizeof(long))); lcop = (long *) copxstr(lstr, sizeof(long)); printf("xstrlen(lcop, %d) == %d\n", sizeof(long), xstrlen(lcop, sizeof(long))); printf("xstreq(lstr, lcop, %d) == %d\n", sizeof(long), xstreq(lstr, lcop, sizeof(long))); printf("changing *lcop\n"); *lcop = -1; printf("xstreq(lstr, lcop, %d) == %d\n", sizeof(long), xstreq(lstr, lcop, sizeof(long))); opt = stropen("foobar.dat", "w!"); printf("putxstr(opt, lstr, %d) == %d\n", sizeof(long), putxstr(opt, lstr, sizeof(long))); printf("putxstr(opt, lcop, %d) == %d\n", sizeof(long), putxstr(opt, lcop, sizeof(long))); fclose(opt); ipt = stropen("foobar.dat", "r"); lcop = (long *) getxstr(ipt, sizeof(long)); if (lcop == NULL) printf("getxstr(ipt, %d) failed\n", sizeof(long)); printf("xstreq(lstr, lcop, %d) == %d\n", sizeof(long), xstreq(lstr, lcop, sizeof(long))); }
int main(int argc, string argv[]) { string prog, itags[MaxBodyFields]; stream xstr, ostr; int nold = -1; initparam(argv, defv); exprs[0] = getparam("group"); prog = mktemp((string) copxstr("/tmp/sm_XXXXXX", sizeof(char))); buildmap(prog, names, exprs, types, NULL, Precision, NDIM, TRUE); xstr = execmap(prog); if (get_tag_ok(xstr, "History")) skip_item(xstr); get_history(xstr); ostr = stropen(getparam("out"), "w"); put_history(ostr); new_field(&GroupField, IntType, "Group"); new_field(&GroupField + 1, NULL, NULL); layout_body(btags, Precision, NDIM); while (get_snap(xstr, &bodytab, &nbody, &tbody, itags, FALSE)) { snaptrak(); put_snap(ostr, &traktab, &ntrak, &tbody, otags); if (ntrak != nold) eprintf("[%s: wrote %d groups at t = %f]\n", getprog(), ntrak, tbody); nold = ntrak; } strclose(ostr); if (unlink(prog) != 0) error("%s: can't unlink %s\n", getprog(), prog); return (0); }
int main(int argc, string argv[]) { string prog, itags[MaxBodyFields]; stream xstr; int nprof, nbody, nsamp; real *prof1, *prof2, rrange[2], tnow; bodyptr btab = NULL; initparam(argv, defv); layout_body(fields, Precision, NDIM); prog = mktemp((string) copxstr("/tmp/sm_XXXXXX", sizeof(char))); xstr = execmap(prog); get_history(xstr); nprof = getiparam("nprof"); prof1 = (real *) allocate((2 + nprof) * sizeof(real)); prof2 = (real *) allocate((2 + nprof) * sizeof(real)); setrange(rrange, getparam("rrange")); nsamp = 0; while (get_snap(xstr, &btab, &nbody, &tnow, itags, FALSE)) { setprofile(prof1, prof2, nprof, rrange, btab, nbody); nsamp++; } if (unlink(prog) != 0) error("%s: can't unlink %s\n", getargv0(), prog); if (nsamp == 0) error("%s: no data in input\n", getargv0()); listdensity(prof1, prof2, nprof, rrange, nsamp); return (0); }
int main(int argc, string argv[]) { string *mdtab, *names, *exprs, prog; int nexp = 0, i, j; initparam(argv, defv); mdtab = getmapdefs(); // get list of mapping vars for (i = 0; mdtab[i] != NULL; i += 2) if (getparamstat(mdtab[i]) & ARGPARAM) // if var has assigned value nexp++; eprintf("[%s: %scounted %d variable assignments]\n", getprog(), nexp > 0 ? "" : "warning: ", nexp); names = (string *) allocate(sizeof(string *) * (nexp + 1)); exprs = (string *) allocate(sizeof(string *) * (nexp + 1)); for (i = j = 0; mdtab[i] != NULL; i += 2) if (getparamstat(mdtab[i]) & ARGPARAM) { // if var has assigned value exprs[j] = getparam(mdtab[i]); // list value given as expr names[j] = mdtab[i+1]; // and name of access macro j++; } exprs[j] = names[j] = NULL; prog = mktemp((string) copxstr("/tmp/sm_XXXXXX", sizeof(char))); buildmap(prog, names, exprs, NULL, strnull(getparam("t")) ? NULL : getparam("t"), Precision, NDIM, TRUE); execmap(prog); if (unlink(prog) != 0) error("%s: can't unlink %s\n", getargv0(), prog); return (0); }
int main(int argc, string argv[]) { string prog, itags[MaxBodyFields], otags[MaxBodyFields]; stream xstr, ostr; bodyptr btab = NULL; int nbody; real tnow; initparam(argv, defv); exprs[0] = getparam("weight"); prog = mktemp((string) copxstr("/tmp/sm_XXXXXX", sizeof(char))); buildmap(prog, names, exprs, types, NULL, Precision, NDIM, TRUE); xstr = execmap(prog); if (get_tag_ok(xstr, "History")) skip_item(xstr); get_history(xstr); ostr = stropen(getparam("out"), "w"); put_history(ostr); new_field(&WeightField, RealType, "Weight"); new_field(&WeightField + 1, NULL, NULL); while (get_snap(xstr, &btab, &nbody, &tnow, itags, TRUE)) { snaprect(btab, nbody); del_tag(otags, itags, "Weight"); put_snap(ostr, &btab, &nbody, &tnow, otags); } strclose(ostr); if (unlink(prog) != 0) error("%s: can't unlink %s\n", getargv0(), prog); return (0); }
int main(int argc, string argv[]) { string prog, coords, itags[MaxBodyFields], otags[MaxBodyFields]; stream xstr, ostr; bodyptr btab = NULL, bp; int nbody; real tnow; vector cmpos, cmvel, cmacc; initparam(argv, defv); exprs[0] = getparam("weight"); prog = mktemp((string) copxstr("/tmp/sm_XXXXXX", sizeof(char))); buildmap(prog, names, exprs, types, NULL, Precision, NDIM, TRUE); xstr = execmap(prog); if (get_tag_ok(xstr, "History")) skip_item(xstr); get_history(xstr); ostr = stropen(getparam("out"), "w"); put_history(ostr); coords = getparam("coords"); new_field(&WeightField, RealType, "Weight"); new_field(&WeightField + 1, NULL, NULL); while (get_snap(xstr, &btab, &nbody, &tnow, itags, TRUE, NULL)) { if (scanopt(coords, PosTag) && set_member(itags, PosTag)) { snapcmpos(cmpos, btab, nbody, WeightField.offset); for (bp = btab; bp < NthBody(btab, nbody); bp = NextBody(bp)) { SUBV(Pos(bp), Pos(bp), cmpos); } eprintf("[%s: centroid position: %f,%f,%f]\n", getprog(), cmpos[0], cmpos[1], cmpos[2]); } if (scanopt(coords, VelTag) && set_member(itags, VelTag)) { snapcmvel(cmvel, btab, nbody, WeightField.offset); for (bp = btab; bp < NthBody(btab, nbody); bp = NextBody(bp)) { SUBV(Vel(bp), Vel(bp), cmvel); } eprintf("[%s: centroid velocity: %f,%f,%f]\n", getprog(), cmvel[0], cmvel[1], cmvel[2]); } if (scanopt(coords, AccTag) && set_member(itags, AccTag)) { snapcmacc(cmacc, btab, nbody, WeightField.offset); for (bp = btab; bp < NthBody(btab, nbody); bp = NextBody(bp)) { SUBV(Acc(bp), Acc(bp), cmacc); } eprintf("[%s: cen. acceleration: %f,%f,%f]\n", getprog(), cmacc[0], cmacc[1], cmacc[2]); } del_tag(otags, itags, "Weight"); put_snap(ostr, &btab, &nbody, &tnow, otags); } strclose(ostr); if (unlink(prog) != 0) error("%s: can't unlink %s\n", getprog(), prog); return (0); }
void *getxstr(stream inpt, int nbyt) { byte buf[MAXLEN], *bp; bool lpflg; int i, ch; bp = &buf[0]; // set point into buffer do { // loop reading data in lpflg = FALSE; // init loop flag for (i = 0; i < nbyt; i++) { // loop over block of bytes ch = getc(inpt); // input next byte if (bp > &buf[MAXLEN-1]) // detect overflow error error("%s.getxstr: buffer overflow\n", getprog()); *bp = ch != EOF ? ch : (int) NULL; // map EOF to NULL if (*bp++ != (byte) NULL) // got a byte of real data? lpflg = TRUE; // repeat for next block } } while (lpflg); // until a block of NULLs return (copxstr(&buf[0], nbyt)); // make copy and return it }