/* OPEN * ------------------------------------------------------------------------- */ struct gstor_open_ret gstor_open(const char * name) { struct gstor_open_ret ret; string_t petri_filename = string_concat(name,".petri",0); if (INVALID_STR(petri_filename)) goto error; string_t dfile_filename = string_concat(name,".dfile",0); if (INVALID_STR(dfile_filename)) goto error; ret.gstor_h = malloc(sizeof(gstor)); ret.gstor_h->index = petri_open(string_tochar(petri_filename)); if (ret.gstor_h->index==0) goto error; ret.gstor_h->data = dfile_open(string_tochar(dfile_filename)); if (ret.gstor_h->data==0) goto close_and_error; string_destroy(petri_filename); string_destroy(dfile_filename); return ret.result = GSTOR_OKAY, ret; close_and_error: petri_close(ret.gstor_h->index); error: return ret.result = GSTOR_ERROR, ret; }
bool test_encode_base64 (Test *test) { char *base64; char bytes[1024 * 16]; size_t i; TITLE (); CATCH (encode_base64 (NULL, 0)); CATCH (error_at (0).error != ErrorInvalidArgument); CATCH (error_at (0).code != 1); CATCH (encode_base64 ("", 0)); CATCH (error_at (0).error != ErrorInvalidArgument); CATCH (error_at (0).code != 2); memory_commit_limit (0); CATCH (encode_base64 ("abc", 3)); CATCH (error_at (0).error != ErrorFunctionCall); memory_commit_limit (ULLONG_MAX); for (i = 0; i < 1000; i++) { CATCH (!(base64 = encode_base64 ("any carnal pleasure.", 20))); CATCH (!string_equals (base64, "YW55IGNhcm5hbCBwbGVhc3VyZS4=")); string_destroy (base64); } CATCH (!random_open ()); for (i = 0; i < 1000; i++) { CATCH (!random_bytes ((unsigned char *)&bytes, 1024 * 16)); CATCH (!(base64 = encode_base64 (bytes, 1024 * 16))); CATCH (string_length (base64) < 21848); string_destroy (base64); } random_close (); PASS (); }
void irc_unqueue(irc_t *irc) { irc_queued_t *entry; size_t events = 0; /* Only when enough time has passed */ if (difftime(time(NULL), irc->lastunqueue) <= IRC_FLOOD_INTERVAL) { irc_manager_wake(irc->manager); return; } while (events != IRC_FLOOD_LINES && (entry = list_shift(irc->queue))) { const size_t targetlen = string_length(entry->target); const char *target = string_contents(entry->target); const irc_command_func_t *func = &irc_commands[entry->command]; /* If there is a payload we use the extended call */ if (entry->payload) { size_t payloadlen = string_length(entry->payload); const char *payload = string_contents(entry->payload); size_t corelen = func->baselen + targetlen + 63; /* 63 is MAX_HOST_LENGTH */ /* Split payload for 512 byte IRC line limit */ while (corelen + payloadlen > 512) { char truncate[512]; size_t size = sizeof(truncate) - corelen; strncpy(truncate, payload, size); truncate[size] = '\0'; func->extended(irc, target, truncate); /* Flood protection */ if (++events == IRC_FLOOD_LINES) { /* Construct a new partial payload */ char *move = string_move(entry->payload); entry->payload = string_create(move + (payload - move) + size); free(move); list_prepend(irc->queue, entry); break; } payloadlen -= size; payload += size; } func->extended(irc, target, payload); events++; string_destroy(entry->payload); } else { /* Otherwise we do a standard call */ func->standard(irc, target); events++; } string_destroy(entry->target); free(entry); } /* Flood protection */ if (events == IRC_FLOOD_LINES) { irc->lastunqueue = time(NULL); irc_manager_wake(irc->manager); } }
bool test_file_directory_current_path (Test *test) { char *path; TITLE (); CATCH (!string_ends_with ((path = directory_current_path ()), "/project.m/app.test.lib.core")); string_destroy (path); CATCH (!file_path_is_valid ((path = directory_current_path ()))); string_destroy (path); PASS (); }
void lib_entry_destructor(void *v) { lib_entry *e=(lib_entry *)v; free(e->path); if(e->name!=empty->str) free(e->name); if(e->group!=empty) string_destroy(e->group); if(e->album!=empty) string_destroy(e->album); }
String *string_add (String *s1, String *s2) { const size_t sz = s1->len + s2->len; String *res = hwmalloc(malloc(sz+1), sz); memcpy(res->ptr, s1->ptr, s1->len); memcpy(res->ptr+s1->len, s2->ptr, s2->len+1); string_destroy(s1); string_destroy(s2); return res; }
map_t map_destroy(map_t map){ if(map != NULL){ map_destroy(map->rightnode); map_destroy(map->leftnode); string_destroy(map->key); string_destroy(map->value); free(map); map = NULL; } return map; }
/* * Get the next token from the XML stream */ xml_token_type_t xml_get_token(xml_reader_t *xr, string_t *res) { #ifdef XMLDEBUG_PARSER xml_parser_state_t old_state = xr->state; #endif xml_token_type_t token; string_destroy(res); switch (xr->state) { default: xml_parse_error(xr, "Unexpected state %u in XML reader", xr->state); case Error: return None; case Initial: token = xml_get_token_initial(xr, res); break; case Tag: token = xml_get_token_tag(xr, res); break; } xml_debug("++ %3u %-7s %-10s (%s)\n", xr->lineCount, xml_parser_state_name(old_state), xml_token_name(token), res->string?: ""); return token; }
bool test_file_directory_find_invalid_argument (Test *test) { char *path; Directory *directory; TITLE (); CATCH (!(path = directory_current_path ())); CATCH (!string_append (&path, "/stage/find")); /* d stage/find f f1 d stage/find/d1 */ CATCH (!(directory = directory_open (path))); string_destroy (path); CATCH (directory_find_file (NULL, "file")); CATCH (error_count () != 1); CATCH (error_at (0).error != ErrorInvalidArgument); error_reset (); CATCH (directory_find_directory (NULL, "directory")); CATCH (error_count () != 1); CATCH (error_at (0).error != ErrorInvalidArgument); error_reset (); CATCH (directory_find_file (directory, NULL)); CATCH (error_count () != 1); CATCH (error_at (0).error != ErrorInvalidArgument); error_reset (); CATCH (directory_find_directory (directory, NULL)); CATCH (error_count () != 1); CATCH (error_at (0).error != ErrorInvalidArgument); error_reset (); directory_close (directory); PASS (); }
void string_move(string_t *to, string_t *from) { string_destroy(to); *to = *from; string_init(from); }
bool bbruntime_destroy(){ runtime_glfw3_destroy(); graphics_destroy(); blitz2d_destroy(); pixmap_destroy(); userlibs_destroy(); audio_fmod_destroy(); audio_destroy(); input_directinput8_destroy(); input_destroy(); timer_windows_destroy(); filesystem_windows_destroy(); filesystem_destroy(); system_windows_destroy(); bank_destroy(); system_destroy(); runtime_destroy(); enet_destroy(); sockets_destroy(); stream_destroy(); stdio_destroy(); string_destroy(); math_destroy(); event_destroy(); hook_destroy(); blitz_destroy(); return true; }
bool test_file_directory_read_4 (Test *test) { char *path; Directory *directory; TITLE (); CATCH (!(path = directory_current_path ())); CATCH (!string_append (&path, "/stage/9d_9f")); /* d stage/9d_9f/d8 d stage/9d_9f/d7 d stage/9d_9f/d1 d stage/9d_9f/d4 d stage/9d_9f/d9 d stage/9d_9f/d2 d stage/9d_9f/d5 d stage/9d_9f/d3 d stage/9d_9f/d6 */ CATCH (!(directory = directory_open (path))); string_destroy (path); CATCH (!directory_read (directory)); CATCH (!directory->directories); CATCH (!directory->files); CATCH (directory->directories->count != 9); CATCH (directory->files->count != 9); directory_close (directory); PASS (); }
bool test_file_directory_find (Test *test) { char *path; Directory *directory; Directory *found_directory; File *found_file; TITLE (); CATCH (!(path = directory_current_path ())); CATCH (!string_append (&path, "/stage/find")); /* d stage/find f f1 d stage/find/d1 */ CATCH (!(directory = directory_open (path))); string_destroy (path); CATCH (!directory_read (directory)); CATCH (!directory->directories); CATCH (!directory->files); CATCH (directory->directories->count != 1); CATCH (directory->files->count != 1); CATCH (!(found_directory = directory_find_directory (directory, "d1"))); CATCH (!string_equals (found_directory->name, "d1")); CATCH (directory_find_directory (directory, "d2")); CATCH (!(found_file = directory_find_file (directory, "f1"))); CATCH (!string_equals (found_file->name, "f1")); CATCH (directory_find_file (directory, "f2")); directory_close (directory); PASS (); }
bool test_file_writer_write_args_system_call (Test *test) { FileWriter *writer; FILE *file_readonly, *file_swap; char *path; TITLE (); CATCH (!(file_readonly = fopen ("stage/file_writer/file", "r"))); CATCH (!(path = directory_current_path ())); CATCH (!string_append (&path, "/stage/file_writer/args")); if (file_exists (path)) { CATCH (!file_remove (path)); } CATCH (!(writer = file_writer_create (path, FileWriterModeTruncate))); file_swap = writer->file; writer->file = file_readonly; CATCH (file_writer_write_args (writer, "%s", "test")); CATCH (error_count () == 0); CATCH (error_at (0).error != ErrorSystemCall); writer->file = file_swap; fclose (file_readonly); file_writer_destroy (writer); string_destroy (path); PASS (); }
bool test_file_directory_read_3 (Test *test) { char *path; Directory *directory; List *directories; List *files; TITLE (); CATCH (!(path = directory_current_path ())); CATCH (!string_append (&path, "/stage/1d_1f")); /* d stage/1d_1f f f1 d stage/1d_1f/d1 */ CATCH (!(directory = directory_open (path))); string_destroy (path); CATCH (!directory_read (directory)); CATCH (!directory->directories); CATCH (!directory->files); CATCH (directory->directories->count != 1); CATCH (directory->files->count != 1); directories = directory->directories; files = directory->files; CATCH (!directory_read (directory)); CATCH (directories != directory->directories); CATCH (files != directory->files); directory_close (directory); PASS (); }
xml_token_type_t xml_get_tag_attributes(xml_reader_t *xr, xml_node_t *node) { string_t tokenValue, attrName, attrValue; xml_token_type_t token; string_init(&tokenValue); string_init(&attrName); string_init(&attrValue); token = xml_get_token(xr, &tokenValue); while (1) { if (token == RightAngle || token == RightAngleQ || token == RightAngleSlash) break; if (token != Identifier) { xml_parse_error(xr, "Unexpected token in tag attributes"); token = None; break; } string_move(&attrName, &tokenValue); token = xml_get_token(xr, &tokenValue); if (token != Equals) { xml_node_add_attr(node, attrName.string, NULL); continue; } token = xml_get_token(xr, &tokenValue); if (token != QuotedString) { xml_parse_error(xr, "Attribute value not a quoted string!"); token = None; break; } xml_debug(" attr %s=%s\n", attrName.string, tokenValue.string); xml_node_add_attr(node, attrName.string, tokenValue.string); token = xml_get_token(xr, &tokenValue); } string_destroy(&tokenValue); string_destroy(&attrName); string_destroy(&attrValue); return token; }
void test_string(void) { string_t str; TEST_EXPECT(string_create(&str)); test_empty_string(&str); string_clear(&str); test_empty_string(&str); string_destroy(&str); TEST_EXPECT(string_create(&str)); test_empty_string(&str); string_destroy(&str); test_append_c_str(); }
void lib_destroy(lib_t *lib) { info(ll, "lib closing"); lib_write(lib); chunked_list_destroy(lib->entries); free((void*)lib->base_path); string_destroy(empty); }
bool test_file_name_from_path (Test *test) { char *name; TITLE (); CATCH (!(name = file_name_from_path ("/"))); CATCH (!string_equals (name, "/")); string_destroy (name); CATCH (!(name = file_name_from_path ("/a"))); CATCH (!string_equals (name, "a")); string_destroy (name); CATCH (!(name = file_name_from_path ("/a/bb"))); CATCH (!string_equals (name, "bb")); string_destroy (name); CATCH (!(name = file_name_from_path ("/a/bb/ccc.dddd"))); CATCH (!string_equals (name, "ccc.dddd")); string_destroy (name); PASS (); }
/* CREATE * ------------------------------------------------------------------------- */ struct gstor_create_ret gstor_create(const char * name) { struct gstor_create_ret ret; string_t petri_filename = string_concat(name,".petri",0); if (INVALID_STR(petri_filename)) goto error; string_t dfile_filename = string_concat(name,".dfile",0); if (INVALID_STR(dfile_filename)) goto error; if (petri_create(string_tochar(petri_filename))!=0) goto error; if (dfile_create(string_tochar(dfile_filename))!=0) goto error; string_destroy(petri_filename); string_destroy(dfile_filename); return ret.result = GSTOR_OKAY, ret; error: return ret.result = GSTOR_ERROR, ret; }
void test__type_get_varg_value__cstr(void** state) { _typeinfo_t t_info; string_t* pstr_result = create_string(); _test__get_type(&t_info, "char*"); t_info._t_style = _TYPE_C_BUILTIN; string_init(pstr_result); _test__type_get_varg_value__stub(pstr_result, &t_info, "linux"); assert_true(strcmp(string_c_str(pstr_result), "linux") == 0); string_destroy(pstr_result); }
bool test_file_directory_close (Test *test) { char *path; Directory *directory; TITLE (); CATCH (!(path = directory_current_path ())); CATCH (!(directory = directory_open_current ())); directory_close (directory); string_destroy (path); PASS (); }
//Load one word of given size to check for valid length; void vector_string_load_one_word_of_size(unsigned int size, Vector_String_Ptr hVectorString){ String_Ptr word = string_init_default(); FILE* file = fopen("dictionary.txt", "r"); while (string_extraction( word, file)) { if (string_get_size(word) == size) { vector_string_push_back(hVectorString, word); break; } } fclose(file); string_destroy(&word); }
bool test_file_path_is_valid (Test *test) { size_t i; char *path; TITLE (); CATCH (file_path_is_valid (NULL)); CATCH (file_path_is_valid ("")); CATCH (file_path_is_valid (" ")); CATCH (file_path_is_valid (" /")); CATCH (file_path_is_valid ("//")); CATCH (file_path_is_valid ("/a/")); CATCH (file_path_is_valid ("a")); CATCH (file_path_is_valid ("a/")); CATCH (file_path_is_valid ("/a//b")); CATCH (!file_path_is_valid ("/")); CATCH (!file_path_is_valid ("/a")); CATCH (!file_path_is_valid ("/a/b")); CATCH (!file_path_is_valid ("/a/b/c")); CATCH (!file_path_is_valid ("/aa")); CATCH (!file_path_is_valid ("/aa/bbb")); CATCH (!file_path_is_valid ("/aa/bbb/cccc")); CATCH (!(path = string_create_with_size (FILE_PATH_SIZE))); path[0] = '/'; for (i = 1; i < FILE_PATH_SIZE - 1; i++) { path[i] = 'a'; } path[FILE_PATH_SIZE - 1] = '\0'; CATCH (!file_path_is_valid (path)); string_destroy (path); CATCH (!(path = string_create_with_size (FILE_PATH_SIZE + 1))); path[0] = '/'; for (i = 1; i < FILE_PATH_SIZE; i++) { path[i] = 'a'; } path[FILE_PATH_SIZE] = '\0'; CATCH (file_path_is_valid (path)); string_destroy (path); PASS (); }
bool test_file_readline_f2 (Test *test) { Directory *directory; File *file; char *path; char *line; size_t bytes_read; TITLE (); CATCH (!(path = directory_current_path ())); CATCH (!string_append (&path, "/stage/readline")); /* d stage/readline f f2 f f3 \ \ 0 \ AB \ 012 \ ABCD \ 01234 \ ABCD \ 012 \ AB \ 0 f f1 */ CATCH (!(directory = directory_open (path))); string_destroy (path); CATCH (!directory_read (directory)); CATCH (!(file = directory_find_file (directory, "f2"))); CATCH (!file_open (file)); CATCH (!(line = string_create_with_size (1))); CATCH (!file_readline (file, line, &bytes_read)); CATCH (bytes_read != 0); CATCH (memory_size (line) != 1); directory_close (directory); string_destroy (line); PASS (); }
static void test1() { String* a = string_create(""); String* b = string_create("Hello"); String* c = string_create("World"); printf("a = \"%s\"\n", a->text); printf("b = \"%s\"\n", b->text); printf("c = \"%s\"\n", c->text); assert(strcmp(a->text, "") == 0); assert(strcmp(b->text, "Hello") == 0); assert(strcmp(c->text, "World") == 0); string_append(a, b->text); string_append(b, c->text); string_append(c, a->text); printf("a = a+b = \"%s\"\n", a->text); printf("b = b+c = \"%s\"\n", b->text); printf("c = c+a = \"%s\"\n", c->text); assert(strcmp(a->text, "Hello") == 0); assert(strcmp(b->text, "HelloWorld") == 0); assert(strcmp(c->text, "WorldHello") == 0); string_prepend(a, b->text); string_prepend(b, c->text); string_prepend(c, a->text); printf("a = b+a = \"%s\"\n", a->text); printf("b = c+b = \"%s\"\n", b->text); printf("c = a+c = \"%s\"\n", c->text); assert(strcmp(a->text, "HelloWorldHello") == 0); assert(strcmp(b->text, "WorldHelloHelloWorld") == 0); assert(strcmp(c->text, "HelloWorldHelloWorldHello") == 0); string_destroy(a); string_destroy(b); string_destroy(c); }
map_t map_put(map_t map, key_t key, value_t value){ if (map == NULL){ map = calloc(1, sizeof(struct _node_t)); map->leftnode = NULL; map->key = key; map->value = value; map->rightnode = NULL; } else if(string_eq(map->key, key)){ string_destroy(map->value); map->value = value; string_destroy(map->key); map->key = key; } else if(string_less(key, map->key)){ map->leftnode = map_put(map->leftnode, key, value); } else { map->rightnode = map_put(map->rightnode, key, value); } return map; }
int main(int argc, char* argv[]) { string_t* pstr_hello = create_string(); string_t* pstr_s = create_string(); string_iterator_t it_pos; if(pstr_hello == NULL || pstr_s == NULL) { return -1; } string_init_cstr(pstr_hello, "Hello, how are you?"); string_init_copy_range(pstr_s, string_begin(pstr_hello), string_end(pstr_hello)); for(it_pos = string_begin(pstr_s); !iterator_equal(it_pos, string_end(pstr_s)); it_pos = iterator_next(it_pos)) { printf("%c", *(char*)iterator_get_pointer(it_pos)); } printf("\n"); algo_reverse(string_begin(pstr_s), string_end(pstr_s)); printf("reverse: %s\n", string_c_str(pstr_s)); algo_sort(string_begin(pstr_s), string_end(pstr_s)); printf("ordered: %s\n", string_c_str(pstr_s)); string_erase_range(pstr_s, algo_unique(string_begin(pstr_s), string_end(pstr_s)), string_end(pstr_s)); printf("uniqued: %s\n", string_c_str(pstr_s)); string_destroy(pstr_hello); string_destroy(pstr_s); return 0; }
//Load ALL words at given length Vector_String_Ptr load_dictionary_of_size(int size){ Vector_String_Ptr dictionary = vector_string_init_default(); String_Ptr word = string_init_default(); FILE* fp = fopen("dictionary.txt","r"); while (string_extraction(word, fp)) { if (string_get_size(word) == size) { vector_string_push_back(dictionary, word); } } string_destroy(&word); fclose(fp); return dictionary; }
char *get_common(char *a, char *b) { int i, j, k; char *stra = string_copy(a); char *strb = string_copy(b); char *r = calloc(SIZE, sizeof(char)); sort(stra); sort(strb); i = j = k = 0; while(stra[i] && strb[j]) { if(stra[i] > strb[j]) ++j; else if(stra[i] < strb[j]) ++i; else { r[k++] = stra[i]; ++i, ++j; } } r[k] = '\0'; string_destroy(stra); string_destroy(strb); return r; }