cstring

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

commit 30d46f7ba2226cf4aba7c5807a46380d4f3d9f81
parent 48893e3c5054d0d5760a258bad9213e10be332f2
Author: Christos Margiolis <christos@margiolis.net>
Date:   Mon,  7 Sep 2020 21:29:02 +0300

added getline

Diffstat:
MREADME.md | 9+++++----
Acstring | 0
Mcstring.c | 12++++++++++++
Mcstring.h | 46++++++++++++++++++++++++----------------------
Acstring.o | 0
Mtest.c | 1-
Atest.o | 0
7 files changed, 41 insertions(+), 27 deletions(-)

diff --git a/README.md b/README.md @@ -30,12 +30,13 @@ The recommended way of initializing an empty string is by doing `cstring foo = c * `cstring_empty`: Checks if the string is empty * `cstring_copy`: Makes a copy of a given `const char *` * `cstring_resize`: Resizes the array stored inside the string `struct` -* `cstring_equals`: True if the strings are equal -* `cstring_not_equals`: True if the strings are not equal +* `cstring_getline`: Reads a line from a stream +* `cstring_equal`: True if the strings are equal +* `cstring_not_equal`: True if the strings are not equal * `cstring_greater`: True if the left hand string is greater than the right hand one -* `cstring_greater_or_equals`: True if the left hand string is greater than or equal to the right hand one +* `cstring_greater_or_equal`: True if the left hand string is greater than or equal to the right hand one * `cstring_less`: True if the left hand string is less than the right hand one -* `cstring_less_or_equals`: True if the left hand string is less than or equal to the right hand one +* `cstring_less_or_equal`: True if the left hand string is less than or equal to the right hand one ## Example diff --git a/cstring b/cstring Binary files differ. diff --git a/cstring.c b/cstring.c @@ -143,6 +143,18 @@ cstring_resize(cstring *cs, size_t newcapacity) } } +cstring * +cstring_getline(FILE *fd, cstring *cs, char delim) +{ + char c; + cstring_clear(cs); + while ((c = fgetc(fd)) != EOF && c != '\n') { + if (c == delim) break; + else cstring_push_back(cs, c); + } + return (c == EOF) ? NULL : cs; +} + int cstring_equals(const cstring *lhs, const cstring *rhs) { diff --git a/cstring.h b/cstring.h @@ -1,37 +1,39 @@ #ifndef CSTRING_H #define CSTRING_H +#include <stdio.h> #include <stdlib.h> #include <string.h> -typedef struct { +typedef struct cstring { char *str; size_t len; size_t capacity; } cstring; -extern cstring cstring_init(const char *); -extern void cstring_delete(cstring *); -extern void cstring_assign(cstring *, const char *); -extern void cstring_append(cstring *, const char *); -extern void cstring_insert(cstring *, const char *, size_t); -extern void cstring_push_back(cstring *, char); -extern void cstring_pop_back(cstring *); -extern void cstring_replace_char(cstring *, size_t, char); -extern void cstring_clear(cstring *); -extern int cstring_exists(const cstring *, const char *); -extern char cstring_front(const cstring *); -extern char cstring_back(const cstring *); -extern int cstring_empty(const cstring *); -extern char *cstring_copy(const char *); -extern void cstring_resize(cstring *, size_t); +extern cstring cstring_init(const char *); +extern void cstring_delete(cstring *); +extern void cstring_assign(cstring *, const char *); +extern void cstring_append(cstring *, const char *); +extern void cstring_insert(cstring *, const char *, size_t); +extern void cstring_push_back(cstring *, char); +extern void cstring_pop_back(cstring *); +extern void cstring_replace_char(cstring *, size_t, char); +extern void cstring_clear(cstring *); +extern int cstring_exists(const cstring *, const char *); +extern char cstring_front(const cstring *); +extern char cstring_back(const cstring *); +extern int cstring_empty(const cstring *); +extern char *cstring_copy(const char *); +extern void cstring_resize(cstring *, size_t); +extern cstring *cstring_getline(FILE *, cstring *, char); /* might be useless */ -extern int cstring_equals(const cstring *, const cstring *); -extern int cstring_not_equals(const cstring *, const cstring *); -extern int cstring_greater(const cstring *, const cstring *); -extern int cstring_greater_or_equals(const cstring *, const cstring *); -extern int cstring_less(const cstring *, const cstring *); -extern int cstring_less_or_equals(const cstring *, const cstring *); +extern int cstring_equal(const cstring *, const cstring *); +extern int cstring_not_equal(const cstring *, const cstring *); +extern int cstring_greater(const cstring *, const cstring *); +extern int cstring_greater_or_equal(const cstring *, const cstring *); +extern int cstring_less(const cstring *, const cstring *); +extern int cstring_less_or_equal(const cstring *, const cstring *); #endif /* CSTRING_H */ diff --git a/cstring.o b/cstring.o Binary files differ. diff --git a/test.c b/test.c @@ -1,4 +1,3 @@ -#include <stdio.h> #include "cstring.h" int diff --git a/test.o b/test.o Binary files differ.