Thread string splitten/in 3 teile teilen (7 answers)
Opened by kale_1990 at 2006-10-18 18:11

esskar
 2006-10-18 19:05
#11509 #11509
User since
2003-08-04
7321 Artikel
ModeratorIn

user image
Code: (dl )
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
void FreeSplit(char **data) {
int i;

if(data == NULL) return;

for(i = 0; i < 3; i++) {
if(data[i]) free(data[i]);
}

free(data);
}

char **SplitConstruct(const char *var) {
char **retval = NULL;
const char *ptr = NULL;
int len = 0, i = 0;

ptr = strstr(var, "REM IF ");
if(ptr == NULL) return NULL;
ptr += 7; // 7 == strlen("REM IF ");

retval = (char **)malloc(sizeof(char *) * 3);
memset(retval, 0, sizeof(char *) * 3);

for(i = 0; i < 3; i++) {
ptr = strchr(ptr, ' ');
if(ptr == NULL) break;

len = ptr - var;
retval[i] = (char *)malloc(len + 16);
strncpy(retval[i], ptr, len);

ptr += len + 1;
} while(0);

if(i != 3) { // error
FreeSplit(retval);
retval = NULL;
}

return retval;
}


ungetestet!

View full thread string splitten/in 3 teile teilen