From 3d9b6ccac90c978c2b62f2ef9cbf4bfb80358425 Mon Sep 17 00:00:00 2001 From: Alexander Zhylin Date: Fri, 14 Aug 2026 18:58:33 +0300 Subject: [PATCH] file_list: double list integration [not finished] --- src/common.h | 7 +-- src/core/file_processor.c | 114 ++++++++++++++++++++------------------ src/core/file_processor.h | 2 +- src/core/query.c | 2 +- src/windows/file_list.c | 12 +++- src/windows/file_list.h | 3 + 6 files changed, 78 insertions(+), 62 deletions(-) diff --git a/src/common.h b/src/common.h index 1b5dd30..11a3a54 100644 --- a/src/common.h +++ b/src/common.h @@ -4,6 +4,7 @@ #ifndef COMMON_H #define COMMON_H +#include "dlist.h" #include #include #include @@ -27,7 +28,7 @@ typedef struct { #define MAIN_UI_RESET(app) \ app->main_ui.ui = NULL; \ - app->main_ui.cb_ui_refresh = NULL; \ + app->main_ui.cb_ui_refresh = NULL; \ app->main_ui.cb_b_press = NULL; \ app->main_ui.b_keys = NULL; \ app->main_ui.b_keys_len = 0; @@ -44,7 +45,6 @@ typedef struct fl_item { char *name; char *description; char *owner; - struct fl_item *next; } fl_item_t; enum main_window_type { @@ -66,15 +66,14 @@ typedef struct main_window { int32_t b_keys_len; } main_window_t; - typedef struct file_args { int file_d; fl_item_t f_selected; fl_item_t *l_start; fl_item_t *l_current; + dlist_t *f_list; } file_args_t; - typedef struct params { unsigned short port; unsigned addr; diff --git a/src/core/file_processor.c b/src/core/file_processor.c index 71769f8..8b5f020 100644 --- a/src/core/file_processor.c +++ b/src/core/file_processor.c @@ -2,8 +2,10 @@ /* Copyright (c) 2026 Oleksandr Zhylin */ #include "alert.h" +#include "dlist.h" #include "server.h" #include +#include #include #include #include @@ -19,28 +21,24 @@ #include #include #include -#include #include #include "file_processor.h" #include "query.h" -static void fl_add(fl_item_t **cur, fl_item_t **start, char *fname); -fl_item_t *fl_select(fl_item_t *start, int num); +static void fl_add(dlist_t *fl_list, char *q_line); void fl_clear(fl_item_t **start, fl_item_t **current); void file_list(file_args_t *f_args, query_args_t *q_args) { int32_t qlen; char *query = NULL; - static char qbuf[INBUFSIZE * 2]; + static char q_buf[INBUFSIZE * 2]; static int32_t qbuf_used = 0; w_ui_file_list_t *fui = (w_ui_file_list_t *)q_args->main_ui->ui; w_dialogue_t *d = (w_dialogue_t *)q_args->active_dialogue; - fui->start = &(f_args->l_start); - fui->current = &(f_args->l_current); if (qbuf_used == 0) - qbuf[0] = 0; + q_buf[0] = 0; while ((qlen = query_extract_from_buf(q_args->buf, &(q_args->buf_used), &query))) { @@ -55,16 +53,16 @@ void file_list(file_args_t *f_args, query_args_t *q_args) { q_args->state = WAIT_CLIENT; break; } - strcat(qbuf, query); - qbuf_used += strlen(qbuf); - if (strchr(qbuf, '\n') == NULL) + strcat(q_buf, query); + qbuf_used += strlen(q_buf); + if (strchr(q_buf, '\n') == NULL) continue; - fl_add(&(f_args->l_current), &(f_args->l_start), qbuf); + fl_add(f_args->f_list, q_buf); fui->current_count++; free(query); query = NULL; qbuf_used = 0; - qbuf[0] = 0; + q_buf[0] = 0; } } @@ -74,7 +72,7 @@ int32_t ui_file_select(file_args_t *f_args, query_args_t *q_args, int32_t idx) { &(f_args->f_selected); /* new copy of file struct (list be cleared) */ struct stat st = {0}; - l_selected = fl_select(f_args->l_start, idx); + l_selected = NULL; /* TODO: FIX */ if (l_selected == NULL) { return -1; @@ -173,8 +171,8 @@ void file_download(file_args_t *f_args, query_args_t *q_args) { if (q_args->file->package_rest <= q_args->buf_used) { write_len = q_args->file->package_rest; qlen = write(f_args->file_d, q_args->buf, write_len); - file_download_receive_after(qlen, q_args, f_args, d, pb) - if (q_args->file->signal == sig_continue) { + file_download_receive_after(qlen, q_args, f_args, d, + pb) if (q_args->file->signal == sig_continue) { server_send_string(q_args, "continue %d\n", PACKAGE_SIZE * 10); if (q_args->file->package_rest == 0) { q_args->file->package_rest = PACKAGE_SIZE * 10; @@ -316,66 +314,77 @@ void clear_file_in_query(query_args_t *q_args) { q_args->file = NULL; } -void init_file_args(file_args_t *f_args) { +void init_file_args(file_args_t *f_args, app_t *app) { + w_ui_file_list_t *fui = NULL; f_args->file_d = 0; f_args->f_selected.name = NULL; - f_args->f_selected.next = NULL; f_args->f_selected.size = 0; f_args->l_current = NULL; f_args->l_start = NULL; + if (app->main_ui.type == mw_fl_server) { + fui = app->main_ui.ui; + if (fui != NULL) { + f_args->f_list = fui->f_list; + } + } } -/* work with file list */ -static void fl_add(fl_item_t **cur, fl_item_t **start, char *line) { +static void fl_q_extract(fl_item_t *f, char *line) { char fowner[32]; char *descr_begin = NULL; int h_len; char *name_begin = strchr(line, '[') + 1; char *name_end = strchr(line, ']'); int name_len = name_end - name_begin; - fl_item_t *fitem = malloc(sizeof(fl_item_t)); - sscanf(name_end, "] %zu %s%n", &(fitem->size), fowner, &h_len); - /* file name */ - fitem->name = malloc((sizeof(char)) * (name_len + 1)); - strncpy(fitem->name, name_begin, name_len); - fitem->name[name_len] = 0; - /* file owner */ - fitem->owner = malloc((sizeof(char)) * (strlen(fowner) + 1)); - strcpy(fitem->owner, fowner); - /* file description */ + sscanf(name_end, "] %zu %s%n", &(f->size), fowner, &h_len); + strncpy(f->name, name_begin, name_len); + f->name[name_len] = 0; + strcpy(f->owner, fowner); descr_begin = name_end + h_len + 1; if (strcmp(descr_begin, "\n")) { int d_len = strlen(line) - h_len; - fitem->description = malloc((sizeof(char)) * (d_len)); - strcpy(fitem->description, descr_begin); // +1 to escape the dividing \32 + strcpy(f->description, descr_begin); // +1 to escape the dividing \32 for (int i = 0; i < d_len; i++) { - if (fitem->description[i] == '\a') { - fitem->description[i] = '\n'; + if (f->description[i] == '\a') { + f->description[i] = '\n'; } } } else { - fitem->description = NULL; + f->description = NULL; } - /* fitem->description[strlen(line + line_pos)] = 0; */ - fitem->next = NULL; - if (*cur != NULL) { - (*cur)->next = fitem; - } else { - *start = fitem; - } - *cur = fitem; } -fl_item_t *fl_select(fl_item_t *start, int num) { - fl_item_t *current = start; - for (; num != 1; num--) { - if (current->next != NULL) { - current = current->next; - } else { - return NULL; - } - } - return current; +static bool fl_add_cb(void *_dst, void *_src) { + fl_item_t *dst = _dst; + fl_item_t *src = _src; + + dst->size = src->size; + + dst->description = malloc(sizeof(char) * (strlen(src->description) + 1)); + strcpy(dst->description, src->description); + + dst->name = malloc(sizeof(char) * (strlen(src->name) + 1)); + strcpy(dst->name, src->name); + + dst->owner = malloc(sizeof(char) * (strlen(src->owner) + 1)); + strcpy(dst->owner, src->owner); + return true; +} + +/* work with file list */ +static void fl_add(dlist_t *fl_list, char *q_line) { + fl_item_t f_item; + char f_description[INBUFSIZE * 100]; + char f_owner[256]; + char f_name[256]; + f_item.description = f_description; + f_item.owner = f_owner; + f_item.name = f_name; + f_item.size = 0; + + fl_q_extract(&f_item, q_line); + + dlist_add(fl_list, &f_item, fl_item_t, fl_add_cb, false); } void fl_clear(fl_item_t **start, fl_item_t **arg_current) { @@ -385,7 +394,6 @@ void fl_clear(fl_item_t **start, fl_item_t **arg_current) { current = *start; do { - next = current->next; free(current->description); free(current->name); free(current); diff --git a/src/core/file_processor.h b/src/core/file_processor.h index 8a70555..00f584d 100644 --- a/src/core/file_processor.h +++ b/src/core/file_processor.h @@ -8,7 +8,7 @@ void file_list(file_args_t *f_args, query_args_t *q_args); void file_download(file_args_t *f_args, query_args_t *q_args); -void init_file_args(file_args_t *fargs); +void init_file_args(file_args_t *fargs, app_t *app); int file_upload_request(char *, query_args_t *q_args); int file_upload_start(query_args_t *q_args); int file_upload(query_args_t *q_args); diff --git a/src/core/query.c b/src/core/query.c index 4ff17aa..ad7a1af 100644 --- a/src/core/query.c +++ b/src/core/query.c @@ -54,7 +54,7 @@ void query_loop(app_t *app) { w_cb_args_t d_args = { .app = app, .element = NULL, .data = NULL, .resp_data.code = cbrc_none}; - init_file_args(&file_args); + init_file_args(&file_args, app); query_args->sd = sd; app->file_args = &file_args; diff --git a/src/windows/file_list.c b/src/windows/file_list.c index 682f3f9..10addf5 100644 --- a/src/windows/file_list.c +++ b/src/windows/file_list.c @@ -4,6 +4,7 @@ #include "file_list.h" #include #include +#include "dlist.h" #include "main_window.h" #include #include @@ -191,6 +192,11 @@ void *w_fl_init(app_t *app) { fui->w.sz.x = getmaxx(app->win); fui->w.sz.y = getmaxy(app->win); + fui->f_list = dlist_init(NULL, fl_item_t, NULL); + if (app->file_args != NULL) { + app->file_args->f_list = fui->f_list; + } + /* * INIT UI * */ /* define the width for each sub window */ @@ -240,8 +246,8 @@ void w_fl_draw(w_ui_file_list_t *fui) { sz_x -= 1; /* do not count the borders */ p_y = 1; p_x = 1; - fl_item_t *el = NULL; - fl_item_t *active_el = NULL; + fl_item_t *el = dlist_it_next(fui->f_list); + fl_item_t *active_el = dlist_get_ptr(fui->f_selected); int32_t cur_el_idx = 0; box(fui->win_list, 0, 0); @@ -279,7 +285,7 @@ void w_fl_draw(w_ui_file_list_t *fui) { } p_y++; cur_el_idx++; - } while ((el = el->next) != NULL && p_y < sz_y_f); + } while ((el = dlist_it_next(fui->f_list)) != NULL && p_y < sz_y_f); p_x = 1; int32_t p_len; diff --git a/src/windows/file_list.h b/src/windows/file_list.h index 2b47eb5..38dc783 100644 --- a/src/windows/file_list.h +++ b/src/windows/file_list.h @@ -4,6 +4,7 @@ #ifndef W_FILE_LIST_H #define W_FILE_LIST_H +#include "dlist.h" #include #include #include @@ -12,6 +13,8 @@ typedef struct { w_t w; fl_item_t **current; fl_item_t **start; + dlist_t *f_list; + dlist_node_t *f_selected; int32_t current_idx; int32_t current_count; int32_t full_count;