2006-03-04 18:16:49 +00:00
|
|
|
/*
|
2015-08-19 16:58:29 +00:00
|
|
|
htop - StringUtils.c
|
2011-05-26 16:35:07 +00:00
|
|
|
(C) 2004-2011 Hisham H. Muhammad
|
2020-10-05 07:51:32 +00:00
|
|
|
Released under the GNU GPLv2, see the COPYING file
|
2006-03-04 18:16:49 +00:00
|
|
|
in the source distribution for its full text.
|
|
|
|
*/
|
|
|
|
|
2011-12-26 21:35:57 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2020-09-19 18:22:34 +00:00
|
|
|
#include "StringUtils.h"
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdlib.h>
|
2006-03-04 18:16:49 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <strings.h>
|
2020-09-19 18:22:34 +00:00
|
|
|
|
|
|
|
#include "XAlloc.h"
|
|
|
|
|
2006-03-04 18:16:49 +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);
|
2016-02-02 14:53:02 +00:00
|
|
|
char* out = xMalloc(l1 + l2 + 1);
|
2020-08-19 23:03:45 +00:00
|
|
|
memcpy(out, s1, l1);
|
|
|
|
memcpy(out+l1, s2, l2+1);
|
|
|
|
out[l1 + l2] = '\0';
|
2006-03-04 18:16:49 +00:00
|
|
|
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--;
|
|
|
|
}
|
2016-02-02 14:53:02 +00:00
|
|
|
char* out = xMalloc(len+1);
|
2006-03-04 18:16:49 +00:00
|
|
|
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;
|
2016-02-02 14:53:02 +00:00
|
|
|
char** out = xCalloc(rate, sizeof(char*));
|
2006-03-04 18:16:49 +00:00
|
|
|
int ctr = 0;
|
|
|
|
int blocks = rate;
|
|
|
|
char* where;
|
|
|
|
while ((where = strchr(s, sep)) != NULL) {
|
|
|
|
int size = where - s;
|
2016-02-02 14:53:02 +00:00
|
|
|
char* token = xMalloc(size + 1);
|
2006-03-04 18:16:49 +00:00
|
|
|
strncpy(token, s, size);
|
|
|
|
token[size] = '\0';
|
|
|
|
out[ctr] = token;
|
|
|
|
ctr++;
|
|
|
|
if (ctr == blocks) {
|
|
|
|
blocks += rate;
|
2016-05-30 15:22:33 +00:00
|
|
|
out = (char**) xRealloc(out, sizeof(char*) * blocks);
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
s += size + 1;
|
|
|
|
}
|
|
|
|
if (s[0] != '\0') {
|
2020-08-20 19:34:28 +00:00
|
|
|
out[ctr] = xStrdup(s);
|
2006-03-04 18:16:49 +00:00
|
|
|
ctr++;
|
|
|
|
}
|
2016-05-30 15:22:33 +00:00
|
|
|
out = xRealloc(out, sizeof(char*) * (ctr + 1));
|
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) {
|
2016-08-24 21:12:35 +00:00
|
|
|
if (!s) {
|
|
|
|
return;
|
|
|
|
}
|
2006-03-04 18:16:49 +00:00
|
|
|
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++;
|
2019-10-31 16:39:12 +00:00
|
|
|
|
2008-09-23 06:21:28 +00:00
|
|
|
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';
|
2020-09-28 10:17:52 +00:00
|
|
|
return xStrdup(match);
|
2008-09-23 06:21:28 +00:00
|
|
|
}
|
2016-06-19 21:55:35 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2020-09-19 18:22:34 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|