From 3ed96d8ef0baad708004369f35639a68773d9543 Mon Sep 17 00:00:00 2001 From: Edward Emelianov Date: Sat, 17 Oct 2026 23:38:32 +0300 Subject: [PATCH] -> 3.6 (added common sorting functions; comments in help messages over socket) --- .gitignore | 6 ++++ CMakeLists.txt | 9 ++++-- Readme.md | 24 ++++++++++++++++ examples/clientserver.c | 5 +++- parseargs.c | 16 ++--------- socket.c | 40 +++++++++++++++++---------- sorting.c | 61 +++++++++++++++++++++++++++++++++++++++++ usefull_macros.h | 11 ++++++++ 8 files changed, 139 insertions(+), 33 deletions(-) create mode 100644 sorting.c diff --git a/.gitignore b/.gitignore index 0be9ac5..0d9505d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,12 @@ # Prerequisites *.d +# Build +mk/ + +# Backups +*.bk + # Object files *.o diff --git a/CMakeLists.txt b/CMakeLists.txt index a91b41d..6832a8a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 4.0) set(PROJ usefull_macros) -set(MINOR_VERSION "5") +set(MINOR_VERSION "6") set(MID_VERSION "3") set(MAJOR_VERSION "0") set(VERSION "${MAJOR_VERSION}.${MID_VERSION}.${MINOR_VERSION}") @@ -25,9 +25,12 @@ endif() add_definitions(-DTHREAD_NUMBER=${PROCESSOR_COUNT}) message("In multithreaded operations will use ${PROCESSOR_COUNT} threads") +option(EXAMPLES "Build examples" OFF) +option(DEBUG "Compile in debug mode" OFF) + # cmake -DDEBUG=1 -> debugging if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug") - if(DEFINED DEBUG AND DEBUG EQUAL 1) + if(DEBUG) set(CMAKE_BUILD_TYPE "Debug") else() set(CMAKE_BUILD_TYPE "Release") @@ -116,7 +119,7 @@ install(TARGETS ${PROJ} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} install(FILES ${PCFILE} DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig) # EXAMPLES -if(DEFINED EXAMPLES AND EXAMPLES EQUAL 1) +if(EXAMPLES) add_subdirectory(examples) endif() diff --git a/Readme.md b/Readme.md index b416279..24956f6 100644 --- a/Readme.md +++ b/Readme.md @@ -569,6 +569,17 @@ typedef struct sl_sock_hitem { } sl_sock_hitem_t; ``` + + +Field `help` allows you to show help message for `key` when user sends "help" message, format: +"key: help". All items shown are strictly in user's order. + +Field and `key` could be NULL, in this case "help" for this item will just send string in `help`. +The `handler` in this case should be inited with `sl_comment`. + +Field `data` is for internal user's usage or for key numbering (see later). +End of list is `handler == NULL`. + Handler results: ```c @@ -588,6 +599,9 @@ sl_sock_hresult_e sl_sock_strhandler(...); // string **Optional key numbering** (`key[0]`, `key(1)`, `key{2}`, `key3`): +In this case you should init field `data` in `sl_sock_hitem_t` with pointer to `sl_sock_keyno_t`. +Then the appropriate handler called with `sl_sock_keyno_t->n` inited with this number. + ```c typedef struct { double magick; int n; } sl_sock_keyno_t; #define SL_SOCK_KEYNO_DEFAULT { .magick = -INFINITY, .n = -1 } @@ -681,6 +695,16 @@ int sl_canread(int fd); // non-blocking select() for read int sl_canwrite(int fd); // non-blocking select() for write ``` +### Sorting helpers + +With `qsort()` you can use these functions: + +- `sl_cmpchar` - sort array of chars; +- `sl_cmpint` - sort array of integers; +- `sl_cmpdouble` - sort array of doubles; +- `sl_cmpstring` - sort array of `char*`; +- `sl_cmpstringp` - sort array of `char**`. + --- ## Data Structures diff --git a/examples/clientserver.c b/examples/clientserver.c index ecefda9..54a8a21 100644 --- a/examples/clientserver.c +++ b/examples/clientserver.c @@ -207,12 +207,15 @@ static sl_sock_hresult_e keyparhandler(struct sl_sock *s, sl_sock_hitem_t *item, } static sl_sock_hitem_t handlers[] = { + {sl_comment, NULL, " Using library parsers:", NULL}, {sl_sock_inthandler, "int", "set/get integer flag", (void*)&iflag}, {sl_sock_dblhandler, "dbl", "set/get double flag", (void*)&dflag}, {sl_sock_strhandler, "str", "set/get string variable", (void*)&sflag}, + {sl_comment, NULL, " Using key numbers:", NULL}, + {keyparhandler, "flags", "set/get bit flags as whole (flags=val) or by bits (flags[bit]=val)", (void*)&kph_number}, + {sl_comment, NULL, " Other functions:", NULL}, {show, "show", "show current flags @ server console", NULL}, {dtimeh, "dtime", "get server's UNIX time for all clients connected", NULL}, - {keyparhandler, "flags", "set/get bit flags as whole (flags=val) or by bits (flags[bit]=val)", (void*)&kph_number}, {NULL, NULL, NULL, NULL} }; diff --git a/parseargs.c b/parseargs.c index e66f1ef..49fcda7 100644 --- a/parseargs.c +++ b/parseargs.c @@ -210,18 +210,6 @@ void *get_aptr(void *paptr, sl_argtype_e type){ return aptr[i - 1]; } -static int cmpstringp(const void *p1, const void *p2){ - if(!p1 || !p2) return 0; - const char *str1 = * (char * const *) p1, *str2 = * (char * const *) p2; - if(!str1 && !str2) return 0; - else if(!str1) return 1; - else if(!str2) return -1; - return strcmp(str1, str2); -} -static int cmpcharp(const void *p1, const void *p2){ - return (int)(*(char * const)p1 - *(char *const)p2); -} - /** * @brief sl_parseargs_hf - parse arguments with user help funtion * @param argc - amount of arguments @@ -277,8 +265,8 @@ void sl_parseargs_hf(int *argc, char ***argv, sl_option_t *options, void (*helpf } } // sort all lists & check for repeating - qsort(longlist, optsize, sizeof(char *), cmpstringp); - qsort(shortlist,optsize, sizeof(char), cmpcharp); + qsort(longlist, optsize, sizeof(char *), sl_cmpstringp); + qsort(shortlist,optsize, sizeof(char), sl_cmpchar); char *prevl = longlist[0], prevshrt = shortlist[0]; // check for repeated args for(int i = 1; i < optsize; ++i){ diff --git a/socket.c b/socket.c index 308245d..0e5e9f9 100644 --- a/socket.c +++ b/socket.c @@ -291,6 +291,28 @@ static sl_sock_hresult_e parse_post_data(sl_sock_t *c, char *str){ return RESULT_SILENCE; } +// stub for comment string in help +sl_sock_hresult_e sl_comment(_U_ struct sl_sock *s, _U_ struct sl_sock_hitem *item, _U_ const char *val){ + return RESULT_SILENCE; +} + +static sl_sock_hresult_e sendhelp(sl_sock_t *client){ + if(!client->handlers || !client->handlers->handler) return RESULT_FAIL; + sl_sock_sendstrmessage(client, "\nHelp:\n"); + for(sl_sock_hitem_t *h = client->handlers; h->handler; ++h){ + if(h->help){ + if(h->key){ + sl_sock_sendstrmessage(client, h->key); + sl_sock_sendstrmessage(client, ": "); + } + sl_sock_sendstrmessage(client, h->help); + sl_sock_sendbyte(client, '\n'); + } + } + sl_sock_sendbyte(client, '\n'); + return RESULT_SILENCE; +} + // parser of client's message // "only-server's" fields of `client` are copies of server's static sl_sock_hresult_e msgparser(sl_sock_t *client, char *str){ @@ -319,22 +341,10 @@ static sl_sock_hresult_e msgparser(sl_sock_t *client, char *str){ } if(N == 1) valptr = NULL; else valptr = val; - if(0 == strcmp(key, "help")){ - sl_sock_sendstrmessage(client, "\nHelp:\n"); - for(sl_sock_hitem_t *h = client->handlers; h->handler; ++h){ - if(h->help){ - sl_sock_sendstrmessage(client, h->key); - sl_sock_sendstrmessage(client, ": "); - sl_sock_sendstrmessage(client, h->help); - sl_sock_sendbyte(client, '\n'); - } - } - sl_sock_sendbyte(client, '\n'); - return RESULT_SILENCE; - } + if(0 == strcmp(key, "help")) return sendhelp(client); // check for strict params like `key=val` for(sl_sock_hitem_t *h = client->handlers; h->handler; ++h){ - if(strcmp(h->key, key)) continue; + if(!h->key || strcmp(h->key, key)) continue; if(h->data){ sl_sock_keyno_t *kn = (sl_sock_keyno_t*)h->data; if(-1 == isinf(kn->magick)) kn->n = -1; // no value number @@ -370,7 +380,7 @@ static sl_sock_hresult_e msgparser(sl_sock_t *client, char *str){ if(eptr != numstart && LL >= 0 && LL <= INT_MAX){ int parno = (int)LL; for(sl_sock_hitem_t *h = client->handlers; h->handler; ++h){ - if(strncmp(h->key, key, keylen)) continue; // now search only in first `keylen` bytes + if(!h->key || strncmp(h->key, key, keylen)) continue; // now search only in first `keylen` bytes DBG("found %s", h->key); if(h->data){ sl_sock_keyno_t *kn = (sl_sock_keyno_t*)h->data; diff --git a/sorting.c b/sorting.c new file mode 100644 index 0000000..5ee90fb --- /dev/null +++ b/sorting.c @@ -0,0 +1,61 @@ +/* + * This file is part of the Snippets project. + * Copyright 2026 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +// different sorting options + +#include + +// compare chars +int sl_cmpchar(const void *p1, const void *p2){ + if(!p1 || !p2) return 0; + return (int)(*(char * const)p1 - *(char *const)p2); +} + +// compare integers +int sl_cmpint(const void *p1, const void *p2){ + if(!p1 || !p2) return 0; + int i1 = *(const int *) p1, i2 = *(const int *) p2; + return i1 - i2; +} + +// compare doubles +int sl_cmpdouble(const void *p1, const void *p2){ + if(!p1 || !p2) return 0; + double d1 = *(const double *) p1, d2 = *(const double *) p2; + if(d1 > d2) return 1; + else if(d1 < d2) return -1; + return 0; +} + +// compare strings +int sl_cmpstring(const void *p1, const void *p2){ + if(!p1 || !p2) return 0; + const char *str1 = (const char *) p1, *str2 = (const char *) p2; + return strcmp(str1, str2); +} + +// compare strings by pointer to them +int sl_cmpstringp(const void *p1, const void *p2){ + if(!p1 || !p2) return 0; + const char *str1 = * (char * const *) p1, *str2 = * (char * const *) p2; + if(!str1 && !str2) return 0; + else if(!str1) return 1; + else if(!str2) return -1; + return strcmp(str1, str2); +} + diff --git a/usefull_macros.h b/usefull_macros.h index ddcc08b..dcd4d36 100644 --- a/usefull_macros.h +++ b/usefull_macros.h @@ -451,6 +451,8 @@ typedef struct sl_sock_hitem{ void *data; // user data (e.g. struct key-varptr-limits) } sl_sock_hitem_t; +sl_sock_hresult_e sl_comment(struct sl_sock *s, struct sl_sock_hitem *item, const char *val); + // socket type typedef enum{ SOCKT_UNIX, // UNIX socket @@ -531,3 +533,12 @@ int sl_sock_sendall(sl_sock_t *sock, uint8_t *data, size_t len); sl_sock_hresult_e sl_sock_inthandler(sl_sock_t *client, sl_sock_hitem_t *hitem, const char *str); sl_sock_hresult_e sl_sock_dblhandler(sl_sock_t *client, sl_sock_hitem_t *hitem, const char *str); sl_sock_hresult_e sl_sock_strhandler(sl_sock_t *client, sl_sock_hitem_t *hitem, const char *str); + +/******************************************************************************\ + Sorting functions +\******************************************************************************/ +int sl_cmpchar(const void *p1, const void *p2); +int sl_cmpint(const void *p1, const void *p2); +int sl_cmpdouble(const void *p1, const void *p2); +int sl_cmpstring(const void *p1, const void *p2); +int sl_cmpstringp(const void *p1, const void *p2);