tests/harness/index_utils.cc

Go to the documentation of this file.
00001 /* index_utils.cc - utility functions for indexing testcase data
00002  *
00003  * Copyright (C) 2005,2007 Olly Betts
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  */
00019 
00020 #include <config.h>
00021 
00022 #include "index_utils.h"
00023 
00024 #include "stringutils.h"
00025 
00026 #include <algorithm>
00027 #include <cstring>
00028 #include <fstream>
00029 
00030 #include "safeerrno.h"
00031 
00032 using namespace std;
00033 
00035 static string
00036 get_paragraph(istream &input)
00037 {
00038     string para, line;
00039     while (true) {
00040         getline(input, line);
00041         if (find_if(line.begin(), line.end(), C_isnotspace) == line.end())
00042             return para;
00043         para += line;
00044         para += '\n';
00045     }
00046 }
00047 
00048 Xapian::Document
00049 FileIndexer::next()
00050 {
00051     if (input.eof()) next_file();
00052 
00053     Xapian::Stem stemmer("english");
00054 
00055     Xapian::Document doc;
00056     string para = get_paragraph(input);
00057     doc.set_data(para);
00058 
00059     // Value 0 contains all possible character values so we can check that
00060     // none of them cause problems.
00061     string value0("X\0\0\0 \1\t"
00062         "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
00063         "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
00064         "\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f"
00065         "\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f"
00066         "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f"
00067         "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f"
00068         "\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f"
00069         "\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f"
00070         "\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"
00071         "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f"
00072         "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf"
00073         "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf"
00074         "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf"
00075         "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf"
00076         "\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef"
00077         "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff",
00078         7 + 256);
00079     if (para.size() > 2) value0[0] = para[2];
00080     value0 += para;
00081     doc.add_value(0, value0);
00082 
00083     for (Xapian::valueno i = min(para.length(), size_t(10)); i >= 1; --i) {
00084         doc.add_value(i, para.substr(i, 1));
00085     }
00086 
00087     Xapian::termcount pos = 0;
00088     string::const_iterator word_end = para.begin();
00089     // Need a const_iterator version of para.end() for find_if.
00090     const string::const_iterator para_end = para.end();
00091     while (word_end != para_end) {
00092         string::const_iterator word_start;
00093         word_start = find_if(word_end, para_end, C_isnotspace);
00094         word_end = find_if(word_start, para_end, C_isspace);
00095         string word = stemmer(munge_term(string(word_start, word_end)));
00096         if (!word.empty()) doc.add_posting(word, ++pos);
00097     }
00098 
00099     return doc;
00100 }
00101 
00102 // Strip unwanted characters, force to lower case, and handle \ escapes.
00103 string
00104 munge_term(const string &term)
00105 {
00106     string result;
00107     for (string::const_iterator i = term.begin(); i != term.end(); ++i) {
00108         char ch = *i;
00109         if (C_isalnum(ch))
00110             result += C_tolower(ch);
00111         else if (ch == '\\') {
00112             ++i;
00113             if (i != term.end()) {
00114                 switch (*i) {
00115                     case '\\': ch = '\\'; break;
00116                     case '0': ch = '\0'; break;
00117                     case 'n': ch = '\n'; break;
00118                     case 'r': ch = '\r'; break;
00119                     case 't': ch = '\t'; break;
00120                     case 'x': {
00121                         // Check we can read the next two characters.
00122                         if (size_t(i - term.begin()) >= term.size() - 2) {
00123                             --i;
00124                             break;
00125                         }
00126                         string::const_iterator j = i;
00127                         char b = *++i;
00128                         char c = *++i;
00129                         if (!C_isxdigit(b) || !C_isxdigit(c)) {
00130                             i = j - 1;
00131                             break;
00132                         }
00133                         if (C_isdigit(b)) {
00134                             ch = b - '0';
00135                         } else {
00136                             ch = C_tolower(b) - 'a' + 10;
00137                         }
00138                         ch *= 16;
00139                         if (C_isdigit(c)) {
00140                             ch |= c - '0';
00141                         } else {
00142                             ch |= C_tolower(c) - 'a' + 10;
00143                         }
00144                         break;
00145                     }
00146                 }
00147             }
00148             result += ch;
00149         }
00150     }
00151     return result;
00152 }
00153 
00154 void
00155 FileIndexer::next_file()
00156 {
00157     if (input.is_open()) {
00158         input.close();
00159         // MSVC doesn't clear fail() on close() and re-open().
00160         input.clear();
00161     }
00162 
00163     // Find the next non-empty filename.
00164     while (file != end && (*file).empty()) {
00165         ++file;
00166     }
00167     if (file == end) return;
00168 
00169     string filename;
00170     if (!datadir.empty()) {
00171         filename = datadir;
00172         if (!endswith(datadir, '/')) filename += '/';
00173     }
00174     filename += *file++;
00175     filename += ".txt";
00176 
00177     input.open(filename.c_str());
00178     // Need to check is_open() - just using operator! fails with MSVC.
00179     if (!input.is_open()) {
00180         string msg = "Can't read file '";
00181         msg += filename;
00182         msg += "' for indexing (";
00183         msg += strerror(errno);
00184         msg += ')';
00185         throw msg;
00186     }
00187 }

Documentation for Xapian (version 1.0.10).
Generated on 24 Dec 2008 by Doxygen 1.5.2.