static int ini_handler(__UNUSED__ void* userData, const char* section, const char* name, const char* value) { if (0 == strcmp(section, "ignore_classes")) { sm_put(gdata->ignore_classes, name, (void*)1); } else if (0 == strcmp(section, "ignore_referenced_by")) { char* endptr, *classname; IgnoreField* old = NULL; int v = strtol(value, &endptr, 10); int idx, nameLen; IgnoreField* newElement; if (*endptr != '\0') { alert("Error: Bad value for [%s] %s which is (%s)\n", section, name, value); return 0; } nameLen = strlen(name); for (idx = nameLen; idx >= 0 && name[idx] != '.'; idx--); if (idx < 0) { alert("Error: Cannot find class name for %s\n", name); return 0; } if (nameLen-idx == 1) { alert("Error: Cannot find field name for %s\n", name); return 0; } newElement = (IgnoreField*)myAlloc(sizeof(IgnoreField)); memset(newElement, 0, sizeof(*newElement)); newElement->fieldName = (char*)myAlloc(nameLen-idx); strcpy(newElement->fieldName, name+idx+1); classname = (char*)myAlloc(idx + 1); strncpy(classname, name, idx); classname[idx] = 0; newElement->threshold = v; debug("Parsing class name [%s], field [%s], limit %d\n", classname, newElement->fieldName, v); sm_get(gdata->ignore_referenced_by, classname, (void**)&old); newElement->next = old; sm_put(gdata->ignore_referenced_by, classname, newElement); myFree(classname); } return 1; }
void filetok(FILE *fp, StrMap *hash, char *filename) { if (fp == NULL) { printf("ERROR: Could not open file. %s\n", strerror(errno)); exit(1); } char *word = malloc(sizeof(char) * 256); char *token = NULL; while (fscanf(fp, "%s", word) != EOF) { TokenizerT *tokenizer = TKCreate(" ", word); while ( (token = TKGetNextToken(tokenizer)) != NULL) { sm_put(hash, token, filename); } } printf("done\n"); }
/* Places all key pairs into strmap, all lowercase keys */ void parseJSON(const char * input, int input_length, StrMap * sm){ int strFlag, i, j; char keyBuffer[256]; char valBuffer[256]; bzero(keyBuffer, sizeof keyBuffer); bzero(valBuffer, sizeof valBuffer); for(strFlag=i=0; i < input_length && input[i] != '\0'; ++i){ /*We're at the start of a string*/ if(input[i] == '"'){ /*Go until we hit the closing qoute*/ i++; for(j=0; i < input_length && input[i] != '\0' && input[i] != '"' && (unsigned int)j < sizeof keyBuffer; ++j,++i){ keyBuffer[j] = (int)input[i] > 64 && input[i] < 91 ? input[i] + 32 : input[i]; /* Lowercase Keys, only ascii */ } keyBuffer[j] = '\0'; /*find the beginning of the value *which is either a " or a number. So skip spaces and commas */ for(i++; i< (int)(sizeof valBuffer)-1 && i < input_length && input[i] != '\0' && (input[i] == ',' || input[i] == ' ' || input[i] == ':'); ++i) ; /*Skip any opening qoute */ if( i < (int)(sizeof valBuffer)-1 && input[i] != '\0' && input[i] == '"'){ i++; strFlag = 1; } for(j=0; i < input_length && input[i] != '\0' && i < (int)(sizeof valBuffer)-1; ++j,++i){ if(strFlag == 0){ if(input[i] == ' ' || input[i] == '\n' || input[i] == '}' || input[i] == ',') break; /*break out if num data*/ }else{ if( ( input[i] == '"' && input[i-1] != '\\') || input[i] == '}' ) break; } valBuffer[j] = input[i]; } valBuffer[j] = '\0'; /* Skip any closing paren. */ if(i < (int)(sizeof valBuffer) && input[i] == '"') i++; if(strlen(keyBuffer) > 0){ if(sm_put(sm, keyBuffer, valBuffer) == 0) fprintf(stderr, "Failed to copy parameters into hash table while parsing JSON Data: Key: %s, Val: %s\n", keyBuffer, valBuffer); } } strFlag = 0; } }