array * string_wildcard_expand(char *str) { glob_t g; int i, j; int flags, flags_init; int retval; string *s, *p; array *t; s = string_new(str); t = string_tokenize_with_quotes(s, NULL, NULL); if(array_length(t) == 0) { fprintf(stderr, "odd number of quotes found\n"); return NULL; } flags_init = GLOB_NOSORT | GLOB_BRACE | GLOB_TILDE; for(j = 0; j < array_length(t); j++) { flags = (j == 0) ? flags_init : (flags_init | GLOB_APPEND); p = array_element(t,j); #ifdef __UNIT_TESTING_DEBUG__ fprintf(stderr, "path: %s\n", string_string(p)); #endif if(string_length(p) > 0) { retval = glob(string_string(array_element(t,j)), flags, string_wildcard_expand_error, &g); switch(retval) { case GLOB_NOSPACE: fprintf(stderr, "Error allocating memory for file search\n"); break; case GLOB_ABORTED: fprintf(stderr, "Error was encountered for \"%s\"\n", string_string(p)); break; case GLOB_NOMATCH: fprintf(stderr, "No matches were found for \"%s\"\n", string_string(p)); break; } #ifdef __UNIT_TESTING_DEBUG__ fprintf(stderr, "retval: %d\n", retval); fprintf(stderr, "path: %d\n", g.gl_pathc); fprintf(stderr, "matches: %d\n", g.gl_matchc); #endif /* __UNIT_TESTING_DEBUG_ _ */ } } for(i = 0; i < g.gl_pathc; i++) { fprintf(stderr, "string[%.3d/%.3d]: <%s>\n", i, strlen(g.gl_pathv[i]), g.gl_pathv[i]); } globfree(&g); return NULL; }
int64_t query_index(query_t *query, int64_t id) { string_t *sql = string_new(); char *where = build_filters(query); sqlite3_stmt *stmt; int64_t result; int64_t index = 1; string_append(sql, query->format->index); string_append(sql, query->format->from); string_append(sql, query->format->join); string_append(sql, where); free(where); if (string_size(query->order) > 0) { string_appendf(sql, " ORDER BY %s", string_string(query->order)); } musicd_log(LOG_DEBUG, "query", "%s", string_string(sql)); if (sqlite3_prepare_v2(db_handle(), string_string(sql), -1, &stmt, NULL) != SQLITE_OK) { musicd_log(LOG_ERROR, "query", "can't prepare '%s': %s", string_string(sql), db_error()); string_free(sql); return -1; } string_free(sql); bind_filters(query, stmt); while (1) { result = sqlite3_step(stmt); if (result == SQLITE_DONE) { result = 0; goto finish; } if (result != SQLITE_ROW) { musicd_log(LOG_ERROR, "query", "query_count: sqlite3_step failed"); result = -1; goto finish; } if (sqlite3_column_int64(stmt, 0) == id) { result = index; goto finish; } ++index; } finish: sqlite3_finalize(stmt); return result; }
int main() { int x; const char big[] = "this is a haystack find the needle!"; const char small[] = "needle"; x = string_string(big, small); printf("found the [%d]\n", x); return 0; }
static void find_protocol(client_t *client) { protocol_t **protocol; for (protocol = protocols; *protocol != NULL; ++protocol) { if ((*protocol)->detect(string_string(client->inbuf), string_size(client->inbuf)) == 1) { break; } } client->protocol = *protocol; }
int64_t query_count(query_t *query) { string_t *sql = string_new(); char *where = build_filters(query); sqlite3_stmt *stmt; int64_t result; string_append(sql, query->format->count); string_append(sql, query->format->from); string_append(sql, query->format->join); string_append(sql, where); free(where); musicd_log(LOG_DEBUG, "query", "%s", string_string(sql)); if (sqlite3_prepare_v2(db_handle(), string_string(sql), -1, &stmt, NULL) != SQLITE_OK) { musicd_log(LOG_ERROR, "query", "can't prepare '%s': %s", string_string(sql), db_error()); string_free(sql); return -1; } string_free(sql); bind_filters(query, stmt); result = sqlite3_step(stmt); if (result != SQLITE_ROW) { musicd_log(LOG_ERROR, "query", "query_count: sqlite3_step failed"); result = -1; goto finish; } result = sqlite3_column_int64(stmt, 0); finish: sqlite3_finalize(stmt); return result; }
int query_start(query_t *query) { string_t *sql = string_new(); char *where = build_filters(query); sqlite3_stmt *stmt; string_append(sql, query->format->body); string_append(sql, query->format->from); string_append(sql, query->format->join); string_append(sql, where); free(where); if (string_size(query->order) > 0) { string_appendf(sql, " ORDER BY %s", string_string(query->order)); } if (query->limit > 0 || query->offset > 0) { string_appendf(sql, " LIMIT %" PRId64 " OFFSET %" PRId64 "", query->limit, query->offset); } musicd_log(LOG_DEBUG, "query", "%s", string_string(sql)); if (sqlite3_prepare_v2(db_handle(), string_string(sql), -1, &stmt, NULL) != SQLITE_OK) { musicd_log(LOG_ERROR, "query", "can't prepare '%s': %s", string_string(sql), db_error()); string_free(sql); return -1; } string_free(sql); bind_filters(query, stmt); query->stmt = stmt; return 0; }
void test(string *s, char *b) { int pass; char *a; a = string_string(s); pass = strcmp(a,b); if(pass != 0) { fprintf(stderr, "Test: %.3d/%.3d %s\n", now, tests, (pass == 0) ? "PASS" : "FAIL" ); } if(!retval) { retval = pass; } if(pass != 0) { fprintf(stderr, "\t<%s> <%d>\n\t<%s>\n", b, pass, a); } now = now + 1; }
static int write_data(client_t *client) { int n; n = write(client->fd, string_string(client->outbuf), string_size(client->outbuf)); if (n < 0) { if (errno == EWOULDBLOCK) { /* It would block right now, ignore */ return 0; } musicd_perror(LOG_INFO, "client", "%s: can't write data", client->address); return -1; } string_remove_front(client->outbuf, n); return 0; }
int client_process(client_t *client) { int result; while ((result = read_data(client)) > 0) { } if (result < 0) { return result; } if (!client->protocol) { /* The client has no protocol detected yet */ find_protocol(client); if (!client->protocol) { musicd_log(LOG_ERROR, "client", "%s: unknown protocol, terminating", client->address); return -1; } musicd_log(LOG_DEBUG, "client", "%s: protocol is '%s'", client->address, client->protocol->name); /* Actually open the client to be processed with detected protocol */ client->self = client->protocol->open(client); } if (client->state == CLIENT_STATE_WAIT_TASK) { /* Client was waiting for task to finish and now the task manager signaled * through the pipe. */ client->state = CLIENT_STATE_NORMAL; task_free(client->wait_task); if (client->wait_callback(client->self, client->wait_data) < 0) { return -1; } } /* (Try to) purge the entire outgoing buffer. */ if (string_size(client->outbuf) > 0) { /* There is outgoing data in buffer, try to write */ result = write_data(client); if (result < 0) { return result; } } if (client->state == CLIENT_STATE_DRAIN) { if (string_size(client->outbuf) == 0) { /* Client was draining, and now it is done - terminate */ return -1; } } /* If there was nothing to write but we have unprocessed data, process it. */ if (string_size(client->inbuf) > 0) { result = client->protocol->process(client->self, string_string(client->inbuf), string_size(client->inbuf)); if (result < 0) { return result; } string_remove_front(client->inbuf, result); } else if (client->state == CLIENT_STATE_FEED && string_size(client->outbuf) == 0) { /* There wasn't anything to process, we can push data to the client and the * outgoing buffer is empty. */ result = client->protocol->feed(client->self); if (result < 0) { return result; } } return 0; }
int main(int argc, char *argv[]) { string *s = NULL; string *s2 = NULL; string *s3 = NULL; s = string_append(s, "Hello There"); test(s, "Hello There"); s = string_append(s, "123"); test(s, "Hello There123"); s = string_insert(s, 1, "<new>"); test(s, "H<new>ello There123"); s = string_insert(s, 23, "<new>"); test(s, "H<new>ello There123<new>"); s = string_remove(s, 11, 5); test(s, "H<new>ello 123<new>"); s = string_replace(s, "<new>", "<nothing>"); test(s, "H<nothing>ello 123<nothing>"); s = string_remove(s, 0, 30); test(s, ""); s = string_append(s, "H<nothing>ello 123<nothing>"); test(s, "H<nothing>ello 123<nothing>"); s = string_trunc(s); test(s, ""); string_free(&s); s = string_append_int(s, 1); test(s, "1"); s = string_append_int(s, 10); test(s, "110"); s = string_append_int(s, 100); test(s, "110100"); s = string_append_int(s, 1000000000); test(s, "1101001000000000"); s = string_append_int(s, -2); test(s, "1101001000000000-2"); s = string_append_int(s, -345); test(s, "1101001000000000-2-345"); s = string_append_int(s, -3.45); test(s, "1101001000000000-2-345-3"); s = string_trunc(s); test(s,""); s = string_prepend_int(s, 1); test(s, "1"); s = string_prepend_int(s, 10); test(s, "101"); s = string_prepend_int(s, 100); test(s, "100101"); s = string_prepend_int(s, 1000000000); test(s, "1000000000100101"); s = string_prepend_int(s, -2); test(s, "-21000000000100101"); s = string_prepend_int(s, -345); test(s, "-345-21000000000100101"); s = string_prepend_int(s, -3.45); test(s, "-3-345-21000000000100101"); s = string_trunc(s); test(s,""); string_free(&s); s = string_new("\"Quoted String\""); test(s, "\"Quoted String\""); s = string_replace(s, "\"", ""); test(s, "Quoted String"); string_free(&s); s = string_prepend(s, "Nothing"); test(s, "Nothing"); s = string_prepend(s, "12345678901234567890"); test(s, "12345678901234567890Nothing"); s = string_prepend(s, "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"); test(s, "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890Nothing"); string_free(&s); s = string_append(s, "Nothing"); test(s, "Nothing"); s = string_append(s, "12345678901234567890"); test(s, "Nothing12345678901234567890"); s = string_append(s, "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"); test(s, "Nothing12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"); string_free(&s); s = string_new("printf_append"); test(s, "printf_append"); s = string_printf_append(s, "%8s %3d %10.6f", "string", 20, 3.4); test(s, "printf_append string 20 3.400000"); string_free(&s); s = string_new(""); s = string_printf(s, "%8s %3d %10.6f", "string", 20, 3.4); test(s, " string 20 3.400000"); s2 = string_copy(s); test(s2, " string 20 3.400000"); testi(string_equal_char(s, string_string(s2)), 1); testi(string_equal(s, s2), 1); testi(string_equal_char(s, " string 20 3.40"), 0); s3 = string_new(" string 20 3.40"); testi(string_equal(s, s3), 0); string_free(&s2); string_free(&s3); s = string_remove(s, 100000, 20); test(s, " string 20 3.400000"); s = string_remove(s, 15, 20); test(s, " string 20 "); s = string_remove(s, 13, -1); test(s, " string 20 "); s = string_remove(s, 1, 1); test(s, " string 20 "); s = string_remove(s, 7, 2); test(s, " string20 "); s = string_remove(s, 9, 1); test(s, " string20"); s = string_remove(s, -1, -1); test(s, ""); s = string_insert(s, -1, " string20"); test(s, " string20"); s = string_insert(s, 9, " "); test(s, " string20 "); s = string_insert(s, 7, " "); test(s, " string 20 "); s = string_insert(s, 1, " "); test(s, " string 20 "); s = string_insert(s, 13, " "); test(s, " string 20 "); s = string_insert(s, 15, "3.400000"); test(s, " string 20 3.400000"); s = string_insert(s, 10000, " morestuff"); test(s, " string 20 3.400000 morestuff"); s = string_insert(s, -1, " morestuff"); test(s, " morestuff string 20 3.400000 morestuff"); s2 = string_substr(s, 1, 4); test(s2, "more"); string_free(&s2); s2 = string_substr(s, 5, 7); test(s2, "stuff "); string_free(&s2); string_free(&s); return retval; }