F
In C kann man sowas dynamisch nur pbe rden void* Pointer machen. Du musst eben zur Compile-Zeit wissen welche Typen du unterstützen willst:
struct dataNode
{
struct dataNode* prev;
struct dataNode* next;
void* data;
int size;
char* type;
};
void freeData(struct dataNode* p)
{
while(p != NULL) {
struct dataNode* next = p->next;
free(p->data);
free(p->type);
free(p);
p = next;
}
}
struct dataNode* readData(const char* buf, int bufsize)
{
const char* typedelim;
const char* content = buf;
struct dataNode *begin = NULL, *current = NULL;
while( typedelim = (char*)memchr(content, ':', content - buf + bufsize) ) {
int tmpsize;
const char* endofline;
struct dataNode* last = current;
current = (struct dataNode*) malloc(sizeof(struct dataNode));
current->prev = last;
current->next = NULL;
if(begin == NULL) {
begin = current;
}
else {
last->next = current;
}
tmpsize = typedelim - content;
current->type = (char*) malloc(tmpsize + 1);
memcpy(current->type, content, tmpsize);
current->type[tmpsize] = 0;
typedelim++;
endofline = (char*)memchr(typedelim, '\n', typedelim - buf + bufsize);
if(endofline == NULL) {
tmpsize = strlen(typedelim);
}
else {
tmpsize = endofline - typedelim;
}
current->data = malloc(tmpsize);
current->size = tmpsize;
memcpy(current->data, typedelim, tmpsize);
if(endofline) {
content = endofline + 1;
}
else {
break;
}
}
return begin;
}
Für dieses Beispiel müssen die Daten in folgendem Format sein:
typA:valueA
typB:valueB
.
.
.