示例#1
0
文件: reader.c 项目: FaizaGara/lima
/* rdr_load:
 *   Read from the given file a reader saved previously with rdr_save. The given
 *   reader must be empty, comming fresh from rdr_new. Be carefull that this
 *   function performs almost no checks on the input data, so if you modify the
 *   reader and make a mistake, it will probably result in a crash.
 */
void rdr_load(rdr_t *rdr, FILE *file) {
	const char *err = "broken file, invalid reader format";
	int autouni = rdr->autouni;
	fpos_t pos;
	fgetpos(file, &pos);
	if (fscanf(file, "#rdr#%"PRIu32"/%"PRIu32"/%d\n",
			&rdr->npats, &rdr->ntoks, &autouni) != 3) {
		// This for compatibility with previous file format
		fsetpos(file, &pos);
		if (fscanf(file, "#rdr#%"PRIu32"/%"PRIu32"\n",
			   &rdr->npats, &rdr->ntoks) != 2) {
		  fatal(err);}
	}
	rdr->autouni = autouni;
	rdr->nuni = rdr->nbi = 0;
	if (rdr->npats != 0) {
		rdr->pats = xmalloc(sizeof(pat_t *) * rdr->npats);
		for (uint32_t p = 0; p < rdr->npats; p++) {
			char *pat = ns_readstr(file);
			rdr->pats[p] = pat_comp(pat);
			switch (tolower(pat[0])) {
				case 'u': rdr->nuni++; break;
				case 'b': rdr->nbi++;  break;
				case '*': rdr->nuni++;
				          rdr->nbi++;  break;
			}
		}
	}
	qrk_load(rdr->lbl, file);
	qrk_load(rdr->obs, file);
}
示例#2
0
文件: quark.c 项目: elif021/Wapiti
/* qrk_load:
 *   Load a list of key from the given file and add them to the map. Each lines
 *   of the file is taken as a single key and mapped to the next available id if
 *   not already present. If all keys are single lines and the given map is
 *   initilay empty, this will load a map exactly as saved by qrk_save.
 */
void qrk_load(qrk_t *qrk, FILE *file) {
    uint64_t cnt = 0;
    if (fscanf(file, "#qrk#%"SCNu64"\n", &cnt) != 1) {
        if (ferror(file) != 0)
            pfatal("cannot read from file");
        pfatal("invalid format");
    }
    for (uint64_t n = 0; n < cnt; ++n) {
        char *str = ns_readstr(file);
        qrk_str2id(qrk, str);
        free(str);
    }
}