String.c 3.03 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1
2
/*
htop
Hisham Muhammad's avatar
Hisham Muhammad committed
3
(C) 2004-2011 Hisham H. Muhammad
Hisham Muhammad's avatar
Hisham Muhammad committed
4
5
6
7
8
9
10
11
12
13
14
15
16
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/

#define _GNU_SOURCE
#include "String.h"
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <stdio.h>

#include "debug.h"

17
18
19
20
/*{
#define String_startsWith(s, match) (strstr((s), (match)) == (s))
}*/

Hisham Muhammad's avatar
Hisham Muhammad committed
21
inline char* String_copy(const char* orig) {
Hisham Muhammad's avatar
Hisham Muhammad committed
22
23
24
   return strdup(orig);
}

Hisham Muhammad's avatar
Hisham Muhammad committed
25
char* String_cat(const char* s1, const char* s2) {
Hisham Muhammad's avatar
Hisham Muhammad committed
26
27
28
29
30
31
32
33
   int l1 = strlen(s1);
   int l2 = strlen(s2);
   char* out = malloc(l1 + l2 + 1);
   strncpy(out, s1, l1);
   strncpy(out+l1, s2, l2+1);
   return out;
}

Hisham Muhammad's avatar
Hisham Muhammad committed
34
char* String_trim(const char* in) {
Hisham Muhammad's avatar
Hisham Muhammad committed
35
36
37
38
39
40
41
42
43
44
45
46
47
   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 = malloc(len+1);
   strncpy(out, in, len);
   out[len] = '\0';
   return out;
}

48
inline int String_eq(const char* s1, const char* s2) {
Hisham Muhammad's avatar
Hisham Muhammad committed
49
50
51
52
53
54
55
56
57
   if (s1 == NULL || s2 == NULL) {
      if (s1 == NULL && s2 == NULL)
         return 1;
      else
         return 0;
   }
   return (strcmp(s1, s2) == 0);
}

58
59
char** String_split(const char* s, char sep, int* n) {
   *n = 0;
Hisham Muhammad's avatar
Hisham Muhammad committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
   const int rate = 10;
   char** out = (char**) malloc(sizeof(char*) * rate);
   int ctr = 0;
   int blocks = rate;
   char* where;
   while ((where = strchr(s, sep)) != NULL) {
      int size = where - s;
      char* token = (char*) malloc(size + 1);
      strncpy(token, s, size);
      token[size] = '\0';
      out[ctr] = token;
      ctr++;
      if (ctr == blocks) {
         blocks += rate;
         out = (char**) realloc(out, sizeof(char*) * blocks);
      }
      s += size + 1;
   }
   if (s[0] != '\0') {
      int size = strlen(s);
      char* token = (char*) malloc(size + 1);
      strncpy(token, s, size + 1);
      out[ctr] = token;
      ctr++;
   }
   out = realloc(out, sizeof(char*) * (ctr + 1));
   out[ctr] = NULL;
87
   *n = ctr;
Hisham Muhammad's avatar
Hisham Muhammad committed
88
89
90
91
92
93
94
95
96
97
   return out;
}

void String_freeArray(char** s) {
   for (int i = 0; s[i] != NULL; i++) {
      free(s[i]);
   }
   free(s);
}

Hisham Muhammad's avatar
Hisham Muhammad committed
98
int String_contains_i(const char* s, const char* match) {
Hisham Muhammad's avatar
Hisham Muhammad committed
99
100
101
102
103
104
105
106
107
   int lens = strlen(s);
   int lenmatch = strlen(match);
   for (int i = 0; i < lens-lenmatch; i++) {
      if (strncasecmp(s, match, strlen(match)) == 0)
         return 1;
      s++;
   }
   return 0;
}
Hisham Muhammad's avatar
Hisham Muhammad committed
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134

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] != EOF) {
            match[foundCount] = line[i];
            foundCount++;
         }
      }
   }

   match[foundCount] = '\0';
   return((char*)strdup(match));
}