/[hyperestraier_wrappers]/trunk/perl/HyperEstraierWrapper.cpp
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/perl/HyperEstraierWrapper.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 6 by dpavlin, Sat Sep 3 20:00:11 2005 UTC revision 12 by dpavlin, Thu Sep 8 22:51:10 2005 UTC
# Line 10  Line 10 
10  #include <map>  #include <map>
11  #include <cassert>  #include <cassert>
12  #include <stdexcept>  #include <stdexcept>
13    #include <estnode.h>
14    
15  /* backward compatibility for 0.5.4 */  /* backward compatibility for 0.5.4 */
16  #ifndef ESTCONDAGITO  #ifndef ESTCONDAGITO
# Line 454  namespace estraier { Line 455  namespace estraier {
455                  return vs;                  return vs;
456          }          }
457    
458            class ResultDocument {
459            public:
460                    ESTRESDOC *rdoc;
461                    ResultDocument(ESTRESDOC *_rdoc) {
462                            rdoc = _rdoc;
463                    }
464                    const char *uri(void) {
465                            return est_resdoc_uri(rdoc);
466                    }
467                    std::vector<std::string> * attr_names() {
468                            std::vector<std::string> * vs = new std::vector<std::string>;
469                            CBLIST * attr_names = est_resdoc_attr_names(rdoc);
470                            for (int i=0; i < cblistnum(attr_names); i++) {
471                                    vs->push_back(cblistval(attr_names, i, NULL));
472                            }
473                            cblistclose(attr_names);
474                            return vs;
475                    }
476                    const char *attr(const char *name) {
477                            return est_resdoc_attr(rdoc, name);
478                    }
479                    const char *snippet(void) {
480                            return est_resdoc_snippet(rdoc);
481                    }
482            };
483    
484            class NodeRes {
485            private:
486                    ESTNODERES *nres;
487            public:
488                    NodeRes(ESTNODE *node, Condition *cond, int depth) {
489                            nres = est_node_search(node, cond->cond, depth);
490                    }
491                    ~NodeRes() {
492                            est_noderes_delete(nres);
493                    }
494                    std::map<std::string, std::string> * hints(void) {
495                            std::map<std::string, std::string> * hints = new std::map<std::string, std::string>;
496                            CBMAP * keys = est_noderes_hints(nres);
497                            cbmapiterinit(keys);
498                            int ksiz;
499                            while (const char *key = cbmapiternext(keys, &ksiz)) {
500                                    hints->insert(std::make_pair(key, cbmapget(keys, key, ksiz, NULL)));
501                            }
502                            return hints;
503                    }
504                    int doc_num(void) {
505                            return est_noderes_doc_num(nres);
506                    }
507                    ResultDocument * get_doc(int index) {
508                            ESTRESDOC *rdoc = est_noderes_get_doc(nres, index);
509                            if (rdoc) {
510                                    return new ResultDocument(rdoc);
511                            } else {
512                                    throw IOError("wtf? no document?");
513                                    return NULL;
514                            }
515                    }
516            };
517    
518            class Node {
519            private:
520                    ESTNODE *node;
521                    int netenv_ok;
522            public:
523                    Node(const char *url) {
524                            netenv_ok = est_init_net_env();
525                            if (! netenv_ok) throw IOError("can't init net env");
526                            node = est_node_new(url);
527                            if (! node) throw IOError("can't create node");
528                    }
529                    ~Node() {
530                            est_node_delete(node);
531                            est_free_net_env();
532                    }
533                    void set_proxy(const char *host, int port) {
534                            est_node_set_proxy(node, host, port);
535                    }
536                    void set_timeout(int sec) {
537                            est_node_set_timeout(node, sec);
538                    }
539                    void set_auth(const char *name, const char *passwd) {
540                            est_node_set_auth(node, name, passwd);
541                    }
542                    int status(void) {
543                            return est_node_status(node);
544                    }
545                    bool put_doc(Document *doc) {
546                            return est_node_put_doc(node, doc->doc);
547                    }
548                    bool out_doc(int id) {
549                            return est_node_out_doc(node, id);
550                    }
551                    bool out_doc_by_uri(const char *uri) {
552                            return est_node_out_doc_by_uri(node, uri);
553                    }
554    #ifdef est_node_edit_doc
555                    bool edit_doc(Document *doc) {
556                            return est_node_edit_doc(node, doc->doc);
557                    }
558    #endif
559                    Document * get_doc(int id) {
560                            ESTDOC *doc = est_node_get_doc(node, id);
561                            if (!doc) {
562                                    return NULL;
563                            } else {
564                                    return new Document(doc);
565                            }
566                    }
567                    Document * get_doc_by_uri(const char *uri) {
568                            ESTDOC *doc = est_node_get_doc_by_uri(node, uri);
569                            if (!doc) {
570                                    return NULL;
571                            } else {
572                                    return new Document(doc);
573                            }
574                    }
575                    char * get_doc_attr(int id, const char *name) {
576                            /* is this leeking memory? shouldn't I create
577                             * object and free memory region returned?
578                             */
579                            return est_node_get_doc_attr(node, id, name);
580                    }
581                    char * get_doc_attr_by_uri(const char *uri, const char *name) {
582                            return est_node_get_doc_attr_by_uri(node, uri, name);
583                    }
584                    int uri_to_id(const char *uri) {
585                            return est_node_uri_to_id(node, uri);
586                    }
587                    const char * name(void) {
588                            return est_node_name(node);
589                    }
590                    const char * label(void) {
591                            return est_node_label(node);
592                    }
593                    int doc_num(void) {
594                            return est_node_doc_num(node);
595                    }
596                    int word_num(void) {
597                            return est_node_word_num(node);
598                    }
599                    double size(void) {
600                            return est_node_size(node);
601                    }
602                    NodeRes * search(Condition *cond, int depth) {
603                            return new NodeRes(node, cond, depth);
604                    }
605                    int set_user(const char *name, int mode) {
606                            return est_node_set_user(node, name, mode);
607                    }
608                    int set_link(const char *url, const char *label, int credit) {
609                            return est_node_set_link(node, url, label, credit);
610                    }
611            };
612    
613  };  };

Legend:
Removed from v.6  
changed lines
  Added in v.12

  ViewVC Help
Powered by ViewVC 1.1.26