summaryrefslogtreecommitdiffstats
path: root/XUtils.c
diff options
context:
space:
mode:
authorBenny Baumann <BenBE@geshi.org>2020-10-14 20:21:09 +0200
committerBenny Baumann <BenBE@geshi.org>2020-10-16 20:30:21 +0200
commit872e542f4eca52ce2198ba3fc30a51bc5d672dae (patch)
treed47d619840ec26bdc099ceff1351f5e6cc901e1c /XUtils.c
parentc6f04a9c5d33d16623f1c20f0987dd3a992379e7 (diff)
Rename StringUtils.[ch] to XUtils.[ch]
Diffstat (limited to 'XUtils.c')
-rw-r--r--XUtils.c179
1 files changed, 179 insertions, 0 deletions
diff --git a/XUtils.c b/XUtils.c
new file mode 100644
index 00000000..9228c5e8
--- /dev/null
+++ b/XUtils.c
@@ -0,0 +1,179 @@
+/*
+htop - StringUtils.c
+(C) 2004-2011 Hisham H. Muhammad
+Released under the GNU GPLv2, see the COPYING file
+in the source distribution for its full text.
+*/
+
+#include "config.h"
+
+#include "XUtils.h"
+
+#include <stdarg.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+
+#include "XAlloc.h"
+
+
+char* String_cat(const char* s1, const char* s2) {
+ int l1 = strlen(s1);
+ int l2 = strlen(s2);
+ char* out = xMalloc(l1 + l2 + 1);
+ memcpy(out, s1, l1);
+ memcpy(out+l1, s2, l2+1);
+ out[l1 + l2] = '\0';
+ return out;
+}
+
+char* String_trim(const char* in) {
+ while (in[0] == ' ' || in[0] == '\t' || in[0] == '\n') {
+ in++;
+ }
+ int len = strlen(in);
+ while (len > 0 && (in[len-1] == ' ' || in[len-1] == '\t' || in[len-1] == '\n')) {
+ len--;
+ }
+ char* out = xMalloc(len+1);
+ strncpy(out, in, len);
+ out[len] = '\0';
+ return out;
+}
+
+inline int String_eq(const char* s1, const char* s2) {
+ if (s1 == NULL || s2 == NULL) {
+ if (s1 == NULL && s2 == NULL)
+ return 1;
+ else
+ return 0;
+ }
+ return (strcmp(s1, s2) == 0);
+}
+
+char** String_split(const char* s, char sep, int* n) {
+ *n = 0;
+ const int rate = 10;
+ char** out = xCalloc(rate, sizeof(char*));
+ int ctr = 0;
+ int blocks = rate;
+ char* where;
+ while ((where = strchr(s, sep)) != NULL) {
+ int size = where - s;
+ char* token = xMalloc(size + 1);
+ strncpy(token, s, size);
+ token[size] = '\0';
+ out[ctr] = token;
+ ctr++;
+ if (ctr == blocks) {
+ blocks += rate;
+ out = (char**) xRealloc(out, sizeof(char*) * blocks);
+ }
+ s += size + 1;
+ }
+ if (s[0] != '\0') {
+ out[ctr] = xStrdup(s);
+ ctr++;
+ }
+ out = xRealloc(out, sizeof(char*) * (ctr + 1));
+ out[ctr] = NULL;
+ *n = ctr;
+ return out;
+}
+
+void String_freeArray(char** s) {
+ if (!s) {
+ return;
+ }
+ for (int i = 0; s[i] != NULL; i++) {
+ free(s[i]);
+ }
+ free(s);
+}
+
+char* String_getToken(const char* line, const unsigned short int numMatch) {
+ const unsigned short int len = strlen(line);
+ char inWord = 0;
+ unsigned short int count = 0;
+ char match[50];
+
+ unsigned short int foundCount = 0;
+
+ for (unsigned short int i = 0; i < len; i++) {
+ char lastState = inWord;
+ inWord = line[i] == ' ' ? 0:1;
+
+ if (lastState == 0 && inWord == 1)
+ count++;
+
+ if(inWord == 1){
+ if (count == numMatch && line[i] != ' ' && line[i] != '\0' && line[i] != '\n' && line[i] != (char)EOF) {
+ match[foundCount] = line[i];
+ foundCount++;
+ }
+ }
+ }
+
+ match[foundCount] = '\0';
+ return 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;
+ }
+}
+
+int xAsprintf(char** strp, const char* fmt, ...) {
+ va_list vl;
+ va_start(vl, fmt);
+ int r = vasprintf(strp, fmt, vl);
+ va_end(vl);
+
+ if (r < 0 || !*strp) {
+ fail();
+ }
+
+ return r;
+}
+
+int xSnprintf(char* buf, int len, const char* fmt, ...) {
+ va_list vl;
+ va_start(vl, fmt);
+ int n = vsnprintf(buf, len, fmt, vl);
+ va_end(vl);
+
+ if (n < 0 || n >= len) {
+ fail();
+ }
+
+ return n;
+}
+
+char* xStrdup(const char* str) {
+ char* data = strdup(str);
+ if (!data) {
+ fail();
+ }
+ return data;
+}

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