-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringlib.c
More file actions
111 lines (93 loc) · 2.32 KB
/
stringlib.c
File metadata and controls
111 lines (93 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <stdio.h>
#include <stdlib.h>
#include "stringlib.h"
#include "logging.h"
char *stringlib_getToken(stringlib_tokens_t *token, char *str) {
LOGGING_DEBUG("START");
char *buffer;
buffer = calloc(sizeof(char), token->length + 1);
memcpy(buffer, str + token->start, token->length);
LOGGING_TRACE("buffer=%s", buffer);
LOGGING_DEBUG("DONE");
return buffer;
}
stringlib_tokens_t *stringlib_splitTokens(char *str, char chr) {
LOGGING_DEBUG("START");
char *lastPos;
char *newPos;
stringlib_tokens_t *tokens = NULL;
stringlib_tokens_t *currentToken;
stringlib_tokens_t *newToken;
newPos = str;
lastPos = str;
while ( *newPos != '\0' ) {
if ( *newPos == chr ) {
newToken = (stringlib_tokens_t *) malloc(sizeof(stringlib_tokens_t));
newToken->next = NULL;
if ( tokens == NULL ) {
tokens = newToken;
currentToken = newToken;
} else {
currentToken->next = newToken;
currentToken = newToken;
}
currentToken->start = ( lastPos - str );
currentToken->length = ( newPos - lastPos );
lastPos = newPos + 1;
}
newPos++;
}
if ( newPos != str ) {
newToken = (stringlib_tokens_t *) malloc(sizeof(stringlib_tokens_t));
newToken->next = NULL;
if ( tokens == NULL ) {
tokens = newToken;
currentToken = newToken;
} else {
currentToken->next = newToken;
currentToken = newToken;
}
currentToken->start = ( lastPos - str );
currentToken->length = ( newPos - lastPos );
}
LOGGING_DEBUG("DONE");
return tokens;
}
void stringlib_freeTokens(stringlib_tokens_t *tokens) {
LOGGING_DEBUG("START");
stringlib_tokens_t *token;
while ( tokens != NULL ) {
token = tokens;
tokens = tokens->next;
LOGGING_TRACE("freeing %X", (unsigned int) token);
free(token);
}
LOGGING_DEBUG("DONE");
}
int stringlib_isInteger(char *str) {
LOGGING_DEBUG("START");
char *ptr = str;
if ( *ptr == '\0' ) {
LOGGING_TRACE("return=0");
LOGGING_DEBUG("DONE");
return 0;
}
while ( *ptr != '\0' ) {
if ( ( *ptr < 48 || *ptr > 57 ) && *ptr != '.' ) {
LOGGING_TRACE("return=0");
LOGGING_DEBUG("DONE");
return 0;
}
ptr++;
}
LOGGING_TRACE("return=1");
LOGGING_DEBUG("DONE");
return 1;
}
char *stringlib_longToString(char *buffer, long value) {
LOGGING_DEBUG("START");
sprintf(buffer, "%ld", value);
LOGGING_TRACE("buffer=%s", buffer);
LOGGING_DEBUG("DONE");
return buffer;
}