Fixed CTPP2 not working

* Fixed templates
* Changed behavior of getHtml() to use higher level VM API
This commit is contained in:
renaud gaudin 2013-11-13 19:58:56 +00:00
parent d2a2802897
commit 2a8d7fde56
5 changed files with 3371 additions and 26503 deletions

View File

@ -19,6 +19,7 @@
#include "searcher.h" #include "searcher.h"
namespace kiwix { namespace kiwix {
/* Constructor */ /* Constructor */
@ -30,13 +31,17 @@ namespace kiwix {
estimatedResultCount(0), estimatedResultCount(0),
resultStart(0), resultStart(0),
resultEnd(0), resultEnd(0),
resultRange(20) { resultRange(20),
template_ct2("_ctpp2results.ct2")
{
if (!fileExists(template_ct2)) {
writeTextFile(template_ct2.c_str(), getResourceAsString("results.ct2"));
}
} }
/* Search strings in the database */ /* Search strings in the database */
void Searcher::search(std::string &search, const unsigned int resultStart, void Searcher::search(std::string &search, const unsigned int resultStart,
const unsigned int resultEnd, const bool verbose) { const unsigned int resultEnd, const bool verbose) {
this->reset(); this->reset();
if (verbose == true) { if (verbose == true) {
@ -108,44 +113,7 @@ namespace kiwix {
string Searcher::getHtml() { string Searcher::getHtml() {
VMOpcodeCollector oVMOpcodeCollector; SimpleVM oSimpleVM;
StaticText oSyscalls;
StaticData oStaticData;
StaticText oStaticText;
HashTable oHashTable;
CTPP2Compiler oCompiler(oVMOpcodeCollector, oSyscalls, oStaticData, oStaticText, oHashTable);
/* Load template & create template parser */
// cout << getResourceAsString("results.tmpl") << endl;
/* Parse template */
const STLW::string & sSourceFile = getResourceAsString("results.tmpl");
CTPP2TextLoader oSourceLoader;
oSourceLoader.LoadTemplate(sSourceFile.c_str());
CTPP2Parser oCTPP2Parser(&oSourceLoader, &oCompiler, "template");
oCTPP2Parser.Compile();
// Get program core
UINT_32 iCodeSize = 0;
const VMInstruction * oVMInstruction = oVMOpcodeCollector.GetCode(iCodeSize);
// Dump program
VMDumper oDumper(iCodeSize, oVMInstruction, oSyscalls, oStaticData, oStaticText, oHashTable);
UINT_32 iSize = 0;
const VMExecutable * aProgramCore = oDumper.GetExecutable(iSize);
// Memory core
const VMMemoryCore vm_core(aProgramCore);
// Initiate the VM
SyscallFactory oSyscallFactory(100);
// Load standard library
STDLibInitializer::InitLibrary(oSyscallFactory);
VM * pVM = new VM(&oSyscallFactory);
// Initiate the logger
FileLogger oLogger(stderr);
// Fill data // Fill data
CDT oData; CDT oData;
@ -159,10 +127,10 @@ namespace kiwix {
result["snippet"] = this->resultOffset->snippet; result["snippet"] = this->resultOffset->snippet;
if (this->resultOffset->size >= 0) if (this->resultOffset->size >= 0)
result["size"] = kiwix::beautifyInteger(this->resultOffset->size); result["size"] = kiwix::beautifyInteger(this->resultOffset->size);
if (this->resultOffset->wordCount >= 0) if (this->resultOffset->wordCount >= 0)
result["wordCount"] = kiwix::beautifyInteger(this->resultOffset->wordCount); result["wordCount"] = kiwix::beautifyInteger(this->resultOffset->wordCount);
resultsCDT.PushBack(result); resultsCDT.PushBack(result);
this->resultOffset++; this->resultOffset++;
@ -188,7 +156,7 @@ namespace kiwix {
page["end"] = (i+1) * this->resultCountPerPage; page["end"] = (i+1) * this->resultCountPerPage;
if (i * this->resultCountPerPage == this->resultStart) if (i * this->resultCountPerPage == this->resultStart)
page["selected"] = true; page["selected"] = true;
pagesCDT.PushBack(page); pagesCDT.PushBack(page);
} }
@ -205,15 +173,28 @@ namespace kiwix {
oData["searchProtocolPrefix"] = this->searchProtocolPrefix; oData["searchProtocolPrefix"] = this->searchProtocolPrefix;
oData["contentId"] = this->contentHumanReadableId; oData["contentId"] = this->contentHumanReadableId;
STLW::string sResult; // Load template file
StringOutputCollector oDataCollector(sResult); VMFileLoader oLoader(this->template_ct2.c_str());
// Run VM // Create logger object
pVM->Init(&vm_core, &oDataCollector, &oLogger); FileLogger oLogger(stderr);
UINT_32 iIP = 0;
pVM -> Run(&vm_core, &oDataCollector, iIP, oData, &oLogger); // Execute template and write data to standard output
oSimpleVM.Run(oData, oLoader, stdout, oLogger);
// Execute template and write data to string
std::string sResult;
oSimpleVM.Run(oData, oLoader, sResult, oLogger);
return sResult; return sResult;
}
/* Destructor */
Searcher::~Searcher() {
if (fileExists(this->template_ct2)) {
remove(template_ct2.c_str());
}
} }
} }

View File

@ -29,21 +29,13 @@
#include <cctype> #include <cctype>
#include <vector> #include <vector>
#include <resourceTools.h> #include <resourceTools.h>
#include <pathTools.h>
#include <stringTools.h> #include <stringTools.h>
#include <CTPP2Parser.hpp> #include <CDT.hpp>
#include <CTPP2FileSourceLoader.hpp>
#include <CTPP2ParserException.hpp>
#include <CTPP2HashTable.hpp>
#include <CTPP2VMDumper.hpp>
#include <CTPP2VMOpcodes.h>
#include <CTPP2VM.hpp>
#include <CTPP2VMSTDLib.hpp>
#include <CTPP2StringOutputCollector.hpp>
#include <CTPP2SyscallFactory.hpp>
#include <CTPP2FileLogger.hpp> #include <CTPP2FileLogger.hpp>
#include <CTPP2SimpleVM.hpp>
#include "ctpp2/CTPP2TextLoader.hpp" #include <CTPP2VMFileLoader.hpp>
using namespace std; using namespace std;
using namespace CTPP; using namespace CTPP;
@ -56,16 +48,16 @@ struct Result
string snippet; string snippet;
int wordCount; int wordCount;
int size; int size;
}; };
namespace kiwix { namespace kiwix {
class Searcher { class Searcher {
public: public:
Searcher(); Searcher();
void search(std::string &search, const unsigned int resultStart, void search(std::string &search, const unsigned int resultStart,
const unsigned int resultEnd, const bool verbose=false); const unsigned int resultEnd, const bool verbose=false);
bool getNextResult(string &url, string &title, unsigned int &score); bool getNextResult(string &url, string &title, unsigned int &score);
unsigned int getEstimatedResultCount(); unsigned int getEstimatedResultCount();
@ -74,11 +66,12 @@ namespace kiwix {
string getHtml(); string getHtml();
void reset(); void reset();
void setContentHumanReadableId(const string &contentHumanReadableId); void setContentHumanReadableId(const string &contentHumanReadableId);
~Searcher();
protected: protected:
std::string beautifyInteger(const unsigned int number); std::string beautifyInteger(const unsigned int number);
virtual void closeIndex() = 0; virtual void closeIndex() = 0;
virtual void searchInIndex(string &search, const unsigned int resultStart, virtual void searchInIndex(string &search, const unsigned int resultStart,
const unsigned int resultEnd, const bool verbose=false) = 0; const unsigned int resultEnd, const bool verbose=false) = 0;
std::vector<Result> results; std::vector<Result> results;
@ -86,6 +79,7 @@ namespace kiwix {
std::string searchPattern; std::string searchPattern;
std::string protocolPrefix; std::string protocolPrefix;
std::string searchProtocolPrefix; std::string searchProtocolPrefix;
const std::string template_ct2;
unsigned int resultCountPerPage; unsigned int resultCountPerPage;
unsigned int estimatedResultCount; unsigned int estimatedResultCount;
unsigned int resultStart; unsigned int resultStart;

File diff suppressed because it is too large Load Diff

View File

@ -26,13 +26,13 @@ do
FILE_ID=`echo "$FILE" | sed "s/\//_/g" | sed "s/\./_/g" | sed "s/\-/_/g"` FILE_ID=`echo "$FILE" | sed "s/\//_/g" | sed "s/\./_/g" | sed "s/\-/_/g"`
echo "Inserting $FILE... [$FILE_ID]" echo "Inserting $FILE... [$FILE_ID]"
reswrap -s -x -oa $RESOURCE_FILE -r $FILE_ID $FILE reswrap -s -x -oa $RESOURCE_FILE -r $FILE_ID $FILE
MAP=$MAP"\tm[\""$FILE"\"] = std::pair <const unsigned char*, unsigned int>("$FILE_ID", sizeof "$FILE_ID"); \n"; MAP=$MAP"\tm[\""$FILE"\"] = std::pair <const unsigned char*, unsigned int>("$FILE_ID", sizeof "$FILE_ID"); \n";
done; done;
MAP=$MAP"\treturn m; \n"; MAP=$MAP"\treturn m; \n";
MAP=$MAP"} \n\n" MAP=$MAP"} \n\n"
MAP=$MAP"static std::map<std::string, std::pair<const unsigned char*, unsigned int> > resourceMap = createResourceMap(); \n\n" MAP=$MAP"static std::map<std::string, std::pair<const unsigned char*, unsigned int> > resourceMap = createResourceMap(); \n\n"
# Create the map table # Create the map table
# map<int, int> m = map_list_of (1,2) (3,4) (5,6) (7,8); # map<int, int> m = map_list_of (1,2) (3,4) (5,6) (7,8);
echo $MAP >> "$RESOURCE_FILE" echo $MAP >> "$RESOURCE_FILE"

View File

@ -1 +1,127 @@
SALUT <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="content-type" />
<style type="text/css">
body{
color: #00000;
font: small/normal Arial,Helvetica,Sans-Serif;
margin-top: 0.5em;
font-size: 90%;
}
a{
color: #04c;
}
a:visited {
color: #639
}
a:hover {
text-decoration: underline
}
.header {
font-size: 120%;
}
ul {
margin:0;
padding:0
}
.results {
font-size: 110%;
}
.results li {
list-style-type:none;
margin-top: 0.5em;
}
.results a {
font-size: 110%;
text-decoration: underline
}
cite {
font-style:normal;
word-wrap:break-word;
display: block;
font-size: 100%;
}
.informations {
color: #388222;
font-size: 100%;
}
.footer {
padding: 0;
margin-top: 1em;
width: 100%;
float: left
}
.footer a, .footer span {
display: block;
padding: .3em .7em;
margin: 0 .38em 0 0;
text-align:center;
text-decoration: none;
}
.footer a:hover {
background: #ededed;
}
.footer ul, .footer li {
list-style:none;
margin: 0;
padding: 0;
}
.footer li {
float: left;
}
.selected {
background: #ededed;
}
</style>
<title>Search: <TMPL_var searchPattern></title>
</head>
<body bgcolor="white">
<div class="header">
<TMPL_if results>Results <b><TMPL_var resultStart>-<TMPL_var resultEnd></b> of <b><TMPL_var count></b> for <b><TMPL_var searchPattern></b><TMPL_else>No result were found for <b><TMPL_var searchPattern></b></TMPL_if>
</div>
<div class="results">
<ul>
<TMPL_foreach results as result>
<li><a href="<TMPL_var protocolPrefix><TMPL_var contentId>/<TMPL_var result.url>"><TMPL_var result.title></a>
<cite><TMPL_if result.snippet><TMPL_var result.snippet>...</TMPL_if></cite>
<TMPL_if wordCount><div class="informations"><TMPL_var wordCount> words</div></TMPL_if>
</li>
</TMPL_foreach>
</ul>
</div>
<div class="footer">
<ul>
<TMPL_if (resultLastPageStart>0)>
<li><a href="<TMPL_var searchProtocolPrefix>content=<TMPL_var contentId>&pattern=<TMPL_var searchPatternEncoded>&start=0&end=<TMPL_var resultRange>">◀</a></li>
</TMPL_if>
<TMPL_foreach pages as page>
<li><a <TMPL_if page.selected>class="selected"</TMPL_if> href="<TMPL_var searchProtocolPrefix>content=<TMPL_var contentId>&pattern=<TMPL_var searchPatternEncoded>&start=<TMPL_var page.start>&end=<TMPL_var page.end>"><TMPL_var page.label></a></li>
</TMPL_foreach>
<TMPL_if (resultLastPageStart>0)>
<li><a href="<TMPL_var searchProtocolPrefix>content=<TMPL_var contentId>&pattern=<TMPL_var searchPatternEncoded>&start=<TMPL_var resultLastPageStart>&end=<TMPL_var (resultLastPageStart+resultRange)>">▶</a></li>
</TMPL_if>
</ul>
</div>
</body>
</html>