void run_linked_list_tests() { char* inputs[] = { "item1", "item2", "item3", }; int num_inputs = sizeof(inputs) / sizeof(char*); char* data; // after init LinkedList *ll = ll_new(); assert("Starts empty", ll_is_empty(ll)); assert("Initial size is 0", ll_size(ll)==0); assert("Peek returns null", ll_peek(ll)==NULL); // after pushing one item ll_push(ll, inputs[0]); assert("Is not empty after pushing an item", !ll_is_empty(ll)); assert("Has size of 1 after pushing an item", ll_size(ll)==1); assert("Peeking returns the item we pushed", ll_peek(ll)==inputs[0]); // after two items ll_push(ll, inputs[1]); assert("Is not empty after pushing a second item", !ll_is_empty(ll)); assert("Has size of 2 after pushing a second item", ll_size(ll)==2); assert("Peeking returns the second item", ll_peek(ll)==inputs[1]); // after three items ll_push(ll, inputs[2]); assert("Is not empty after pushing a third item", !ll_is_empty(ll)); assert("Has size of 3 after pushing a third item", ll_size(ll)==3); assert("Peeking returns the third item", ll_peek(ll)==inputs[2]); // iterating through the items int index = num_inputs; int all_match = 1; ll_each(ll, char* input, ({ all_match &= (inputs[--index] == input); })); assert("It iterates the correct number of times", index==0); assert("The item provided matched each time", all_match); // popping an item data = ll_pop(ll); assert("It is not empty after popping the third item", !ll_is_empty(ll)); assert("Has size of 2 after popping the third item", ll_size(ll)==2); assert("Peeking returns the second item", ll_peek(ll)==inputs[1]); // cleanup ll_free(ll); }
void *thr_time(void *arg__) { ArgTime *arg= arg__; tstamp t0 = nanotime(arg->clock), t0epoch = nanoepoch(), lastt = t0, curt; Tok *t; while (!inQ->EOT) { t = ll_pop(inQ); nlock_lock(t->edit); ll_push(timeQ, t); curt = nanotime(arg->clock); t->sstart = curt - t0; t->slast = curt - lastt; t->epoch = t->sstart - t0epoch; nlock_unlock(t->edit); lastt = curt; } timeQ->EOT = true; return 0; }
bool ll_append(node_t *head, int val) { node_t n = *head; node_t new_node = NULL; if (!n) { ll_push(head, val); return true; } n = get_tail(n); new_node = mem_alloc(sizeof(struct node_s)); new_node->val = val; new_node->next = NULL; n->next = new_node; return true; }
void *thr_read(void *arg__) { ArgRead *arg = arg__; Tok *t; while (feof(arg->F)==0) { // Each token (mostly lines) t = newTok(); nlock_lock(t->edit); fjoin(arg->F); // Wait for the stream to have data ll_push(inQ, t); // Pass on for timestamp // Now read the data getItok(arg->F, arg->delim, t->str); nlock_unlock(t->edit); } inQ->EOT = true; return 0; }
// set a pair key value. static int hash_add(hash_t *hashp, char *key, void *valuep, bool copy) { hash_key_t md = hash_compute_md(hashp, key); linked_list_t *listp = NULL; if (hashp->array[md] == NULL) { listp = ll_create(); hashp->array[md] = listp; } else { listp = hashp->array[md]; ll_node_t *nodep = listp->first; while (nodep) { hash_pair_t *pairp = (hash_pair_t *) nodep->valuep; if (EQUALS(key, pairp->key)) { hashp->cfg->free_func(pairp->value); if (copy) { pairp->value = hashp->cfg->copy_func(valuep); } else { pairp->value = valuep; } return 0; } nodep = nodep->nextp; } } hash_pair_t *pairp = (hash_pair_t *) malloc(sizeof(hash_pair_t)); pairp->key = strdup(key); if (copy) { pairp->value = hashp->cfg->copy_func(valuep); } else { pairp->value = valuep; } ll_push(listp, pairp); dll_push(hashp->dlistp, pairp); pairp->nodep = hashp->dlistp->first; return 0; }
pll_entry lexer_parse_brwords(pll_entry tokens, plinkedlist* list) { *list = ll_new(); ptoken t = GETTKN(tokens); char even = 0; CHECK_TOKEN(t, TOKEN_BRSTART); tokens = NEXTTKN(tokens); t = GETTKN(tokens); while(t->base.type != TOKEN_BREND) { if(!even) { CHECK_TOKEN(t, TOKEN_WORD); ll_push(*list, t->string); } else CHECK_TOKEN(t, TOKEN_COMMA); even = !even; tokens = NEXTTKN(tokens); t = GETTKN(tokens); } return tokens; }
/* * Recycle a buffer when not needing it any more. */ void rcyclbuf(IBuf *ibuf) { ll_push(recyclr, ibuf); }
int main(int argc, char** argv){ if(argc == 1){ printf("Please specify input file\n"); exit(1); } FILE* f = fopen(argv[1], "r"); if(!f){ printf("File not found: %s\n", argv[1]); exit(1); } int pc = 0; /* Position in stream of current instruction */ char mem[1024]; /* Memory array */ char* dp = mem; /* Data pointer */ bool debug = false; int line = 1; for(int i=0; i < 1024; ++i) mem[i] = 0; int c; do{ c = getc(f); pc++; if (debug) printf("Character: %c, line: %d\n", c, line); switch(c){ case '\n': if(debug) printf("Line %d\n", line); line += 1; break; case '>': dp++; break; case '<': dp--; break; case '+': (*dp)++; break; case '-': (*dp)--; break; case '.': if(debug) printf("Print statement: %d\n", line); printf("%c", *dp); break; case ',': (*dp) = getchar(); break; case '[': if(*dp == 0){ /* If pointer is zero, find matching closing bracket */ int depth = 1; do{ c = getc(f); pc++; if(debug){ printf("depth = %d\n", depth); printf("%c", c); } if(c == 91) depth += 1; else if(c == 93) depth -=1; else if(c == 10) line += 1; else if(c == EOF){ printf("Unmatched bracket at %d", top->pos); exit(1); } } while(depth != 0); } else{ /* else push the position onto the stack and move to next instruction */ ll_push(pc, line); if(debug){ printf("[ not taken\n"); print_stack(); } } break; case ']': if(*dp){ /* Loop back to beginning, but don't remove loop start point from stack */ struct ll* st = ll_peek(); pc = st->pos; line = st->line; fseek(f, pc, SEEK_SET); } else{ /* Finished looping, remove loop start from stack */ ll_del(); if(debug){ printf("Popped stack"); print_stack(); } } break; } } while (c!=EOF); printf("\n"); }
plinkedlist lexer_parse(char* source) { plinkedlist tokens = ll_new(); char inSlComment = 0, inMlComment = 0; int l, currline = 1; char* curr, *start; while(*curr!='\0') { if(*curr=='\n') { currline++; if(inSlComment) //single inSlComment = !inSlComment; } else if(isspace(*curr)) { /* curr++; continue; */ //<-- same as leaving execution to go on (jumping through ifelses has the same effect) } else if(*curr=='/') { if(!inSlComment && !inMlComment) { if(curr[1]=='/') inSlComment = !inSlComment; else if(curr[1]=='*') inMlComment = !inMlComment; } } else if(*curr=='*' && !inSlComment && inMlComment) { if(curr[1]=='/') inMlComment = !inMlComment; } else if(inSlComment || inMlComment) { /* curr++; continue; */ //<-- same as leaving execution to go on (jumping through ifelses has the same effect) } else if(isdigit(*curr)) { start = curr; while(isdigit(*++curr)) ; /* to point on the last character of token */ curr--; l = (int)(curr - start+1); start = (char*)malloc(l+1); strncpy(start, curr-l+1, l); start[l] = '\0'; ll_push(tokens, lexer_token_create(TOKEN_NUMBER, start, currline)); } else if(*curr=='"') { start = ++curr; /* skip first doublequote */; char slashactive = 0; while(*curr!='\0' && !(*curr=='"' && !slashactive)) { if(*curr=='\\') slashactive = !slashactive; else if(slashactive) slashactive = 0; if(*curr=='\n') currline++; curr++; } /* curr pointing on the last character of token */ l = (int)(curr - start +1); start = (char*)malloc(l+1); strncpy(start, curr-l+1, l); start[l] = '\0'; ll_push(tokens, lexer_token_create(TOKEN_STRING, start, currline)); } else if(isalpha(*curr) || *curr=='_') { /* nothing found here, it's not a constant token */ /* could it be an identifier? */ start = curr; do { curr++; } while(isalnum(*curr) || *curr=='_'); l = (int)(curr - start); start = (char*)malloc(l+1); strncpy(start, curr-l, l); start[l] = '\0'; /* to ensure curr points on characters last token */ curr--; int i; for(i = 0;keywords[i].base.type != TOKEN_EOL;i++) if(*start==*keywords[i].string) /* if the first character equals, check the full string */ if(strcmp(start, keywords[i].string) == 0) { ll_push(tokens, lexer_token_copy(&keywords[i], currline)); /* (we don't need the local copy of the string anymore) */ free(start); break; } /* if they aren't equal, continue */ /* if the first character isn't equal, continue searching */ /* if this is not a keyword, it's a word (indentfier) */ if(keywords[i].base.type == TOKEN_EOL) ll_push(tokens, lexer_token_create(TOKEN_WORD, start, currline)); } else { int i; for(i = 0;operators[i].base.type != TOKEN_EOL;i++) if(strncmp(operators[i].string, curr, strlen(operators[i].string)) == 0) { ll_push(tokens, lexer_token_copy(&operators[i], currline)); curr += strlen(operators[i].string)-1; break; } if(operators[i].base.type == TOKEN_EOL) FATAL("Invalid token '%c' found on line %d", *curr, currline); } curr++; } /* mark the EOF */ ll_push(tokens, lexer_token_create(TOKEN_EOF, NULL, currline)); /* return the linked list with tokens */ return tokens; }
void ls_iteration(double *F, double *phi, double* label, long* dims, LL* Lz, LL* Ln1, LL* Lp1, LL *Ln2, LL *Lp2, LL *Lin2out, LL* Lout2in){ int x,y,z,i,idx; int u,d,r,l,f,b; double p, phi_old; LL *Sz, *Sn1, *Sp1, *Sn2, *Sp2; // create 'changing status' lists Sz = ll_create(); Sn1 = ll_create(); Sp1 = ll_create(); Sn2 = ll_create(); Sp2 = ll_create(); // #1) Normalize F double Fmax = .001; for(i=0;i<Lz->length;i++){ if(fabs(F[i])>Fmax) Fmax = fabs(F[i]); } for(i=0;i<Lz->length;i++){ F[i] = F[i]/Fmax*0.4; } // #2) add F to phi(Lz), create Sn1 & Sp1 // ======== // (a) scan Lz values [-2.5 -1.5)[-1.5 -.5)[-.5 .5](.5 1.5](1.5 2.5] ll_init(Lz); i = 0; while(Lz->curr != NULL){ x= Lz->curr->x; y= Lz->curr->y; z= Lz->curr->z; idx= Lz->curr->idx; phi_old = phi[idx]; phi[idx] = phi[idx]+F[i]; //check to see if point crossed interface if(phi_old<=0 && phi[idx]>0 ){ ll_pushnew(Lin2out,x,y,z,idx); } if(phi_old>0 && phi[idx]<=0){ ll_pushnew(Lout2in,x,y,z,idx); } if(phi[idx] > .5){ ll_push(Sp1, ll_remcurr(Lz)); } else if(phi[idx] < -.5){ ll_push(Sn1, ll_remcurr(Lz)); } else{ ll_step(Lz); } i++; //increment index into F } if(F!= NULL) mxFree(F); // free F (no longer needed); // #3) update Ln1,Ln2,Lp1,Lp2 // ========== // (c) scan Ln1 values [-2.5 -1.5)[-1.5 -.5)[-.5 .5](.5 1.5](1.5 2.5] ll_init(Ln1); while(Ln1->curr != NULL){ x= Ln1->curr->x; y= Ln1->curr->y; z= Ln1->curr->z; idx= Ln1->curr->idx; p = ls_max_hood_onlevel(idx, x, y, z, dims, phi,label,0); if(p>=-0.5){ // found something phi[idx] = p-1; if(phi[idx]>=-0.5){ ll_push(Sz,ll_remcurr(Ln1)); } else if(phi[idx]<-1.5){ ll_push(Sn2,ll_remcurr(Ln1)); } else ll_step(Ln1); } else{ ll_push(Sn2,ll_remcurr(Ln1)); } } // ======== // (c) scan Lp1 values [-2.5 -1.5)[-1.5 -.5)[-.5 .5](.5 1.5](1.5 2.5] ll_init(Lp1); while(Lp1->curr != NULL){ x= Lp1->curr->x; y= Lp1->curr->y; z= Lp1->curr->z; idx= Lp1->curr->idx; p = ls_min_hood_onlevel(idx, x, y, z, dims, phi,label,0); if(p<=0.5){ // found something phi_old = phi[idx]; phi[idx] = p+1; if(phi[idx]<=0.5){ ll_push(Sz,ll_remcurr(Lp1)); } else if(phi[idx]>1.5){ ll_push(Sp2,ll_remcurr(Lp1)); } else ll_step(Lp1); } else{ ll_push(Sp2,ll_remcurr(Lp1)); } } // =========== // (c) scan Ln2 values [-2.5 -1.5)[-1.5 -.5)[-.5 .5](.5 1.5](1.5 2.5] ll_init(Ln2); while(Ln2->curr != NULL){ x = Ln2->curr->x; y = Ln2->curr->y; z = Ln2->curr->z; idx= Ln2->curr->idx; p = ls_max_hood_onlevel(idx, x, y, z, dims, phi,label,-1); if(p>=-1.5){ // found something phi[idx] = p-1; if(phi[idx]>=-1.5){ ll_push(Sn1,ll_remcurr(Ln2)); } else if(phi[idx]<-2.5){ ll_remcurr_free(Ln2); phi[idx] = -3; label[idx] = -3; } else ll_step(Ln2); } else{ ll_remcurr_free(Ln2); phi[idx] = -3; label[idx] = -3; } } // ========= // (d) scan Lp2 values [-2.5 -1.5)[-1.5 -.5)[-.5 .5](.5 1.5](1.5 2.5] ll_init(Lp2); while(Lp2->curr != NULL){ x = Lp2->curr->x; y = Lp2->curr->y; z = Lp2->curr->z; idx= Lp2->curr->idx; p = ls_min_hood_onlevel(idx, x, y, z, dims, phi,label,1); if(p<=1.5){ // found something phi[idx] = p+1; if(phi[idx]<=1.5){ ll_push(Sp1,ll_remcurr(Lp2)); } else if(phi[idx]>2.5){ ll_remcurr_free(Lp2); phi[idx] = 3; label[idx] = 3; } else ll_step(Lp2); } else{ ll_remcurr_free(Lp2); phi[idx] = 3; label[idx] = 3; } } // #4) Deal with S-lists Sz,Sn1,Sp1,Sn2,Sp2 // (a) Scan Sz ll_init(Sz); while(Sz->curr != NULL){ idx= Sz->curr->idx; ll_push(Lz,ll_remcurr(Sz)); label[idx] = 0; } // (b) Scan Sn1 ll_init(Sn1); while(Sn1->curr != NULL){ x = Sn1->curr->x; y = Sn1->curr->y; z = Sn1->curr->z; idx = Sn1->curr->idx; ll_push(Ln1,ll_remcurr(Sn1)); label[idx] = -1; if(((y+1)<DIMY) && phi[idx+OFFY]==-3){ ll_pushnew(Sn2,x,y+1,z,idx+OFFY); phi[idx+OFFY] = phi[idx] - 1; }//up if(((y-1)>=0) && phi[idx-OFFY]==-3){ ll_pushnew(Sn2,x,y-1,z,idx-OFFY); phi[idx-OFFY] = phi[idx] - 1; }//down if(((x+1)<DIMX) && phi[idx+OFFX]==-3){ ll_pushnew(Sn2,x+1,y,z,idx+OFFX); phi[idx+OFFX] = phi[idx] - 1; }//right if(((x-1)>=0) && phi[idx-OFFX]==-3){ ll_pushnew(Sn2,x-1,y,z,idx-OFFX); phi[idx-OFFX] = phi[idx] - 1; }//left if(((z+1)<DIMZ) && phi[idx+OFFZ]==-3){ ll_pushnew(Sn2,x,y,z+1,idx+OFFZ); phi[idx+OFFZ] = phi[idx] - 1; }//front if(((z-1)>=0) && phi[idx-OFFZ]==-3){ ll_pushnew(Sn2,x,y,z-1,idx-OFFZ); phi[idx-OFFZ] = phi[idx] - 1; }//back } // (c) Scan Sp1 ll_init(Sp1); while(Sp1->curr != NULL){ x = Sp1->curr->x; y = Sp1->curr->y; z = Sp1->curr->z; idx=Sp1->curr->idx; ll_push(Lp1,ll_remcurr(Sp1)); label[idx] = 1; if(((y+1)<DIMY) && phi[idx+OFFY]==3){ ll_pushnew(Sp2,x,y+1,z,idx+OFFY); phi[idx+OFFY] = phi[idx] + 1; }//up if(((y-1)>=0) && phi[idx-OFFY]==3){ ll_pushnew(Sp2,x,y-1,z,idx-OFFY); phi[idx-OFFY] = phi[idx] + 1; }//down if(((x+1)<DIMX) && phi[idx+OFFX]==3){ ll_pushnew(Sp2,x+1,y,z,idx+OFFX); phi[idx+OFFX] = phi[idx] + 1; }//right if(((x-1)>=0) && phi[idx-OFFX]==3){ ll_pushnew(Sp2,x-1,y,z,idx-OFFX); phi[idx-OFFX] = phi[idx] + 1; }//left if(((z+1)<DIMZ) && phi[idx+OFFZ]==3){ ll_pushnew(Sp2,x,y,z+1,idx+OFFZ); phi[idx+OFFZ] = phi[idx] + 1; }//front if(((z-1)>=0) && phi[idx-OFFZ]==3){ ll_pushnew(Sp2,x,y,z-1,idx-OFFZ); phi[idx-OFFZ] = phi[idx] + 1; }//back } // (d) Scan Sn2 ll_init(Sn2); while(Sn2->curr != NULL){ idx = Sn2->curr->idx; ll_push(Ln2,ll_remcurr(Sn2)); label[idx] = -2; } // (e) Scan Sp2 ll_init(Sp2); while(Sp2->curr != NULL){ idx = Sp2->curr->idx; ll_push(Lp2,ll_remcurr(Sp2)); label[idx] = 2; } ll_destroy(Sz); ll_destroy(Sn1); ll_destroy(Sp1); ll_destroy(Sn2); ll_destroy(Sp2); }
int main(int argc, char** argv) { llist *l = malloc(sizeof(llist)); const int reference[11] = {20, 19, 18, 14, 12, 10, 8, 6, 4, 2, -1}; const int size = sizeof(reference) / sizeof(int); int error = 1; // Add numbers 20, 18, 16, 14, ... 2, 0 to the list. { int i; for (i = 20; i >= 0; i -= 2) { ll_push(l, i); } } if (ll_size(l) != 11) { return error; } ll_print(l); error++; ll_pop(l); ll_push(l, -1); ll_insert(l, 1, 19); ll_remove(l, 3); printf("Phase 2 complete (pop, push, insert, remove).\n"); // Check first and last index. if (l -> first -> value != reference[0]) { return error; } printf("Phase 3A complete (check first value).\n"); error++; if (l -> last -> value != reference[size - 1]) { return error; } printf("Phase 3B complete (check last value).\n"); error++; // Check by traversing. { node *n = l -> first; int index = 0; while (n != NULL) { if (n -> value == reference[index++]) { n = n -> next; } else { return error; } } } printf("Phase 4A complete (traverse forward).\n"); error++; // Check by backward traversing. { node *n = l -> last; int index = size - 1; while (n != NULL) { if (n -> value == reference[index--]) { n = n -> prev; } else { return error; } } } printf("Phase 4B complete (traverse backward).\n"); error++; // Check by indexing. { int i; for (i = 0; i < ll_size(l); i++) { if (reference[i] != ll_get(l, i)) { return error; } } } printf("Phase 5 complete (iterate).\n"); error++; printf("Completed without error.\n"); return 0; }
void ll_pushnew(LL *list, long x, long y, long z, long idx){ if(list == NULL) return; PT* add = pt_create(x,y,z,idx); if(add == NULL) return; ll_push(list,add); }
static void init_conf_items(void) { struct conf_item *item; conf_items = ll_create(); item = calloc(1, sizeof(*item)); item->name = strdup("Interface"); item->type = t_sep; ll_push(conf_items, "*", item); item = calloc(1, sizeof(*item)); item->name = strdup("Interface"); item->cfname = strdup("interface"); item->type = t_list; item->v.i = &conf.if_idx; item->list = if_list; ll_push(conf_items, "*", item); item = calloc(1, sizeof(*item)); item->name = strdup("Cisco-style MAC addresses"); item->cfname = strdup("cisco_mac"); item->type = t_list; item->v.i = &conf.cisco_mac; item->list = on_off_names; ll_push(conf_items, "*", item); item = calloc(1, sizeof(*item)); item->name = strdup("Scan sort type"); item->cfname = strdup("sort_order"); item->type = t_list; item->v.i = &conf.scan_sort_order; item->list = sort_order; ll_push(conf_items, "*", item); item = calloc(1, sizeof(*item)); item->name = strdup("Scan sort in ascending order"); item->cfname = strdup("sort_ascending"); item->type = t_list; item->v.i = &conf.scan_sort_asc; item->list = on_off_names; ll_push(conf_items, "*", item); item = calloc(1, sizeof(*item)); item->name = strdup("Statistics updates"); item->cfname = strdup("stat_updates"); item->type = t_int; item->v.i = &conf.stat_iv; item->min = 10; item->max = 4000; item->inc = 10; item->unit = strdup("ms"); ll_push(conf_items, "*", item); item = calloc(1, sizeof(*item)); item->name = strdup("Histogram update cycles"); item->cfname = strdup("lhist_slot_size"); item->type = t_int; item->v.i = &conf.slotsize; item->min = 1; item->max = 64; item->inc = 1; ll_push(conf_items, "*", item); item = calloc(1, sizeof(*item)); item->name = strdup("Level meter smoothness"); item->cfname = strdup("meter_smoothness"); item->type = t_int; item->v.i = &conf.meter_decay; item->min = 0; item->max = 99; item->inc = 1; item->unit = strdup("%"); ll_push(conf_items, "*", item); item = calloc(1, sizeof(*item)); item->name = strdup("Dynamic info updates"); item->cfname = strdup("info_updates"); item->type = t_int; item->v.i = &conf.info_iv; item->min = 1; item->max = 60; item->inc = 1; item->unit = strdup("s"); ll_push(conf_items, "*", item); /* level scale items */ item = calloc(1, sizeof(*item)); item->type = t_sep; ll_push(conf_items, "*", item); item = calloc(1, sizeof(*item)); item->name = strdup("Level scales"); item->type = t_sep; ll_push(conf_items, "*", item); item = calloc(1, sizeof(*item)); item->name = strdup("Override scale autodetect"); item->cfname = strdup("override_auto_scale"); item->type = t_list; item->v.i = &conf.override_bounds; item->list = on_off_names; ll_push(conf_items, "*", item); item = calloc(1, sizeof(*item)); item->name = strdup("Minimum signal level"); item->cfname = strdup("min_signal_level"); item->type = t_int; item->v.i = &conf.sig_min; item->min = -100; item->max = -39; item->inc = 1; item->unit = strdup("dBm"); item->dep = &conf.override_bounds; ll_push(conf_items, "*", item); item = calloc(1, sizeof(*item)); item->name = strdup("Maximum signal level"); item->cfname = strdup("max_signal_level"); item->type = t_int; item->v.i = &conf.sig_max; item->min = -40; item->max = -10; item->inc = 1; item->unit = strdup("dBm"); item->dep = &conf.override_bounds; ll_push(conf_items, "*", item); item = calloc(1, sizeof(*item)); item->name = strdup("Minimum noise level"); item->cfname = strdup("min_noise_level"); item->type = t_int; item->v.i = &conf.noise_min; item->min = -120; item->max = -70; item->inc = 1; item->unit = strdup("dBm"); item->dep = &conf.override_bounds; ll_push(conf_items, "*", item); item = calloc(1, sizeof(*item)); item->name = strdup("Maximum noise level"); item->cfname = strdup("max_noise_level"); item->type = t_int; item->v.i = &conf.noise_max; item->min = -69; item->max = -40; item->inc = 1; item->unit = strdup("dBm"); item->dep = &conf.override_bounds; ll_push(conf_items, "*", item); /* thresholds */ item = calloc(1, sizeof(*item)); item->name = strdup("Low threshold action"); item->cfname = strdup("lo_threshold_action"); item->type = t_list; item->v.i = &conf.lthreshold_action; item->list = action_items; ll_push(conf_items, "*", item); item = calloc(1, sizeof(*item)); item->name = strdup("Low threshold"); item->cfname = strdup("lo_threshold"); item->type = t_int; item->v.i = &conf.lthreshold; item->min = -120; item->max = -60; item->inc = 1; item->unit = strdup("dBm"); item->dep = &conf.lthreshold_action; ll_push(conf_items, "*", item); item = calloc(1, sizeof(*item)); item->name = strdup("High threshold action"); item->cfname = strdup("hi_threshold_action"); item->type = t_list; item->v.i = &conf.hthreshold_action; item->list = action_items; ll_push(conf_items, "*", item); item = calloc(1, sizeof(*item)); item->name = strdup("High threshold"); item->cfname = strdup("hi_threshold"); item->type = t_int; item->v.i = &conf.hthreshold; item->min = -59; item->max = 120; item->inc = 1; item->unit = strdup("dBm"); item->dep = &conf.hthreshold_action; ll_push(conf_items, "*", item); /* start-up items */ item = calloc(1, sizeof(*item)); item->type = t_sep; ll_push(conf_items, "*", item); item = calloc(1, sizeof(*item)); item->name = strdup("Startup"); item->type = t_sep; ll_push(conf_items, "*", item); item = calloc(1, sizeof(*item)); item->name = strdup("Use transparent background"); item->cfname = strdup("transparent_bg"); item->type = t_list; item->v.i = &conf.transparent_bg; item->list = on_off_names; item->hidden = true; ll_push(conf_items, "*", item); item = calloc(1, sizeof(*item)); item->name = strdup("Startup screen"); item->cfname = strdup("startup_screen"); item->type = t_list; item->v.i = &conf.startup_scr; item->list = screen_names; ll_push(conf_items, "*", item); /* separator (dummy entry) */ item = calloc(1, sizeof(*item)); item->type = t_sep; ll_push(conf_items, "*", item); /* functions */ item = calloc(1, sizeof(*item)); item->name = strdup("Save configuration"); item->type = t_func; item->v.fp = write_cf; ll_push(conf_items, "*", item); }
static void write_cf(void) { char tmp[0x100], rv[0x40]; struct conf_item *ci = NULL; char *lp, *cp; int add, i; char *cfname = get_confname(); int cfld = ll_create(); FILE *fd = fopen(cfname, "w"); if (fd == NULL) err_sys("failed to open configuration file '%s'", cfname); for (ll_reset(conf_items); (ci = ll_getall(conf_items)); ) { if (ci->type != t_sep && ci->type != t_func && (!ci->dep || (ci->dep && *ci->dep))) { switch (ci->type) { case t_int: sprintf(rv, "%d", *ci->v.i); break; case t_list: if (!argv_count(ci->list)) continue; sprintf(rv, "%s", ci->list[*ci->v.i]); str_tolower(rv); break; case t_sep: case t_func: break; } add = 1; for (i = 0; i < ll_size(cfld); i++) { lp = ll_get(cfld, i); cp = lp += strspn(lp, " "); if (!strncasecmp(cp, ci->cfname, strcspn(cp, " =")) && strlen(ci->cfname) == strcspn(cp, " =")) { add = 0; cp += strcspn(cp, "=") + 1; cp += strspn(cp, " "); strncpy(tmp, cp, strcspn(cp, " #\n")); if (strcasecmp(tmp, rv)) { strncpy(tmp, lp, strcspn(lp, " =")); tmp[strcspn(lp, " =")] = '\0'; strcat(tmp, " = "); strcat(tmp, rv); strcat(tmp, "\n"); ll_replace(cfld, i, "s", tmp); } } } if (add) { strcpy(tmp, ci->cfname); strcat(tmp, " = "); strcat(tmp, rv); strcat(tmp, "\n"); ll_push(cfld, "s", tmp); } } } for (ll_reset(cfld); (lp = ll_getall(cfld)); ) fputs(lp, fd); fclose(fd); ll_destroy(cfld); free(cfname); }