/* HTS_Question_load: Load questions from file */ static void HTS_Question_load(HTS_Question * question, FILE * fp) { char buff[HTS_MAXBUFLEN]; HTS_Pattern *pattern, *last_pattern; /* get question name */ HTS_get_pattern_token(fp, buff); question->string = HTS_strdup(buff); question->head = NULL; /* get pattern list */ HTS_get_pattern_token(fp, buff); last_pattern = NULL; if (strcmp(buff, "{") == 0) { while (1) { HTS_get_pattern_token(fp, buff); pattern = (HTS_Pattern *) HTS_calloc(1, sizeof(HTS_Pattern)); if (question->head) last_pattern->next = pattern; else /* first time */ question->head = pattern; pattern->string = HTS_strdup(buff); pattern->next = NULL; HTS_get_pattern_token(fp, buff); if (!strcmp(buff, "}")) break; last_pattern = pattern; } } }
/* HTS_Question_load: Load questions from file */ static HTS_Boolean HTS_Question_load(HTS_Question * question, HTS_File * fp) { char buff[HTS_MAXBUFLEN]; HTS_Pattern *pattern, *last_pattern; if (question == NULL || fp == NULL) return FALSE; /* get question name */ if (HTS_get_pattern_token(fp, buff) == FALSE) return FALSE; question->string = HTS_strdup(buff); question->head = NULL; /* get pattern list */ if (HTS_get_pattern_token(fp, buff) == FALSE) { free(question->string); question->string = NULL; return FALSE; } last_pattern = NULL; if (strcmp(buff, "{") == 0) { while (1) { if (HTS_get_pattern_token(fp, buff) == FALSE) { HTS_Question_clear(question); return FALSE; } pattern = (HTS_Pattern *) HTS_calloc(1, sizeof(HTS_Pattern)); if (question->head) last_pattern->next = pattern; else /* first time */ question->head = pattern; pattern->string = HTS_strdup(buff); pattern->next = NULL; if (HTS_get_pattern_token(fp, buff) == FALSE) { HTS_Question_clear(question); return FALSE; } if (!strcmp(buff, "}")) break; last_pattern = pattern; } } return TRUE; }
/* HTS_Tree_load: Load trees */ static void HTS_Tree_load(HTS_Tree * tree, FILE * fp, HTS_Question * question) { char buff[HTS_MAXBUFLEN]; HTS_Node *node, *last_node; HTS_get_pattern_token(fp, buff); node = (HTS_Node *) HTS_calloc(1, sizeof(HTS_Node)); node->index = 0; tree->root = last_node = node; if (strcmp(buff, "{") == 0) { while (HTS_get_pattern_token(fp, buff), strcmp(buff, "}") != 0) { node = HTS_Node_find(last_node, atoi(buff)); HTS_get_pattern_token(fp, buff); /* load question at this node */ node->quest = HTS_Question_find_question(question, buff); node->yes = (HTS_Node *) HTS_calloc(1, sizeof(HTS_Node)); node->no = (HTS_Node *) HTS_calloc(1, sizeof(HTS_Node)); HTS_get_pattern_token(fp, buff); if (HTS_is_num(buff)) node->no->index = atoi(buff); else node->no->pdf = HTS_name2num(buff); node->no->next = last_node; last_node = node->no; HTS_get_pattern_token(fp, buff); if (HTS_is_num(buff)) node->yes->index = atoi(buff); else node->yes->pdf = HTS_name2num(buff); node->yes->next = last_node; last_node = node->yes; } } else { node->pdf = HTS_name2num(buff); } }
/* HTS_Model_load_tree: load trees */ static void HTS_Model_load_tree(HTS_Model * model, FILE * fp) { char buff[HTS_MAXBUFLEN]; HTS_Question *question, *last_question; HTS_Tree *tree, *last_tree; int state; /* check */ if (fp == NULL) HTS_error(1, "HTS_Model_load_tree: File for trees is not specified.\n"); model->ntree = 0; last_question = NULL; last_tree = NULL; while (!feof(fp)) { HTS_get_pattern_token(fp, buff); /* parse questions */ if (strcmp(buff, "QS") == 0) { question = (HTS_Question *) HTS_calloc(1, sizeof(HTS_Question)); HTS_Question_load(question, fp); if (model->question) last_question->next = question; else model->question = question; question->next = NULL; last_question = question; } /* parse trees */ state = HTS_get_state_num(buff); if (state != 0) { tree = (HTS_Tree *) HTS_calloc(1, sizeof(HTS_Tree)); tree->next = NULL; tree->root = NULL; tree->head = NULL; tree->state = state; HTS_Tree_parse_pattern(tree, buff); HTS_Tree_load(tree, fp, model->question); if (model->tree) last_tree->next = tree; else model->tree = tree; tree->next = NULL; last_tree = tree; model->ntree++; } } /* No Tree information in tree file */ if (model->tree == NULL) HTS_error(1, "HTS_Model_load_tree: No trees are loaded.\n"); }
/* HTS_Tree_load: Load trees */ static HTS_Boolean HTS_Tree_load(HTS_Tree * tree, HTS_File * fp, HTS_Question * question) { char buff[HTS_MAXBUFLEN]; HTS_Node *node, *last_node; if (tree == NULL || fp == NULL) return FALSE; if (HTS_get_pattern_token(fp, buff) == FALSE) { HTS_Tree_clear(tree); return FALSE; } node = (HTS_Node *) HTS_calloc(1, sizeof(HTS_Node)); node->index = 0; tree->root = last_node = node; if (strcmp(buff, "{") == 0) { while (HTS_get_pattern_token(fp, buff) == TRUE && strcmp(buff, "}") != 0) { node = HTS_Node_find(last_node, atoi(buff)); if (node == NULL) { HTS_Tree_clear(tree); return FALSE; } if (HTS_get_pattern_token(fp, buff) == FALSE) { HTS_Tree_clear(tree); return FALSE; } node->quest = HTS_Question_find_question(question, buff); if (node->quest == NULL) { HTS_Tree_clear(tree); return FALSE; } node->yes = (HTS_Node *) HTS_calloc(1, sizeof(HTS_Node)); node->no = (HTS_Node *) HTS_calloc(1, sizeof(HTS_Node)); if (HTS_get_pattern_token(fp, buff) == FALSE) { node->quest = NULL; free(node->yes); free(node->no); HTS_Tree_clear(tree); return FALSE; } if (HTS_is_num(buff)) node->no->index = atoi(buff); else node->no->pdf = HTS_name2num(buff); node->no->next = last_node; last_node = node->no; if (HTS_get_pattern_token(fp, buff) == FALSE) { node->quest = NULL; free(node->yes); free(node->no); HTS_Tree_clear(tree); return FALSE; } if (HTS_is_num(buff)) node->yes->index = atoi(buff); else node->yes->pdf = HTS_name2num(buff); node->yes->next = last_node; last_node = node->yes; } } else { node->pdf = HTS_name2num(buff); } return TRUE; }