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

commit 81310f422139c76ec92401b10aab034e65d31a38
parent f192036611bee5e17dcb2de39723e205605e9b68
Author: Remy Noulin <loader2x@gmail.com>
Date:   Mon,  8 May 2023 10:02:28 +0200

Add clean functions for baset object

It is useful for when the object type is unknown (getG and rtBaset).

Update examples to remove warnings.

example/b2j.c                                 |  3 ++-
example/b2y.c                                 |  3 ++-
example/csv.c                                 |  6 ++---
example/demo.c                                |  6 ++---
example/dmce.c                                |  2 +-
example/j2b.c                                 |  3 ++-
example/j2y.c                                 |  3 ++-
example/logging.c                             |  2 +-
example/mkparents.c                           |  2 +-
example/mve.c                                 |  4 ++--
example/readme.c                              | 19 ++++++++--------
example/regexDemo.c                           |  4 ++--
example/search_and_replace_strings_in_files.c |  4 ++--
example/showdir.c                             |  2 +-
example/smallObjectsUsage.c                   |  4 ++--
example/threads.c                             |  2 +-
example/y2b.c                                 |  3 ++-
example/y2j.c                                 |  3 ++-
release/libsheepy.h                           |  2 +-
release/libsheepyObject.h                     | 32 +++++++++++++++++++++++++++
src/json/libsheepyObject.c                    | 24 ++++++++++++++++++++
src/libsheepy.h                               |  2 +-
src/libsheepyObject.h                         | 32 +++++++++++++++++++++++++++
23 files changed, 131 insertions(+), 36 deletions(-)

Diffstat:
Mexample/b2j.c | 3++-
Mexample/b2y.c | 3++-
Mexample/csv.c | 6+++---
Mexample/demo.c | 6+++---
Mexample/dmce.c | 2+-
Mexample/j2b.c | 3++-
Mexample/j2y.c | 3++-
Mexample/logging.c | 2+-
Mexample/mkparents.c | 2+-
Mexample/mve.c | 4++--
Mexample/readme.c | 19++++++++++---------
Mexample/regexDemo.c | 4++--
Mexample/search_and_replace_strings_in_files.c | 4++--
Mexample/showdir.c | 2+-
Mexample/smallObjectsUsage.c | 4++--
Mexample/threads.c | 2+-
Mexample/y2b.c | 3++-
Mexample/y2j.c | 3++-
Mrelease/libsheepy.h | 2+-
Mrelease/libsheepyObject.h | 32++++++++++++++++++++++++++++++++
Msrc/json/libsheepyObject.c | 24++++++++++++++++++++++++
Msrc/libsheepy.h | 2+-
Msrc/libsheepyObject.h | 32++++++++++++++++++++++++++++++++
23 files changed, 131 insertions(+), 36 deletions(-)

