#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
typedef int Type;
#define INT 1
#define STR 2
typedef struct vector {
Type type;
size_t size;
size_t capacity;
void *array;
} Vector;
Vector *vec_make(Type type);
void vec_push(Vector* v, ...);
size_t vec_size(Vector *v);
void* vec_getArray(Vector *v);
void vec_drop(Vector *v);
typedef struct elset {
Vector* layer;
Vector* data;
} Elset;
#define RECORD_MAX 10
int main(void){
Elset elset[RECORD_MAX];
int input_count=-1;
FILE *fp;
char buff[1024];
fp=fopen("data.txt", "r");
while(fgets(buff, sizeof(buff), fp)){
if(*buff == '*'){
char *p;
input_count += 1;//countup
elset[input_count].layer = vec_make(STR);
elset[input_count].data =vec_make(INT);
for(p=strchr(buff, '_');p=strtok(p, "_\n");p=NULL){
vec_push(elset[input_count].layer, strdup(p));
}
} else {
vec_push(elset[input_count].data, atoi(buff));
}
}
input_count += 1;
fclose(fp);
{ //check print
int i, j, layerSize, dataSize;
int *datas;
char **layers;
for(i=0;i<input_count;++i){
layerSize=vec_size(elset[i].layer);
layers=(char**)vec_getArray(elset[i].layer);
for(j=0;j<layerSize;++j)
printf("%s ", layers[j]);
printf("\n");
dataSize=vec_size(elset[i].data);
datas=(int*)vec_getArray(elset[i].data);
for(j=0;j<dataSize;++j)
printf("%d ", datas[j]);
printf("\n\n");
}
//deallocation
for(i=0;i<input_count;++i){
layerSize=vec_size(elset[i].layer);
layers=(char**)vec_getArray(elset[i].layer);
for(j=0;j<layerSize;++j)
free(layers[j]);
vec_drop(elset[i].layer);
vec_drop(elset[i].data);
}
}
return 0;
}
#define TypeSize(t) ((t == 1)? sizeof(int):sizeof(char*))
Vector *vec_make(Type type){
Vector *v;
v = (Vector*)malloc(sizeof(Vector));
v->type = type;
v->size = 0;
v->capacity=4;
v->array=realloc(NULL, TypeSize(type)*(v->capacity += 4));
return v;
}
void vec_push(Vector* v, ...){
va_list ap;
va_start(ap, v);
switch(v->type){
case INT:
((int*)v->array)[v->size] = va_arg(ap, int);
break;
case STR:
((char**)v->array)[v->size] = va_arg(ap, char*);
break;
}
va_end(ap);
if(++v->size == v->capacity)
v->array=realloc(v->array, TypeSize(v->type)*(v->capacity += 16));
}
size_t vec_size(Vector *v){
return v->size;
}
void* vec_getArray(Vector *v){
return v->array;
}
void vec_drop(Vector *v){
free(v->array);
free(v);
}