mirror of
https://github.com/eddyem/snippets_library.git
synced 2026-11-22 13:50:19 +03:00
-> 3.6 (added common sorting functions; comments in help messages over socket)
This commit is contained in:
6
.gitignore
vendored
6
.gitignore
vendored
@@ -1,6 +1,12 @@
|
|||||||
# Prerequisites
|
# Prerequisites
|
||||||
*.d
|
*.d
|
||||||
|
|
||||||
|
# Build
|
||||||
|
mk/
|
||||||
|
|
||||||
|
# Backups
|
||||||
|
*.bk
|
||||||
|
|
||||||
# Object files
|
# Object files
|
||||||
*.o
|
*.o
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
cmake_minimum_required(VERSION 4.0)
|
cmake_minimum_required(VERSION 4.0)
|
||||||
set(PROJ usefull_macros)
|
set(PROJ usefull_macros)
|
||||||
set(MINOR_VERSION "5")
|
set(MINOR_VERSION "6")
|
||||||
set(MID_VERSION "3")
|
set(MID_VERSION "3")
|
||||||
set(MAJOR_VERSION "0")
|
set(MAJOR_VERSION "0")
|
||||||
set(VERSION "${MAJOR_VERSION}.${MID_VERSION}.${MINOR_VERSION}")
|
set(VERSION "${MAJOR_VERSION}.${MID_VERSION}.${MINOR_VERSION}")
|
||||||
@@ -25,9 +25,12 @@ endif()
|
|||||||
add_definitions(-DTHREAD_NUMBER=${PROCESSOR_COUNT})
|
add_definitions(-DTHREAD_NUMBER=${PROCESSOR_COUNT})
|
||||||
message("In multithreaded operations will use ${PROCESSOR_COUNT} threads")
|
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
|
# cmake -DDEBUG=1 -> debugging
|
||||||
if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
|
if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||||
if(DEFINED DEBUG AND DEBUG EQUAL 1)
|
if(DEBUG)
|
||||||
set(CMAKE_BUILD_TYPE "Debug")
|
set(CMAKE_BUILD_TYPE "Debug")
|
||||||
else()
|
else()
|
||||||
set(CMAKE_BUILD_TYPE "Release")
|
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)
|
install(FILES ${PCFILE} DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig)
|
||||||
|
|
||||||
# EXAMPLES
|
# EXAMPLES
|
||||||
if(DEFINED EXAMPLES AND EXAMPLES EQUAL 1)
|
if(EXAMPLES)
|
||||||
add_subdirectory(examples)
|
add_subdirectory(examples)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
|||||||
24
Readme.md
24
Readme.md
@@ -569,6 +569,17 @@ typedef struct sl_sock_hitem {
|
|||||||
} sl_sock_hitem_t;
|
} 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:
|
Handler results:
|
||||||
|
|
||||||
```c
|
```c
|
||||||
@@ -588,6 +599,9 @@ sl_sock_hresult_e sl_sock_strhandler(...); // string
|
|||||||
|
|
||||||
**Optional key numbering** (`key[0]`, `key(1)`, `key{2}`, `key3`):
|
**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
|
```c
|
||||||
typedef struct { double magick; int n; } sl_sock_keyno_t;
|
typedef struct { double magick; int n; } sl_sock_keyno_t;
|
||||||
#define SL_SOCK_KEYNO_DEFAULT { .magick = -INFINITY, .n = -1 }
|
#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
|
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
|
## Data Structures
|
||||||
|
|||||||
@@ -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[] = {
|
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_inthandler, "int", "set/get integer flag", (void*)&iflag},
|
||||||
{sl_sock_dblhandler, "dbl", "set/get double flag", (void*)&dflag},
|
{sl_sock_dblhandler, "dbl", "set/get double flag", (void*)&dflag},
|
||||||
{sl_sock_strhandler, "str", "set/get string variable", (void*)&sflag},
|
{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},
|
{show, "show", "show current flags @ server console", NULL},
|
||||||
{dtimeh, "dtime", "get server's UNIX time for all clients connected", 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}
|
{NULL, NULL, NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
16
parseargs.c
16
parseargs.c
@@ -210,18 +210,6 @@ void *get_aptr(void *paptr, sl_argtype_e type){
|
|||||||
return aptr[i - 1];
|
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
|
* @brief sl_parseargs_hf - parse arguments with user help funtion
|
||||||
* @param argc - amount of arguments
|
* @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
|
// sort all lists & check for repeating
|
||||||
qsort(longlist, optsize, sizeof(char *), cmpstringp);
|
qsort(longlist, optsize, sizeof(char *), sl_cmpstringp);
|
||||||
qsort(shortlist,optsize, sizeof(char), cmpcharp);
|
qsort(shortlist,optsize, sizeof(char), sl_cmpchar);
|
||||||
char *prevl = longlist[0], prevshrt = shortlist[0];
|
char *prevl = longlist[0], prevshrt = shortlist[0];
|
||||||
// check for repeated args
|
// check for repeated args
|
||||||
for(int i = 1; i < optsize; ++i){
|
for(int i = 1; i < optsize; ++i){
|
||||||
|
|||||||
40
socket.c
40
socket.c
@@ -291,6 +291,28 @@ static sl_sock_hresult_e parse_post_data(sl_sock_t *c, char *str){
|
|||||||
return RESULT_SILENCE;
|
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
|
// parser of client's message
|
||||||
// "only-server's" fields of `client` are copies of server's
|
// "only-server's" fields of `client` are copies of server's
|
||||||
static sl_sock_hresult_e msgparser(sl_sock_t *client, char *str){
|
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;
|
if(N == 1) valptr = NULL;
|
||||||
else valptr = val;
|
else valptr = val;
|
||||||
if(0 == strcmp(key, "help")){
|
if(0 == strcmp(key, "help")) return sendhelp(client);
|
||||||
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;
|
|
||||||
}
|
|
||||||
// check for strict params like `key=val`
|
// check for strict params like `key=val`
|
||||||
for(sl_sock_hitem_t *h = client->handlers; h->handler; ++h){
|
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){
|
if(h->data){
|
||||||
sl_sock_keyno_t *kn = (sl_sock_keyno_t*)h->data;
|
sl_sock_keyno_t *kn = (sl_sock_keyno_t*)h->data;
|
||||||
if(-1 == isinf(kn->magick)) kn->n = -1; // no value number
|
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){
|
if(eptr != numstart && LL >= 0 && LL <= INT_MAX){
|
||||||
int parno = (int)LL;
|
int parno = (int)LL;
|
||||||
for(sl_sock_hitem_t *h = client->handlers; h->handler; ++h){
|
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);
|
DBG("found %s", h->key);
|
||||||
if(h->data){
|
if(h->data){
|
||||||
sl_sock_keyno_t *kn = (sl_sock_keyno_t*)h->data;
|
sl_sock_keyno_t *kn = (sl_sock_keyno_t*)h->data;
|
||||||
|
|||||||
61
sorting.c
Normal file
61
sorting.c
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the Snippets project.
|
||||||
|
* Copyright 2026 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// different sorting options
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
|
||||||
@@ -451,6 +451,8 @@ typedef struct sl_sock_hitem{
|
|||||||
void *data; // user data (e.g. struct key-varptr-limits)
|
void *data; // user data (e.g. struct key-varptr-limits)
|
||||||
} sl_sock_hitem_t;
|
} sl_sock_hitem_t;
|
||||||
|
|
||||||
|
sl_sock_hresult_e sl_comment(struct sl_sock *s, struct sl_sock_hitem *item, const char *val);
|
||||||
|
|
||||||
// socket type
|
// socket type
|
||||||
typedef enum{
|
typedef enum{
|
||||||
SOCKT_UNIX, // UNIX socket
|
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_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_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);
|
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);
|
||||||
|
|||||||
Reference in New Issue
Block a user