diff --git a/example/b2j.c b/example/b2j.c @@ -4,6 +4,7 @@ #define internal static +#include <stdio.h> #ifndef unitTest #endif @@ -38,7 +39,7 @@ int MAIN(int ARGC, char** ARGV) { // remove extension *p = 0; } - appendG(&oFName, ".json"); + pErrorNULL(appendG(&oFName, ".json")); // create json object cleanAllocateSmallJson(json); diff --git a/example/b2y.c b/example/b2y.c @@ -4,6 +4,7 @@ #define internal static +#include <stdio.h> #ifndef unitTest #endif @@ -38,7 +39,7 @@ int MAIN(int ARGC, char** ARGV) { // remove extension *p = 0; } - appendG(&oFName, ".yml"); + pErrorNULL(appendG(&oFName, ".yml")); // create json object cleanAllocateSmallJson(json); diff --git a/example/csv.c b/example/csv.c @@ -17,12 +17,12 @@ int MAIN(int ARGC, char** ARGV); int argc; char **argv; char *get(void **csv, size_t line, size_t col) { - + return(((char **)csv[line])[col]); } char *get2(void **csv, intmax_t line, intmax_t col) { - + return(iListGetS(listGet(csv, line), col)); } @@ -49,7 +49,7 @@ int MAIN(int ARGC, char** ARGV) { // split lines and store in csv list forEachCharP(list, l) { char **spl = split(*l, ","); - listPush(&csv, spl); + pErrorNULL(listPush(&csv, spl)); } listFreeS(list); diff --git a/example/demo.c b/example/demo.c @@ -47,18 +47,18 @@ int MAIN(int ARGC, char** ARGV) { exitFailure(list); // append "libsheepy" at the end of the list - listPushS(&list, " libsheepy "); + pErrorNULL(listPushS(&list, " libsheepy ")); exitFailure(list); // if there is an argument, add it to the list if (argc > 1) { - listPushS(&list, argv[1]); + pErrorNULL(listPushS(&list, argv[1])); exitFailure(list); } // print list elements - listPrintS(list); + pError0(listPrintS(list)); printf("\nPrint lines with string \"two\" or \"libsheepy\":\n"); printf("\n"); diff --git a/example/dmce.c b/example/dmce.c @@ -53,7 +53,7 @@ int MAIN(int ARGC, char** ARGV) { char **line; line = split(*l, ":"); if (listLengthS(line) >= 3) { - listPushS(&coverage, *l); + pErrorNULL(listPushS(&coverage, *l)); } listFreeS(line); } diff --git a/example/j2b.c b/example/j2b.c @@ -5,6 +5,7 @@ #define internal static #include <stdlib.h> +#include <stdio.h> #ifndef unitTest #endif @@ -39,7 +40,7 @@ int MAIN(int ARGC, char** ARGV) { // remove extension *p = 0; } - appendG(&oFName, ".bin"); + pErrorNULL(appendG(&oFName, ".bin")); // create json object createAllocateSmallJson(json); diff --git a/example/j2y.c b/example/j2y.c @@ -5,6 +5,7 @@ #define internal static #include <stdlib.h> +#include <stdio.h> #ifndef unitTest #endif @@ -39,7 +40,7 @@ int MAIN(int ARGC, char** ARGV) { // remove extension *p = 0; } - appendG(&oFName, ".yml"); + pErrorNULL(appendG(&oFName, ".yml")); // create json object createAllocateSmallJson(json); diff --git a/example/logging.c b/example/logging.c @@ -21,7 +21,7 @@ int MAIN(int ARGC, char** ARGV) { argc = ARGC; argv = ARGV;;// logging // delete previous log - rmAll("logging.log"); + pError0(rmAll("logging.log")); setLogFile("logging.log"); pLog(LOG_INFO,"logging started"); diff --git a/example/mkparents.c b/example/mkparents.c @@ -28,6 +28,6 @@ int MAIN(int ARGC, char** ARGV) { char *path = ARGV[1]; char *p = normalizePath(path); if (!fileExists(p)) { - mkdirParents(p); + pError0(mkdirParents(p)); } } diff --git a/example/mve.c b/example/mve.c @@ -30,13 +30,13 @@ int MAIN(int ARGC, char** ARGV) { // only one parameter char *cmd = catS("mv ", argv[1], " ."); puts(cmd); - systemNFree(cmd); + pErrorNot0(systemNFree(cmd)); } else { rangeFrom(i, 1, argc-1) { char *cmd = catS("mv ", argv[i], " ", argv[argc-1]); puts(cmd); - systemNFree(cmd); + pErrorNot0(systemNFree(cmd)); } } } diff --git a/example/readme.c b/example/readme.c @@ -6,6 +6,7 @@ #define internal static #include <stdlib.h> +#include <stdio.h> #ifndef unitTest #endif @@ -45,7 +46,7 @@ int MAIN(int ARGC, char** ARGV) { char *s; s = trimS(*e); if (!isEmptyS(s) && !findS(*e, "// _libsheepyH")) - listPushS(&functionPrototypes, *e); + pErrorNULL(listPushS(&functionPrototypes, *e)); free(s); } if (findS(*e, "libsheepy API Header file _libsheepyH")) { @@ -68,13 +69,13 @@ int MAIN(int ARGC, char** ARGV) { if (strEq(s, "/**")) funcStatus = PROTOTYPE; if (funcStatus == PROTOTYPE) { - listPushS(&descriptions, *e); + pErrorNULL(listPushS(&descriptions, *e)); } if (funcStatus == DESCRIPTION) { free(s); s = replaceS(*e, " {", "", 0); - listPushS(&descriptions, s); - listPushS(&descriptions, "\n"); + pErrorNULL(listPushS(&descriptions, s)); + pErrorNULL(listPushS(&descriptions, "\n")); funcStatus = START; } if (findS(s, "*/") && (funcStatus == PROTOTYPE)) { @@ -86,7 +87,7 @@ int MAIN(int ARGC, char** ARGV) { status = FUNCTIONS; } } - + //listPrintS(descriptions); // read template @@ -95,18 +96,18 @@ int MAIN(int ARGC, char** ARGV) { // process template forEachCharP(template, e) { if (findS(*e, "__functionPrototypes")) { - listAppendS(&readmeResult, functionPrototypes); + pErrorNULL(listAppendS(&readmeResult, functionPrototypes)); } else if (findS(*e, "__functionDescriptions")) { - listAppendS(&readmeResult, descriptions); + pErrorNULL(listAppendS(&readmeResult, descriptions)); } else { - listPushS(&readmeResult, *e); + pErrorNULL(listPushS(&readmeResult, *e)); } } // save result - writeText("../README.md", readmeResult); + pError0(writeText("../README.md", readmeResult)); listFreeS(readmeResult); listFreeS(template); diff --git a/example/regexDemo.c b/example/regexDemo.c @@ -54,7 +54,7 @@ void replace() { forEachCharP(config, f) { if (!isBlankS(*f)) { if (*f[0] != '#') { - iTrimS(f); + pErrorNULL(iTrimS(f)); char **src = readText(*f); if (!src) { printf("Cant read file %s", *f); @@ -64,7 +64,7 @@ void replace() { forEachCharP(src, l) { enumerateCharP(search_what, a, i) { - iReplaceS_max(l, *a, replace_with[i]); + pErrorNULL(iReplaceS_max(l, *a, replace_with[i])); } } diff --git a/example/search_and_replace_strings_in_files.c b/example/search_and_replace_strings_in_files.c @@ -72,7 +72,7 @@ void replace() { printf("\n"); printf("%s\n", replace_with[i]); char *reg = catS("\\b", *e, "\\b"); - iListPushS(&strRegex, reg); + pErrorNULL(iListPushS(&strRegex, reg)); } //listPrintS(strRegex); @@ -116,7 +116,7 @@ void replace() { forEachCharP(config, f) { if (!isBlankS(*f)) { if (*f[0] != '#') { - iTrimS(f); + pErrorNULL(iTrimS(f)); char **src = readText(*f); if (!src) { printf("Cant read file %s", *f); diff --git a/example/showdir.c b/example/showdir.c @@ -54,7 +54,7 @@ int MAIN(int ARGC, char** ARGV) { printf("%s", c); free(c); char **text = readText(*e); - listPrintS(text); + pError0(listPrintS(text)); listFreeS(text); } } diff --git a/example/smallObjectsUsage.c b/example/smallObjectsUsage.c @@ -20,7 +20,7 @@ int argc; char **argv; #define MAIN main #endif int MAIN(int ARGC, char** ARGV) { - char **list = NULL; + const char **list = NULL; char *s = NULL; argc = ARGC; argv = ARGV;;// Manipulate dictionaries and arrays @@ -92,7 +92,7 @@ int MAIN(int ARGC, char** ARGV) { printf("Array element types:"); printf("\n"); list = typeStringsG(tommy); - s = join(list, ", "); + s = join((char**)list, ", "); // smallArrays return list of static strings, use free instead of listFreeS free(list); logNFree(s); diff --git a/example/threads.c b/example/threads.c @@ -67,7 +67,7 @@ int main(int ARGC, char** ARGV) { if (args.list) { puts("asWalkdir success"); - listPrintS(args.list); + pError0(listPrintS(args.list)); printf("The ring has %d messages", (int)ringCount(&c)); printf("\n"); printf("last %d", (int)c.last); diff --git a/example/y2b.c b/example/y2b.c @@ -5,6 +5,7 @@ #define internal static #include <stdlib.h> +#include <stdio.h> #ifndef unitTest #endif @@ -39,7 +40,7 @@ int MAIN(int ARGC, char** ARGV) { // remove extension *p = 0; } - appendG(&oFName, ".bin"); + pErrorNULL(appendG(&oFName, ".bin")); // create yml object createAllocateSmallJson(json); diff --git a/example/y2j.c b/example/y2j.c @@ -5,6 +5,7 @@ #define internal static #include <stdlib.h> +#include <stdio.h> #ifndef unitTest #endif @@ -39,7 +40,7 @@ int MAIN(int ARGC, char** ARGV) { // remove extension *p = 0; } - appendG(&oFName, ".json"); + pErrorNULL(appendG(&oFName, ".json")); // create yml object createAllocateSmallJson(json); diff --git a/release/libsheepy.h b/release/libsheepy.h @@ -98,7 +98,7 @@ // version accoring to the version package: Release.Major.minor.patch // https://noulin.net/version/file/README.md.html -#define LIBSHEEPY_VERSION "2.2.11" +#define LIBSHEEPY_VERSION "2.2.12" #ifndef SH_PREFIX #define SH_PREFIX(NAME) NAME diff --git a/release/libsheepyObject.h b/release/libsheepyObject.h @@ -9599,6 +9599,38 @@ struct base { baseFunctionst *f; }; +// terminate baset val when it is out of scope +void cleanUpBaseTerminateG(baset **val); + +// free baset val when it is out of scope +void cleanUpBaseFreeG(baset **val); + +// finish baset val when it is out of scope +void cleanUpBaseFinishG(baset **val); + +// smash baset val when it is out of scope +void cleanUpBaseSmashG(baset **val); + +/** + * declare pointer name with type baset and terminate name when it is out of scope + */ +#define cleanBaseP(name) baset *name CLEANUP(cleanUpBaseTerminateG) + +/** + * declare pointer name with type baset and free name when it is out of scope + */ +#define cleanFreeBase(name) baset *name CLEANUP(cleanUpBaseFreeG) + +/** + * declare pointer name with Type baset and finish name when it is out of scope + */ +#define cleanFinishBaseP(name) baset *name CLEANUP(cleanUpBaseFinishG) + +/** + * declare pointer name with Type baset and smash name when it is out of scope + */ +#define cleanSmashBaseP(name) baset *name CLEANUP(cleanUpBaseSmashG) + // end class base /** diff --git a/src/json/libsheepyObject.c b/src/json/libsheepyObject.c @@ -61,6 +61,10 @@ smallDictt *shSysinfo(void); smallDictt *shSysinfo(void); #endif +void cleanUpBaseTerminateG(baset **val); +void cleanUpBaseFreeG(baset **val); +void cleanUpBaseFinishG(baset **val); +void cleanUpBaseSmashG(baset **val); baset *duplicateBaseG(baset *self); void freeManyOF(void *paramType, ...); void terminateManyOF(void *paramType, ...); @@ -548,6 +552,26 @@ smallDictt *shSysinfo(void) { #endif // #if __APPLE__ || __FreeBSD__ || __OpenBSD__ || __DragonFly__ || __sun__ || __HAIKU__ +void cleanUpBaseTerminateG(baset **val) { + + terminateO(*val); +} + +void cleanUpBaseFreeG(baset **val) { + + freeO(*val); +} + +void cleanUpBaseFinishG(baset **val) { + + finishO(*val); +} + +void cleanUpBaseSmashG(baset **val) { + + smashO(*val); +} + baset *duplicateBaseG(baset *self) { return(self->f->duplicate(self)); diff --git a/src/libsheepy.h b/src/libsheepy.h @@ -98,7 +98,7 @@ // version accoring to the version package: Release.Major.minor.patch // https://noulin.net/version/file/README.md.html -#define LIBSHEEPY_VERSION "2.2.11" +#define LIBSHEEPY_VERSION "2.2.12" #ifndef SH_PREFIX #define SH_PREFIX(NAME) NAME diff --git a/src/libsheepyObject.h b/src/libsheepyObject.h @@ -9599,6 +9599,38 @@ struct base { baseFunctionst *f; }; +// terminate baset val when it is out of scope +void cleanUpBaseTerminateG(baset **val); + +// free baset val when it is out of scope +void cleanUpBaseFreeG(baset **val); + +// finish baset val when it is out of scope +void cleanUpBaseFinishG(baset **val); + +// smash baset val when it is out of scope +void cleanUpBaseSmashG(baset **val); + +/** + * declare pointer name with type baset and terminate name when it is out of scope + */ +#define cleanBaseP(name) baset *name CLEANUP(cleanUpBaseTerminateG) + +/** + * declare pointer name with type baset and free name when it is out of scope + */ +#define cleanFreeBase(name) baset *name CLEANUP(cleanUpBaseFreeG) + +/** + * declare pointer name with Type baset and finish name when it is out of scope + */ +#define cleanFinishBaseP(name) baset *name CLEANUP(cleanUpBaseFinishG) + +/** + * declare pointer name with Type baset and smash name when it is out of scope + */ +#define cleanSmashBaseP(name) baset *name CLEANUP(cleanUpBaseSmashG) + // end class base /**