2006-03-04 18:16:49 +00:00
|
|
|
/*
|
2011-12-26 21:35:57 +00:00
|
|
|
htop - String.c
|
2011-05-26 16:35:07 +00:00
|
|
|
(C) 2004-2011 Hisham H. Muhammad
|
2006-03-04 18:16:49 +00:00
|
|
|
Released under the GNU GPL, see the COPYING file
|
|
|
|
in the source distribution for its full text.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "String.h"
|
2011-12-26 21:35:57 +00:00
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2006-03-04 18:16:49 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <strings.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2006-07-11 06:13:32 +00:00
|
|
|
/*{
|
|
|
|
#define String_startsWith(s, match) (strstr((s), (match)) == (s))
|
2014-01-14 02:21:37 +00:00
|
|
|
#define String_contains_i(s1, s2) (strcasestr(s1, s2) != NULL)
|
2006-07-11 06:13:32 +00:00
|
|
|
}*/
|
|
|
|
|
2010-02-25 01:43:18 +00:00
|
|
|
char* String_cat(const char* s1, const char* s2) {
|
2006-03-04 18:16:49 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-02-25 01:43:18 +00:00
|
|
|
char* String_trim(const char* in) {
|
2006-03-04 18:16:49 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2006-08-04 20:54:37 +00:00
|
|
|
inline int String_eq(const char* s1, const char* s2) {
|
2006-03-04 18:16:49 +00:00
|
|
|
if (s1 == NULL || s2 == NULL) {
|
|
|
|
if (s1 == NULL && s2 == NULL)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return (strcmp(s1, s2) == 0);
|
|
|
|
}
|
|
|
|
|
2011-08-29 20:45:29 +00:00
|
|
|
char** String_split(const char* s, char sep, int* n) {
|
|
|
|
*n = 0;
|
2006-03-04 18:16:49 +00:00
|
|
|
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;
|
2014-04-21 21:23:34 +00:00
|
|
|
char** newOut = (char**) realloc(out, sizeof(char*) * blocks);
|
|
|
|
if (newOut) {
|
|
|
|
out = newOut;
|
|
|
|
} else {
|
|
|
|
blocks -= rate;
|
|
|
|
break;
|
|
|
|
}
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
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++;
|
|
|
|
}
|
2014-04-21 21:23:34 +00:00
|
|
|
char** newOut = realloc(out, sizeof(char*) * (ctr + 1));
|
|
|
|
if (newOut) {
|
|
|
|
out = newOut;
|
|
|
|
}
|
2006-03-04 18:16:49 +00:00
|
|
|
out[ctr] = NULL;
|
2011-08-29 20:45:29 +00:00
|
|
|
*n = ctr;
|
2006-03-04 18:16:49 +00:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
void String_freeArray(char** s) {
|
|
|
|
for (int i = 0; s[i] != NULL; i++) {
|
|
|
|
free(s[i]);
|
|
|
|
}
|
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
|
2008-09-23 06:21:28 +00:00
|
|
|
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){
|
2012-02-17 13:45:58 +00:00
|
|
|
if (count == numMatch && line[i] != ' ' && line[i] != '\0' && line[i] != '\n' && line[i] != (char)EOF) {
|
2008-09-23 06:21:28 +00:00
|
|
|
match[foundCount] = line[i];
|
|
|
|
foundCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
match[foundCount] = '\0';
|
|
|
|
return((char*)strdup(match));
|
|
|
|
}
|