44 #define HEX2DEC(x) (((x)>='0' && (x)<='9')?((x)-'0'):\ 45 ((x)>='a' && (x)<='f')?((x)-'a'+10):\ 46 ((x)>='A' && (x)<='F')?((x)-'A'+10):-1) 52 DWORD ftyp = GetFileAttributesA(dirName.c_str());
53 if (ftyp == INVALID_FILE_ATTRIBUTES)
56 if (ftyp & FILE_ATTRIBUTE_DIRECTORY)
61 if (
stat(dirName.c_str(), &sb)==0 && S_ISDIR(sb.st_mode))
78 const unsigned char * pSrc = (
const unsigned char *)sSrc.c_str();
79 const int SRC_LEN = sSrc.length();
80 const unsigned char *
const SRC_END = pSrc + SRC_LEN;
82 const unsigned char *
const SRC_LAST_DEC = SRC_END - 2;
84 char *
const pStart =
new char[SRC_LEN];
87 while (pSrc < SRC_LAST_DEC)
92 unsigned char c1=*(pSrc+1);
93 unsigned char c2=*(pSrc+2);
97 *pEnd++ = (dec1 << 4) + dec2;
102 else if (*pSrc ==
'+')
104 *pEnd++ =
' '; pSrc++;
111 while (pSrc < SRC_END) *pEnd++ = *pSrc++;
123 std::vector<std::string> elems;
124 std::stringstream ss(s);
126 while (getline(ss, item, delim)) elems.push_back(item);
134 std::istringstream
stream (s);
186 it!=ranges.end(); ++it
190 if (start>=r.
start && start+len<r.
end)
203 const std::vector<std::string> &words,
204 std::vector<Fragment> &fragments)
209 const int fragLen = 60;
214 std::vector<WordPosition> positions;
223 while ((i=s.find(word,pos))!=std::string::npos)
232 std::vector<Range>
ranges;
245 fragments.push_back(
Fragment(spanStart+w+spanEnd,1));
246 ranges.push_back(
Range(i,i+wl));
251 int bi=i-(fragLen-wl)/2;
252 int ei=i+wl+(fragLen-wl)/2;
254 if (bi<0) { ei-=bi; bi=0; }
else startFragment=dots;
255 if (ei>sl) { ei=sl; }
else endFragment=dots;
256 while (bi>0 && !isspace(s[bi])) bi--;
257 while (ei<sl && !isspace(s[ei])) ei++;
262 it2!=positions.end();
267 int wl2 = w2.length();
270 fragment+=s.substr(pos,wp2.
start-pos)+
272 s.substr(wp2.
start,wl2)+
278 fragment+=s.substr(pos,ei-pos)+endFragment;
279 fragments.push_back(
Fragment(fragment,occ));
280 ranges.push_back(
Range(bi,ei));
290 std::stringstream dst;
291 for (
unsigned int i=0;i<s.length();i++)
296 case '\"': dst <<
"\\\"";
break;
297 default: dst << ch;
break;
305 std::cout << callback <<
"({\"error\":\"" << error <<
"\"})";
313 std::cout <<
"Content-Type:application/javascript;charset=utf-8\r\n\n";
318 const char *queryEnv =
getenv(
"QUERY_STRING");
322 queryString = queryEnv;
326 queryString = argv[1];
330 std::cout <<
"No input!\n";
335 std::vector<std::string> parts =
split(queryString,
'&');
340 std::vector<std::string> kv =
split(*it,
'=');
344 if (kv[0]==
"q") searchFor =
val;
345 else if (kv[0]==
"n") num = fromString<int>(
val);
346 else if (kv[0]==
"p") page = fromString<int>(
val);
347 else if (kv[0]==
"cb") callback =
val;
353 if (queryString==
"test")
358 std::cout <<
"Test successful.";
362 std::cout <<
"Test failed: cannot find search index " << indexDir;
369 Xapian::Enquire enquire(db);
371 std::vector<std::string> words =
split(searchFor,
' ');
374 query = Xapian::Query(Xapian::Query::OP_OR,query,Xapian::Query(*it));
376 enquire.set_query(query);
379 Xapian::MSet
matches = enquire.get_mset(page*num,num);
380 unsigned int hits = matches.get_matches_estimated();
381 unsigned int offset = page*
num;
382 unsigned int pages = num>0 ? (hits+num-1)/num : 0;
383 if (offset>hits) offset=hits;
384 if (offset+num>hits) num=hits-offset;
387 std::cout << callback.c_str() <<
"(";
389 <<
" \"hits\":" << hits <<
"," <<
std::endl 390 <<
" \"first\":" << offset <<
"," <<
std::endl 391 <<
" \"count\":" << num <<
"," <<
std::endl 392 <<
" \"page\":" << page <<
"," <<
std::endl 393 <<
" \"pages\":" << pages <<
"," <<
std::endl 397 unsigned int o = offset;
398 for (Xapian::MSetIterator i = matches.begin(); i != matches.end(); ++i,++o)
400 std::vector<Fragment> hl;
401 Xapian::Document
doc = i.get_document();
403 std::cout <<
" {\"type\": \"" << doc.get_value(
FIELD_TYPE) <<
"\"," << std::endl
405 <<
" \"tag\": \"" << doc.get_value(
FIELD_TAG) <<
"\"," << std::endl
407 std::cout <<
" \"fragments\":[" <<
std::endl;
412 if (!first) std::cout <<
"," <<
std::endl;
419 if (o<offset+num-1) std::cout <<
",";
422 std::cout <<
" ]" << std::endl <<
"})" <<
std::endl;
430 showError(callback,
"Unknown Exception!");
static std::string escapeString(const std::string &s)
end
while True: pbar.update(maxval-len(onlies[E][S])) #print iS, "/", len(onlies[E][S]) found = False for...
static void highlighter(const std::string &s, const std::vector< std::string > &words, std::vector< Fragment > &fragments)
static std::string uriDecode(const std::string &sSrc)
T fromString(const std::string &s)
nvidia::inferenceserver::client::Error Error
std::string getenv(std::string const &name)
static std::vector< std::string > split(const std::string &s, char delim)
Fragment(const std::string &t, int occ)
static void showError(const std::string &callback, const std::string &error)
bool dirExists(const std::string &dirName)
bool operator()(const Fragment &p1, const Fragment &p2)
static bool insideRange(const std::vector< Range > &ranges, int start, int len)
int main(int argc, char **argv)
query_result< Args... > query(sqlite3 *db, std::string const &ddl)
WordPosition(int s, int i)
bool operator()(const WordPosition &p1, const WordPosition &p2)
union ptb::content::word::word word
QTextStream & endl(QTextStream &s)