Statistics
| Branch: | Revision:

peerstreamer-src / src / tokens.c @ 58fb2cdc

History | View | Annotate | Download (1.57 KB)

1 59457bd3 Luca Baldesi
/*
2
 * This is SSSim: the Simple & Stupid Simulator
3
 *
4
 *  Copyright (c) 2015 Luca Baldesi
5
 *
6
 *  This is free software; see gpl-3.0.txt
7
 */
8
9
#include "tokens.h"
10
#include <malloc.h>
11
#include <string.h>
12
13
char * substring_trim(const char * s,uint32_t start,uint32_t len)
14
{
15
        uint32_t leading=0,trailing=0;
16
17
        while(leading < len && (s[start+leading] == '\n' || s[start+leading] == ' '))
18
                leading++;
19
        while(trailing < len && (s[start+len-trailing-1] == '\n' || s[start+len-trailing-1] == ' '))
20
                trailing++;
21
22
        if(len - leading -trailing > 0)
23
                return strndup(s+start+leading,len-trailing);
24
        else
25
                return NULL;
26
}
27
28
void tokens_destroy(char *** sv,uint32_t n)
29
{
30
        uint32_t i;
31
32
        if (sv && *sv && **sv)
33
        {
34
                for( i = 0; i < n; i++)
35
                        free((*sv)[i]);
36
                free(*sv);
37
                *sv = NULL;
38
        }
39
}
40
41
char ** tokens_create(char * s,const char delim,uint32_t * n)
42
{
43 e9551155 Luca Baldesi
        char * np =s, *nnp;
44 59457bd3 Luca Baldesi
        char ** sv = NULL;
45
        uint32_t i;
46
47
48
        if(s && n && strcmp(s,""))
49
        {
50
                *n = 0;
51 e9551155 Luca Baldesi
                while((nnp = strchr(np,delim)))
52 59457bd3 Luca Baldesi
                {
53 e9551155 Luca Baldesi
                        if ((nnp - np) > 1)
54
                                (*n)++;
55
                        np = nnp + 1;
56 59457bd3 Luca Baldesi
                }
57 e9551155 Luca Baldesi
                if (strlen(np) > 0)
58
                        (*n)++;
59 59457bd3 Luca Baldesi
                sv = (char **) malloc(sizeof(char *) * (*n));
60
61 e9551155 Luca Baldesi
                np = nnp = s;
62
                i = 0;
63
                while((nnp = strchr(np,delim)))
64 59457bd3 Luca Baldesi
                {
65 e9551155 Luca Baldesi
                        if ((nnp - np) > 1)
66 59457bd3 Luca Baldesi
                        {
67 e9551155 Luca Baldesi
                                sv[i++] = substring_trim(np,0,nnp-np);
68
                        }
69
                        np = nnp+1;
70 59457bd3 Luca Baldesi
                }
71 e9551155 Luca Baldesi
                if (strlen(np) > 0)
72
                        sv[i] = substring_trim(np,0,strlen(np));
73 59457bd3 Luca Baldesi
        }
74
        return sv;
75
}
76
77
int32_t tokens_check(char ** tokens, uint32_t n, char * target)
78
{
79
        int32_t pos = -1;
80
        uint32_t i;
81
82
        if(tokens && *tokens && target)
83
        {
84
                i = 0;
85
                while (pos < 0 && i < n)
86
                {
87
                        if (strcmp(tokens[i], target) == 0)
88
                                pos = i;
89
                        i++;
90
                }
91
        }
92
        return pos;
93
}