static int update_format_list ( char *origin, FF_BUFSIZE_PTR desc_buffer, FORMAT_LIST_HANDLE hf_list ) { FORMAT_PTR format = NULL; int error; error = make_format(origin, desc_buffer, &format); if (!error) { if (!*hf_list) { *hf_list = dll_init(); if (!*hf_list) error = ERR_MEM_LACK; } } if (!error) { if (dll_add(*hf_list)) dll_assign(format, DLL_FMT, dll_last(*hf_list)); else error = ERR_MEM_LACK; } if (error && format) ff_destroy_format(format); return(error); }
static int append_EOL_to_format ( FORMAT_PTR format ) { VARIABLE_PTR EOL_var = NULL; EOL_var = ff_create_variable("EOL"); if (!EOL_var) return(ERR_MEM_LACK); if (!dll_add(format->variables)) { ff_destroy_variable(EOL_var); return(ERR_MEM_LACK); } dll_assign(EOL_var, DLL_VAR, dll_last(format->variables)); EOL_var->type = FFV_EOL; EOL_var->start_pos = FORMAT_LENGTH(format) + 1; EOL_var->end_pos = EOL_var->start_pos; format->length = EOL_var->end_pos; EOL_var->precision = 0; format->num_vars++; return(0); }
int main(int argc, char *argv[]) { dllist_t *list = (dllist_t*) malloc(sizeof(dllist_t)); dll_init(list); dll_print(list); dll_add(list, 12); dll_print(list); dll_add(list, 12); dll_add(list, 13); dll_add(list, 14); dll_print(list); dll_remove(list, 12); dll_print(list); dll_delete(list); free(list); return 0; }
int main(void) { A* a = malloc(sizeof(A)), *b = malloc(sizeof(A)), *c = NULL; List* l = list_init(); Stack* s = stack_init(); Queue* q = queue_init(); DoubleLinkedList* d = dll_init(); printf("a: %d\nB: %d\nc: %d\n", (int)a, (int)b, (int)c); a->a1 = 1; a->a2 = 'c'; b->a1 = 2; b->a2 = 'a'; printf("\n=== LIST TEST ===\n"); list_add(l, a, 0); list_append(l, b); list_remove(l, 0); list_print(l); c = list_get(l, 0); printf("c: %d\n", (int)c); list_delete(l); printf("\n=== STACK TEST ===\n"); stack_push(s, b); stack_push(s, a); stack_pop(s); stack_print(s); c = stack_peek(s); printf("c: %d\n", (int)c); stack_delete(s); printf("\n=== QUEUE TEST ===\n"); queue_push(q, a); queue_push(q, b); queue_pop(q); queue_print(q); c = queue_peek(q); printf("c: %d\n", (int)c); queue_delete(q); printf("\n=== DOUBLE LINKED LIST TEST ===\n"); dll_add(d, b, 0); dll_prepend(d, a); dll_remove(d, 1); dll_print(d); c = dll_get(d, 0); printf("c: %d\n", (int)c); dll_delete(d); free(a); free(b); return 0; }
static int add_to_variable_list(char *text_line, FORMAT_PTR format) { VARIABLE_PTR var = NULL; char save_char = STR_END; char *token = NULL; char *endptr = NULL; int error = 0; if (!format->variables) { format->variables = dll_init(); if (!format->variables) return(ERR_MEM_LACK); } token = text_line; token = get_token(token, &save_char); if (FF_STRLEN(token)) { var = ff_create_variable(token); if (var == NULL) return ERR_MEM_LACK; #if 0 error = ERR_MEM_LACK; #endif if (var->name[0] == '"' && var->name[strlen(var->name) - 1] == '"') { memmove(var->name, var->name + 1, strlen(var->name) - 2); var->name[strlen(var->name) - 2] = STR_END; } } else { error = err_push(ERR_VARIABLE_DESC, "Expecting a variable name (\"%s\")", format->name); goto add_to_variable_list_exit; } if (!dll_add(format->variables)) { ff_destroy_variable(var); error = ERR_MEM_LACK; goto add_to_variable_list_exit; } dll_assign(var, DLL_VAR, dll_last(format->variables)); token = get_token(token, &save_char); if (FF_STRLEN(token)) { errno = 0; var->start_pos = strtol(token, &endptr, 10); if (errno || FF_STRLEN(endptr)) { error = err_push(errno ? errno : ERR_PARAM_VALUE, "Bad number for variable start position: %s", token); goto add_to_variable_list_exit; } } else { error = err_push(ERR_VARIABLE_DESC, "Expecting a start position for \"%s\"", var->name); goto add_to_variable_list_exit; } token = get_token(token, &save_char); if (FF_STRLEN(token)) { errno = 0; var->end_pos = strtol(token, &endptr, 10); if (errno || FF_STRLEN(endptr)) { error = err_push(errno ? errno : ERR_PARAM_VALUE, "Bad number for variable end position: %s", token); goto add_to_variable_list_exit; } } else { error = err_push(ERR_VARIABLE_DESC, "Expecting an end position for \"%s\"", var->name); goto add_to_variable_list_exit; } token = get_token(token, &save_char); if (FF_STRLEN(token)) { FFV_TYPE(var) = ff_lookup_number(variable_types, token); if (FFV_TYPE(var) == FF_VAR_TYPE_FLAG) { if (os_strncmpi("ARRAY", token, 5) == 0) { RESTORE_CHAR(token, save_char); save_char = STR_END; error = parse_array_variable(&token, var); if (error) goto add_to_variable_list_exit; format->type |= FF_ARRAY; } else { /* Is this a keyworded variable type? If so, remember name of keyword in record_title */ if (IS_KEYWORDED_PARAMETER(token)) { FFV_TYPE(var) = 0; assert(!var->record_title); if (var->record_title) memFree(var->record_title, "var->record_title"); var->record_title = (char *)memStrdup(token, "token"); if (!var->record_title) { error = err_push(ERR_MEM_LACK, ""); goto add_to_variable_list_exit; } } else { error = err_push(ERR_UNKNOWN_VAR_TYPE, token); goto add_to_variable_list_exit; } } } } else { error = err_push(ERR_VARIABLE_DESC, "Expecting a variable type or array description for \"%s\"", var->name); goto add_to_variable_list_exit; } token = get_token(token, &save_char); if (FF_STRLEN(token)) { errno = 0; var->precision = (short)strtol(token, &endptr, 10); if (errno || FF_STRLEN(endptr)) { error = err_push(errno ? errno : ERR_PARAM_VALUE, "Bad number for variable precision: %s", token); goto add_to_variable_list_exit; } } else { if (IS_ARRAY(var)) { error = err_push(ERR_VARIABLE_DESC, "Expecting a precision for \"%s\"", var->name); goto add_to_variable_list_exit; } } if (var->end_pos < var->start_pos) { error = err_push(ERR_VARIABLE_DESC,"End Position < Start Position\n%s", text_line); goto add_to_variable_list_exit; } /* Determine The Variable Type */ if (var->start_pos == 0 && var->end_pos == 0) { if (IS_BINARY(format)) { error = err_push(ERR_UNKNOWN_FORMAT_TYPE, "Illegal to have delimited binary format"); goto add_to_variable_list_exit; } else if (IS_ARRAY(format)) { error = err_push(ERR_UNKNOWN_FORMAT_TYPE, "Illegal to have delimited array format"); goto add_to_variable_list_exit; } format->type |= FFF_VARIED; } if (NEED_TO_CHECK_VARIABLE_SIZE(format, var)) { if (ffv_type_size(var->type) != var->end_pos - var->start_pos + 1) { char save_eol_char = STR_END; char *end_of_line = find_EOL(text_line); if (end_of_line) { save_eol_char = *end_of_line; *end_of_line = STR_END; } error = err_push(ERR_VARIABLE_SIZE,"Expecting ending position for binary field %s to be %d", var->name, var->start_pos + ffv_type_size(var->type) - 1); if (end_of_line) *end_of_line = save_eol_char; goto add_to_variable_list_exit; } } check_old_style_EOL_var(var); /* Does length of CONSTANT variable name equal length of variable? */ if (IS_CONSTANT(var) && !IS_EOL(var)) { if (FF_STRLEN(var->name) > FF_VAR_LENGTH(var)) { error = err_push(ERR_VARIABLE_SIZE, "Constant variable initializer (%s) is too long for field", var->name); goto add_to_variable_list_exit; } else if (FF_STRLEN(var->name) < FF_VAR_LENGTH(var)) error = err_push(ERR_WARNING_ONLY + ERR_VARIABLE_SIZE, "Constant variable initializer (%s) is shorter than field", var->name); } format->num_vars++; format->length = max(format->length, var->end_pos); add_to_variable_list_exit: if (error) { char *cp; char EOL_char = STR_END; /* Don't destroy variable since it will be destroyed in ff_destroy_format */ cp = find_EOL(text_line); if (cp) { EOL_char = *cp; *cp = STR_END; } error = err_push(ERR_VARIABLE_DESC + (error > ERR_WARNING_ONLY ? ERR_WARNING_ONLY : 0),text_line); if (cp) *cp = EOL_char; } RESTORE_CHAR(token, save_char); return(error); }