summaryrefslogtreecommitdiffstats
path: root/StringUtils.c
diff options
context:
space:
mode:
authorHisham Muhammad <hisham@gobolinux.org>2016-06-19 18:55:35 -0300
committerGitHub <noreply@github.com>2016-06-19 18:55:35 -0300
commit0fa03322a935a6ffc564852068f13302f3dd1e72 (patch)
tree2f7fc7ef742e55a46cec55fac9114256c68cb002 /StringUtils.c
parent52f814481cdfa4be2290953978a81014f8bfd391 (diff)
Dynamically adjust the size of line reads
* Dynamically adjust the size of line reads. * Remove some more uses of fgets with arbitrary sizes. * Fix reading of lines and width of n column. Fixes #514.
Diffstat (limited to 'StringUtils.c')
-rw-r--r--StringUtils.c29
1 files changed, 28 insertions, 1 deletions
diff --git a/StringUtils.c b/StringUtils.c
index 4465fb15..b53f5eea 100644
--- a/StringUtils.c
+++ b/StringUtils.c
@@ -13,9 +13,10 @@ in the source distribution for its full text.
#include <string.h>
#include <strings.h>
#include <stdlib.h>
-#include <stdio.h>
/*{
+#include <stdio.h>
+
#define String_startsWith(s, match) (strstr((s), (match)) == (s))
#define String_contains_i(s1, s2) (strcasestr(s1, s2) != NULL)
}*/
@@ -119,3 +120,29 @@ char* String_getToken(const char* line, const unsigned short int numMatch) {
match[foundCount] = '\0';
return((char*)xStrdup(match));
}
+
+char* String_readLine(FILE* fd) {
+ const int step = 1024;
+ int bufSize = step;
+ char* buffer = xMalloc(step + 1);
+ char* at = buffer;
+ for (;;) {
+ char* ok = fgets(at, step + 1, fd);
+ if (!ok) {
+ free(buffer);
+ return NULL;
+ }
+ char* newLine = strrchr(at, '\n');
+ if (newLine) {
+ *newLine = '\0';
+ return buffer;
+ } else {
+ if (feof(fd)) {
+ return buffer;
+ }
+ }
+ bufSize += step;
+ buffer = xRealloc(buffer, bufSize + 1);
+ at = buffer + bufSize - step;
+ }
+}

© 2014-2024 Faster IT GmbH | imprint | privacy policy