char *quote_get(xdb_pair_t * pair, char *string) { DB *db = NULL; int count = 0, recnum = 0; char *str = NULL; xdb_pair_t *pair_2 = NULL; if (!pair) return NULL; db = xdb_open(pair->value); if (!db) return NULL; db->stat_print(db, 0); count = xdb_count(db); printf("count=%i\n", count); recnum = rand() % count; pair_2 = xdb_get_recnum(db, recnum); str = strdup(pair_2->value); if (db) xdb_fini(db); if (pair_2) xdb_pair_destroy(pair_2); return str; }
/* setup & open the dict */ xdict_t xdict_open(const char *fpath, int mode) { xdict_t xd; xdb_t x; if (!(x = xdb_open(fpath, 'r'))) return NULL; xd = (xdict_t) malloc(sizeof(xdict_st)); memset(xd, 0, sizeof(xdict_st)); xd->ref = 1; if (mode & SCWS_XDICT_MEM) { xtree_t xt; /* convert the xdb(disk) -> xtree(memory) */ if ((xt = xdb_to_xtree(x, NULL)) != NULL) { xdb_close(x); xd->xdict = (void *) xt; xd->xmode = SCWS_XDICT_MEM; return xd; } } xd->xmode = SCWS_XDICT_XDB; xd->xdict = (void *) x; return xd; }
void moby_create_db(bot_t * bot) { DB *db = NULL; FILE *fp = NULL; struct stat st; int count = 0; char *str, *str_db, *tok_1 = NULL, *tok_2 = NULL; char buf[10024]; /* str = str_unite_static ("%s/mods/mod_moby_files/syns.txt", gi->confdir); fp = fopen (str, "r"); if (!fp) goto cleanup; */ strlcpy_buf(moby_info.db_name, str_unite_static("%s/mods/mod_moby_files/syns.moby_db", gi->confdir)); str_db = moby_info.db_name; if (!stat(str_db, &st)) { /* db already exists */ moby_info.initialized = 1; return; } str = str_unite_static("%s/mods/mod_moby_files/syns.txt", gi->confdir); fp = fopen(str, "r"); if (!fp) goto cleanup; db = xdb_open(str_db); if (!db) goto cleanup; while (1) { _memset(buf, 0, sizeof(buf)); if (fgets(buf, sizeof(buf) - 1, fp) == NULL) break; str_apply(buf, tolower); _strstrip_nl(buf); if (buf[0] == '#') continue; if (strlen(buf) == 0) continue; tok_1 = strtok(buf, ","); if (!tok_1) continue; tok_2 = strtok(NULL, ""); if (!tok_2) continue; xdb_write(db, tok_1, tok_2); count++; } moby_info.initialized = 1; moby_info.count = count; cleanup: if (fp) fclose(fp); if (db) xdb_fini(db); return; }
char *moby_change_string(char *string, int opt) { DB *db = NULL; int i, j, tok_nonalpha_len, retchar; char *str, *syn, *tok_ptr, *tok_nonalpha; char buf[MAX_BUF_SZ]; char *sep_ptr; str = NULL; if (!string) return NULL; sep_ptr = str_find_sep(string); if (sep_ptr) string = sep_ptr; _memset(buf, 0, sizeof(buf)); tok_ptr = string; db = xdb_open(moby_info.db_name); if (!db) { return NULL; } j = 0; while (1) { if (j > 0 && opt == MOBY_LIST) break; retchar = 0; tok_ptr = strtokbionic_nonalpha(tok_ptr, &retchar); if (!tok_ptr) break; if (strlen(tok_ptr)) { tok_nonalpha = tok_ptr + strlen(tok_ptr) + 1; for (i = 0; i < strlen(tok_nonalpha); i++) { if (isalpha(tok_nonalpha[i])) break; } tok_nonalpha_len = i; syn = moby_get_syn(db, tok_ptr, opt); if (syn) { moby_transform_word(syn, tok_ptr); strlcat_bot(buf, syn); } else { strlcat_bot(buf, tok_ptr); } if (retchar != 0) { charcat_bot(buf, retchar); for (i = 0; i < tok_nonalpha_len; i++) { charcat_bot(buf, tok_nonalpha[i]); } } } tok_ptr = NULL; j++; } if (_sNULL(buf) != NULL) str = strdup(buf); return str; }
void quote_add_files_to_db_file(char *name) { DB *db = NULL; FILE *fp; char *name_db; char buf[1024]; char *tok_1 = NULL, *tok_2 = NULL; int count = 0; if (!name) return; name_db = _strrchr(name, '/'); if (!name_db) return; fp = fopen(name, "r"); if (!fp) return; name_db = str_unite_static("%s.quot", name); unlink(name_db); db = xdb_open(name_db); if (!db) goto cleanup; while (1) { _memset(buf, 0, sizeof(buf)); if (fgets(buf, sizeof(buf) - 1, fp) == NULL) break; _strstrip_nl(buf); tok_2 = _eat_whitespace(buf); if (!_sNULL(tok_2)) continue; str_shrink_quotes(tok_2); if (!_sNULL(tok_2)) continue; if (buf[0] == '#') continue; if (!_sNULL(buf)) continue; tok_1 = itocstr(count); if (!_sNULL(tok_1)) continue; xdb_write(db, tok_1, tok_2); count++; } cleanup: if (fp) fclose(fp); if (db) xdb_fini(db); return; }
/* open the text dict */ static xdict_t _xdict_open_txt(const char *fpath, int mode, unsigned char *ml) { xdict_t xd; xtree_t xt; char buf[XDICT_PATH_MAX], tmpfile[XDICT_PATH_MAX]; struct stat st1, st2; // check the input filepath _realpath(fpath, buf); if (stat(buf, &st1) < 0) return NULL; // check dest file & orginal file, compare there mtime #ifdef WIN32 { char *tmp_ptr; GetTempPath(sizeof(tmpfile) - 20, tmpfile); tmp_ptr = tmpfile + strlen(tmpfile); if (tmp_ptr[-1] == '\\') tmp_ptr--; sprintf(tmp_ptr, "\\scws-%08x.xdb", scws_crc32(buf)); } #else sprintf(tmpfile, "/tmp/scws-%08x.xdb", scws_crc32(buf)); #endif if (!stat(tmpfile, &st2) && st2.st_mtime > st1.st_mtime) { xdb_t x; if ((x = xdb_open(tmpfile, 'r')) != NULL) { xd = (xdict_t) malloc(sizeof(xdict_st)); memset(xd, 0, sizeof(xdict_st)); xd->ref = 1; if (mode & SCWS_XDICT_MEM) { /* convert the xdb(disk) -> xtree(memory) */ if ((xt = xdb_to_xtree(x, NULL)) != NULL) { xdb_close(x); xd->xdict = (void *) xt; xd->xmode = SCWS_XDICT_MEM; return xd; } } xd->xmode = SCWS_XDICT_XDB; xd->xdict = (void *) x; return xd; } } // create xtree if ((xt = xtree_new(0, 0)) == NULL) return NULL; else { int cl, kl; FILE *fp; word_st word, *w; char *key, *part, *last, *delim = " \t\r\n"; // re-build the xdb file from text file if ((fp = fopen(buf, "r")) == NULL) return NULL; // parse every line word.attr[2] = '\0'; while (fgets(buf, sizeof(buf) - 1, fp) != NULL) { // <word>[\t<tf>[\t<idf>[\t<attr>]]] if (buf[0] == ';' || buf[0] == '#') continue; key = _strtok_r(buf, delim, &last); if (key == NULL) continue; kl = strlen(key); // init the word do { word.tf = word.idf = 1.0; word.flag = SCWS_WORD_FULL; word.attr[0] = '@'; word.attr[1] = '\0'; if (!(part = _strtok_r(NULL, delim, &last))) break; word.tf = (float) atof(part); if (!(part = _strtok_r(NULL, delim, &last))) break; word.idf = (float) atof(part); if ((part = _strtok_r(NULL, delim, &last))) { word.attr[0] = part[0]; if (part[1]) word.attr[1] = part[1]; } } while (0); // save into xtree if ((w = xtree_nget(xt, key, kl, NULL)) == NULL) { w = (word_st *) pmalloc(xt->p, sizeof(word_st)); memcpy(w, &word, sizeof(word)); xtree_nput(xt, w, sizeof(word), key, kl); } else { w->tf = word.tf; w->idf = word.idf; w->flag |= word.flag; strcpy(w->attr, word.attr); } // parse the part cl = ml[(unsigned char) (key[0])]; while (1) { cl += ml[(unsigned char) (key[cl])]; if (cl >= kl) break; if ((w = xtree_nget(xt, key, cl, NULL)) != NULL) w->flag |= SCWS_WORD_PART; else { w = (word_st *) pmalloc_z(xt->p, sizeof(word_st)); w->flag = SCWS_WORD_PART; xtree_nput(xt, w, sizeof(word), key, cl); } } } fclose(fp); // optimize the xtree & save to xdb xtree_optimize(xt); unlink(tmpfile); xtree_to_xdb(xt, tmpfile); chmod(tmpfile, 0777); // return xtree xd = (xdict_t) malloc(sizeof(xdict_st)); memset(xd, 0, sizeof(xdict_st)); xd->ref = 1; xd->xdict = (void *) xt; xd->xmode = SCWS_XDICT_MEM; return xd; } }