bKeyValues *get_key_values(const_bstring input) { int i = 0; struct bstrList *values; struct bstrList *keyvalue; bKeyValues *keyvalues; values = bsplit(input, '&'); keyvalues = malloc(sizeof(bKeyValues)); keyvalues->entry = malloc(values->qty * sizeof(bKeyValue)); keyvalues->qty = values->qty; for(i = 0; i < values->qty; i++) { keyvalue = bsplit(values->entry[i], '='); if(keyvalue->qty == 2) { keyvalues->entry[i].key = bstrcpy(keyvalue->entry[0]); keyvalues->entry[i].value = bstrcpy(keyvalue->entry[1]); } else { printf("Invalid keyvalue: %s", values->entry[i]->data); return NULL; } bstrListDestroy(keyvalue); } bstrListDestroy(values); return keyvalues; }
int parse_streams(Workgroup* group, const_bstring str, int numberOfStreams) { struct bstrList* tokens; struct bstrList* subtokens; tokens = bsplit(str,','); if (tokens->qty < numberOfStreams) { fprintf(stderr, "Error: Testcase requires at least %d streams\n", numberOfStreams); bstrListDestroy(tokens); return -1; } group->streams = (Stream*) malloc(numberOfStreams * sizeof(Stream)); if (group->streams == NULL) { bstrListDestroy(tokens); return -1; } for (int i=0; i<numberOfStreams; i++) { subtokens = bsplit(tokens->entry[i],':'); if (subtokens->qty >= 2) { int index = str2int(bdata(subtokens->entry[0])); if ((index < 0) && (index >= numberOfStreams)) { free(group->streams); bstrListDestroy(subtokens); bstrListDestroy(tokens); return -1; } group->streams[index].domain = bstrcpy(subtokens->entry[1]); group->streams[index].offset = 0; if (subtokens->qty == 3) { group->streams[index].offset = str2int(bdata(subtokens->entry[2])); if (group->streams[index].offset < 0) { free(group->streams); bstrListDestroy(subtokens); bstrListDestroy(tokens); return -1; } } } else { fprintf(stderr, "Error in parsing stream definition %s\n", bdata(tokens->entry[i])); bstrListDestroy(subtokens); bstrListDestroy(tokens); free(group->streams); return -1; } bstrListDestroy(subtokens); } bstrListDestroy(tokens); return 0; }
void alder_align_option_free(alder_align_option_t *option) { if (option->refile != NULL) bstrListDestroy(option->refile); if (option->infile != NULL) bstrListDestroy(option->infile); if (option->outfile != NULL) bstrListDestroy(option->outfile); if (option->logfilename != NULL) bdestroy(option->logfilename); alder_vector_pair_t_free(option->pair); }
static int parse_bind_address(allium_ptcfg *cfg, const char *addrs) { struct bstrList *l; struct tagbstring str; int i, j; assert(NULL != cfg); if (NULL == addrs) { fprintf(stdout, "ENV-ERROR No Bind Addresses\n"); return (-1); } btfromcstr(str, addrs); if (0 == btlength(str)) { fprintf(stdout, "ENV-ERROR Empty Bind Address\n"); return (-1); } l = bsplit(&str, ','); if (NULL == l) { fprintf(stdout, "ENV-ERROR OOM parsing Bind Addresses\n"); return (-1); } if (l->qty != cfg->nr_methods) { fprintf(stdout, "ENV-ERROR Malformed Bind Addresses\n"); bstrListDestroy(l); return (-1); } for (i = 0; i < l->qty; i++) { /* * Per pt-spec.txt, the order is identical to the transport * list. I have no idea what is supposed to happen when * the transport list is wildcarded, so this routine will * currently fail gracefully. */ j = bstrrchr(l->entry[i], '-'); if ((j != blength(cfg->methods[i].name)) || bstrncmp(l->entry[i], cfg->methods[i].name, j - 1)) { fprintf(stdout, "ENV-ERROR Unexpected method in Bind Address\n"); bstrListDestroy(l); return (-1); } cfg->methods[i].bind_addr_len = sizeof(cfg->methods[i].bind_addr); if (parse_addr(bdataofs(l->entry[i], j + 1), (struct sockaddr *)&cfg->methods[i].bind_addr, &cfg->methods[i].bind_addr_len)) { fprintf(stdout, "ENV-ERROR Invalid address in Bind Address (%s)\n", bdata(l->entry[i])); bstrListDestroy(l); return (-1); } cfg->methods[i].has_bind_addr = 1; } bstrListDestroy(l); return (0); }
void tor_pt_free_config(tor_pt_config_t *cfg) { bdestroy(cfg->state_location); bstrListDestroy(cfg->transport_ver); if (cfg->is_client) { bstrListDestroy(cfg->client.methods); } else { /* Free the server bits */ } free(cfg); }
static int parse_transports(allium_ptcfg *cfg, const char *transports) { struct bstrList *l; struct tagbstring str; int i; assert(NULL != cfg); cfg->is_server = (NULL != transports); if (!cfg->is_server) transports = getenv(PTCFG_CLIENT_TRANSPORTS); if (NULL == transports) { fprintf(stdout, "ENV-ERROR No Transports\n"); return (-1); } btfromcstr(str, transports); if (0 == btlength(str)) { fprintf(stdout, "ENV-ERROR Empty Transport List\n"); return (-1); } l = bsplit(&str, ','); if (NULL == l) { fprintf(stdout, "ENV-ERROR OOM parsing Transports\n"); return (-1); } cfg->methods = calloc(l->qty, sizeof(*cfg->methods)); if (NULL == cfg->methods) { bstrListDestroy(l); fprintf(stdout, "ENV-ERROR OOM parsing Transports\n"); return (-1); } for (i = 0; i < l->qty; i++) { if (0 == blength(l->entry[i])) { fprintf(stdout, "ENV-ERROR Invalid Transport\n"); bstrListDestroy(l); return (-1); } cfg->methods[i].name = bstrcpy(l->entry[i]); if (NULL == cfg->methods[i].name) { fprintf(stdout, "ENV-ERROR OOM parsing Transports\n"); bstrListDestroy(l); return (-1); } cfg->nr_methods++; } return (0); }
int bstr_to_workgroup(Workgroup* group, const_bstring str, DataType type, int numberOfStreams) { int parseStreams = 0; struct bstrList* tokens; tokens = bsplit(str,'-'); bstring domain; if (tokens->qty == 2) { domain = parse_workgroup(group, tokens->entry[0], type); if (domain == NULL) { bstrListDestroy(tokens); return 1; } parse_streams(group, tokens->entry[1], numberOfStreams); bdestroy(domain); } else if (tokens->qty == 1) { domain = parse_workgroup(group, tokens->entry[0], type); if (domain == NULL) { bstrListDestroy(tokens); return 1; } group->streams = (Stream*) malloc(numberOfStreams * sizeof(Stream)); if (group->streams == NULL) { bstrListDestroy(tokens); return 1; } for (int i = 0; i< numberOfStreams; i++) { group->streams[i].domain = bstrcpy(domain); group->streams[i].offset = 0; } bdestroy(domain); } else { fprintf(stderr, "Error in parsing workgroup string %s\n", bdata(str)); bstrListDestroy(tokens); return 1; } bstrListDestroy(tokens); group->size /= numberOfStreams; return 0; }
void Action_dependency_assign(void *value, void *data) { Action *action = (Action *)value; Action *target = NULL; tst_t *targets = (tst_t *)data; int i = 0; if(!action->depends) return; debug("Processed %s action depending on: %s", bdata(action->name), bdata(action->depends)); if(action->depends) { struct bstrList *dep_list = bsplit(action->depends, ' '); for(i = 0; i < dep_list->qty; i++) { bstring dep = dep_list->entry[i]; target = (Action *)tst_search(targets, bdata(dep), blength(dep)); if(target) { Action_depends(action, target); } else { log_err("Could not find dependency %s has on %s.", bdata(action->name), bdata(dep)); } } bstrListDestroy(dep_list); } }
TSTree *add_route_data(TSTree *routes, bstring line) { struct bstrList *data = bsplit(line, ' '); // eg. /hello && Hello check(data->qty == 2, "Line '%s' does not have 2 columns", bdata(line)); // insert to routes, root is the first, the 2 and 3 is key anf length of key and value routes = TSTree_insert(routes, bdata(data->entry[0]), blength(data->entry[0]), bstrcpy(data->entry[1])); void *dl = load_dl(data->entry[1]); Handler *handler = Handler_create(bdata(data->entry[0]), dl); bstrListDestroy(data); return routes; error: return NULL; }
/* ---------------------------------------------------------- * FUNCTION : conf_module_plugin * DESCRIPTON : This function takes a string retrieved from * : the configuration file, breaks it in half * : at the ':', and then sends it to the * : appropriate function. * INPUT : 0 - Value * : 1 - Pointer to Function * RETURN : 0 - Success * : -1 - Error * ---------------------------------------------------------- */ int conf_module_plugin (bstring value, int (*ptrFunc)(bstring, bstring)) { struct bstrList *list; if (*ptrFunc == NULL) return -1; /* Split line in half. There should only be one ':'. */ if ((list = bsplit(value, ':')) != NULL) { if (list->qty > 1) { /* Input processor contains an argument. */ if ((btrim(list->entry[1])) == -1) log_message("warning: 'btrim' in function 'conf_module_processor' faild."); if (((*ptrFunc)(list->entry[0], list->entry[1])) == -1) log_message("warning: 'ptrFunc' in function 'conf_module_processor' failed."); } else { /* Input processor does not contain an argument. */ if (((*ptrFunc)(list->entry[0], bfromcstr(""))) == -1) log_message("warning: 'ptrFunc' in function 'conf_module_processor' failed."); } if (list != NULL) bstrListDestroy(list); } else { log_message("warning: 'split' in function 'conf_module_processor' failed."); } return 0; }
/* ---------------------------------------------------------- * FUNCTION : init_configuration * DESCRIPTION : This function will read in and process a * : specified configuration file. * INPUT : 0 - Config File * RETURN : None! * ---------------------------------------------------------- */ void init_configuration (bstring filename) { FILE *fp; bstring filedata; struct bstrList *lines; int i; verbose_message("config - Processing '%s'.", bdata(filename)); if ((fp = fopen(bdata(filename), "r")) == NULL) { err_message("Unable to open configuration file - %s", bdata(filename)); } /* Read file into 'filedata' and process it accordingly. */ filedata = bread ((bNread) fread, fp); if ((lines = bsplit(filedata, '\n')) != NULL) { for (i = 0; i < lines->qty; i++) { parse_line(lines->entry[i]); } } /* Clean Up */ bdestroy(filedata); bstrListDestroy(lines); fclose(fp); }
/* ---------------------------------------------------------- * FUNCTION : read_report_file * DESC : This function will read in a specified * : report CSV file. It will then break a part * : the line and add the assets to the * : specified asset data structure. * INPUT : None * RETURN : None * ---------------------------------------------------------- */ void read_report_file (output_plugin *log) { FILE *fp; bstring filedata; struct bstrList *lines; int i; printf("[*] Processing Assets from persistent file %s\n", log->path); /* Open Signature File */ if ((fp = fopen(log->path, "r")) == NULL) { printf("Unable to open CSV file - %s", log->path); } /* Read file into 'filedata' and process it accordingly. */ filedata = bread ((bNread) fread, fp); if ((lines = bsplit(filedata, '\n')) != NULL) { for (i = 0; i < lines->qty; i++) { parse_raw_report(lines->entry[i]); } } /* Clean Up */ bdestroy(filedata); bstrListDestroy(lines); fclose(fp); }
IDENT_MPTR_RAW * BetterString_$_splits_$_String(IDENT_MPTR_RAW * b_, IDENT_MPTR_RAW * s_, IDENT_MPTR_RAW * $_mptr_in) { const_bstring b = b_->obj; String * s = s_->obj; bstring sBstring = bfromcstr(s->buffer); struct bstrList * result = bsplits(b,sBstring); _$_VARIABLE(_$_temp_vector); _$_VARIABLE(_$_temp_vector_item); _$_VARIABLE(_$_temp_betterstring); //printf("Parts %d ",result->qty); Vector_$_Vector_$_int(result->qty, _$_temp_vector, false, BetterString); /* Vector_$_putObjectAtIndex_$_Generic_$$_$_Generic_$$(_$v$_arr, _$v$_idx, _$v$_primIdx, _$v$_elem, _$v$_primElem,\ _$v$_mptr, _$v$_primRet, _$v$_typeRet) */ int i; for (i=0;i<result->qty;i++) { BetterString_$_BetterString_$_BetterString_$_(bstrcpy(result->entry[i]),_$_temp_betterstring); Vector_$_putObjectAtIndex_$_Generic_$$_$_Generic_$$(_$_temp_vector, i, true, _$_temp_betterstring, false, _$_temp_vector_item, false, _$_mptr); } _$_mptr_prepare(_$_temp_vector,$_mptr_in); bdestroy(sBstring); bstrListDestroy(result); return $_mptr_in; }
void str2BitMask(const char* str, BitMask* mask) { char* endptr; errno = 0; struct bstrList* tokens; bstring q = bfromcstralloc (60, str); tokens = bsplit(q,' '); for (int i=0; i<tokens->qty; i++) { uint64_t val = strtoull((char*) tokens->entry[i]->data, &endptr, 16); if ((errno == ERANGE && val == LONG_MAX ) || (errno != 0 && val == 0)) { ERROR; } if (endptr == str) { ERROR_PLAIN_PRINT(No digits were found); } mask->mask[i] = val; } bstrListDestroy(tokens); bdestroy(q); }
char *test_getlines() { bstring str = bfromcstr("one\ntwo\nthree\nfour\nfive\n"); struct bstrList *file = bsplit(str, '\n'); DArray *lines = getlines(file, 2, 4); bstring two = bfromcstr("two"); bstring three = bfromcstr("three"); bstring four = bfromcstr("four"); mu_assert(DArray_count(lines) == 3, "Wrong number of lines."); mu_assert(bstrcmp((bstring)DArray_at(lines, 0), two) == 0, "First line is wrong."); mu_assert(bstrcmp((bstring)DArray_at(lines, 1), three) == 0, "Second line is wrong."); mu_assert(bstrcmp((bstring)DArray_at(lines, 2), four) == 0, "Third line is wrong."); bstrListDestroy(file); bdestroy(str); bdestroy(two); bdestroy(three); bdestroy(four); DArray_destroy(lines); return NULL; }
static void req_free_hash(hnode_t *node, void *notused) { (void)notused; bstrListDestroy((struct bstrList *)hnode_get(node)); bdestroy((bstring)hnode_getkey(node)); free(node); }
void alder_seqid_file_free(alder_seqid_file_t *asf) { if (asf != NULL) { if (asf->filename != NULL) bstrListDestroy(asf->filename); if (asf->type != NULL) free(asf->type); if (asf->format != NULL) free(asf->format); if (asf->pair != NULL) free(asf->pair); free(asf); } else { assert(0); } }
void handleUser(cli *client, blist leftOverMessages, bstring incompleteMessage){ /*take care of any messages left over from registration*/ if (leftOverMessages->qty){ handleMessages(leftOverMessages, client); } bstrListDestroy(leftOverMessages); blist messages; bstring raw; incompleteMessage = incompleteMessage ? incompleteMessage : safe_BfromCstr(""); /*recv messages until you receive a nick and user msg*/ while (1){ raw = safeRecv(client); messages = extractMessages(raw, &incompleteMessage); if (messages){ handleMessages(messages, client); bstrListDestroy(messages); } } /*exit pthread*/ }
/*! * @brief Create an empty list with preallocated storage for at least 'min' members */ struct bstrList *bstrListCreateMin(int min) { struct bstrList *sl = NULL; if ((sl = bstrListCreate()) == NULL) return NULL; if ((bstrListAlloc(sl, min)) != BSTR_OK) { bstrListDestroy(sl); return NULL; } return sl; }
char *test_bsplit() { bstring bstr = bfromcstr("aaaaLaaaaaaaL"); struct bstrList *list = bsplit(bstr, 'L'); mu_assert(list != NULL, "bsplit failed"); mu_assert(biseqStatic(list->entry[0], "aaaa") == 1, "bsplit first part should be aaaa"); mu_assert(biseqStatic(list->entry[1], "aaaaaaa") == 1, "bsplit second part should be aaaaaaa"); mu_assert(biseqStatic(list->entry[2], "") == 1, "bsplit third part is empty string"); mu_assert(list->qty == 3, "bsplit length error"); bdestroy(bstr); bstrListDestroy(list); return NULL; }
TSTree* add_route_data(TSTree* routes, bstring line) { struct bstrList* data = bsplit(line, ' '); check(data->qty == 2, "Line '%s' does not have 2 columns", bdata(line)); routes = TSTree_insert(routes, bdata(data->entry[0]), blength(data->entry[0]), bstrcpy(data->entry[1])); bstrListDestroy(data); return routes; error: return NULL; }
static int bstrListDestroy_safe(struct bstrList *sl) { int i; if (NULL != sl) for (i = 0; i < sl->qty; i++) { if (NULL == sl->entry[i]->data) continue; allium_scrub(sl->entry[i]->data, sl->entry[i]->mlen); } return (bstrListDestroy(sl)); }
char *test_bsplit(void) { struct bstrList *strlist = NULL; bstring b = bfromcstr("peter liu,, yahoo"); mu_assert(b != NULL, "Failed to create bstring on bsplit()."); strlist = bsplit(b, ' '); mu_assert(strlist->qty == 3, "Wrong split substring quy."); mu_assert(strcmp((const char *)strlist->entry[0]->data, "peter") == 0, "Wrong 1st split substring."); mu_assert(strcmp((const char *)strlist->entry[1]->data, "liu,,") == 0, "Wrong 2nd split substring."); mu_assert(strcmp((const char *)strlist->entry[2]->data, "yahoo") == 0, "Wrong 3nd split substring."); mu_assert(bstrListDestroy(strlist) == BSTR_OK, "Failed to destroy string list on bsplit()."); mu_assert(bdestroy(b) == BSTR_OK, "Failed to bdestroy() afetr bsplit()."); return NULL; }
static int nodeDistanceList(int node, int numberOfNodes, uint32_t** list) { FILE *fp; bstring filename; int count = 0; bstring src; struct bstrList* tokens; *list = (uint32_t*) malloc(numberOfNodes * sizeof(uint32_t)); if (!(*list)) { return -ENOMEM; } /* the distance interface should be always there */ filename = bformat("/sys/devices/system/node/node%d/distance", node); if (NULL != (fp = fopen (bdata(filename), "r"))) { src = bread ((bNread) fread, fp); tokens = bsplit(src,' '); for (int i=0; i<(tokens->qty); i++) { if (count < numberOfNodes) { (*list)[count] = (uint32_t)strtoul((char*) (tokens->entry[i]->data), NULL, 10); } else { ERROR_PRINT(Number Of nodes %d too large,count); return -EFAULT; } count++; } bstrListDestroy(tokens); bdestroy(src); bdestroy(filename); fclose(fp); return count; } /* something went wrong */ return -1; }
/* ---------------------------------------------------------- * FUNCTION : init_identification * DESCRIPTION : This function will read the signature file * : into the signature data structure. * INPUT : 0 - Data Structure * RETURN : -1 - Error * : 0 - Normal Return * ---------------------------------------------------------- */ int load_servicefp_file(char *sigfile, signature **db, int len) { FILE *fp; bstring filename; bstring filedata; struct bstrList *lines; int i; (void)(len); // doesn't matter /* * Check for a PADS_SIGNATURE_LIST file within the current directory. */ if ((fp = fopen(TCP_SIGNATURE_LIST, "r")) != NULL) { filename = bformat("%s", sigfile); fclose(fp); } else { filename = bformat(sigfile); } /* * Open Signature File */ if ((fp = fopen(bdata(filename), "r")) == NULL) { printf("Unable to open signature file - %s\n", bdata(filename)); return 1; } /* * Read file into 'filedata' and process it accordingly. */ filedata = bread((bNread) fread, fp); if ((lines = bsplit(filedata, '\n')) != NULL) { for (i = 0; i < lines->qty; i++) { parse_raw_signature(lines->entry[i], i + 1, db); } } /* * Clean Up */ bdestroy(filename); bdestroy(filedata); bstrListDestroy(lines); fclose(fp); return 0; }
const char* glswGetShaders(const char* combinedKeys) { glswContext* gc = __glsw__Context; int tokenIndex=0; struct bstrList* tokens; bstring effectKeys; glswList* pCombinedEffect; effectKeys = bfromcstr(combinedKeys); tokens = bsplit(effectKeys, '+'); pCombinedEffect = gc->CombinedEffects; while (pCombinedEffect) { if (1 == biseq(pCombinedEffect->Key, effectKeys)) break; pCombinedEffect = pCombinedEffect->Next; } if(!pCombinedEffect) { glswList* temp = gc->CombinedEffects; gc->CombinedEffects = (glswList*) calloc(sizeof(glswList), 1); if(gc->CombinedEffects) { gc->CombinedEffects->Key = bstrcpy(effectKeys); gc->CombinedEffects->Value = bfromcstr(""); gc->CombinedEffects->Next = temp; for (tokenIndex = 0; tokenIndex < tokens->qty; tokenIndex++) { bstring token = tokens->entry[tokenIndex]; bconcat(gc->CombinedEffects->Value, bfromcstr(glswGetShader(token->data))); //bdestroy(token); } bstrListDestroy(tokens); bdestroy(effectKeys); return (char*)gc->CombinedEffects->Value->data; } else return 0; } else return (char*)pCombinedEffect->Value->data; }
char *test_Request_speeds() { int i = 0; FILE *infile = fopen("tests/request_payloads.txt", "r"); mu_assert(infile != NULL, "Failed to open the tests/request_payloads.txt test file."); struct bstrList *list = bstrListCreate(); bstrListAlloc(list, 300); // load up all the ones we can for(i = 0; i < 300; i++, list->qty++) { bstring sender = bgets((bNgetc)fgetc, infile, ' '); bstring conn_id = bgets((bNgetc)fgetc, infile, ' '); bstring path = bgets((bNgetc)fgetc, infile, ' '); list->entry[i] = bgets((bNgetc)fgetc, infile, '\n'); // stop if we didn't read anything if(!(sender && conn_id && path)) break; bdestroy(sender); bdestroy(conn_id); bdestroy(path); } fclose(infile); // now rip through and parse them for speed test int j = 0; for(j = 0; j < 200; j++) { for(i = 0; i < list->qty; i++) { tns_value_t *val = tns_parse(bdata(list->entry[i]), blength(list->entry[i]), NULL); mu_assert(val->type == tns_tag_string || val->type == tns_tag_dict, "Got an invalid data chunk from file."); size_t len = 0; char *payload = tns_render(val, &len); mu_assert(len > 0, "Failed to render the payload."); free(payload); tns_value_destroy(val); } } bstrListDestroy(list); return NULL; }
TSTree *add_route_data(TSTree *routes , bstring line ) /* take line from load route func , break into two parts * store into the tstree , first as key , second as data */ { struct bstrList *data = bsplit(line , ' '); check(data->qty == 2 , "line '%s' does not have 2 columns" , bdata(line)); routes = TSTree_insert(routes , bdata(data->entry[0] ), blength(data->entry[0]), bstrcpy(data->entry[1])); bstrListDestroy(data); return routes; error: return NULL; }
unsigned int glswGetShadersAlt(const char* combinedKeys, const char** pArray, unsigned int maxItems) { glswContext* gc = __glsw__Context; int tokenIndex=0; struct bstrList* tokens; bstring effectKeys; int idx = 0; effectKeys = bfromcstr(combinedKeys); tokens = bsplit(effectKeys, '+'); for (tokenIndex = 0; tokenIndex < tokens->qty; tokenIndex++) { bstring token = tokens->entry[tokenIndex]; pArray[idx++] = glswGetShader(token->data); //bdestroy(token); } bdestroy(effectKeys); bstrListDestroy(tokens); return idx; }
bstring resolve_path(STATE, bstring path) { char *absolute_path = malloc(PATH_MAX); bstring h = executable_name(state->binary); struct bstrList *x = bsplit(h, '/'); bdestroy(h); bstring slash = bfromcstr("/"); x->qty--; h = bjoin(x, slash); x->qty++; bstrListDestroy(x); bconcat(h, slash); bconcat(h, path); bdestroy(slash); realpath(bdata(h), absolute_path); bstring bpath = bfromcstr(absolute_path); bdestroy(h); free(absolute_path); return bpath; }