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

libsheepyCSmallBytes.h (11842B)


      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 // Class smallBytes
     25 typedef struct smallBytes smallBytest;
     26 
     27 /**
     28  * help text for this class
     29  * It is public declaration so that child classes can add their help text easily:
     30  * ret "Child help text \n\n" helpTextSmallBytes;
     31  */
     32 #define helpTextSmallBytes "TODO smallBytes help brief, class description /*, definitions,*/ methods, examples"
     33 
     34 // for object inheriting smallBytes, cast to smallBytes to be able to use this class functions and generics
     35 #define cBy(self) ( (smallBytest*) self )
     36 
     37 typedef void            (*freeSmallBytesFt)      (smallBytest *self);
     38 typedef void            (*terminateSmallBytesFt) (smallBytest **self);
     39 typedef char*           (*toStringSmallBytesFt)  (smallBytest *self);
     40 typedef smallBytest*    (*duplicateSmallBytesFt) (smallBytest *self);
     41 
     42 /**
     43  * free container
     44  */
     45 typedef void            (*finishSmallBytesFt)(smallBytest **self);
     46 
     47 typedef const char*     (*helpSmallBytesFt)   (smallBytest *self);
     48 
     49 /**
     50  * get buffer in object
     51  */
     52 typedef void*           (*getSmallBytesFt) (smallBytest *self);
     53 
     54 /**
     55  * set data buffer in object
     56  *
     57  * after this call, the smallBytes object will have only data
     58  *
     59  * \param
     60  *    self small bytes
     61  *    data buffer to push
     62  *    size size of buffer
     63  */
     64 typedef smallBytest*    (*setSmallBytesFt) (smallBytest *self, void *data, uint32_t size);
     65 
     66 /**
     67  * push char to object
     68  *
     69  * \param
     70  *    data char to push
     71  */
     72 typedef smallBytest*    (*pushSmallBytesFt) (smallBytest *self, char data);
     73 
     74 /**
     75  * push data buffer to object
     76  *
     77  * \param
     78  *    self small bytes
     79  *    data buffer to push
     80  *    size size of buffer
     81  */
     82 typedef smallBytest*    (*pushBufferSmallBytesFt) (smallBytest *self, void *data, uint32_t size);
     83 
     84 /**
     85  * get char at index
     86  *
     87  * \param
     88  *    index
     89  * \return
     90  *    char at index
     91  */
     92 typedef char            (*getAtSmallBytesFt) (smallBytest *self, int64_t index);
     93 
     94 /**
     95  * return buffer size in bytes
     96  *
     97  * \return
     98  *   size in bytes
     99  *   0 when smallBytes is NULL
    100  */
    101 typedef size_t          (*lenSmallBytesFt) (smallBytest *self);
    102 
    103 /**
    104  * \return
    105  *   true when self is empty (len=0)
    106  *   false when len > 0
    107  */
    108 typedef bool            (*isEmptySmallBytesFt)(smallBytest *self);
    109 
    110 /**
    111  * read file to self
    112  *
    113  * \param
    114  *   filePath: path to file
    115  * \return
    116  *   0 data in file
    117  *   -1 an error occured
    118   */
    119 typedef smallBytest* (*readFileSmallBytesFt)(smallBytest *self, const char *filePath);
    120 typedef smallBytest* (*readFileSmallJsonSmallBytesFt)(smallBytest *self, smallJsont *filePath);
    121 typedef smallBytest* (*readFileSmallStringSmallBytesFt)(smallBytest *self, smallStringt *filePath);
    122 typedef smallBytest* (*readStreamSmallBytesFt)(smallBytest *self, FILE *fp);
    123 
    124 /**
    125  * write self to file
    126  *
    127  * \param
    128  *   filePath path to file
    129  * \return
    130  *   1 success
    131  *   0 error
    132  */
    133 typedef int (*writeFileSmallBytesFt)(smallBytest *self, const char *filePath);
    134 typedef int (*writeFileSmallJsonSmallBytesFt)(smallBytest *self, smallJsont *filePath);
    135 typedef int (*writeFileSmallStringSmallBytesFt)(smallBytest *self, smallStringt *filePath);
    136 typedef int (*writeStreamSmallBytesFt)(smallBytest *self, FILE *fp);
    137 
    138 /**
    139  * append self to filePath
    140  *
    141  * \param
    142  *   filePath
    143  *   list
    144  * \return
    145  *   true success
    146  *   false failed, filePath or smallBytes are NULL
    147  */
    148 typedef int (*appendFileSmallBytesFt)(smallBytest *self, const char *filePath);
    149 typedef int (*appendFileSmallStringSmallBytesFt)(smallBytest *self, smallStringt *filePath);
    150 
    151 /**
    152  * equal
    153  * compare self to value
    154  *
    155  * \param
    156  *   value to compare
    157  * \return
    158  *   true self and value have identical content
    159  *   false they differ
    160  */
    161 typedef bool (*equalSmallBytesFt)(smallBytest *self, smallBytest *value);
    162 typedef bool (*equalSmallBytesBoolFt)(smallBytest *self, bool value);
    163 typedef bool (*equalSmallBytesDoubleFt)(smallBytest *self, double value);
    164 typedef bool (*equalSmallBytesInt64Ft)(smallBytest *self, int64_t value);
    165 typedef bool (*equalSmallBytesInt32Ft)(smallBytest *self, int32_t value);
    166 typedef bool (*equalSmallBytesUint32Ft)(smallBytest *self, uint32_t value);
    167 typedef bool (*equalSmallBytesUint64Ft)(smallBytest *self, uint64_t value);
    168 typedef bool (*equalSmallBytesSmallBoolFt)(smallBytest *self, smallBoolt *value);
    169 typedef bool (*equalSmallBytesSmallDoubleFt)(smallBytest *self, smallDoublet *value);
    170 typedef bool (*equalSmallBytesSmallIntFt)(smallBytest *self, smallIntt *value);
    171 typedef bool (*equalSmallBytesCharFt)(smallBytest *self, const char *value);
    172 typedef bool (*equalSmallBytesSmallStringFt)(smallBytest *self, smallStringt *value);
    173 typedef bool (*equalSmallBytesBaseFt)(smallBytest *self, baset *value);
    174 
    175 /* *
    176  * class functions
    177  * allocated once for all objects
    178  *
    179  * freed with finalizeSmallBytes or finalizeLibsheepy
    180  */
    181 
    182 /**
    183  * use this define in child classes and add the new function after this class functions
    184  *
    185  * in this define, add the methods after <finishClassTemplateFt    finish;>
    186  *
    187  * Example:
    188  * #define RINGFUNCTIONST \
    189  *   CLASSTEMPLATEFUNCTIONST; \
    190  *   setSizeRingFt           setSize
    191  */
    192 #define SMALLBYTESFUNCTIONST \
    193   helpSmallBytesFt       help;\
    194   getSmallBytesFt        get;\
    195   setSmallBytesFt        set;\
    196   pushSmallBytesFt       push;\
    197   pushBufferSmallBytesFt pushBuffer;\
    198   getAtSmallBytesFt      getAt;\
    199   /*setAt*/\
    200   lenSmallBytesFt        len;\
    201   isEmptySmallBytesFt    isEmpty;\
    202   readFileSmallBytesFt   readFile;\
    203   readFileSmallJsonSmallBytesFt   readFileSmallJson;\
    204   readFileSmallStringSmallBytesFt readFileSmallString;\
    205   readStreamSmallBytesFt readStream;\
    206   writeFileSmallBytesFt  writeFile;\
    207   writeFileSmallJsonSmallBytesFt writeFileSmallJson;\
    208   writeFileSmallStringSmallBytesFt writeFileSmallString;\
    209   writeStreamSmallBytesFt writeStream;\
    210   appendFileSmallBytesFt  appendFile;\
    211   appendFileSmallStringSmallBytesFt appendFileSmallString;\
    212   equalSmallBytesFt            equal;\
    213   equalSmallBytesBoolFt        equalBool;\
    214   equalSmallBytesDoubleFt      equalDouble;\
    215   equalSmallBytesInt64Ft       equalInt64;\
    216   equalSmallBytesInt32Ft       equalInt32;\
    217   equalSmallBytesUint32Ft      equalUint32;\
    218   equalSmallBytesUint64Ft      equalUint64;\
    219   equalSmallBytesSmallBoolFt   equalSmallBool;\
    220   equalSmallBytesSmallDoubleFt equalSmallDouble;\
    221   equalSmallBytesSmallIntFt    equalSmallInt;\
    222   equalSmallBytesCharFt        equalS;\
    223   equalSmallBytesSmallStringFt equalSmallString;\
    224   equalSmallBytesBaseFt  equalBase
    225 
    226  typedef struct {
    227   freeSmallBytesFt       free;
    228   terminateSmallBytesFt  terminate;
    229   toStringSmallBytesFt   toString;
    230   duplicateSmallBytesFt  duplicate;
    231   finishSmallBytesFt     smash;
    232   finishSmallBytesFt     finish;
    233   SMALLBYTESFUNCTIONST;
    234 } smallBytesFunctionst;
    235 
    236 /**
    237  * class
    238  */
    239 struct smallBytes {
    240   const char           *type;
    241   smallBytesFunctionst *f;
    242 
    243   // internal
    244   sBytest *B;
    245 };
    246 
    247 // smallBytes
    248 
    249 #define createSmallBytes(obj) ;smallBytest obj; initiateSmallBytes(&obj)
    250 #define createAllocateSmallBytes(obj) ;smallBytest *obj; initiateAllocateSmallBytes(&obj)
    251 
    252 void initiateSmallBytes(smallBytest *self);
    253 void initiateAllocateSmallBytes(smallBytest **self);
    254 void finalizeRecycleSmallBytes(void *arg UNUSED);
    255 void finalizeSmallBytes(void);
    256 
    257 // initialize class methods, call registerMethodsSmallBytes from classes inheriting this class
    258 void registerMethodsSmallBytes(smallBytesFunctionst *f);
    259 
    260 smallBytest* allocSmallBytes(void *data, uint32_t size);
    261 
    262 // terminate smallBytest val when it is out of scope
    263 void cleanUpSmallBytesTerminateG(smallBytest **val);
    264 
    265 // free smallBytest val declared locally in a code block when it is out of scope
    266 void cleanUpSmallBytesFreeLocalG(smallBytest *val);
    267 
    268 // free smallBytest val when it is out of scope
    269 void cleanUpSmallBytesFreeG(smallBytest **val);
    270 
    271 // finish smallBytest val when it is out of scope
    272 void cleanUpSmallBytesFinishG(smallBytest **val);
    273 
    274 /**
    275  * declare pointer name with type smallBytest and terminate name when it is out of scope
    276  */
    277 #define cleanSmallBytesP(name) smallBytest *name CLEANUP(cleanUpSmallBytesTerminateG)
    278 
    279 /**
    280  * allocate smallBytes (pointer) and clean up when it is out of scope
    281  */
    282 #define cleanAllocateSmallBytes(obj) ;cleanSmallBytesP(obj); initiateAllocateSmallBytes(&obj)
    283 
    284 /**
    285  * declare local object name with type smallBytest and free name when it is out of scope
    286  */
    287 #define cleanSmallBytes(name) smallBytest name CLEANUP(cleanUpSmallBytesFreeLocalG); initiateSmallBytes(&name)
    288 
    289 /**
    290  * declare pointer name with type smallBytest and free name when it is out of scope
    291  */
    292 #define cleanFreeSmallBytes(name) smallBytest *name CLEANUP(cleanUpSmallBytesFreeG)
    293 
    294 /**
    295  * declare pointer name with Type smallBytest and finish name when it is out of scope
    296  */
    297 #define cleanFinishSmallBytesP(name) smallBytest *name CLEANUP(cleanUpSmallBytesFinishG)
    298 
    299 smallBytest* duplicateSmallBytesG (smallBytest *self);
    300 
    301 void            freeSmallBytesG      (smallBytest *self);
    302 
    303 smallBytest* setSmallBytesG(smallBytest *self, void *data, uint32_t size);
    304 char getAtSmallBytesG(smallBytest *self, char retType UNUSED, int64_t index);
    305 smallBytest* pushSmallBytesG (smallBytest *self, char data);
    306 size_t lenSmallBytesG(smallBytest *self);
    307 bool isEmptySmallBytesG(smallBytest *self);
    308 smallBytest* readFileSmallJsonSmallBytesG(smallBytest *self, smallJsont *filePath);
    309 smallBytest* readFileSmallStringSmallBytesG(smallBytest *self, smallStringt *filePath);
    310 smallBytest* readFileSmallBytesG(smallBytest *self, const char *filePath);
    311 smallBytest* readStreamSmallBytesG(smallBytest *self, FILE *fp);
    312 int writeFileSmallBytesG(smallBytest *self, const char *filePath);
    313 int writeFileSmallJsonSmallBytesG(smallBytest *self, smallJsont *filePath);
    314 int writeFileSmallStringSmallBytesG(smallBytest *self, smallStringt *filePath);
    315 int writeStreamSmallBytesG(smallBytest *self, FILE *fp);
    316 int appendFileSmallBytesG(smallBytest *self, const char *filePath);
    317 int appendFileSmallStringSmallBytesG(smallBytest *self, smallStringt *filePath);
    318 bool equalSmallBytesG(smallBytest *self, smallBytest *value);
    319 bool equalSmallBytesBoolG(smallBytest *self, bool value);
    320 bool equalSmallBytesDoubleG(smallBytest *self, double value);
    321 bool equalSmallBytesInt64G(smallBytest *self, int64_t value);
    322 bool equalSmallBytesInt32G(smallBytest *self, int32_t value);
    323 bool equalSmallBytesUint32G(smallBytest *self, uint32_t value);
    324 bool equalSmallBytesUint64G(smallBytest *self, uint64_t value);
    325 bool equalSmallBytesSmallBoolG(smallBytest *self, smallBoolt *value);
    326 bool equalSmallBytesSmallDoubleG(smallBytest *self, smallDoublet *value);
    327 bool equalSmallBytesSmallIntG(smallBytest *self, smallIntt *value);
    328 bool equalSmallBytesCharG(smallBytest *self, const char *value);
    329 bool equalSmallBytesSmallStringG(smallBytest *self, smallStringt *value);
    330 bool equalSmallBytesBaseG(smallBytest *self, baset *value);
    331 
    332 // end class smallBytes