C lib for handling text files, strings and json like data structure with an OO system
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
example
release
src
.gitignore
LICENSE
Makefile
README.md
build.sh
clean.sh
completion.txt
documentation.md
genMake.c
install.sh
make.sh
makeTermux.sh
uninstall.sh
wordexp.h

README.md

libsheepy

libsheepy is C library for handling text files and strings.

Status: libsheepy and libsheepyObject implemented

The API in libsheepy.h and libsheepyObject.h is stable but not yet completely tested.

Future: (mainly speed improvements)

  • optimize memory management
  • optimize internals
  • add functions saving results in given objects
  • create generics calling class functions directly

Usage: I use libsheepy in C scripts and resource limited environments

Platforms:

  • Linux/GNU, Intel (Ivy Bridge or newer for DRNG RDRAND)
  • Linux/GNU, ARM (raspberry pi)
  • Android/Termux ARM
  • MacOS
  • FreeBSD

libsheepy is the default library in the sheepy project (sheepy is a build system to run c programs like scripts).

The functions in libsheepy are similar to their equivalent functions in javascript or python. For example:

  • upperS is equivalent to .upper() in python.
  • forEach is similar to the javascript forEach.
  • enumerate is similar to the python enumerate.

There are 2 parts in libsheepy:

  1. C functions: simple data structures and functions, implemented in libsheepy.h and libsheepy.c
  2. C objects: json, dictionary, array objects, libsheepyObject.h is the header to include.

libsheepy uses the type char ** to represent the lists (dynamic array of strings). The end of the list is NULL.

libsheepy is NULL safe and any parameter can be NULL.

  • the list*S functions assume the list elements are not NULL
  • the listn*S functions can process lists with NULL elements. When there are NULL elements, the developer has to keep track of the list length.

The documentation is here and a more detailed documentation is available at http://spartatek.se/libsheepy/

Status: (libsheepy.h and libsheepy.c)

  • Unit tests: 94% code coverage of the core functionality - 98% branch coverage (mainly malloc failures are not tested)
  • Valgrind - Memcheck: No memory leaks - 94% code coverage of the core functionality
  • Static analysis: cppcheck, clang static analyzer - pending result

Features

  • few generics to handle all types (for example: eqG(a,b) true when the a and b values are equal, a can be an int and b can be a string representing an int)
  • error reporting
  • setjmp, longjmp macros
  • short int names: i8, i16, i32...
  • MIN, MAX macros ...
  • cast macros
  • logging
  • terminal color control codes
  • time functions
  • file/path functions
  • random numbers software and hardware
  • user input functions (passwords)
  • string functions
  • array (static, dynamic) - for queues, rings, stacks...
  • dictionary, json object, stringify and parsing
  • thread pool
  • UTF-8 string functions: length, makeValid...

Compilation and install

To be able to compile, gcc (4.9 or newer for libsheepyObject) and make have to be installed on the machine.

In the root directory run:

# Termux only
pkg install git clang
# End Termux

git clone https://github.com/RemyNoulin/libsheepy.git
cd libsheepy
git pull

# All except Termux
./make.sh

# Termux only
./makeTermux.sh

The compiled lib and headers are in release/ after make all or build.sh successfully executed

The lib is installed in /usr/local/ and in /data/data/com.termux/files/usr/ in Termux

Examples

This code shows some features of the smallArray and smallJson classes:

// array declaration
smallArrayt *array;
// object init
initiateG(&array);

// push strings to array
pushManySG(array, "first string", "second string", "end");

// print array
logVarG(array);

// add a bool at index -3 (0 is beginning of the array, -1 is the end)
injectG(array, -3, TRUE);

// add an int element at the end
pushG(array, 2);

// print array
putsG(array);

// loop on the array elements
iter(array, element) {
  // print index and element
  printf("Element index %d, value: %m\n", iterIndexG(array), element);
}

// free array
terminateG(array);

// create a local json object
createSmallJson(json);

// parse json string
parseG(&json, "{\"key1\": [1,2], \"key2\": {\"subkey1\": \"a value\"}}");
// or use _ instead of \" for readabilty:
// parseG(&json, "{"_"key1"_": [1,2], "_"key2"_": {"_"subkey1"_": "_"a value"_"}}");

// print json
logVarG(&json);

// free buffers in json object
freeG(&json);

