1 /* index_utils.cc - utility functions for indexing testcase data
2  *
3  * Copyright (C) 2005,2007 Olly Betts
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19 
20 #include <config.h>
21 
22 #include "index_utils.h"
23 
24 #include "stringutils.h"
25 
26 #include <algorithm>
27 #include <cstring>
28 #include <fstream>
29 
30 #include "safeerrno.h"
31 
32 using namespace std;
33 
34 /// Read a paragraph from stream @a input.
35 static string
get_paragraph(istream & input)36 get_paragraph(istream &input)
37 {
38     string para, line;
39     while (true) {
40 	getline(input, line);
41 	if (find_if(line.begin(), line.end(), C_isnotspace) == line.end())
42 	    return para;
43 	para += line;
44 	para += '\n';
45     }
46 }
47 
48 Xapian::Document
next()49 FileIndexer::next()
50 {
51     if (input.eof()) next_file();
52 
53     Xapian::Stem stemmer("english");
54 
55     Xapian::Document doc;
56     string para = get_paragraph(input);
57     doc.set_data(para);
58 
59     // Value 0 contains all possible character values so we can check that
60     // none of them cause problems.
61     string value0("X\0\0\0 \1\t"
62 	"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
63 	"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
64 	"\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f"
65 	"\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f"
66 	"\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f"
67 	"\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f"
68 	"\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f"
69 	"\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f"
70 	"\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"
71 	"\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f"
72 	"\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf"
73 	"\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf"
74 	"\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf"
75 	"\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf"
76 	"\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef"
77 	"\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff",
78 	7 + 256);
79     if (para.size() > 2) value0[0] = para[2];
80     value0 += para;
81     doc.add_value(0, value0);
82 
83     for (Xapian::valueno i = min(para.length(), size_t(10)); i >= 1; --i) {
84 	doc.add_value(i, para.substr(i, 1));
85     }
86     // Value 11 is useful for tests of sorting
87     doc.add_value(11, Xapian::sortable_serialise(para.size()));
88 
89     // Value 12 is useful for tests of collapsing
90     doc.add_value(12, Xapian::sortable_serialise(para.size() % 5));
91 
92     // Value 13 contains the first 3 letters of the paragraph
93     doc.add_value(13, para.substr(0, 3));
94 
95     Xapian::termcount pos = 0;
96     string::const_iterator word_end = para.begin();
97     // Need a const_iterator version of para.end() for find_if.
98     const string::const_iterator para_end = para.end();
99     while (word_end != para_end) {
100 	string::const_iterator word_start;
101 	word_start = find_if(word_end, para_end, C_isnotspace);
102 	word_end = find_if(word_start, para_end, C_isspace);
103 	string word = stemmer(munge_term(string(word_start, word_end)));
104 	if (!word.empty()) doc.add_posting(word, ++pos);
105     }
106 
107     return doc;
108 }
109 
110 // Strip unwanted characters, force to lower case, and handle \ escapes.
111 string
munge_term(const string & term)112 munge_term(const string &term)
113 {
114     string result;
115     for (string::const_iterator i = term.begin(); i != term.end(); ++i) {
116 	char ch = *i;
117 	if (C_isalnum(ch))
118 	    result += C_tolower(ch);
119 	else if (ch == '\\') {
120 	    ++i;
121 	    if (i != term.end()) {
122 		switch (*i) {
123 		    case '\\': ch = '\\'; break;
124 		    case '0': ch = '\0'; break;
125 		    case 'n': ch = '\n'; break;
126 		    case 'r': ch = '\r'; break;
127 		    case 't': ch = '\t'; break;
128 		    case 'x': {
129 			// Check we can read the next two characters.
130 			if (size_t(i - term.begin()) >= term.size() - 2) {
131 			    --i;
132 			    break;
133 			}
134 			string::const_iterator j = i;
135 			char b = *++i;
136 			char c = *++i;
137 			if (!C_isxdigit(b) || !C_isxdigit(c)) {
138 			    i = j - 1;
139 			    break;
140 			}
141 			if (C_isdigit(b)) {
142 			    ch = b - '0';
143 			} else {
144 			    ch = C_tolower(b) - 'a' + 10;
145 			}
146 			ch *= 16;
147 			if (C_isdigit(c)) {
148 			    ch |= c - '0';
149 			} else {
150 			    ch |= C_tolower(c) - 'a' + 10;
151 			}
152 			break;
153 		    }
154 		}
155 	    }
156 	    result += ch;
157 	}
158     }
159     return result;
160 }
161 
162 void
next_file()163 FileIndexer::next_file()
164 {
165     if (input.is_open()) {
166 	input.close();
167 	// MSVC doesn't clear fail() on close() and re-open().
168 	input.clear();
169     }
170 
171     // Find the next non-empty filename.
172     while (file != end && (*file).empty()) {
173 	++file;
174     }
175     if (file == end) return;
176 
177     string filename;
178     if (!datadir.empty()) {
179 	filename = datadir;
180 	if (!endswith(datadir, '/')) filename += '/';
181     }
182     filename += *file++;
183     filename += ".txt";
184 
185     input.open(filename.c_str());
186     // Need to check is_open() - just using operator! fails with MSVC.
187     if (!input.is_open()) {
188 	string msg = "Can't read file '";
189 	msg += filename;
190 	msg += "' for indexing (";
191 	msg += strerror(errno);
192 	msg += ')';
193 	throw msg;
194     }
195 }
196