aboutsummaryrefslogtreecommitdiffstats
path: root/StringUtils.c
diff options
context:
space:
mode:
authorDaniel Lange <DLange@git.local>2016-07-25 18:25:28 +0200
committerDaniel Lange <DLange@git.local>2016-07-25 18:25:28 +0200
commit31b71b67011fa52f091df6fe536a11d6d0bfb256 (patch)
tree31cb222871017376af8b7435d24cf4961863fa93 /StringUtils.c
parent8c82a38002ce09db2a0b83dab8b598cf1ab0596c (diff)
downloaddebian_htop-31b71b67011fa52f091df6fe536a11d6d0bfb256.tar.gz
debian_htop-31b71b67011fa52f091df6fe536a11d6d0bfb256.tar.bz2
debian_htop-31b71b67011fa52f091df6fe536a11d6d0bfb256.zip
Imported Upstream version 2.0.2upstream/2.0.2
Diffstat (limited to 'StringUtils.c')
-rw-r--r--StringUtils.c42
1 files changed, 30 insertions, 12 deletions
diff --git a/StringUtils.c b/StringUtils.c
index 173caba..b53f5ee 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)
}*/
@@ -69,13 +70,7 @@ char** String_split(const char* s, char sep, int* n) {
ctr++;
if (ctr == blocks) {
blocks += rate;
- char** newOut = (char**) xRealloc(out, sizeof(char*) * blocks);
- if (newOut) {
- out = newOut;
- } else {
- blocks -= rate;
- break;
- }
+ out = (char**) xRealloc(out, sizeof(char*) * blocks);
}
s += size + 1;
}
@@ -86,10 +81,7 @@ char** String_split(const char* s, char sep, int* n) {
out[ctr] = token;
ctr++;
}
- char** newOut = xRealloc(out, sizeof(char*) * (ctr + 1));
- if (newOut) {
- out = newOut;
- }
+ out = xRealloc(out, sizeof(char*) * (ctr + 1));
out[ctr] = NULL;
*n = ctr;
return out;
@@ -128,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