int main() { char str[1000]; scanf("%s", str); int size; int* arr = stringparse(str,&size); print_array(arr,size); }
void read_hlist(char *filename) { int64_t n, c=0; FILE *input; struct halo h = {0}; char buffer[1024]; SHORT_PARSETYPE; #define NUM_INPUTS 33 enum short_parsetype stypes[NUM_INPUTS] = { D64, D64, F, F, F, // #id desc_id mvir vmax vrms F, F, D64, F, // Rvir Rs Np x F, F, F, F, F, // y z vx vy vz F, F, F, F, F, // JX JY JZ Spin rs_klypin F, F, F, F, F, // M_all M1 M2 M3 M4 F, F, F, F, F, // Xoff Voff spin_bullock b_to_a c_to_a F, F, F, F, // A[x] A[y] A[z] T/|U| }; enum parsetype types[NUM_INPUTS]; void *data[NUM_INPUTS] = {&(h.id), &(h.descid), &(h.mvir), &(h.vmax), &(h.vrms), &(h.rvir), &(h.rs), &(h.np), &(h.pos[0]), &(h.pos[1]), &(h.pos[2]), &(h.vel[0]), &(h.vel[1]), &(h.vel[2]), &(h.J[0]), &(h.J[1]), &(h.J[2]), &(h.spin), &(h.klypin_rs), &(h.m_all), &(h.alt_m[0]), &(h.alt_m[1]), &(h.alt_m[2]), &(h.alt_m[3]), &(h.Xoff), &(h.Voff), &(h.bullock_spin), &(h.b_to_a), &(h.c_to_a), &(h.A[0]), &(h.A[1]), &(h.A[2]), &(h.kin_to_pot)}; for (n=0; n<NUM_INPUTS; n++) types[n] = stypes[n]; input = check_fopen(filename, "r"); while (fgets(buffer, 1024, input)) { if (buffer[0] == '#') { if (c==0) { c=1; buffer[strlen(buffer)-1] = 0; printf("%s PID\n", buffer); } else { printf("%s", buffer); } } n = stringparse(buffer, data, (enum parsetype *)types, NUM_INPUTS); if (n<NUM_INPUTS) continue; if (!(all_halos.num_halos%3000)) all_halos.halos = check_realloc(all_halos.halos, sizeof(struct halo)*(all_halos.num_halos+3000), "Allocating Halos."); all_halos.halos[all_halos.num_halos] = h; all_halos.num_halos++; } fclose(input); all_halos.halos = check_realloc(all_halos.halos, sizeof(struct halo)*all_halos.num_halos, "Allocating Halos."); for (n=0; n<all_halos.num_halos; n++) { all_halos.halos[n].vmax_r = all_halos.halos[n].rvir; } find_parents(all_halos.num_halos); for (n=0; n<all_halos.num_halos; n++) { struct halo *th = all_halos.halos + n; printf("%"PRId64" %"PRId64" %.3e %.2f %.2f %.3f %.3f %"PRId64" %.5f %.5f %.5f %.2f %.2f %.2f %.3e %.3e %.3e %.5f %.5f %.4e %.4e %.4e %.4e %.4e %.5f %.2f %.5f %.5f %.5f %.5f %.5f %.5f %.4f %"PRId64"\n", th->id, th->descid, th->mvir, th->vmax, th->vrms, th->rvir, th->rs, th->np, th->pos[0], th->pos[1], th->pos[2], th->vel[0], th->vel[1], th->vel[2], th->J[0], th->J[1], th->J[2], th->spin, th->klypin_rs, th->m_all, th->alt_m[0], th->alt_m[1], th->alt_m[2], th->alt_m[3], th->Xoff, th->Voff, th->bullock_spin, th->b_to_a, th->c_to_a, th->A[0], th->A[1], th->A[2], th->kin_to_pot, th->pid); } }
int cmdparse( struct cmds cmds[], char *line, void *p ){ struct cmds *cmdp; char *argv[NARG]; char **pargv; int argc,i; /* Remove cr/lf */ rip(line); for(argc = 0;argc < NARG;argc++) argv[argc] = NULL; for(argc = 0;argc < NARG;){ int qflag = FALSE; /* Skip leading white space */ while(isspace(*line & 0xff)) line++; if(*line == '\0') break; /* '#' is start of comment */ if(*line == '#') break; /* Check for quoted token */ if(*line == '"'){ line++; /* Suppress quote */ qflag = TRUE; } argv[argc++] = line; /* Beginning of token */ if(qflag){ /* Find terminating delimiter */ if((line = stringparse(line)) == NULL){ return -1; } } else { /* Find space or tab. If not present, * then we've already found the last * token. */ while(*line && !isspace(*line & 0xff)) line++; } if(*line) *line++ = 0; } if (argc < 1) { /* empty command line */ argc = 1; argv[0] = ""; } if (argv[0][0] == '?') return print_help(cmds); /* Look up command in table; prefix matches are OK */ for(cmdp = cmds;cmdp->name != NULL;cmdp++){ if(strncmp(argv[0],cmdp->name,strlen(argv[0])) == 0) break; } if(cmdp->name == NULL) { if(cmdp->argc_errmsg != NULL) printf("%s\n",cmdp->argc_errmsg); return -1; } argv[0] = cmdp->name; if(argc < cmdp->argcmin) { /* Insufficient arguments */ printf("Usage: %s\n",cmdp->argc_errmsg); return -1; } if(cmdp->func == NULL) return 0; if(cmdp->stksize == 0){ return (*cmdp->func)(argc,argv,p); } else { #ifndef SINGLE_THREADED /* Make private copy of argv and args, * spawn off subprocess and return. */ pargv = (char **)callocw(argc,sizeof(char *)); for(i=0;i<argc;i++) pargv[i] = strdup(argv[i]); newproc(cmdp->name,cmdp->stksize, (void (*)(int,void *,void *))cmdp->func,argc,pargv,p,1); #endif return 0; } }