--- trunk/perl/HyperEstraierWrapper.cpp 2005/09/03 18:44:31 4 +++ trunk/perl/HyperEstraierWrapper.cpp 2005/09/03 20:00:11 6 @@ -9,6 +9,7 @@ #include #include #include +#include /* backward compatibility for 0.5.4 */ #ifndef ESTCONDAGITO @@ -16,6 +17,12 @@ #endif namespace estraier { + + class IOError : public std::runtime_error { + public: + explicit IOError (const std::string& w) : std::runtime_error(w) {} + }; + class Condition { public: enum { // enumeration for options @@ -76,7 +83,6 @@ std::string text_buf; public: ESTDOC *doc; - Document() { /** * constructor @@ -147,8 +153,7 @@ /** * get a list of sentences of the text of a document object */ - // return est_doc_cat_texts(doc); - return "This is mockup!"; + return est_doc_cat_texts(doc); } std::vector* texts() { /** @@ -174,24 +179,26 @@ */ CBLIST * words; std::vector::iterator iter; - words = cblistopen(); - for (iter = _words.begin(); _words.end() != iter; iter++) { cblistpush(words, iter->c_str(), -1); } - const char *result = est_doc_make_snippet(doc, words, wwidth, hwidth, awidth); - cblistclose(words); - return result; } + const char * hidden_texts() { + /** + * get the hidden texts of a document object. + */ + return est_doc_hidden_texts(doc); + } }; class Database { private: ESTMTDB *db; + int ecode; public: enum { // enumeration for error codes ERRNOERR = ESTENOERR, // no error @@ -230,41 +237,47 @@ /** * constructor(dummy) */ + db = NULL; + ecode = ERRNOERR; } ~Database() { - close(); + if (db) close(); } bool open(const char * dbname, int mode) { /** * open the database */ - int ecode; - db = est_mtdb_open(dbname, mode, &ecode); + if (db) close(); + int ec; + db = est_mtdb_open(dbname, mode, &ec); + if (!db) ecode = ec; return db; } bool close() { /** * close the database */ - if (db) { - int ecode; - bool result = est_mtdb_close(db, &ecode); - db = NULL; - return result; - } else { - return false; - } + if (!db) throw IOError("closed database"); + int ec; + bool result = est_mtdb_close(db, &ec); + if (!result) ecode = ec; + db = NULL; + return result; } bool put_doc(Document *doc, int options) { /** * add a document to a database */ - return est_mtdb_put_doc(db, doc->doc, options); + if (!db) throw IOError("closed database"); + bool result = est_mtdb_put_doc(db, doc->doc, options); + if (!result) ecode = est_mtdb_error(db); + return result; } std::vector * search(Condition * cond, int options) { /** * search documents corresponding a condition for a database */ + if (!db) throw IOError("closed database"); int resnum; int * result = est_mtdb_search(db, cond->cond, &resnum, NULL); std::vector *numbers = new std::vector; @@ -283,44 +296,68 @@ /** * get the last happended error code of a database */ - return est_mtdb_error(db); + return ecode; } bool fatal() { /** * check whether a database has a fatal error */ + if (!db) throw IOError("closed database"); return est_mtdb_fatal(db); } bool flush(int _max) { /** * flush index words in the cache of a database */ - return est_mtdb_flush(db, _max); + if (!db) throw IOError("closed database"); + bool result = est_mtdb_flush(db, _max); + if (!result) ecode = est_mtdb_error(db); + return result; } bool sync() { /** * synchronize updating contents of a database */ - return est_mtdb_sync(db); + if (!db) throw IOError("closed database"); + bool result = est_mtdb_sync(db); + if (!result) ecode = est_mtdb_error(db); + return result; } bool optimize(int options) { /** * optimize a database */ - return est_mtdb_optimize(db, options); + if (!db) throw IOError("closed database"); + bool result = est_mtdb_optimize(db, options); + if (!result) ecode = est_mtdb_error(db); + return result; } bool out_doc(int id, int options) { /** * remove a document from a database */ - return est_mtdb_out_doc(db, id, options); + if (!db) throw IOError("closed database"); + bool result = est_mtdb_out_doc(db, id, options); + if (!result) ecode = est_mtdb_error(db); + return result; + } + bool edit_doc(Document *doc) { + /** + * edit an attribute of a document in a database + */ + if (!db) throw IOError("closed database"); + bool result = est_mtdb_edit_doc(db, doc->doc); + if (!result) ecode = est_mtdb_error(db); + return result; } Document * get_doc(int id, int options) { /** * retrieve a document in a database */ + if (!db) throw IOError("closed database"); ESTDOC *doc = est_mtdb_get_doc(db, id, options); if (!doc) { + ecode = est_mtdb_error(db); throw est_err_msg(est_mtdb_error(db)); } else { return new Document(doc); @@ -330,16 +367,18 @@ /** * get the ID of a document spacified by URI */ - return est_mtdb_uri_to_id(db, uri); + if (!db) throw IOError("closed database"); + int result = est_mtdb_uri_to_id(db, uri); + if(result == -1) ecode = est_mtdb_error(db); + return result; } std::map * etch_doc(Document * doc, int max) { /** * extract keywords of a document object */ + if (!db) throw IOError("closed database"); std::map * mss = new std::map; - CBMAP * keys = est_mtdb_etch_doc(db, doc->doc, max); - cbmapiterinit(keys); int ksiz; while (const char *key = cbmapiternext(keys, &ksiz)) { @@ -347,46 +386,39 @@ } return mss; } - bool iter_init() { - /** - * initialize the iterator of a database - */ - return est_mtdb_iter_init(db); - } - int iter_next() { - /** - * get the next ID of the iterator of a database - */ - return est_mtdb_iter_next(db); - } const char * name() { /** * get the name of a database */ + if (!db) throw IOError("closed database"); return est_mtdb_name(db); } int doc_num() { /** * get the number of documents in a database */ + if (!db) throw IOError("closed database"); return est_mtdb_doc_num(db); } int word_num() { /** * get the number of unique words in a database */ + if (!db) throw IOError("closed database"); return est_mtdb_word_num(db); } double size() { /** * get the size of a database */ + if (!db) throw IOError("closed database"); return est_mtdb_size(db); } void set_cache_size(size_t size, int anum, int tnum) { /** * set the maximum size of the cache memory of a database */ + if (!db) throw IOError("closed database"); est_mtdb_set_cache_size(db, size, anum, tnum); } void set_special_cache(const char *name, int num) { @@ -397,4 +429,29 @@ est_mtdb_set_special_cache(db, name, num); } }; + + static std::vector * break_text(const char *text, bool norm, bool tail) { + std::vector * vs = new std::vector; + CBLIST *list; + list = cblistopen(); + est_break_text(text, list, norm, tail); + for (int i=0; i < cblistnum(list); i++) { + vs->push_back(cblistval(list, i, NULL)); + } + cblistclose(list); + return vs; + } + + static std::vector * break_text_perfng(const char *text, bool norm, bool tail) { + std::vector * vs = new std::vector; + CBLIST *list; + list = cblistopen(); + est_break_text_perfng(text, list, norm, tail); + for (int i=0; i < cblistnum(list); i++) { + vs->push_back(cblistval(list, i, NULL)); + } + cblistclose(list); + return vs; + } + };