add table, item, define.

This commit is contained in:
Saturn&Eric 2017-06-30 22:44:00 +08:00
parent 3b49e7e05f
commit a6a462c0f7
8 changed files with 204 additions and 30 deletions

View File

@ -9,6 +9,9 @@
#include "ID.h" #include "ID.h"
long get_id(void){ long get_id(void){
srand((unsigned int)time(NULL));
return (long)(rand()+rand()%1000); return (long)(rand()+rand()%1000);
} }
void init_id(void){
srand((unsigned int)time(NULL));
}

View File

@ -14,5 +14,6 @@
#include <time.h> #include <time.h>
extern long get_id(void); extern long get_id(void);
extern void init_id(void);
#endif /* ID_h */ #endif /* ID_h */

View File

@ -10,6 +10,8 @@
#include "Debug.h" #include "Debug.h"
#include "ID.h" #include "ID.h"
List *items_bin = NULL;
List *build_list(void){ List *build_list(void){
List *p_list = (List *) malloc(sizeof(List)); List *p_list = (List *) malloc(sizeof(List));
p_list->avaliable = 1; p_list->avaliable = 1;
@ -65,6 +67,8 @@ int del_node(List *p_list, Node *p_node){
} }
int free_node(Node *p_node){ int free_node(Node *p_node){
p_node->avaliable = -1;
p_node->safe = -1;
free(p_node ->value); free(p_node ->value);
free(p_node); free(p_node);
return 0; return 0;
@ -148,12 +152,12 @@ int free_list(List *p_list){
while (p_node != NULL){ while (p_node != NULL){
pl_node = p_node; pl_node = p_node;
p_node = p_node->n_node; p_node = p_node->n_node;
pl_node->avaliable = 0; pl_node->avaliable = -1;
pl_node->safe = 0; pl_node->safe = -1;
free_node(pl_node); free_node(pl_node);
} }
p_list->avaliable = 0; p_list->avaliable = -1;
p_list->safe = 0; p_list->safe = -1;
free(p_list); free(p_list);
return 0; return 0;
} }
@ -216,39 +220,75 @@ char *set_valuestring(char *string){
return p_string; return p_string;
} }
int list_tofile(FILE *p_file, List *p_list){ int table_tofile(FILE *p_file, Table *p_table){
//fprintf(p_file, "%d,%d,"); fprintf(p_file, "!#TABLE_INFO\n");
fprintf(p_file, "id:%ld\n",p_table->id);
fprintf(p_file, "item_num:%d\n",p_table->num);
fprintf(p_file, "define_id:%ld\n", p_table->p_define->id);
Node *pi_node = p_table->item_list->h_node;
Item *p_item = (Item *)pi_node->value;
fprintf(p_file, "\n");
fprintf(p_file, "!#ITEM_INFO\n");
while (pi_node != NULL){
p_item = (Item *)pi_node->value;
fprintf(p_file, "id:%ld\n",p_item->id);
fprintf(p_file, "data_num:%d\n",p_item->data_num);
fprintf(p_file, "define_id:%ld\n",p_item->p_define->id);
Node *pt_node = p_item->p_define->type_list->h_node;
Node *pd_node = p_item->data_list->h_node;
for (int i = 0; i < (p_item->p_define->num); i++){
if (!strcmp((char *)pt_node->value, "int")){
fprintf(p_file, "value[%d]:%d\n",i,*((int *)pd_node->value));
}
else if(!strcmp((char *)pt_node->value, "string")){
fprintf(p_file, "value[%d]:%s\n",i,((char *)pd_node->value));
}
else if(!strcmp((char *)pt_node->value, "float")){
fprintf(p_file, "value[%d]:%lf\n",i,*((double *)pd_node->value));
}
else{
fprintf(p_file, "vlaue[%d]:not_avaliable\n",i);
}
pt_node = pt_node->n_node;
pd_node = pd_node->n_node;
}
fprintf(p_file, "END\n");
pi_node = pi_node->n_node;
}
return 0; return 0;
} }
Item *build_item(void){ Item *build_item(Define *p_define){
Item *p_item = (Item *)malloc(sizeof(Item)); Item *p_item = (Item *)malloc(sizeof(Item));
p_item->id = get_id(); p_item->id = get_id();
p_item->data_list = build_list(); p_item->data_list = build_list();
p_item->data_num = 0; p_item->data_num = 0;
p_item->type_list = build_list(); p_item->p_define = p_define;
p_item->define_list = build_list();
p_item->define_num = 0;
p_item->avaliable = 1; p_item->avaliable = 1;
p_item->safe = 1; p_item->safe = 1;
Node *p_node = build_node();
p_node->value = p_item;
add_node(items_bin,p_node);
return p_item; return p_item;
} }
int add_define(Item *p_item, char *define, char *type){ int add_define(Define *p_define, char *define, char *type){
Node *pd_node = build_node(); Node *pd_node = build_node();
pd_node->value = set_valuestring(define); pd_node->value = set_valuestring(define);
Node *pt_node = build_node(); Node *pt_node = build_node();
pt_node->value = set_valuestring(type); pt_node->value = set_valuestring(type);
add_node(p_item->define_list, pd_node); add_node(p_define->define_list, pd_node);
add_node(p_item->type_list, pt_node); add_node(p_define->type_list, pt_node);
p_item->define_num++; p_define->num++;
return 0; return 0;
} }
int add_data(Item *p_item, void *value){ int add_data(Item *p_item, void *value){
if (p_item->data_num >= p_item->define_num){ if (p_item->data_num >= p_item->p_define->num){
DEBUG_CREATE_INFO *t_deg_crt_ifo = get_degcrtifo();; DEBUG_CREATE_INFO *t_deg_crt_ifo = get_degcrtifo();;
t_deg_crt_ifo->type = ERROR; t_deg_crt_ifo->type = ERROR;
t_deg_crt_ifo->showvar = 1; t_deg_crt_ifo->showvar = 1;
@ -256,7 +296,100 @@ int add_data(Item *p_item, void *value){
t_deg_crt_ifo->message = "data is more than define."; t_deg_crt_ifo->message = "data is more than define.";
debug_printf(t_deg_crt_ifo); debug_printf(t_deg_crt_ifo);
} }
Node *p_node = build_node();
p_node->value = value;
add_node(p_item->data_list, p_node);
p_item->data_num++;
return 0; return 0;
} }
Define *build_define(void){
Define *p_define = (Define *) malloc(sizeof(Define));
p_define->id = get_id();
p_define->num = 0;
p_define->define_list = build_list();
p_define->type_list = build_list();
p_define->avaliable = 1;
p_define->safe = 1;
return p_define;
}
int free_define(Define *p_define){
free_list(p_define->define_list);
free_list(p_define->type_list);
p_define->avaliable = -1;
p_define->safe = -1;
free(p_define);
return 0;
}
int free_item(Item *p_item){
p_item->avaliable = -1;
p_item->safe = -1;
free_list(p_item->data_list);
free(p_item);
return 0;
}
Table *build_table(Define *p_define){
Table *p_table = (Table *)malloc(sizeof(Table));
p_table->id = get_id();
p_table->item_list = build_list();
p_table->p_define = p_define;
p_table->avaliable = 1;
p_table->safe = 1;
return p_table;
}
int add_item(Table *p_table, Item *p_item){
if(p_item->p_define->id != p_table->p_define->id){
DEBUG_CREATE_INFO *t_deg_crt_ifo = get_degcrtifo();;
t_deg_crt_ifo->type = ERROR;
t_deg_crt_ifo->showvar = 1;
t_deg_crt_ifo->variable = &p_item;
t_deg_crt_ifo->message = "item's define is different form the table's.";
debug_printf(t_deg_crt_ifo);
}
Node *p_node = build_node();
p_node->value = p_item;
add_node(p_table->item_list,p_node);
p_table->num++;
return 0;
}
int free_table(Table *p_table){
free_list(p_table->item_list);
p_table->avaliable = -1;
p_table->safe = -1;
free(p_table);
return 0;
}
void list_init(void){
items_bin = build_list();
}
void list_delinit(void){
Node *p_node = items_bin->h_node;
while (p_node != NULL){
Item *p_item = (Item *)p_node->value;
if (p_item->avaliable == 1){
free_item(p_item);
}
p_node = p_node->n_node;
}
free_list(items_bin);
}
int define_tofile(FILE *p_file, Define *p_define){
fprintf(p_file, "!#define\n");
fprintf(p_file, "id:%ld\n",p_define->id);
Node *pd_node = p_define->define_list->h_node, *pt_node = p_define->type_list->h_node;
for (int i = 0 ; i < p_define->num; i++){
fprintf(p_file, "%s/%s\n",(char *)pd_node->value, (char *)pt_node->value);
pd_node = pd_node->n_node;
pt_node = pt_node->n_node;
}
fprintf(p_file, "END");
return 0;
}

View File

@ -47,11 +47,8 @@ typedef struct item{
int avaliable; int avaliable;
int safe; int safe;
Define *p_define; Define *p_define;
List *define_list;
List *type_list;
List *data_list; List *data_list;
int data_num; int data_num;
int define_num;
} Item; } Item;
typedef struct table{ typedef struct table{
@ -70,6 +67,8 @@ typedef struct bin{
List *item_list; List *item_list;
} Bin; } Bin;
extern List *items_bin;
extern List *build_list(void); extern List *build_list(void);
extern Node *build_node(void); extern Node *build_node(void);
extern int free_list(List *); extern int free_list(List *);
@ -83,8 +82,9 @@ extern Node *get_locationByid(List *,long);
extern Node *get_locationBystring(List *, char *); extern Node *get_locationBystring(List *, char *);
extern int list_tofile(FILE *, List *); extern int table_tofile(FILE *p_file, Table *p_table);
extern int file_tolist(FILE *, List *); extern int define_tofile(FILE *p_file, Define *p_define);
extern int file_totable(FILE *, List *);
extern int *set_valueint(int); extern int *set_valueint(int);
extern double *set_valuefloat(double); extern double *set_valuefloat(double);
@ -98,5 +98,12 @@ extern Define *build_define(void);
extern int add_define(Define *, char *, char *); extern int add_define(Define *, char *, char *);
extern int free_define(Define *); extern int free_define(Define *);
extern Table *build_table(Define *p_define);
extern int add_item(Table*, Item *);
extern int free_table(Table *);
extern void list_init(void);
extern void list_delinit(void);
#endif /* List_h */ #endif /* List_h */

View File

@ -1,18 +1,48 @@
#include "Support.h" #include "Support.h"
#include <time.h>
int main(int argc, char *argv[]){ int main(int argc, char *argv[]){
Node *nl[50]; list_init();
List *pl = build_list(); init_id();
for(int i = 0; i < 50; i++){ Define *p_define = build_define();
nl[i] = build_node(); add_define(p_define, "Name", "string");
nl[i]->value = (void *)set_valueint(i); add_define(p_define, "Age", "int");
add_node(pl, nl[i]); add_define(p_define, "Power", "float");
} Table *p_table = build_table(p_define);
Item *items[3];
items[0] = build_item(p_define);
add_data(items[0], set_valuestring("Tim"));
add_data(items[0], set_valueint(23));
add_data(items[0], set_valuefloat(100.0));
add_item(p_table, items[0]);
items[1] = build_item(p_define);
add_data(items[1], set_valuestring("Pat"));
add_data(items[1], set_valueint(33));
add_data(items[1], set_valuefloat(120.0));
add_item(p_table, items[1]);
items[2] = build_item(p_define);
add_data(items[2], set_valuestring("Tom"));
add_data(items[2], set_valueint(43));
add_data(items[2], set_valuefloat(130.0));
add_item(p_table, items[2]);
FILE *fp = fopen("table.data", "w");
table_tofile(fp, p_table);
fclose(fp);
FILE *dfp = fopen("define.settings", "w");
define_tofile(dfp, p_define);
fclose(dfp);
free_define(p_define);
free_table(p_table);
//list_delinit();
Node *p_node = get_locationByindex(pl, 5);
free_list(pl);
return 0; return 0;
} }