Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
mx_util: Add mx_strvec_() and remove mxq_util
  • Loading branch information
mariux committed Jul 27, 2015
1 parent f9538ed commit c5382e1
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 260 deletions.
1 change: 0 additions & 1 deletion .gitignore
Expand Up @@ -8,7 +8,6 @@ mxq_job.o
mxqd.o
mxqsub.o
mxqkill.o
mxq_util.o
test_mx_util.o
test_mx_log.o
test_mx_mysql.o
Expand Down
21 changes: 2 additions & 19 deletions Makefile
Expand Up @@ -184,11 +184,6 @@ mx_mysql.h += $(mx_util.h)

mx_mxq.h += mx_mxq.h

### mxq_util.h ---------------------------------------------------------

mxq_util.h += mxq_util.h
mxq_util.h += $(mx_log.h)

### mxq_group.h --------------------------------------------------------

mxq_group.h += mxq_group.h
Expand Down Expand Up @@ -250,7 +245,7 @@ clean: CLEAN += mxq_log.o
### mxqdump.o ---------------------------------------------------

mxqdump.o: $(mx_log.h)
mxqdump.o: $(mxq_util.h)
mxqdump.o: $(mx_util.h)
mxqdump.o: $(mx_mysql.h)
mxqdump.o: $(mx_getopt.h)
mxqdump.o: CFLAGS += $(CFLAGS_MYSQL)
Expand All @@ -270,15 +265,6 @@ mxqkill.o: CFLAGS += $(CFLAGS_MYSQL)

clean: CLEAN += mxqkill.o

### mxq_util.o ---------------------------------------------------------

mxq_util.o: $(mx_log.h)
mxq_util.o: $(mxq_util.h)
mxq_util.o: CFLAGS += $(CFLAGS_MYSQL)
mxq_util.o: CFLAGS += -Wno-unused-variable

clean: CLEAN += mxq_util.o

### mxq_group.o --------------------------------------------------------

mxq_group.o: $(mx_log.h)
Expand Down Expand Up @@ -324,7 +310,7 @@ mxqsub.o: $(mx_mysql.h)
mxqsub.o: $(mxq.h)
mxqsub.o: $(mxq_group.h)
mxqsub.o: $(mxq_job.h)
mxqsub.o: $(mxq_util.h)
mxqsub.o: $(mx_util.h)
mxqsub.o: CFLAGS += $(CFLAGS_MYSQL)

clean: CLEAN += mxqsub.o
Expand All @@ -340,7 +326,6 @@ mxqd: mxq_log.o
mxqd: mx_getopt.o
mxqd: mxq_group.o
mxqd: mxq_job.o
mxqd: mxq_util.o
mxqd: mx_mysql.o
mxqd: LDLIBS += $(LDLIBS_MYSQL)

Expand All @@ -354,7 +339,6 @@ install:: mxqd
### mxqsub ------------------------------------------------------------

mxqsub: mx_getopt.o
mxqsub: mxq_util.o
mxqsub: mx_util.o
mxqsub: mx_log.o
mxqsub: mx_mysql.o
Expand All @@ -373,7 +357,6 @@ mxqdump: mx_log.o
mxqdump: mx_mysql.o
mxqdump: mxq_group.o
mxqdump: mxq_job.o
mxqdump: mxq_util.o
mxqdump: mx_util.o
mxqdump: mx_getopt.o
mxqdump: LDLIBS += $(LDLIBS_MYSQL)
Expand Down
191 changes: 191 additions & 0 deletions mx_util.c
Expand Up @@ -633,3 +633,194 @@ void *mx_calloc_forever_sec(size_t nmemb, size_t size, unsigned int time)

return ptr;
}

char **mx_strvec_new(void)
{
char **strvec;

strvec = calloc(sizeof(*strvec), 1);

return strvec;
}

