StringUtils.c 3.56 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1
/*
Hisham Muhammad's avatar
Hisham Muhammad committed
2
htop - StringUtils.c
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
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/

David Hunt's avatar
David Hunt committed
8
#include "StringUtils.h"
Hisham's avatar
Hisham committed
9
#include "XAlloc.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
10
11
12

#include "config.h"

Hisham Muhammad's avatar
Hisham Muhammad committed
13
14
15
16
#include <string.h>
#include <strings.h>
#include <stdlib.h>

17
/*{
18
19
#include <stdio.h>

Explorer09's avatar
Explorer09 committed
20
#define String_startsWith(s, match) (strncmp((s),(match),strlen(match)) == 0)
21
#define String_contains_i(s1, s2) (strcasestr(s1, s2) != NULL)
22
23
}*/

Explorer09's avatar
Explorer09 committed
24
25
26
27
28
/*
 * String_startsWith gives better performance if strlen(match) can be computed
 * at compile time (e.g. when they are immutable string literals). :)
 */

Hisham Muhammad's avatar
Hisham Muhammad committed
29
char* String_cat(const char* s1, const char* s2) {
30
31
   size_t l1 = strlen(s1);
   size_t l2 = strlen(s2);
Hisham's avatar
Hisham committed
32
   char* out = xMalloc(l1 + l2 + 1);
Hisham Muhammad's avatar
Hisham Muhammad committed
33
   strncpy(out, s1, l1);
34
   strncpy(out + l1, s2, l2 + 1);
Hisham Muhammad's avatar
Hisham Muhammad committed
35
36
37
   return out;
}

Hisham Muhammad's avatar
Hisham Muhammad committed
38
char* String_trim(const char* in) {
Hisham Muhammad's avatar
Hisham Muhammad committed
39
40
41
   while (in[0] == ' ' || in[0] == '\t' || in[0] == '\n') {
      in++;
   }
42
   size_t len = strlen(in);
Hisham Muhammad's avatar
Hisham Muhammad committed
43
44
45
   while (len > 0 && (in[len-1] == ' ' || in[len-1] == '\t' || in[len-1] == '\n')) {
      len--;
   }
Hisham's avatar
Hisham committed
46
   char* out = xMalloc(len+1);
Hisham Muhammad's avatar
Hisham Muhammad committed
47
48
49
50
51
   strncpy(out, in, len);
   out[len] = '\0';
   return out;
}

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

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

void String_freeArray(char** s) {
96
97
98
   if (!s) {
      return;
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
99
100
101
102
103
104
   for (int i = 0; s[i] != NULL; i++) {
      free(s[i]);
   }
   free(s);
}

Hisham Muhammad's avatar
Hisham Muhammad committed
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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){
121
         if (count == numMatch && line[i] != ' ' && line[i] != '\0' && line[i] != '\n' && line[i] != (char)EOF) {
Hisham Muhammad's avatar
Hisham Muhammad committed
122
123
124
125
126
127
128
            match[foundCount] = line[i];
            foundCount++;
         }
      }
   }

   match[foundCount] = '\0';
Hisham's avatar
Hisham committed
129
   return((char*)xStrdup(match));
Hisham Muhammad's avatar
Hisham Muhammad committed
130
}
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156

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;
   }
}