Demo

A demo illustrating how to use libsheepy is located in the example folder.

The demo runs on both linux and macOS.

The demo shows how to use: execOut, exitFailure, listPushS, listPrintS, forEach, split, listGetS, strEq, trimS, listCreateS, enumerate

Linux

In linux, the demo can be executed as a C script if tcc is installed.

apt-get install tcc
cd example
./demoScript.c

Alternatively, compile the C files with:

cd example
./compileLinux.sh
./demo

Termux

Compile with (adding -D__TERMUX__=1 -D__arm__=1 to the linux gcc commands):

cd example
gcc -D__TERMUX__=1 -D__arm__=1 -ggdb -std=gnu99 -c ../release/libsheepy.c
gcc -D__TERMUX__=1 -D__arm__=1 -std=gnu99 -o demo demo.c libsheepy.o
./demo

MacOS

Compile the C files and run like this:

cd example
./compileMacOS.sh
./demo

Expected results from the demo

The results from the demo look like this:

./demo

Steps
- execOut "ls"
- push "libsheepy" at the end of list
- push CLI argument
- print list
- forEach print lines containing the words two or libsheepy

total 0
drwxr-xr-x 1 remy remy  12 Dec 22 14:19 .
drwxr-xr-x 1 remy remy 742 Dec 24 16:09 ..
-rw-r--r-- 1 remy remy   0 Dec 22 14:19 one
drwxr-xr-x 1 remy remy  18 Dec 22 14:19 two
  libsheepy

Print lines with string "two" or "libsheepy":

drwxr-xr-x 1 remy remy  18 Dec 22 14:19 two
libsheepy

demoStatic

This demo shows how to use the objects in libsheepy.

make
cd example
gcc -ggdb -std=gnu99 -o demoStatic demoStatic.c ../release/libsheepy.a -pthread
# or with clang
clang -ggdb -std=gnu11 -o demoStatic demoStatic.c ../release/libsheepy.a -pthread
./demoStatic

demoStatic links statically to libsheepy

readme example

This README file is generated using example/README.template and readme.c

The readme shows how to use: readText, forEach, trimS, isEmptyS, findS, listPushS, sliceS, strEq, replaceS, listAppendS

showdir example

showdir is a simple program showing the text content of files in current directory or a given path.

It shows how to use: walkDir, forEach, catS, readText, listPrintS, listFreeS

sumNums example

sumNums.c sums integers in a text file line by line.

INPUT FILE: sum.txt
1
2
3

./sumNumsScript.c sum.txt

RESULT: 6

The sumNums shows how to use: readText, listCompactS, trimS, forEach, split, listGetS, parseInt

cfp example

cfp.c prints the current working directory in front of the given parameters on the command line.

search and replace example

search and replace is useful for refactoring source code.

It takes a list of files in files.txt and a list of strings to search and replace in replace_configuration.txt.

It uses the pcre library for the regular expressions.

Install pcre with the command:

apt-get install libpcre3-dev

regex demo example

Example with the pcre library.

Install pcre with the command:

apt-get install libpcre3-dev

dmce example

dmce stands for did my code execute. It displays the lines changed in the unstaged files and not executed the unit tests.

The program has to be compiled with gcov.

dmce uses git diff to list the changes.

csv example

csv.c loads a csv file into 2 lists: a void** list to hold the lines, the lines are split into char** lists.

inotify example

inotify.c checks the changes in *.h and *.c files in the tree under the current working directory and runs a command given in parameter.

objects example

objects.c prints the list of object types with effects and colors.

make
cd example
gcc -ggdb -std=gnu11 -o objects objects.c ../release/libsheepy.a -pthread
./objects

json example

json.c shows how to load/print JSON files and YML files.

It also shows how to use the foreach in dictt objects, parse, parseYML, stringify, toYML, freeO and toStringO

make
cd example
gcc -ggdb -std=gnu11 -o json json.c ../release/libsheepy.a -pthread
./json

csvStatic and dmceStatic examples

Theses examples are identical to the csv and dmce examples using libsheepy objects instead.

gcc -ggdb -std=gnu11 -o csvStatic csvStatic.c ../release/libsheepy.a -pthread
./csvStatic
gcc -ggdb -std=gnu11 -o dmceStatic dmceStatic.c ../release/libsheepy.a -pthread
./dmceStatic