/* HTS_get_token: get token from file pointer (separators are space, tab, and line break) */ HTS_Boolean HTS_get_token_from_fp(HTS_File * fp, char *buff) { char c; size_t i; if (fp == NULL || HTS_feof(fp)) return FALSE; c = HTS_fgetc(fp); while (c == ' ' || c == '\n' || c == '\t') { if (HTS_feof(fp)) return FALSE; c = HTS_fgetc(fp); if (c == EOF) return FALSE; } for (i = 0; c != ' ' && c != '\n' && c != '\t';) { buff[i++] = c; if (HTS_feof(fp)) break; c = HTS_fgetc(fp); if (c == EOF) break; } buff[i] = '\0'; return TRUE; }
/* HTS_get_token_with_separator: get token from file pointer with specified separator */ HTS_Boolean HTS_get_token_from_fp_with_separator(HTS_File * fp, char *buff, char separator) { char c; size_t i; if (fp == NULL || HTS_feof(fp)) return FALSE; c = HTS_fgetc(fp); while (c == separator) { if (HTS_feof(fp)) return FALSE; c = HTS_fgetc(fp); if (c == EOF) return FALSE; } for (i = 0; c != separator;) { buff[i++] = c; if (HTS_feof(fp)) break; c = HTS_fgetc(fp); if (c == EOF) break; } buff[i] = '\0'; return TRUE; }
/* HTS_get_pattern_token: get pattern token */ HTS_Boolean HTS_get_pattern_token(HTS_File * fp, char *buff) { char c; int i; HTS_Boolean squote = FALSE, dquote = FALSE; if (fp == NULL || HTS_feof(fp)) return FALSE; c = HTS_fgetc(fp); while (c == ' ' || c == '\n') { if (HTS_feof(fp)) return FALSE; c = HTS_fgetc(fp); } if (c == '\'') { /* single quote case */ if (HTS_feof(fp)) return FALSE; c = HTS_fgetc(fp); squote = TRUE; } if (c == '\"') { /*double quote case */ if (HTS_feof(fp)) return FALSE; c = HTS_fgetc(fp); dquote = TRUE; } if (c == ',') { /*special character ',' */ strcpy(buff, ","); return TRUE; } i = 0; while (1) { buff[i++] = c; c = HTS_fgetc(fp); if (squote && c == '\'') break; if (dquote && c == '\"') break; if (!squote && !dquote) { if (c == ' ') break; if (c == '\n') break; if (HTS_feof(fp)) break; } } buff[i] = '\0'; return TRUE; }
/* HTS_Model_load_tree: load trees */ static HTS_Boolean HTS_Model_load_tree(HTS_Model * model, HTS_File * fp) { char buff[HTS_MAXBUFLEN]; HTS_Question *question, *last_question; HTS_Tree *tree, *last_tree; int state; /* check */ if (model == NULL || fp == NULL) { HTS_error(1, "HTS_Model_load_tree: File for trees is not specified.\n"); return FALSE; } model->ntree = 0; last_question = NULL; last_tree = NULL; while (!HTS_feof(fp)) { HTS_get_pattern_token(fp, buff); /* parse questions */ if (strcmp(buff, "QS") == 0) { question = (HTS_Question *) HTS_calloc(1, sizeof(HTS_Question)); if (HTS_Question_load(question, fp) == FALSE) { free(question); HTS_Model_clear(model); return FALSE; } 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); if (HTS_Tree_load(tree, fp, model->question) == FALSE) { free(tree); HTS_Model_clear(model); return FALSE; } 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"); return FALSE; } return TRUE; }