added a string split function
This commit is contained in:
parent
7d5c478bcb
commit
15e119014b
@ -2,21 +2,21 @@
|
||||
|
||||
typedef struct
|
||||
{
|
||||
size_t admin_count;
|
||||
char * username;
|
||||
char * password;
|
||||
char * channel;
|
||||
char * server;
|
||||
char ** admins;
|
||||
int port;
|
||||
char * username;
|
||||
char * password;
|
||||
char * channel;
|
||||
char * server;
|
||||
int port;
|
||||
} creds_t;
|
||||
|
||||
VARDECL creds_t creds;
|
||||
extern creds_t creds;
|
||||
|
||||
DECL char * dump(void);
|
||||
DECL char * raw(char const * const sql);
|
||||
DECL char * remind(char * who);
|
||||
DECL char * slurp(char const * fn);
|
||||
DECL char ** str_split(char const * s, char c);
|
||||
DECL void split_clean(char ** split)
|
||||
DECL char * dump(void);
|
||||
DECL char * raw(char const * const sql);
|
||||
DECL char * remind(char * who);
|
||||
DECL char * slurp(char const * fn);
|
||||
DECL int is_admin(char const * user);
|
||||
DECL void parse_admins(char * admin_string);
|
||||
DECL int parse_pair(char const * buf, size_t const len);
|
||||
|
61
src/parse.c
61
src/parse.c
@ -52,6 +52,67 @@ VARDECL size_t const cred_names_len[] =
|
||||
|
||||
VARDECL creds_t creds = {0};
|
||||
|
||||
DECL char **
|
||||
str_split(char const * s, char c)
|
||||
{
|
||||
char ** ret = NULL;
|
||||
size_t i = 0;
|
||||
|
||||
size_t current_token_i = 0;
|
||||
|
||||
size_t token_start_i = 0;
|
||||
size_t tokens_q = 0;
|
||||
|
||||
/* count tokens */
|
||||
for (i = 1; s[i]; ++i)
|
||||
{
|
||||
/* end of a token*/
|
||||
if (s[i] == c && s[i - 1] != c)
|
||||
{ ++tokens_q; }
|
||||
}
|
||||
++tokens_q;
|
||||
|
||||
ret = (char **)calloc(tokens_q + 1, sizeof(char *));
|
||||
if (!ret)
|
||||
{ return ret; }
|
||||
|
||||
for (i = 1; s[i]; ++i)
|
||||
{
|
||||
|
||||
if (s[i] == c && s[i - 1] != c)
|
||||
{
|
||||
/* end of a token*/
|
||||
ret[current_token_i] = strndup(s + token_start_i, i - token_start_i);
|
||||
if (!ret[current_token_i])
|
||||
{
|
||||
split_clean(ret);
|
||||
return NULL;
|
||||
}
|
||||
++current_token_i;
|
||||
}
|
||||
else if (s[i] != c && s[i - 1] == c)
|
||||
{
|
||||
/* start of a token */
|
||||
token_start_i = i;
|
||||
}
|
||||
}
|
||||
|
||||
/* Signal that the split array is ended (for iteration purposes) */
|
||||
ret[current_token_i + 1] = NULL;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
DECL void
|
||||
split_clean(char ** split)
|
||||
{
|
||||
while (*split)
|
||||
{
|
||||
free(*split);
|
||||
}
|
||||
free(split);
|
||||
}
|
||||
|
||||
DECL char *
|
||||
slurp(char const * fn)
|
||||
{
|
||||
|
Reference in New Issue
Block a user