void* alist_applist(void *ls, void *apl) { void **x; if (!ls) return apl; for (x=apl; x?*x:0; x++) ls = alist_append(ls,*x); if (apl) alist_free(apl); return ls; }
void exampleArrayList() { ArrayList* l = newArrayList(4); alist_add(l, "Aye"); alist_add(l, "Bee"); alist_add(l, "Sea"); alist_add(l, "Dee"); alist_add(l, "Eee"); alist_add(l, "Eff"); alist_add(l, "Gee"); alist_add(l, "Ach"); alist_add(l, "Eye"); alist_add(l, "Jay"); alist_add(l, "Kay"); alist_add(l, "Ell"); printf("Size: %d\n", l->size); alist_traverseForward(l, &process); printf("\n"); printf("Remove 'Sea'\n"); alist_remove(l, "Sea", equals); alist_traverseForward(l, &process); printf("\n"); printf("Remove at 0\n"); alist_removeAt(l, 0); alist_traverseForward(l, &process); printf("\n"); printf("Remove at size\n"); alist_removeAt(l, l->size - 1); alist_traverseForward(l, &process); printf("\n"); printf("Insert 'Aye' at 0\n"); alist_insert(l, "Aye", 0); alist_traverseForward(l, &process); printf("\n"); printf("Insert 'Ell' at size\n"); alist_insert(l, "Ell", l->size); alist_traverseForward(l, &process); printf("\n"); printf("Insert 'Sea' at 3\n"); alist_insert(l, "Sea", 2); alist_traverseForward(l, &process); printf("\n"); printf("0: '%s'\n", (char*)alist_get(l, 0)); printf("3: '%s'\n", (char*)alist_get(l, 3)); alist_free(l); }
void dispose_alist_ext(void *ls, void (*ff)(void*)) { void **pt,**et,**x; if (!ls) {PROGERROREXIT("Cannot dispose NULL list!"); return;} et = alist_getend(ls); pt = et[1]; for (x=pt; x?*x:0; x++) { /* frees all elements of the list, possibly using supplied (*ff)() */ if (ff) (*ff)(*x); else FREE(*x); } if (x!=et) {PROGERROREXIT("Fatal error - probably broken list %p: %p %p !",ls,x,et);} alist_free(et); }
int frame_getoptionnum_ext(framestruc *fr, char *onm, int anc, long nout[], int nsz, long def) { optionstruc **ol; int i,r; ol = frame_getoption_ext(fr,onm,anc,0); /* gets the last option of the given name */ r = (ol?(ol[0]!=NULL):0); if (nout && r) { for (i=0; i<nsz; i++) nout[i] = def; /* extracts the option numeric parameters */ for (i=0; i<nsz && i<MAXOPTPARAMS; i++) if (OPTNPARAM(ol[0],i)!=OPTNOPARAM) nout[i] = OPTNPARAM(ol[0],i); } if (ol) alist_free(ol); return r; }
void dispose_frame_ext(framestruc *fr, int rec, int par) { framestruc **rr; int nums; DEBUG(CURDLEV+1,"Call to dispose frame %p, rec=%d, par=%d\n",fr,rec,par); FRMAGIC(fr); /* (a magic number test is used to check correct access to frames) */ nums = FRNUMSONS(fr); if (nums==-12345) {PROGERROR("Probably cycled recursion when disposing frames!"); return;} FRNUMSONS(fr) = -12345; /* recursive disposal of all sons of this frame */ if (rec && FRSONS(fr)) for (rr=FRSONS(fr); *rr; rr++) { dispose_frame_ext(*rr,1,0); /* (must not call with par==1 !!!) */ *rr = (void*)1; nums--; } if (nums!=0) {PROGERROR("Wrong number of sons %d when disposing.",nums);} /* removing from the list of sons of the parent frame */ if (par) frame_setson(fr,NULL); /* disposing the matrix, option/command lists, the the son list */ if (FRMATRIX(fr)!=NULL) dispose_ematrix(FRMATRIX(fr)); if (FROPTIONS(fr)) dispose_alist_copts(FROPTIONS(fr)); if (FRCOMMANDS(fr)) dispose_alist_copts(FRCOMMANDS(fr)); if (FRSONS(fr)) alist_free(FRSONS(fr)); if (FRNAME(fr)) FREE(FRNAME(fr)); if (FREXTENS(fr)) FREE(FREXTENS(fr)); if (FRCOMMENT(fr)) FREE(FRCOMMENT(fr)); FRMAGICCLR(fr); /* finally, trashing this frame structure */ FREE(fr); fr_alframes--; }
alist_t * load_file(char *filename) { alist_t *a, *rtop, *rbot; char *s, line[1024], *t; int linenum, not; FILE *fp; fp = fopen(filename + 7, "r"); if (fp == NULL) { fprintf(stderr, "load_file cannot open '%s'\n", filename); return NULL; } a = NULL; rtop = NULL; rbot = NULL; linenum = 0; while (fgets(line, sizeof(line) - 1, fp)) { line[sizeof(line) - 1] = '\0'; linenum++; /* * Hunt for CR/LF. If no LF, stop processing. */ s = strchr(line, '\n'); if (s == NULL) { fprintf(stderr, "%d:%s: line too long\n", linenum, filename); fclose(fp); alist_free(rtop); return NULL; } *s = '\0'; s = strchr(line, '\r'); if (s != NULL) *s = '\0'; for (t = line; isspace(*t); t++) ; if (*t == '!') { not = 1; t++; } else not = 0; /* * Remove comment markers */ for (s = t; *s; s++) { if (*s == '#') *s = '\0'; } if (!*t) continue; /* * Trim off tailing white spaces */ s = strlen(t) + t - 1; while (isspace(*s)) *s-- = '\0'; if (isdigit(*t)) { a = alist_new(4, t); a->al_not = not; if (rbot != NULL) rbot->al_next = a; else rtop = a; rbot = a; } else { fprintf(stderr, "%s: unrecognised content line %d\n", filename, linenum); } } fclose(fp); return rtop; }