static int parse_tileset(xmlTextReaderPtr reader, tmx_tileset **ts_headadr, const char *filename) { tmx_tileset *res = NULL; int ret; char *value, *ab_path; xmlTextReaderPtr sub_reader; if (!(res = alloc_tileset())) return 0; res->next = *ts_headadr; *ts_headadr = res; /* parses each attribute */ if ((value = (char*)xmlTextReaderGetAttribute(reader, (xmlChar*)"firstgid"))) { /* fisrtgid */ res->firstgid = atoi(value); tmx_free_func(value); } else { tmx_err(E_MISSEL, "xml parser: missing 'firstgid' attribute in the 'tileset' element"); return 0; } if ((value = (char*)xmlTextReaderGetAttribute(reader, (xmlChar*)"source"))) { /* source */ if (!(ab_path = mk_absolute_path(filename, value))) return 0; tmx_free_func(value); if (!(sub_reader = create_parser(ab_path))) return 0; /* opens */ ret = parse_tileset_sub(sub_reader, res, ab_path); /* and parses the tsx file */ xmlFreeTextReader(sub_reader); tmx_free_func(ab_path); return ret; } return parse_tileset_sub(reader, res, filename); }
int crm_expr_correlate_learn(CSL_CELL *csl, ARGPARSE_BLOCK *apb, VHT_CELL **vht, CSL_CELL *tdw, char *txtptr, int txtstart, int txtlen) { // learn the given text as correlative text // belonging to a particular type. // learn <flags> (classname) /regex/ (regex is ignored) // int i, j, k; char ptext[MAX_PATTERN]; // the regex pattern int plen; char ltext[MAX_PATTERN]; // the variable to learn int llen; char htext[MAX_PATTERN]; // the hash name int hlen; int cflags, eflags; struct stat statbuf; // for statting the hash file FILE *f; // hashfile fd // //regex_t regcb; int textoffset; int textlen; int sense; int vhtindex; int microgroom; int fev; int made_new_file; char *learnfilename; if (internal_trace) fprintf(stderr, "executing a LEARN (correlation format)\n"); // Keep the gcc compiler from complaining about unused variables // i = hctable[0]; // extract the hash file name hlen = crm_get_pgm_arg(htext, MAX_PATTERN, apb->p1start, apb->p1len); hlen = crm_nexpandvar(htext, hlen, MAX_PATTERN, vht, tdw); // // extract the variable name (if present) llen = crm_get_pgm_arg(ltext, MAX_PATTERN, apb->b1start, apb->b1len); llen = crm_nexpandvar(ltext, llen, MAX_PATTERN, vht, tdw); // get the "this is a word" regex plen = crm_get_pgm_arg(ptext, MAX_PATTERN, apb->s1start, apb->s1len); plen = crm_nexpandvar(ptext, plen, MAX_PATTERN, vht, tdw); // set our cflags, if needed. The defaults are // "case" and "affirm", (both zero valued). // and "microgroom" disabled. cflags = REG_EXTENDED; eflags = 0; sense = +1; if (apb->sflags & CRM_NOCASE) { cflags = cflags | REG_ICASE; eflags = 1; if (user_trace) fprintf(stderr, "turning oncase-insensitive match\n"); } if (apb->sflags & CRM_REFUTE) { sense = -sense; if (user_trace) fprintf(stderr, " refuting learning\n"); } microgroom = 0; if (apb->sflags & CRM_MICROGROOM) { microgroom = 1; if (user_trace) fprintf(stderr, " enabling microgrooming.\n"); } // // grab the filename, and stat the file // note that neither "stat", "fopen", nor "open" are // fully 8-bit or wchar clean... if (!crm_nextword(htext, hlen, 0, &i, &j) || j == 0) { fev = nonfatalerror_ex(SRC_LOC(), "\nYou didn't specify a valid filename: '%.*s'\n", (int)hlen, htext); return fev; } j += i; CRM_ASSERT(i < hlen); CRM_ASSERT(j <= hlen); // filename starts at i, ends at j. null terminate it. htext[j] = 0; learnfilename = &htext[i]; if (!learnfilename) { untrappableerror("Cannot allocate classifier memory", "Stick a fork in us; we're _done_."); } // and stat it to get it's length k = stat(learnfilename, &statbuf); made_new_file = 0; // quick check- does the file even exist? if (k != 0) { // file didn't exist... create it CRM_PORTA_HEADER_INFO classifier_info = { 0 }; if (user_trace) { fprintf(stderr, "\nCreating new correlate file %s\n", learnfilename); fprintf(stderr, "Opening file %s for write\n", learnfilename); } f = fopen(learnfilename, "wb"); if (!f) { char dirbuf[DIRBUFSIZE_MAX]; fev = fatalerror_ex(SRC_LOC(), "\n Couldn't open your new CORRELATE file %s for writing; (full path: '%s') errno=%d(%s)\n", learnfilename, mk_absolute_path(dirbuf, WIDTHOF(dirbuf), learnfilename), errno, errno_descr(errno)); return fev; } classifier_info.classifier_bits = CRM_CORRELATE; classifier_info.hash_version_in_use = selected_hashfunction; if (0 != fwrite_crm_headerblock(f, &classifier_info, NULL)) { fev = nonfatalerror_ex(SRC_LOC(), "\n Couldn't write header to file %s; errno=%d(%s)\n", learnfilename, errno, errno_descr(errno)); fclose(f); return fev; } // file_memset(f, 0, count); // don't do any output at all. made_new_file = 1; statbuf.st_size = 0; } else { if (user_trace) { fprintf(stderr, "Opening correlate file %s for append\n", learnfilename); } // Now a nasty bit. Because there might be data of the // file retained, we need to force an unmap-by-name which will allow a remap // with the new file length later on. if (internal_trace) { fprintf(stderr, "un-mmap-ping file %s for known state\n", learnfilename); } crm_force_munmap_filename(learnfilename); f = fopen(learnfilename, "ab+"); if (!f) { char dirbuf[DIRBUFSIZE_MAX]; fev = fatalerror_ex(SRC_LOC(), "\n Couldn't open your CORRELATE file %s for append; (full path: '%s') errno=%d(%s)\n", learnfilename, mk_absolute_path(dirbuf, WIDTHOF(dirbuf), learnfilename), errno, errno_descr(errno)); return fev; } if (is_crm_headered_file(f)) { statbuf.st_size -= CRM114_HEADERBLOCK_SIZE; } // And make sure the file pointer is at EOF. (void)fseek(f, 0, SEEK_END); if (ftell(f) == 0) { CRM_PORTA_HEADER_INFO classifier_info = { 0 }; classifier_info.classifier_bits = CRM_CORRELATE; classifier_info.hash_version_in_use = selected_hashfunction; if (0 != fwrite_crm_headerblock(f, &classifier_info, NULL)) { int err = errno; fclose(f); fev = nonfatalerror_ex(SRC_LOC(), "Couldn't write the header to the .hypsvm file named '%s': error %d(%s)", learnfilename, err, errno_descr(err)); return fev; } // file_memset(f, 0, count); // don't do any output at all. made_new_file = 1; statbuf.st_size = 0; } } // if (user_trace) { fprintf(stderr, "Correlation text file %s has length %d characters\n", learnfilename, (int)(statbuf.st_size / sizeof(FEATUREBUCKET_TYPE))); } // // get the text to "learn" (well, append to the correlation file) // // This is the text that we'll append to the correlation file. /* removed i=0: re-init here: important! */ if (llen > 0) { if (!crm_is_legal_variable(ltext, llen)) { int q = fatalerror_ex(SRC_LOC(), "Attempt to LEARN from an illegal variable '%.*s'. How very bizarre.", llen, ltext); return q; } vhtindex = crm_vht_lookup(vht, ltext, llen, csl->calldepth); } else { vhtindex = crm_vht_lookup(vht, ":_dw:", 5, csl->calldepth); } if (vht[vhtindex] == NULL) { int q; CRM_ASSERT(f != NULL); fclose(f); q = nonfatalerror(" Attempt to LEARN from a nonexistent variable ", ltext); return q; } mdw = NULL; if (tdw->filetext == vht[vhtindex]->valtxt) mdw = tdw; if (cdw->filetext == vht[vhtindex]->valtxt) mdw = cdw; if (mdw == NULL) { int q; CRM_ASSERT(f != NULL); fclose(f); q = nonfatalerror(" Bogus text block containing variable ", ltext); return q; } else { ssize_t old_fileoffset; textoffset = vht[vhtindex]->vstart; textlen = vht[vhtindex]->vlen; if (user_trace) { fprintf(stderr, "learning the text (len %d) :", textlen); fwrite4stdio(&(mdw->filetext[textoffset]), ((textlen < 128) ? textlen : 128), stderr); fprintf(stderr, "\n"); } // append the "learn" text to the end of the file. // CRM_ASSERT(f != NULL); (void)fseek(f, 0, SEEK_END); old_fileoffset = ftell(f); if (textlen != fwrite(&(mdw->filetext[textoffset]), 1, textlen, f)) { int fev; int err = errno; fclose(f); // try to correct the failure by ditching the new, partially(?) written(?) data truncate(learnfilename, old_fileoffset); fev = nonfatalerror_ex(SRC_LOC(), "Failed to append the 'learn' text to the correlation file '%s': error %d(%s)\n", learnfilename, err, errno_descr(err)); return fev; } } CRM_ASSERT(f != NULL); fclose(f); return 0; }