aboutsummaryrefslogtreecommitdiffstats
path: root/StringUtils.c
diff options
context:
space:
mode:
authorDaniel Lange <DLange@git.local>2016-04-11 13:01:07 +0200
committerDaniel Lange <DLange@git.local>2016-04-11 13:01:07 +0200
commitff9409b1737627857eb47f64f536a3f66b6a09a4 (patch)
tree61b631ba551e68a4f656b8b76ff7bd0d9955fc64 /StringUtils.c
parentf75ab6d2c11e8a8e18191b087564aedebbeb96c5 (diff)
downloaddebian_htop-ff9409b1737627857eb47f64f536a3f66b6a09a4.tar.gz
debian_htop-ff9409b1737627857eb47f64f536a3f66b6a09a4.tar.bz2
debian_htop-ff9409b1737627857eb47f64f536a3f66b6a09a4.zip
Imported Upstream version 2.0.0upstream/2.0.0
Diffstat (limited to 'StringUtils.c')
-rw-r--r--StringUtils.c130
1 files changed, 130 insertions, 0 deletions
diff --git a/StringUtils.c b/StringUtils.c
new file mode 100644
index 0000000..173caba
--- /dev/null
+++ b/StringUtils.c
@@ -0,0 +1,130 @@
+/*
+htop - StringUtils.c
+(C) 2004-2011 Hisham H. Muhammad
+Released under the GNU GPL, see the COPYING file
+in the source distribution for its full text.
+*/
+
+#include "StringUtils.h"
+#include "XAlloc.h"
+
+#include "config.h"
+
+#include <string.h>
+#include <strings.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+/*{
+#define String_startsWith(s, match) (strstr((s), (match)) == (s))
+#define String_contains_i(s1, s2) (strcasestr(s1, s2) != NULL)
+}*/
+
+char* String_cat(const char* s1, const char* s2) {
+ int l1 = strlen(s1);
+ int l2 = strlen(s2);
+ char* out = xMalloc(l1 + l2 + 1);
+ strncpy(out, s1, l1);
+ strncpy(out+l1, s2, l2+1);
+ 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;
+ char** newOut = (char**) xRealloc(out, sizeof(char*) * blocks);
+ if (newOut) {
+ out = newOut;
+ } else {
+ blocks -= rate;
+ break;
+ }
+ }
+ s += size + 1;
+ }
+ if (s[0] != '\0') {
+ int size = strlen(s);
+ char* token = xMalloc(size + 1);
+ strncpy(token, s, size + 1);
+ out[ctr] = token;
+ ctr++;
+ }
+ char** newOut = xRealloc(out, sizeof(char*) * (ctr + 1));
+ if (newOut) {
+ out = newOut;
+ }
+ out[ctr] = NULL;
+ *n = ctr;
+ return out;
+}
+
+void String_freeArray(char** s) {
+ 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((char*)xStrdup(match));
+}

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