diff options
| author | arf20 <aruizfernandez05@gmail.com> | 2025-12-02 00:00:12 +0100 |
|---|---|---|
| committer | arf20 <aruizfernandez05@gmail.com> | 2025-12-02 00:00:12 +0100 |
| commit | f982fb95044581435e31c765487b45d5ee963519 (patch) | |
| tree | 5bc5a4e47612959e00464d0f77bc622a841770d3 | |
| parent | 262c4593ac6df668ed4d4d899c68a256fb237214 (diff) | |
| download | arfnet2-search-f982fb95044581435e31c765487b45d5ee963519.tar.gz arfnet2-search-f982fb95044581435e31c765487b45d5ee963519.zip | |
search actually working :D
| -rw-r--r-- | index.c | 21 | ||||
| -rw-r--r-- | index.h | 3 | ||||
| -rw-r--r-- | index.htm.tmpl | 20 | ||||
| -rw-r--r-- | main.c | 37 | ||||
| -rwxr-xr-x | search | bin | 46312 -> 47088 bytes |
5 files changed, 63 insertions, 18 deletions
@@ -115,6 +115,13 @@ results_insert(results_t *results, const node_data_t *result) results->results[results->size++] = result; } +void +results_destroy(results_t *results) +{ + free(results->results); + free(results); +} + int index_init() { @@ -138,7 +145,7 @@ index_deinit() } map_t * -index_new(size_t icapacity, const char *dir, int examine) +index_recurse(size_t size, const char *dir, int examine, size_t rootlen) { DIR *dirp = opendir(dir); if (!dirp) { @@ -147,7 +154,7 @@ index_new(size_t icapacity, const char *dir, int examine) return NULL; } - map_t *map = map_new(icapacity); + map_t *map = map_new(size); char path[4096]; struct dirent *de = NULL; @@ -165,6 +172,7 @@ index_new(size_t icapacity, const char *dir, int examine) /* stat it */ node_data_t *data = malloc(sizeof(node_data_t)); data->name = strdup(de->d_name); + data->path = strdup(&path[rootlen]); if (stat(path, &data->stat) < 0) { fprintf(stderr, "[index] error stat() %s: %s\n", path, strerror(errno)); @@ -183,7 +191,7 @@ index_new(size_t icapacity, const char *dir, int examine) /* recurse */ map_t *child = NULL; if (de->d_type == DT_DIR) { - index_new(icapacity, path, examine); + child = index_recurse(size, path, examine, rootlen); } map_insert(map, de->d_name, data, child); @@ -192,6 +200,11 @@ index_new(size_t icapacity, const char *dir, int examine) return map; } +map_t * +index_new(size_t size, const char *dir, int examine) { + return index_recurse(size, dir, examine, strlen(dir) + 1); +} + void index_lookup_substr(map_t *index, const char *query, results_t *results) @@ -200,7 +213,7 @@ index_lookup_substr(map_t *index, const char *query, if (!index->map[i].data) continue; - for (struct node_s *node = &index->map[i]; node->next; node = node->next) { + for (struct node_s *node = &index->map[i]; node; node = node->next) { if (strstr(node->data->name, query)) results_insert(results, node->data); if (node->child) @@ -33,7 +33,7 @@ typedef enum { } lookup_type_t; typedef struct { - char *name; + const char *name, *path; struct stat stat; const char *mime; } node_data_t; @@ -50,6 +50,7 @@ void index_deinit(); index_t index_new(size_t icapacity, const char *root, int examine); results_t *index_lookup(index_t index, lookup_type_t type, const char *query); void index_destroy(index_t index); +void results_destroy(results_t *results); #endif /* _INDEX_H */ diff --git a/index.htm.tmpl b/index.htm.tmpl index 1443aa0..fd21578 100644 --- a/index.htm.tmpl +++ b/index.htm.tmpl @@ -13,19 +13,16 @@ display: block; } -/* Style the form - display items horizontally */ .form-inline { display: flex; flex-flow: row wrap; align-items: center; } -/* Add some margins for each label */ .form-inline label { } -/* Style the input fields */ .form-inline input { flex-grow: 1; vertical-align: middle; @@ -37,6 +34,20 @@ border: none; } +.result { + margin-left: 1em; + margin-bottom: 1em; +} + +.name { + font-weight: bold; + font-size: 16pt; + margin: 0; +} + +.path { + +} </style> </head> @@ -50,10 +61,11 @@ <p>Search all of the ARFNET content fast</p> <div class="box"> <form class="form-inline" action="/query" method="get"> - <input class="input" type="text" name="query"> + <input class="input" type="text" name="query" value="%s"> <button type="submit">Search</button> </form> </div> + %s </main> </body> </html> @@ -41,6 +41,27 @@ static char *index_format_template = NULL; static index_t g_index = NULL; +static const char *result_html_template = + "<div class=\"result\"><p class=\"name\">%s</p><a class=\"path\">%s</a></div>\n"; + + +static const char * +generate_results_html(results_t *results) +{ + static char buff[65535]; + + char *pos = buff; + + for (int i = 0; i < results->size; i++) { + pos += snprintf(pos, 65535 - (pos - buff), + result_html_template, + results->results[i]->name, + results->results[i]->path + ); + } + + return buff; +} enum MHD_Result answer_to_connection( void *cls, struct MHD_Connection *connection, @@ -69,8 +90,7 @@ enum MHD_Result answer_to_connection( int ret; if (strcmp(method, "GET") == 0 && strcmp(url, "/") == 0) { - snprintf(buff, BUFF_SIZE, - index_format_template); + snprintf(buff, BUFF_SIZE, index_format_template, "", ""); response = MHD_create_response_from_buffer(strlen(buff), (void*)buff, MHD_RESPMEM_PERSISTENT); @@ -80,20 +100,19 @@ enum MHD_Result answer_to_connection( MHD_destroy_response(response); } else if (strcmp(method, "GET") == 0 && strcmp(url, "/query") == 0) { - snprintf(buff, BUFF_SIZE, - index_format_template); - const char *query = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "query"); results_t *results = index_lookup(g_index, LOOKUP_SUBSTR, query); - printf("results\n"); - for (size_t i = 0; i < results->size; i++) - printf("\t%s\n", results->results[i]->name); + + snprintf(buff, BUFF_SIZE, index_format_template, query, + generate_results_html(results)); response = MHD_create_response_from_buffer(strlen(buff), (void*)buff, MHD_RESPMEM_PERSISTENT); + results_destroy(results); + printf("%d\n", 200); ret = MHD_queue_response(connection, MHD_HTTP_OK, response); MHD_destroy_response(response); @@ -123,7 +142,7 @@ int main() { fseek(tf, 0, SEEK_END); size_t tfs = ftell(tf); rewind(tf); - index_format_template = malloc(tfs); + index_format_template = malloc(tfs + 1); fread(index_format_template, 1, tfs, tf); fclose(tf); index_format_template[tfs] = '\0'; Binary files differ |
