2. Documentation Library
2.1. doc.hh
#ifndef qef_INC_doc_HH_
#define qef_INC_doc_HH_
#include <string>
#include <vector>
#include <hash_map>
#include <stdexcept>
#include <cstdio>
#include <unistd.h>
#include <arpa/inet.h>
#include <gdbm.h>
namespace libdoc {
The port which the daemon listens on by default.
const unsigned short DEFAULT_PORT = 2317;
A general exception abstract base class.
class Exception : public runtime_error
{
public:
Exception (const string &msg) : runtime_error (msg) {}
virtual string explain () const
{ return what(); }
};
An exception which occurs when a library or system call which sets errno
fails.
class ExceptionSystemError : public Exception
{
private:
int err;
public:
ExceptionSystemError (const string &msg);
virtual string explain () const;
};
An exception for errors reported by the GDBM library.
class ExceptionGDBMError : public Exception
{
private:
gdbm_error err;
public:
ExceptionGDBMError (const string &msg);
virtual string explain () const;
};
An exception thrown by misuse of the Trie class below.
class ExceptionTrie : public Exception
{
public:
ExceptionTrie (const string &msg) : Exception (msg) {}
virtual string explain () const
{ return what(); }
};
An exception thrown by misuse of the Table class in table.hh.
class ExceptionTable : public Exception
{
public:
ExceptionTable (const string &msg) : Exception (msg) {}
virtual string explain () const
{ return what(); }
};
A chomp function, like the one in Perl.
void chomp (string &s);
Split a string into substrings at single character delimiters, similar to
the Perl `split' function. The substrings are appended to results.
void split (vector <string> &results, const string &s, char delim);
Return true if the end of s matches ext.
bool check_extension (const string &s, const string &ext);
A class for building and using trie structures which can index words (made
up entirely of English letters, case insensitive). It is only efficient
for very short words.
This is not actually used.
class Trie
{
protected:
struct TrieNode
{
TrieNode *children[26];
unsigned long endmask;
};
TrieNode *root;
static void delete_trienode (TrieNode *t);
static TrieNode *make_new_trienode ();
public:
Trie () : root (0) {};
virtual ~Trie ()
{ delete_trienode (root); }
void add_word (const string &w);
bool check_word (const string &w) const;
void extract_words (vector <string> &words) const;
void print_debug () const;
void print_debug (TrieNode *node, unsigned int indent) const;
};
This is used by `DocumentSender' to build a map with string keys.
struct eqstr
{
bool operator()(const char* s1, const char* s2) const
{
return strcmp(s1, s2) == 0;
}
};
A class for sending indexing information about a document to docd.
class DocumentSender
{
private:
string path, title, date, manual;
FILE *dest;
const string format;
bool sent;
This hash is used to keep track of the number of occurrences of each
word, which is done client-side to avoid sending words repetitively.
hash_map <const char *, int, hash <const char *>, eqstr> words;
words_buffer stores words which are queued to be transmitted to the
daemon, and queue_words is `true' if words should be queued
here or `false' if we are ready to transmit them already.
public:
DocumentSender (const string &filename, FILE *stream, const string &frm)
: path (filename), dest (stream), format (frm), sent (false) {}
DocumentSender (const string &filename, int sock, const string &frm);
~DocumentSender ();
void set_header_info (const string &thetitle, const string &thedate,
const string &themanual);
void set_title (const string &thetitle)
{ title = thetitle; }
void add_word (const string &word);
void send_document ();
void send_word (const string &word);
};
A single `result', which is everything we need to know about a document which
matches a search.
struct SearchResult
{
string document_filename;
string document_title;
string section_title;
string section_ref;
string format_name;
string format_icon;
string format_viewers;
};
Send a search query to the socket s and wait for the results, which
are put into sr.
void perform_search (int s, const string &query, vector <SearchResult> &sr);
Networking stuff.
#ifndef MAXHOSTNAMELEN
const int MAXHOSTNAMELEN = 256;
#endif
in_addr find_hostip ();
This is here because ANSI doesn't define it.
char *x_strdup (const char *s);
Stuff for reading from and writing to sockets.
int open_connection_to_docd (const char *hostname, int port);
void sock_read_string (int sock, string &s);
void sock_send_str (int s, const string &str);
void sock_send_str_nl (int s, const string &str);
void sock_send_char (int s, char c);
}
#endif