libsheepy

C lib for handling text files, strings and json like data structure with an object oriented system
git clone https://spartatek.se/git/libsheepy.git
Log | Files | Refs | README | LICENSE

libsheepySmall.h (9718B)


      1 // MIT License
      2 //
      3 // Copyright (c) 2026 Remy Noulin
      4 //
      5 // Permission is hereby granted, free of charge, to any person obtaining a copy
      6 // of this software and associated documentation files (the "Software"), to deal
      7 // in the Software without restriction, including without limitation the rights
      8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      9 // copies of the Software, and to permit persons to whom the Software is
     10 // furnished to do so, subject to the following conditions:
     11 //
     12 // The above copyright notice and this permission notice shall be included in all
     13 // copies or substantial portions of the Software.
     14 //
     15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     21 // SOFTWARE.
     22 #pragma once
     23 
     24 #include <stdbool.h>
     25 #include <stdint.h>
     26 #include "libsheepy.h"
     27 
     28 /** \file
     29  * *Tiny functions dont check the parameter validity
     30  * *ShortTiny do the shortest possible operations
     31  */
     32 
     33 /**
     34  * values defining the small object types given as parameter to the isSType
     35  * function
     36  *
     37  * obj->type is a value from this enum
     38  */
     39 typedef enum {
     40   NOT_AN_OBJECT, UNDEFINED, BOOL, CONTAINER, DICT, DICT_ELEM, DOUBLE, INT, STRING, FASTSTRING, ARRAY, BYTES
     41 } smallTypet;
     42 
     43 /**
     44  * object types in strings
     45  */
     46 static const char *SMALL_TYPE_NAMES[] UNUSED = {
     47     "not a sheepy object",
     48     "undefined",
     49     "bool",
     50     "container",
     51     "dict",
     52     "element",
     53     "double",
     54     "int",
     55     "string",
     56     "faststring",
     57     "array",
     58     "bytes"
     59 };
     60 
     61 /**
     62  * base class for the small objects
     63  * all small objects have a type
     64  *
     65  * top struct - all other structs have this struct in common
     66  */
     67 typedef struct {
     68   char type;
     69 } smallt;
     70 
     71 /**
     72  * small bool
     73  */
     74 typedef struct {
     75   char type;
     76   bool value;
     77 } sBoolt;
     78 
     79 /**
     80  * small container holding a pointer to a buffer
     81  */
     82 typedef void (*freeSContainerFt)(void *data);
     83 typedef struct {
     84   char type;
     85   char dataType;
     86   // data is an array of size size
     87   void *data;
     88   freeSContainerFt free;
     89 } sContainert;
     90 
     91 // libsheepy dataTypes in sContainert (SH for libsheepy, DT for dataType)
     92 enum { SH_DT_UNKNOWN, SH_DT_BASET};
     93 
     94 /**
     95  * dictionary element: key, value
     96  */
     97 typedef struct {
     98   char *key;
     99   smallt *data;
    100 } sDictElemt;
    101 
    102 /**
    103  * dictionary
    104  *
    105  * count amount of elements in the dictionary
    106  * maxCount is the maximum amount of elements,
    107  * when count exceeds maxCount, the dictionary is reallocated
    108  */
    109 typedef struct {
    110   char type;
    111   uint32_t count;
    112   uint32_t maxCount;
    113   // data is an array of sDictElemt
    114   sDictElemt elements;
    115 } sDictt;
    116 
    117 /**
    118  * small double
    119  */
    120 typedef struct {
    121   char type;
    122   double value;
    123 } sDoublet;
    124 
    125 /**
    126  * small int
    127  */
    128 typedef struct {
    129   char type;
    130   int64_t value;
    131 } sIntt;
    132 
    133 /**
    134  * small string
    135  */
    136 typedef struct {
    137   char type;
    138   // data is the start of the string
    139   char data;
    140 } sStringt;
    141 
    142 // TODO
    143 typedef struct {
    144   char type;
    145   uint32_t count;
    146   uint32_t maxCount;
    147   // data is the start of the string
    148   char data;
    149 } sFastStringt;
    150 
    151 /**
    152  * array
    153  *
    154  * count amount of elements in the array
    155  * maxCount is the maximum amount of elements,
    156  * when count exceeds maxCount, the array is reallocated
    157  */
    158 typedef struct {
    159   char type;
    160   uint32_t count;
    161   uint32_t maxCount;
    162   // data is an array of pointers
    163   smallt *data;
    164 } sArrayt;
    165 
    166 /**
    167  * undefined
    168  */
    169 typedef struct {
    170   char type;
    171 } sUndefinedt;
    172 
    173 /**
    174  * small bytes
    175  *
    176  * array of bytes
    177  *
    178  * count is the buffer size
    179  * data is the buffer
    180  */
    181 typedef struct {
    182   char type;
    183   uint32_t count;
    184   // data is a byte array
    185   char data;
    186 } sBytest;
    187 
    188 /**
    189  * true when object is type sType
    190  *
    191  * isSType crashes when obj is NULL, use isSTypeF for NULL check
    192  */
    193 #define isSType(obj, sType) (((smallt *) (obj))->type == sType)
    194 bool isSTypeF(smallt *obj, char sType);
    195 
    196 /**
    197  * get object type
    198  * return -1 when obj is NULL
    199  */
    200 #define getSType(obj) obj ? ((smallt *) (obj))->type : -1
    201 
    202 /**
    203  * get object type as a string
    204  * the return string is static
    205  *
    206  * return NULL when obj is NULL
    207  */
    208 #define getSTypeS(obj) obj ? SMALL_TYPE_NAMES[(unsigned char)((smallt *) (obj))->type] : NULL
    209 
    210 size_t sSizeTiny(smallt *obj);
    211 
    212 sBoolt* allocSBool(bool value) MUST_CHECK;
    213 sContainert* allocSContainer(void *data) MUST_CHECK;
    214 sDictt* allocSDict(void) MUST_CHECK;
    215 sDoublet* allocSDouble(double value) MUST_CHECK;
    216 sIntt* allocSInt(int64_t value) MUST_CHECK;
    217 sStringt* allocSStringTiny(const char* data) MUST_CHECK;
    218 sArrayt* allocSArray(void) MUST_CHECK;
    219 sUndefinedt* allocSUndefined(void) MUST_CHECK;
    220 sBytest* allocSBytes(void) MUST_CHECK;
    221 
    222 void sFree(smallt *obj);
    223 void sFreeTiny(smallt *obj);
    224 void sDictFreeTiny(sDictt *dict);
    225 void sArrayFreeTiny(sArrayt *array);
    226 
    227 smallt* sDuplicate(smallt *o) MUST_CHECK;
    228 smallt* sDuplicateTiny(smallt *obj) MUST_CHECK;
    229 sDictt* sDictDuplicateTiny(sDictt *dict) MUST_CHECK;
    230 sArrayt* sArrayDuplicateTiny(sArrayt *obj) MUST_CHECK;
    231 
    232 char* sToStringTiny(smallt *obj) MUST_CHECK;
    233 char* sToString(smallt *obj) MUST_CHECK;
    234 char* sBoolToStringTiny(sBoolt* obj) MUST_CHECK;
    235 char* sContainerToStringTiny(sContainert* obj) MUST_CHECK;
    236 char* sDictToStringTiny(sDictt* obj) MUST_CHECK;
    237 char* sDoubleToStringTiny(sDoublet* obj) MUST_CHECK;
    238 char* sIntToStringTiny(sIntt* obj) MUST_CHECK;
    239 char* sStringToStringTiny(sStringt* obj) MUST_CHECK;
    240 char* sArrayToStringTiny(sArrayt* obj) MUST_CHECK;
    241 char* sUndefinedToStringTiny(sUndefinedt* obj) MUST_CHECK;
    242 char* sBytesToStringTiny(sBytest* obj) MUST_CHECK;
    243 
    244 char* sEscapeTiny(smallt *obj) MUST_CHECK;
    245 char* sEscape(smallt *obj) MUST_CHECK;
    246 char* sDictEscapeTiny(sDictt* obj) MUST_CHECK;
    247 char* sStringEscapeTiny(sStringt* obj) MUST_CHECK;
    248 char* sArrayEscapeTiny(sArrayt* obj) MUST_CHECK;
    249 
    250 char* sTypesTiny(smallt* obj) MUST_CHECK;
    251 sBytest* sTypesToBytesTiny(smallt *obj) MUST_CHECK;
    252 const char** sDictTypeStrings(sDictt* obj) MUST_CHECK;
    253 sBytest* sDictTypes(sDictt* obj) MUST_CHECK;
    254 sBytest* sDictTypesTiny(sDictt* obj) MUST_CHECK;
    255 const char** sArrayTypeStrings(sArrayt* obj) MUST_CHECK;
    256 sBytest* sArrayTypes(sArrayt* obj) MUST_CHECK;
    257 sBytest* sArrayTypesTiny(sArrayt* obj) MUST_CHECK;
    258 
    259 // forEach - loop macro on elements
    260 #define forEachSDict(dict, element) \
    261   sDictElemt *element = &((dict)->elements); \
    262   for (size_t UNIQVAR(libsheepyInternalIndex) = 0; UNIQVAR(libsheepyInternalIndex) < (dict)->count ; UNIQVAR(libsheepyInternalIndex)++, element = UNIQVAR(libsheepyInternalIndex) < (dict)->count ? &((dict)->elements) + UNIQVAR(libsheepyInternalIndex) : NULL)
    263 
    264 #define enumerateSDict(dict, element, index) \
    265   sDictElemt *element = &((dict)->elements); \
    266   for (size_t index = 0; index < (dict)->count ; index++, index < (dict)->count ? element = &((dict)->elements) + index : NULL)
    267 
    268 #define forEachSArray(array, element) \
    269   smallt *element = ((smallt **) &((array)->data))[0]; \
    270   for (size_t UNIQVAR(libsheepyInternalIndex) = 0; UNIQVAR(libsheepyInternalIndex) < (array)->count ; UNIQVAR(libsheepyInternalIndex)++, element = UNIQVAR(libsheepyInternalIndex) < (array)->count ? ((smallt **) &((array)->data))[UNIQVAR(libsheepyInternalIndex)] : NULL)
    271 
    272 #define enumerateSArray(array, element, index) \
    273   smallt *element = ((smallt **) &((array)->data))[0]; \
    274   for (size_t index = 0; index < (array)->count ; index++, element = index < (array)->count ? ((smallt **) &((array)->data))[index] : NULL)
    275 
    276 // TODO forEachSBytes
    277 
    278 smallt* sDictGet(sDictt *dict, const char *key) MUST_CHECK;
    279 smallt** sDictGetP(sDictt *dict, const char *key) MUST_CHECK;
    280 smallt* sDictGetTiny(sDictt *dict, const char *key) MUST_CHECK;
    281 smallt* sDictElemGet(sDictElemt *dictElement) MUST_CHECK;
    282 void sDictSetP(sDictt **dict, const char *key, smallt *data);
    283 void sDictSetTiny(sDictt **dict, const char *key, smallt *data);
    284 void sDictPushTiny(sDictt **dict, const char *key, smallt *data);
    285 void sDictDelTiny(sDictt *dict, const char *key);
    286 
    287 char* sStringGetTiny(sStringt *string) MUST_CHECK;
    288 void sStringSetTiny(sStringt **string, const char *news);
    289 
    290 void sArrayPushTiny(sArrayt **array, smallt* data);
    291 void sArrayPrependTiny(sArrayt **array, smallt* data);
    292 smallt* sArrayPopTiny(sArrayt *array) MUST_CHECK;
    293 smallt* sArrayDequeueTiny(sArrayt *array) MUST_CHECK;
    294 smallt** sArrayGetP(sArrayt *array, uint32_t index) MUST_CHECK;
    295 smallt* sArrayGetTiny(sArrayt *array, uint32_t index) MUST_CHECK;
    296 void sArraySetShortTiny(sArrayt *array, uint32_t index, smallt *value);
    297 void sArraySetP(sArrayt *array, uint32_t index, smallt *value);
    298 void sArraySetTiny(sArrayt *array, uint32_t index, smallt *value);
    299 int sArrayReverseTiny(sArrayt *array) MUST_CHECK;
    300 void sArrayDelTiny(sArrayt *array, uint32_t index);
    301 void sArrayDelRangeTiny(sArrayt *array, uint32_t start, uint32_t end);
    302 
    303 // get void* from sBytes
    304 void* sBytesGet(sBytest *bytes) MUST_CHECK;
    305 void* sBytesGetTiny(sBytest *bytes) MUST_CHECK;
    306 void sBytesPush(sBytest **bytes, char data);
    307 // function to copy data from void* to sBytes
    308 void sBytesPushBuffer(sBytest **bytes, void* data, uint32_t size);
    309 void sBytesPushBufferTiny(sBytest **bytes, void* data, uint32_t size);
    310 
    311 // serialyzer/deserialyzer
    312 sBytest *sSerial(smallt *obj) MUST_CHECK;
    313 sBytest *sSerialTiny(smallt *obj) MUST_CHECK;
    314 void sDictSerialElementsTiny(sBytest **r, sDictt *dict);
    315 void sArraySerialElementsTiny(sBytest **r, sArrayt *array);
    316 smallt* sDeserial(sBytest *obj) MUST_CHECK;
    317 smallt* sDeserialTiny(sBytest *obj) MUST_CHECK;
    318 void sDictDeserialElementsTiny(sDictt **dict, char **data);
    319 void sArrayDeserialElementsTiny(sArrayt **array, char **data);