cstring

Lightweight string library for C
git clone git://git.christosmarg.xyz/cstring.git
Log | Files | Refs | README | LICENSE

commit 76e965dfaa56d81fff432641517a0b1e69b53463
parent c333ea917961b609f6b85a36606922b7280498b4
Author: Christos Margiolis <christos@margiolis.net>
Date:   Mon, 21 Sep 2020 21:07:22 +0300

made it an actual static library, added manpage and improved README

Diffstat:
MMakefile | 42+++++++++++++++++++++++++-----------------
MREADME.md | 26++++++++++++++++++++++----
Acstring.3 | 79+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mtest.c | 2++
4 files changed, 128 insertions(+), 21 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,12 +1,15 @@ -BIN = cstring -MAN1 = ${BIN}.1 +LIB = cstring +MAN3 = ${LIB}.3 PREFIX = /usr/local -MAN_DIR = ${PREFIX}/man/man1 -BIN_DIR = ${PREFIX}bin +MAN_DIR = ${PREFIX}/man/man3 +HDR_DIR = ${PREFIX}/include +LIB_DIR = ${PREFIX}/lib SRC = ${wildcard *.c} OBJ = ${SRC:%.c=%.o} +AR = ar +ARFLAGS = rs CC = gcc CPPFLAGS += -Iinclude -pedantic CFLAGS += -Wall -std=c99 -O3 @@ -18,23 +21,28 @@ MKDIR = mkdir -p .PHONY: all clean -all: ${BIN} +all: ${LIB} -${BIN}: ${OBJ} - ${CC} ${LDFLAGS} $^ ${LDLIBS} -o $@ +${LIB}: ${OBJ} + ${AR} ${ARFLAGS} lib${LIB}.a ${OBJ} %.o: %.c ${CC} ${CPPFLAGS} ${CFLAGS} -c $< -o $@ -run: - ./${BIN} - -#install: all - #${MKDIR} ${DESTDIR}${BIN_DIR} - #${CP} ${BIN} ${BIN_DIR} - #${MKDIR} ${DESTDIR}${MAN_DIR} - #${CP} ${MAN1} ${DESTDIR}${MAN_DIR} - #chmod 644 ${DESTDIR}${MAN_DIR}/${MAN1} +install: all + ${MKDIR} ${DESTDIR}${LIB_DIR} ${DESTDIR}${HDR_DIR} + ${CP} ${LIB}.h ${DESTDIR}${HDR_DIR} + ${CP} lib${LIB}.a ${DESTDIR}${LIB_DIR} + chmod 644 ${DESTDIR}${HDR_DIR}/${LIB}.h + chmod 644 ${DESTDIR}${LIB_DIR}/lib${LIB}.a + ${MKDIR} ${DESTDIR}${MAN_DIR} + ${CP} ${MAN3} ${DESTDIR}${MAN_DIR} + chmod 644 ${DESTDIR}${MAN_DIR}/${MAN3} + +uninstall: all + sudo ${RM} ${DESTDIR}${HDR_DIR}/${LIB}.h + sudo ${RM} ${DESTDIR}${LIB_DIR}/lib${LIB}.a + sudo ${RM} ${DESTDIR}${MAN_DIR}/${MAN3} clean: - ${RM} ${OBJ} ${BIN} + ${RM} ${OBJ} ${LIB} lib${LIB}.a diff --git a/README.md b/README.md @@ -2,10 +2,28 @@ A simple and lightweight string library for C. -## Usage +## Building + +`cstring` is a static library. The header file is installed in `/usr/local/include` and +the library file in `/usr/local/lib`. In order to install it do the following + +```shell +$ cd /path/to/cstring +$ make +$ sudo make install +$ make clean +``` + +If you want to uninstall the library do the following -Simply include the source files in your projects and compile them -along with your other files. +```shell +$ cd /path/to/cstring +$ sudo make uninstall +``` + +In order to link `cstring` to your project use the `-lcstring` flag during the compilation. + +## Usage When using this library, you must to **always** call the `cstring_create` and `cstring_delete` functions whenever you want to make a new instance of `cstring` and stop using it respectively, @@ -44,7 +62,7 @@ See `test.c` for more. ```c #include <stdio.h> -#include "cstring.h" +#include <cstring.h> int main(int argc, char **argv) diff --git a/cstring.3 b/cstring.3 @@ -0,0 +1,79 @@ +.TH cstring 3 +.SH NAME +cstring \- A simple and lightweight string library for C inspired by C++'s +STL string class +.SH FUNCTIONS +.TP +.BR cstring\ cstring_create(const\ char\ *) +Instanciates and initializes a +.I cstring +object. +.TP +.BR void\ cstring_delete(cstring\ *) +Deallocate string. +.TP +.BR void\ cstring_assign(cstring\ *,\ const\ char\ *) +Assign a new string to the current string. +.TP +.BR void\ cstring_append(cstring\ *,\ const\ char\ *) +Append a string at the end of the current string. +.TP +.BR void\ cstring_insert(cstring\ *,\ const\ char\ *,\ size_t) +Insert a string at a specific index. +.TP +.BR void\ cstring_push_back(cstring\ *,\ char) +Add a character at the end of the string. +.TP +.BR void\ cstring_pop_back(cstring\ *) +Remove the last character in the string. +.TP +.BR void\ cstring_replace_char(cstring\ *,\ const\ char\ *) +Replace character at a specific index. +.TP +.BR void\ cstring_clear(cstring\ *) +Erase the whole string. +.TP +.BR int\ cstring_exists(const\ cstring\ *,\ const\ char\ *) +Check to see if a (sub)string exists in the string. +.TP +.BR char\ cstring_front(const\ cstring\ *) +Returns the first character of the string. +.TP +.BR char\ cstring_back(const\ cstring\ *) +Returns the last character of the string. +.TP +.BR int\ cstring_empty(const\ cstring\ *) +Check to see if the string is empty. +.TP +.BR char\ *cstring_copy(const\ char\ *) +Make a copy of a given +.I const\ char\ * +.TP +.BR void\ cstring_resize(cstring\ *,\ size_t) +Resize the +.I str +array inside a given +.I cstring +struct. +.TP +.BR cstring\ *cstring_getline(FILE\ *,\ cstring\ *,\ char) +Read a line from a +.I FILE +stream. Similar behavior to +.I stdio's\ getline +.SH USAGE +You must +.B always +call the +.I cstring_create +and +.I cstring_delete +functions whenever you want to make a new instance of +.I cstring +and stop using it respectively, in order to not cause any memory +leaks. +.P +The recommended way of initializing an empty string is by doing +.I cstring foo = cstring_create("") +.SH AUTHORS +Christos Margiolis <christos@christosmarg.xyz> diff --git a/test.c b/test.c @@ -1,5 +1,7 @@ #include "cstring.h" +// Compilation: gcc test.c -lcstring + int main(int argc, char **argv) {