plaintorich test main

This commit is contained in:
Jean-Francois Dockes 2021-11-02 09:11:36 +01:00
parent 00085e70a3
commit fa790f52de
2 changed files with 152 additions and 1 deletions

View File

@ -38,7 +38,7 @@ AM_CPPFLAGS = -Wall -Wno-unused -std=c++11 \
-D_GNU_SOURCE \
$(DEFS)
noinst_PROGRAMS = textsplit utf8iter fstreewalk rclconfig hldata unac mbox \
noinst_PROGRAMS = plaintorich textsplit utf8iter fstreewalk rclconfig hldata unac mbox \
circache wipedir mimetype pathut fileudi x11mon trqrstore ecrontab
ecrontab_SOURCES = trecrontab.cpp
@ -71,6 +71,9 @@ rclconfig_LDADD = ../librecoll.la
textsplit_SOURCES = trtextsplit.cpp
textsplit_LDADD = ../librecoll.la
plaintorich_SOURCES = trplaintorich.cpp
plaintorich_LDADD = ../librecoll.la
unac_SOURCES = trunac.cpp
unac_LDADD = ../librecoll.la

View File

@ -0,0 +1,148 @@
/* Copyright (C) 2021-2021 J.F.Dockes
*
* License: LGPL 2.1
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
// A test driver for the plaintorich highlighting module.
// Very incomplete yet, nothing about groups
#include "autoconfig.h"
#include "plaintorich.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include "readfile.h"
#include "log.h"
using namespace std;
#define OPT_H 0x1
#define OPT_L 0x2
#define OPT_t 0x4
static string thisprog;
static string usage =
" plaintorich [opts] [filename]\n"
" -H : input is html\n"
" -L <loglevel> : set log level 0-6\n"
" -t term : add term to match. Must in index form (lowercase/unac)\n";
;
static void
Usage(void)
{
cerr << thisprog << ": usage:\n" << usage;
exit(1);
}
static int op_flags;
class TstPlainToRich : public PlainToRich {
public:
virtual string startMatch(unsigned int idx) {
(void)idx;
#if 0
if (m_hdata) {
string s1, s2;
stringsToString<vector<string> >(m_hdata->groups[idx], s1);
stringsToString<vector<string> >(
m_hdata->ugroups[m_hdata->grpsugidx[idx]], s2);
LOGDEB2("Reslist startmatch: group " << s1 << " user group " <<
s2 << "\n");
}
#endif
return "<SPAN class='RCLMATCH'>";
}
virtual string endMatch() {
return "</SPAN>";
}
};
int main(int argc, char **argv)
{
int loglevel = 4;
HighlightData hldata;
bool ishtml{false};
thisprog = argv[0];
argc--; argv++;
while (argc > 0 && **argv == '-') {
(*argv)++;
if (!(**argv))
/* Cas du "adb - core" */
Usage();
while (**argv)
switch (*(*argv)++) {
case 'H': ishtml = true; break;
case 'L': op_flags |= OPT_L; if (argc < 2) Usage();
loglevel = atoi(*(++argv)); argc--; goto b1;
case 't': {
if (argc < 2) Usage();
HighlightData::TermGroup tg;
tg.term = *(++argv); argc--;
hldata.index_term_groups.push_back(tg);
goto b1;
}
default: Usage(); break;
}
b1: argc--; argv++;
}
Logger::getTheLog("")->setLogLevel(Logger::LogLevel(loglevel));
string inputdata, reason;
if (argc == 1) {
const char *filename = *argv++; argc--;
if (!file_to_string(filename, inputdata, &reason)) {
cerr << "Failed: file_to_string(" << filename << ") failed: " << reason << endl;
exit(1);
}
} else if (argc == 0) {
char buf[1024];
int nread;
while ((nread = read(0, buf, 1024)) > 0) {
inputdata.append(buf, nread);
}
} else {
Usage();
}
TstPlainToRich hiliter;
if (ishtml) {
hiliter.set_inputhtml(true);
}
std::list<std::string> result;
hiliter.plaintorich(inputdata, result, hldata);
std::cout << *result.begin() << "\n";
return 0;
}