PhoenixFileParser  1.5.1
Set of tools to ease file parsing
Loading...
Searching...
No Matches
PHighlightParser.cpp
Go to the documentation of this file.
1/***************************************
2 Auteur : Pierre Aubert
3 Mail : pierre.aubert@lapp.in2p3.fr
4 Licence : CeCILL-C
5****************************************/
6
7#include "PHighlightParser.h"
8
13
18
20
24void PHighlightParser::addToken(const PVecString & vecToken, const PString & replaceBeforePattern, const PString & replaceAfterPattern){
27 action->replaceBeforePattern = replaceBeforePattern;
28 action->replaceAfterPattern = replaceAfterPattern;
29 p_vecPtrAction.push_back(action);
30 for(PVecString::const_iterator it(vecToken.begin()); it != vecToken.end(); ++it){
31 p_replaceTree.addKeyValue(it->toVector(), action);
32 }
33}
34
36
41void PHighlightParser::addToken(const PString & beginPattern, const PString & endPattern, const PString & replaceBeforePattern, const PString & replaceAfterPattern){
44 action->replaceBeforePattern = replaceBeforePattern;
45 action->replaceAfterPattern = replaceAfterPattern;
46 action->endPattern = endPattern;
47 p_vecPtrAction.push_back(action);
48 p_replaceTree.addKeyValue(beginPattern.toVector(), action);
49}
50
52
55PString PHighlightParser::makeHighlighting(const PString & content){
56 if(content.size() == 0lu || p_replaceTree.getMapChild().size() == 0lu){return content;}
57 PFileParser parser;
58 parser.setFileContent(content);
59 PReplaceHightlightTree::iterator it(p_replaceTree.begin());
60 bool isPreviousMatchingString(false);
61 PString out(""), pattern(""), replacePattern("");
62 PHighlightingAction* action = NULL;
63 size_t beginTest(0lu), nbMatch(0lu), indexLastMatchReplace(0lu);
64 while(!parser.isEndOfFile()){
65 char currentChar = parser.getCurrentCh();
66// std::cerr << "PString::replace : content["<<i<<"] = '"<<currentChar<<"'" << std::endl;
67 if(it.next(currentChar)){ //We found a matching character
68 if(nbMatch == 0lu){ //This is the first matched character for this sequence
69 beginTest = parser.getCurrentCharIdx(); //So we remember the begining position
70 }
71 pattern += currentChar;
72 ++nbMatch; //Let's remember how much characters were OK
73 if(it->hasValue()){ //If the current node tree has a value
74 action = it->getValue();
75 replacePattern = pattern; //We have to remember the current pattern because it will change if we have other partial matches
76 isPreviousMatchingString = true; //And we say that we have a value
77 indexLastMatchReplace = parser.getCurrentCharIdx();
78 }
79 parser.incrementCurrentChar();
80 }else{
81 if(nbMatch == 0lu){
82 it = p_replaceTree.begin(); //Let's check again from the begining of the tree
83 isPreviousMatchingString = false;
84 pattern = "";
85 out += currentChar;
86 parser.incrementCurrentChar();
87 }else{
88 if(isPreviousMatchingString){ //If we had something to replace
89 doReplaceAction(out, parser, action, replacePattern, indexLastMatchReplace); //Let's replace it
90 }else{
91 out += parser.getChar(beginTest); //no sequence matches
92 parser.setCurrentCharIdx(beginTest); //so we start again one character later (we will have the ++i of the for loop for that)
93 parser.incrementCurrentChar();
94 }
95 isPreviousMatchingString = false;
96 pattern = "";
97 action = NULL;
98 nbMatch = 0lu;
99 it = p_replaceTree.begin(); //Let's check again from the begining of the tree
100 }
101 }
102 }
103 if(isPreviousMatchingString){ //If we had something to replace
104 doReplaceAction(out, parser, action, replacePattern, indexLastMatchReplace); //Let's replace it
105 }else if(nbMatch != 0lu){
106 for(size_t i(beginTest); i < parser.getNbTotalChar(); ++i){
107 out += parser.getChar(i);
108
109 }
110 }
111 //We are potentially at the end of the source, so no more test
112 return out;
113}
114
117 for(std::vector<PHighlightingAction*>::iterator it(p_vecPtrAction.begin()); it != p_vecPtrAction.end(); ++it){
118 delete *it;
119 }
120}
121
126
128
134void PHighlightParser::doReplaceAction(PString & out, PFileParser & parser, PHighlightingAction* action, const PString & replacePattern, size_t indexLastMatchReplace){
135 if(action == NULL){return;}
136 if(action->type == PHighlightType::REPLACE){
137 out += action->replaceBeforePattern + replacePattern + action->replaceAfterPattern; //Let's replace it
138 parser.setCurrentCharIdx(indexLastMatchReplace);
139 parser.incrementCurrentChar();
141 out += action->replaceBeforePattern + replacePattern + parser.getUntilKeyWithoutPatern(action->endPattern) + action->endPattern + action->replaceAfterPattern; //Let's replace it
142 }
143}
144
145
146
classe qui permet de parser des fichiers texte en renvoyant les tokens les uns après les autres
Definition PFileParser.h:20
PString getUntilKeyWithoutPatern(const PString &patern)
Renvoie la chaine de caractère du caractère courant jusqu'à patern exclu.
void incrementCurrentChar(size_t nbChar=1lu)
Increment the current caracter.
void setCurrentCharIdx(size_t index)
Set the index of the current char.
char getChar(size_t index) const
Get the char at the given index.
char getCurrentCh() const
Renvoie le caractère courant.
void setFileContent(const PString &fileContent)
Set the file content.
size_t getCurrentCharIdx() const
Return the index of the current character.
bool isEndOfFile() const
Dit si on est à la fin du fichier.
size_t getNbTotalChar() const
Return the number of characters in the current opened file.
virtual ~PHighlightParser()
Destructor of PHighlightParser.
PHighlightParser()
Default constructor of PHighlightParser.
PReplaceHightlightTree p_replaceTree
Map of tree to be used to do the hightlighting.
std::vector< PHighlightingAction * > p_vecPtrAction
Vector of pointer of PHighlightingAction.
void doReplaceAction(PString &out, PFileParser &parser, PHighlightingAction *action, const PString &replacePattern, size_t indexLastMatchReplace)
Do the replace action.
void clear()
Clear all PHighlightingAction.
void initialisationPHighlightParser()
Initialisation function of the class PHighlightParser.
void addToken(const PVecString &vecToken, const PString &replaceBeforePattern, const PString &replaceAfterPattern)
Add token in the highlighter.
PString makeHighlighting(const PString &content)
Do the highlighting of the given PString.
Action of the hightlighting.
PString endPattern
End pattern to be used with the GET_UNITL_WITHOUT_PATTERN.
PString replaceBeforePattern
String to be placed before the replaced pattern.
PString replaceAfterPattern
String to be placed after the replaced pattern.
PHighlightType::PHighlightType type
Type of the hightlighting.