static void parse_url_params(const char *buffer, size_t length, Array *names, Array *values) { int is_name = 1; String *name = NULL, *value = NULL; for (size_t i = 0; i < length; ++i) { char ch = buffer[i]; if (is_name == 1) { if (name == NULL) { name = newString(); } if (ch == '=') { is_name = 0; arrayAdd(names, name); name = NULL; continue; } if (ch == '&') { //Empty value is_name = 1; arrayAdd(names, name); arrayAdd(values, newString()); name = NULL; continue; } stringAppendChar(name, ch); continue; } if (is_name == 0) { if (value == NULL) { value = newString(); } if (ch == '&') { is_name = 1; arrayAdd(values, value); value = NULL; continue; } stringAppendChar(value, ch); continue; } } if (is_name == 1 && name != NULL) { //Query ended with a name arrayAdd(names, name); arrayAdd(values, newString()); } else if (is_name == 0 && value != NULL) { //Query ended with a value arrayAdd(values, value); } else if (is_name == 0 && value == NULL) { //Query ended with an empty value arrayAdd(values, newString()); } }
static void readMaterial(const uint8_t* ptr, a3dsDataT* a3ds) { chunkT* chunk = (chunkT*)ptr; ptr = chunk->data; assert(chunk->id == 0xafff); a3dsMaterialDataT* material = calloc(1, sizeof(a3dsMaterialDataT)); arrayAdd(a3ds->materials, &material); material->ambient = (vec3) { 0.0f, 0.0f, 0.0f }; material->diffuse = (vec3) { 0.5f, 0.5f, 0.5f }; material->specular = (vec3) { 1.0f, 1.0f, 1.0f }; material->shininess = 10.0f; void* chunk_end = (uint8_t*)chunk + chunk->length; while (ptr < chunk_end) { chunk = (chunkT*)ptr; if (chunk->id == 0xa000) material->name = (string*)chunk->data; else if (chunk->id == 0xa010) material->ambient = readColorChunk((uint8_t*)chunk->data); else if (chunk->id == 0xa020) material->diffuse = readColorChunk((uint8_t*)chunk->data); else if (chunk->id == 0xa030) material->specular = readColorChunk((uint8_t*)chunk->data); else if (chunk->id == 0xa040) material->shininess = readPercentageChunk((uint8_t*)chunk->data); else if (chunk->id == 0xa200) readMaterialTexture(ptr, material); ptr = (uint8_t*)chunk + chunk->length; } }
static Formula *parseFormula(Parser *p) { Stmt *stmt; Array stmts; arrayInit(&stmts, sizeof(Stmt *), 8); do { p->tok = lexerGetToken(p->lex, &p->tokval, &p->toklen); if (p->tok == TK_ID) { stmt = parseStmt(p); if (stmt) { Stmt **p = (Stmt **)arrayAdd(&stmts); *p = stmt; } else if (p->isquit){ goto end; } } else if (p->tok == TK_EOF) { break; } else { if (handleParserError(p, 1, "解析formula,不识别符号")) { break; } } } while (1); end: #ifdef LOG_PARSE info("解析得到formula\n"); #endif return formulaNew(&stmts); }
static ExprList *parseExprList(Parser *p) { bool ok = true; Array args; arrayInit(&args, sizeof(Node *), 4); do { Node *e = parseExpr(p); if (e) { Node **p = (Node **)arrayAdd(&args); if (p) { *p = e; } else { ok = false; break; } } else { ok = false; if (handleParserError(p, 1, "解析exprList出错")) break; } if (p->tok != TK_COMMA) break; p->tok = lexerGetToken(p->lex, &p->tokval, &p->toklen); } while (1); if (ok) { #ifdef LOG_PARSE info("解析得到ExprList\n"); #endif return exprListNew(&args); } return 0; }
Array arrayMakeFromArray(int length, int* array) { Array a = arrayMake(length); for(int i=0; i<length; i++) arrayAdd(array[i], a); return a; }
int main(void){ int arr1[5]; int arr2[] = {1,2,3,4,5}; int arr3[] = {1,2,3,4,5}; arrayAdd(arr1,arr2,arr3, 5); int i; for(i = 0; i < 5; i++){ printf("%d ", arr1[i]); } }
SelectableFd* selectorAdd(Selector* selector, int fd) { assert(selector != NULL); SelectableFd* selectableFd = calloc(1, sizeof(SelectableFd)); if (selectableFd != NULL) { selectableFd->selector = selector; selectableFd->fd = fd; arrayAdd(selector->selectableFds, selectableFd); } return selectableFd; }
void trieAdd(Node *root, int p, int c, int index){ arrayAdd(root, p, c, index); if(root[p].kids == NULL) { root[p].kids = (kidNode *) malloc (sizeof (kidNode) * 2); root[p].numKids = 1; root[p].kids[0].code = index; root[p].kids[0].myChar = c; } else{ int i = 0; for(i = 0; i >= 0; i++) { if(i == root[p].numKids) { break; } if(c > root[p].kids[i].myChar) { continue; } if(c < root[p].kids[i].myChar) break; } root[p].kids = (kidNode *) realloc (root[p].kids, (sizeof (kidNode) * (root[p].numKids + 1))); if(root[p].numKids != i) { int j = root[p].numKids - 1; while(j >= i) { root[p].kids[j+1].code = root[p].kids[j].code; root[p].kids[j+1].myChar = root[p].kids[j].myChar; j--; } root[p].kids[i].code = index; root[p].numKids = root[p].numKids + 1; root[p].kids[i].myChar = c; } else if(root[p].numKids == i) { root[p].kids[i].code = index; root[p].numKids = root[p].numKids + 1; root[p].kids[i].myChar = c; } } }
char * stralloc(const char *s) { sp_item_t *item,*key; if (!s) return (NULL); mutexLock(&sp_mutex); if (!stringpool) stringpool = arrayNew((array_func_t)compare_sp_items, NULL); if (!stringpool) { mutexUnlock(&sp_mutex); return (NULL); } key = (sp_item_t *)(s - offsetof(sp_item_t, str)); if ((item = (sp_item_t *)arrayFind(stringpool, key)) != NULL) { item->ref_count ++; mutexUnlock(&sp_mutex); return (item->str); } item = (sp_item_t *)calloc(1, sizeof(sp_item_t) + strlen(s)); if (!item) { mutexUnlock(&sp_mutex); return (NULL); } item->ref_count = 1; strcpy(item->str, s); arrayAdd(stringpool, item); mutexUnlock(&sp_mutex); return (item->str); }
static void readObject(const uint8_t* ptr, a3dsDataT* a3ds) { chunkT* chunk = (chunkT*)ptr; ptr = chunk->data; assert(chunk->id == 0x4000); a3dsObjectDataT* obj_data = calloc(1, sizeof(a3dsObjectDataT)); arrayAdd(a3ds->objects, &obj_data); obj_data->name = (string*)ptr; ptr += strlen(obj_data->name)+1; void* chunk_end = (uint8_t*)chunk + chunk->length; while (ptr < chunk_end) { chunk = (chunkT*)ptr; if (chunk->id == 0x4100) readTrimesh(ptr, obj_data); ptr = (uint8_t*)chunk + chunk->length; } }
void arrayAdd(int value, Array a) { if(a->count < a->length) { a->data[a->count] = value; a->count++; return; } int* tmp = malloc(sizeof(int) * a->length * 2); for(int i=0; i<a->count; i++) tmp[i] = a->data[i]; free(a->data); a->data = tmp; a->length *= 2; tmp = NULL; arrayAdd(value, a); }
int proxyServerLoadResponse(ProxyServer *p, const char *uniqueId, ResponseRecord *rec) { reset_response_record(rec); char file_name[512]; struct stat stat_buf; snprintf(file_name, sizeof(file_name), "%s/%s.res", stringAsCString(p->persistenceFolder), uniqueId); rec->fd = open(file_name, O_RDONLY); DIE(p, rec->fd, "Failed to open response file."); int status = fstat(rec->fd, &stat_buf); DIE(p, status, "Failed to get response file size."); rec->map.length = stat_buf.st_size; //Save the size //If file size is 0 then just return if (rec->map.length == 0) { return 0; } rec->map.buffer = mmap(NULL, stat_buf.st_size, PROT_READ, MAP_SHARED, rec->fd, 0); if (rec->map.buffer == MAP_FAILED) { DIE(p, -1, "Failed to map response file."); } //We are good to go. Start parsing header. String *str = newString(); int state = PARSE_PROTOCOL_VERSION; for (size_t i = 0; i < rec->map.length; ++i) { char ch = rec->map.buffer[i]; //printf("[%c %d]", ch, state); if (ch == '\r') { continue; //Ignored } if (state == PARSE_PROTOCOL_VERSION) { if (ch == ' ') { state = PARSE_STATUS_CODE; continue; } //Don't store version continue; } if (state == PARSE_STATUS_CODE) { if (ch == ' ') { state = PARSE_STATUS_MESSAGE; continue; } stringAppendChar(rec->statusCode, ch); continue; } if (state == PARSE_STATUS_MESSAGE) { if (ch == '\n') { state = PARSE_HEADER_NAME; continue; } stringAppendChar(rec->statusMessage, ch); continue; } if (state == PARSE_HEADER_NAME) { if (ch == ':') { arrayAdd(rec->headerNames, newStringWithString(str)); str->length = 0; state = PARSE_HEADER_VALUE; continue; } if (ch == '\n') { //End of request headers rec->headerBuffer.buffer = rec->map.buffer; rec->headerBuffer.length = i + 1; rec->bodyBuffer.buffer = rec->map.buffer + i + 1; rec->bodyBuffer.length = rec->map.length - rec->headerBuffer.length; //End parsing for now break; } stringAppendChar(str, ch); continue; } if (state == PARSE_HEADER_VALUE) { if (ch == '\n') { arrayAdd(rec->headerValues, newStringWithString(str)); str->length = 0; state = PARSE_HEADER_NAME; continue; } if ((str->length == 0) && ch == ' ') { //Skip leading space. continue; } stringAppendChar(str, ch); continue; } } deleteString(str); //It is safe to close the file now. It will //closed anyway by reset method. close(rec->fd); rec->fd = -1; return 0; }
int proxyServerLoadRequest(ProxyServer *p, const char *uniqueId, RequestRecord *rec) { reset_request_record(rec); char file_name[512]; struct stat stat_buf; snprintf(file_name, sizeof(file_name), "%s/%s.req", stringAsCString(p->persistenceFolder), uniqueId); rec->fd = open(file_name, O_RDONLY); DIE(p, rec->fd, "Failed to open request file."); int status = fstat(rec->fd, &stat_buf); DIE(p, status, "Failed to get request file size."); rec->map.length = stat_buf.st_size; //Save the size //If file size is 0 then just return if (rec->map.length == 0) { return 0; } rec->map.buffer = mmap(NULL, stat_buf.st_size, PROT_READ, MAP_SHARED, rec->fd, 0); if (rec->map.buffer == MAP_FAILED) { DIE(p, -1, "Failed to map request file."); } //We are good to go. Start parsing. String *str = newString(); int state = PARSE_METHOD; for (size_t i = 0; i < rec->map.length; ++i) { char ch = rec->map.buffer[i]; //printf("[%c %d]", ch, state); if (ch == '\r') { continue; //Ignored } if (state == PARSE_METHOD) { if (ch == ' ') { if (strcmp("CONNECT", stringAsCString(rec->method)) == 0) { state = PARSE_CONNECT_ADDRESS; } else { state = PARSE_PATH; } continue; } stringAppendChar(rec->method, ch); continue; } if (state == PARSE_PATH) { if (ch == '?') { state = PARSE_QUERY_STRING; continue; } if (ch == ' ') { state = PARSE_PROTOCOL_VERSION; continue; } stringAppendChar(rec->path, ch); continue; } if (state == PARSE_QUERY_STRING) { if (ch == ' ') { state = PARSE_PROTOCOL_VERSION; continue; } stringAppendChar(rec->queryString, ch); continue; } if (state == PARSE_PROTOCOL_VERSION) { if (ch == '\n') { state = PARSE_HEADER_NAME; continue; } //Don't store version continue; } if (state == PARSE_CONNECT_ADDRESS) { if (ch == ' ') { state = PARSE_PROTOCOL_VERSION; continue; } //Don't store address continue; } if (state == PARSE_HEADER_NAME) { if (ch == ':') { arrayAdd(rec->headerNames, newStringWithString(str)); str->length = 0; state = PARSE_HEADER_VALUE; continue; } if (ch == '\n') { //End of request headers rec->headerBuffer.buffer = rec->map.buffer; rec->headerBuffer.length = i + 1; rec->bodyBuffer.buffer = rec->map.buffer + i + 1; rec->bodyBuffer.length = rec->map.length - rec->headerBuffer.length; //End parsing for now break; } stringAppendChar(str, ch); continue; } if (state == PARSE_HEADER_VALUE) { if (ch == '\n') { arrayAdd(rec->headerValues, newStringWithString(str)); str->length = 0; state = PARSE_HEADER_NAME; continue; } if ((str->length == 0) && ch == ' ') { //Skip leading space. continue; } stringAppendChar(str, ch); continue; } } deleteString(str); //Parse URL parameters parse_url_params(rec->queryString->buffer, rec->queryString->length, rec->parameterNames, rec->parameterValues); //If form post then parse request body for parameters String *contentType = get_header_value(CONTENT_TYPE, rec); if (contentType != NULL && stringEqualsCString(contentType, FORM_ENC) && rec->bodyBuffer.length > 0) { parse_url_params(rec->bodyBuffer.buffer, rec->bodyBuffer.length, rec->parameterNames, rec->parameterValues); } //It is safe to close the file now. It will //closed anyway by reset method. close(rec->fd); rec->fd = -1; return 0; }