static inline size_t mx_strvec_length_cache(char **strvec, int32_t len)
{
static char ** sv = NULL;
static size_t l = 0;

if (likely(len == -1)) {
if (likely(sv == strvec)) {
return l;
}
return -1;
}

if (likely(sv == strvec)) {
l = len;
} else {
sv = strvec;
l = len;
}
return l;
}

size_t mx_strvec_length(char ** strvec)
{
char ** sv;
size_t len;

assert(strvec);

sv = strvec;

len = mx_strvec_length_cache(sv, -1);
if (len != -1)
return len;

for (; *sv; sv++);

len = sv-strvec;
mx_strvec_length_cache(sv, len);
return len;
}

int mx_strvec_push_str(char *** strvecp, char * str)
{
char ** sv;

size_t len;

assert(strvecp);
assert(*strvecp);
assert(str);

len = mx_strvec_length(*strvecp);

sv = realloc(*strvecp, sizeof(**strvecp) * (len + 2));
if (!sv) {
return 0;
}

sv[len++] = str;
sv[len] = NULL;

mx_strvec_length_cache(sv, len);

*strvecp = sv;

return 1;
}

int mx_strvec_push_strvec(char ***strvecp, char **strvec)
{
char **sv;

size_t len1;
size_t len2;

assert(strvecp);
assert(*strvecp);
assert(strvec);

len1 = mx_strvec_length(*strvecp);
len2 = mx_strvec_length(strvec);

sv = realloc(*strvecp, sizeof(**strvecp) * (len1 + len2 + 1));
if (!sv) {
return 0;
}

memcpy(sv+len1, strvec, sizeof(*strvec) * (len2 + 1));

mx_strvec_length_cache(sv, len1+len2);

*strvecp = sv;

return 1;
}

char *mx_strvec_to_str(char **strvec)
{
char **sv;
char* buf;
char* s;
size_t totallen;
char* str;

assert(strvec);

totallen = 0;
for (sv = strvec; *sv; sv++) {
totallen += strlen(*sv);
}

buf = malloc(sizeof(*buf) * (totallen * 2 + 2) + 1);
if (!buf)
return NULL;

str = buf;
*str = 0;

for (sv = strvec; *sv; sv++) {
for (s=*sv; *s; s++) {
switch (*s) {
case '\\':
*(str++) = '\\';
*(str++) = '\\';
break;

default:
*(str++) = *s;
}
}
*(str++) = '\\';
*(str++) = '0';
}

*str = '\0';

return buf;
}

void mx_strvec_free(char **strvec)
{
char **sv;

if (!strvec)
return;

for (sv = strvec; *sv; sv++) {
free(sv);
}
free(strvec);
}

char **mx_strvec_from_str(char *str)
{
int res;
char* s;
char* p;
char** strvec;

strvec = mx_strvec_new();
if (!strvec)
return NULL;

for (s=str; *s; s=p+2) {
p = strstr(s, "\\0");
if (!p) {
free(strvec);
errno = EINVAL;
return NULL;
}
*p = 0;

res = mx_strvec_push_str(&strvec, s);
if (!res) {
free(strvec);
return NULL;
}
}

return strvec;
}
9 changes: 9 additions & 0 deletions mx_util.h
Expand Up @@ -4,6 +4,7 @@
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>

#include "mx_log.h"

Expand Down Expand Up @@ -111,4 +112,12 @@ int mx_sleep_nofail(unsigned int seconds);

void *mx_calloc_forever_sec(size_t nmemb, size_t size, unsigned int time);

char** mx_strvec_new(void);
size_t mx_strvec_length(char **strvec);
int mx_strvec_push_str(char ***strvecp, char *str);
int mx_strvec_push_strvec(char*** strvecp, char **strvec);
char* mx_strvec_to_str(char **strvec);
char** mx_strvec_from_str(char *str);
void mx_strvec_free(char **strvec);

#endif

0 comments on commit c5382e1

Please sign in to comment.