#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//Linked List
typedef struct _node {
char *name;
struct _node *next;
} Node;
typedef struct _list {
Node *data;
struct _list *next;
} List;
Node *new_Node(char *name){
Node *p;
p=(Node*)malloc(sizeof(Node));
p->name = strdup(name);
p->next = NULL;
return p;
}
void free_Node(Node *p){
if(p == NULL) return;
free(p->name);
free_Node(p->next);
free(p);
}
List *new_List(){
List *p;
p=(List*)malloc(sizeof(List));
p->data = NULL;
p->next = NULL;
return p;
}
void free_List(List *p){
if(p == NULL) return;
free_Node(p->data);
free_List(p->next);
free(p);
}
Node *findNode(List *root, char *name){
if(root->data == NULL){
root->data = new_Node(name);
return root->data;
}
if(0==strcmp(root->data->name, name)){
return root->data;
}
if(root->next == NULL){
root->next = new_List();
root->next->data = new_Node(name);
return root->next->data;
}
return findNode(root->next, name);
}
void addChildNode(Node *node, char *name){
if(node->next == NULL){
node->next = new_Node(name);
} else {
addChildNode(node->next, name);
}
}
void addNode(List *root, char *typeName, char *idName){
Node *np;
np = findNode(root, typeName);
addChildNode(np, idName);
}
void toEnumSrcOut(FILE *fo, const char *srcFile){
FILE *fp;
int count;
char enumName[64]={0};
char enumId[64]={0};
List *root,*lp;
Node *np;
root = new_List();
fp=fopen(srcFile, "r");
while(EOF!=(count=fscanf(fp, "%[^(]%*c %*[^,]%*c %*c%[^\"]%*[^\n]%*c", enumName, enumId))){
if(count == 2){
addNode(root, enumName, enumId);
}
}
fclose(fp);
for(lp = root; lp != NULL ; lp=lp->next){
fprintf(fo, "typedef enum {\n");
for(np = lp->data->next; np != NULL ; np=np->next){
fprintf(fo, "\t%s",np->name);
fprintf(fo, (np->next == NULL)? "\n" : ",\n");
}
fprintf(fo, "} %s;\n",lp->data->name);
}
free_List(root);
}
int main(){
toEnumSrcOut(stdout, "enumsrc.txt");
return 0;
}