edn/sources/appl/Highlight/Highlight.cpp

277 lines
9.1 KiB
C++
Raw Normal View History

/**
* @author Edouard DUPIN
2012-11-25 11:55:06 +01:00
*
* @copyright 2010, Edouard DUPIN, all right reserved
*
* @license GPL v3 (see license file)
*/
2012-04-23 10:15:43 +02:00
#include <appl/Debug.h>
2012-04-24 09:42:14 +02:00
#include <appl/global.h>
#include <Highlight.h>
#include <exml/exml.h>
2013-10-25 22:12:34 +02:00
#include <ewol/resources/ResourceManager.h>
#undef __class__
2013-10-25 22:12:34 +02:00
#define __class__ "Highlight"
2013-10-25 22:12:34 +02:00
void appl::Highlight::parseRules(exml::Element* _child,
etk::Vector<HighlightPattern*>& _mListPatern,
int32_t _level) {
// Create the patern ...
2013-10-25 22:12:34 +02:00
HighlightPattern *myPattern = new HighlightPattern(m_paintingProperties);
// parse under Element
2013-10-09 22:00:24 +02:00
myPattern->parseRules(_child, _level);
// add element in the list
2013-10-09 22:00:24 +02:00
_mListPatern.pushBack(myPattern);
}
2013-10-25 22:12:34 +02:00
appl::Highlight::Highlight(const etk::UString& _xmlFilename, const etk::UString& _colorFile) :
ewol::Resource(_xmlFilename) {
// keep color propertiy file :
m_paintingProperties = appl::GlyphPainting::keep(_colorFile);
2013-06-24 21:17:45 +02:00
exml::Document doc;
2013-10-07 22:04:21 +02:00
if (doc.load(_xmlFilename) == false) {
APPL_ERROR(" can not load file XML : " << _xmlFilename);
return;
}
2013-10-25 22:12:34 +02:00
exml::Element* root = doc.getNamed("EdnLang");
2013-06-24 21:17:45 +02:00
if (NULL == root ) {
APPL_ERROR("(l ?) main node not find: \"EdnLang\" ...");
return;
}
int32_t level1 = 0;
int32_t level2 = 0;
2013-06-24 21:17:45 +02:00
// parse all the elements :
2013-10-25 22:12:34 +02:00
for(int32_t iii = 0; iii < root->size(); ++iii) {
2013-10-07 22:04:21 +02:00
exml::Element* child = root->getElement(iii);
if (child == NULL) {
// trash here all that is not element ...
2013-06-24 21:17:45 +02:00
continue;
}
2013-10-07 22:04:21 +02:00
if (child->getValue() == "ext") {
etk::UString myData = child->getText();
if (myData.size()!=0) {
2012-04-23 10:15:43 +02:00
//APPL_INFO(PFX"(l %d) node fined : %s=\"%s\"", child->Row(), child->Value() , myData);
2013-10-07 22:04:21 +02:00
m_listExtentions.pushBack(myData);
}
2013-10-07 22:04:21 +02:00
} else if (child->getValue() == "pass1") {
// get sub Nodes ...
for(int32_t jjj=0; jjj< child->size(); jjj++) {
exml::Element* passChild = child->getElement(jjj);
if (passChild == NULL) {
2013-06-24 21:17:45 +02:00
continue;
}
2013-10-07 22:04:21 +02:00
if (passChild->getValue() != "rule") {
2013-10-09 22:00:24 +02:00
APPL_ERROR("(l "<< passChild->getPos() << ") node not suported : \""<< passChild->getValue() << "\" must be [rule]" );
2013-06-24 21:17:45 +02:00
continue;
}
2013-10-09 22:00:24 +02:00
parseRules(passChild, m_listHighlightPass1, level1++);
}
2013-10-07 22:04:21 +02:00
} else if (child->getValue() == "pass2") {
// get sub Nodes ...
for(int32_t jjj=0; jjj< child->size(); jjj++) {
exml::Element* passChild = child->getElement(jjj);
if (passChild == NULL) {
2013-06-24 21:17:45 +02:00
continue;
}
2013-10-07 22:04:21 +02:00
if (passChild->getValue() != "rule") {
2013-10-09 22:00:24 +02:00
APPL_ERROR("(l "<< passChild->getPos() << ") node not suported : \""<< passChild->getValue() << "\" must be [rule]" );
2013-06-24 21:17:45 +02:00
continue;
}
2013-10-09 22:00:24 +02:00
parseRules(passChild, m_listHighlightPass2, level2++);
}
} else {
2013-10-09 22:00:24 +02:00
APPL_ERROR("(l "<< child->getPos() << ") node not suported : \""<< child->getValue() << "\" must be [ext,pass1,pass2]" );
}
}
}
2013-10-25 22:12:34 +02:00
appl::Highlight::~Highlight(void) {
int32_t i;
// clean all Element
2013-10-25 22:12:34 +02:00
for (esize_t iii = 0; iii < m_listHighlightPass1.size(); ++iii) {
if (m_listHighlightPass1[iii] != NULL) {
delete(m_listHighlightPass1[iii]);
m_listHighlightPass1[iii] = NULL;
}
}
// clear the compleate list
2013-10-07 22:04:21 +02:00
m_listHighlightPass1.clear();
// clear the compleate list
2013-10-07 22:04:21 +02:00
m_listExtentions.clear();
}
2013-10-25 22:12:34 +02:00
bool appl::Highlight::hasExtention(const etk::UString& _ext) {
2013-10-07 22:04:21 +02:00
for (int32_t iii=0; iii<m_listExtentions.size(); iii++) {
2013-10-25 22:12:34 +02:00
if (m_listExtentions[iii] == _ext) {
return true;
}
}
return false;
}
2013-10-25 22:12:34 +02:00
bool appl::Highlight::fileNameCompatible(const etk::UString& _fileName) {
2012-02-15 16:23:20 +01:00
etk::UString extention;
2013-10-25 22:12:34 +02:00
etk::FSNode file(_fileName);
if (true == file.fileHasExtention() ) {
2011-08-07 10:47:06 +02:00
extention = "*.";
2013-10-25 22:12:34 +02:00
extention += file.fileGetExtention();
} else {
2013-10-25 22:12:34 +02:00
extention = file.getNameFile();
}
2013-10-25 22:12:34 +02:00
APPL_DEBUG(" try to find : in \"" << file << "\" extention:\"" << extention << "\" ");
2013-10-07 22:04:21 +02:00
for (int32_t iii=0; iii<m_listExtentions.size(); iii++) {
if (extention == m_listExtentions[iii] ) {
return true;
}
}
return false;
}
2013-10-25 22:12:34 +02:00
void appl::Highlight::display(void) {
2012-04-23 10:15:43 +02:00
APPL_INFO("List of ALL Highlight : ");
2013-10-07 22:04:21 +02:00
for (int32_t iii=0; iii< m_listExtentions.size(); iii++) {
APPL_INFO(" Extention : " << iii << " : " << m_listExtentions[iii] );
}
2013-10-07 22:04:21 +02:00
// display all elements
for (int32_t iii=0; iii< m_listHighlightPass1.size(); iii++) {
APPL_INFO(" " << iii << " Pass 1 : " << m_listHighlightPass1[iii]->getName() );
2013-10-25 22:12:34 +02:00
//m_listHighlightPass1[iii]->display();
}
2013-10-07 22:04:21 +02:00
// display all elements
for (int32_t iii=0; iii< m_listHighlightPass2.size(); iii++) {
APPL_INFO(" " << iii << " Pass 2 : " << m_listHighlightPass2[iii]->getName() );
2013-10-25 22:12:34 +02:00
//m_listHighlightPass2[iii]->display();
}
}
2013-10-25 22:12:34 +02:00
/* TODO : Celui qui appelle suprime des element pour rien ... Enfin c'est pas tr<74>grave...
* Il suffirait juste de suprimer celuis d'avant si il n'est pas terminer...
*/
void appl::Highlight::parse(int32_t start,
2011-08-07 22:34:27 +02:00
int32_t stop,
2013-10-23 21:19:30 +02:00
etk::Vector<appl::ColorInfo> &metaData,
int32_t addingPos,
2013-10-09 22:00:24 +02:00
etk::Buffer &buffer) {
if (0 > addingPos) {
addingPos = 0;
}
2013-10-07 22:04:21 +02:00
//APPL_DEBUG("Parse element 0 => " << m_listHighlightPass1.size() << " == > position search: (" << start << "," << stop << ")" );
int32_t elementStart = start;
int32_t elementStop = stop;
2013-10-23 21:19:30 +02:00
appl::ColorInfo resultat;
while (elementStart<elementStop) {
2012-04-23 10:15:43 +02:00
//APPL_DEBUG("Parse element in the buffer id=" << elementStart);
//try to fond the HL in ALL of we have
2013-10-07 22:04:21 +02:00
for (int32_t jjj=0; jjj<m_listHighlightPass1.size(); jjj++){
resultFind_te ret = HLP_FIND_OK;
2013-10-07 22:04:21 +02:00
//APPL_DEBUG("Parse HL id=" << jjj << " position search: (" << start << "," << buffer.size() << ")" );
// Stop the search to the end (to get the end of the pattern)
2013-10-07 22:04:21 +02:00
ret = m_listHighlightPass1[jjj]->find(elementStart, buffer.size(), resultat, buffer);
if (HLP_FIND_ERROR != ret) {
2012-04-23 10:15:43 +02:00
//APPL_INFO("Find Pattern in the Buffer : (" << resultat.beginStart << "," << resultat.endStop << ")" );
2013-10-07 22:04:21 +02:00
// remove element in the current List where the current Element have a end inside the next...
int32_t kkk=addingPos;
2013-10-07 22:04:21 +02:00
while(kkk < metaData.size() ) {
if (metaData[kkk].beginStart <= resultat.endStop) {
2013-10-07 22:04:21 +02:00
// remove element
2012-04-23 10:15:43 +02:00
//APPL_INFO("Erase element=" << kkk);
2013-10-09 22:00:24 +02:00
metaData.eraseLen(kkk, kkk+1);
// Increase the end of search
2013-10-07 22:04:21 +02:00
if (kkk < metaData.size()) {
// just befor the end of the next element
elementStop = metaData[kkk].beginStart-1;
} else {
// end of the buffer
2013-10-07 22:04:21 +02:00
elementStop = buffer.size();
}
} else {
2013-10-07 22:04:21 +02:00
// Not find == > exit the cycle :
break;
}
}
2013-10-07 22:04:21 +02:00
// add curent element in the list ...
metaData.insert(addingPos, resultat);
2012-04-23 10:15:43 +02:00
//APPL_DEBUG("INSERT at "<< addingPos << " S=" << resultat.beginStart << " E=" << resultat.endStop );
2013-10-07 22:04:21 +02:00
// update the current research starting element: (set position at the end of the current element
elementStart = resultat.endStop-1;
// increment the position of insertion:
addingPos++;
2013-10-07 22:04:21 +02:00
// We find a pattern == > Stop search for the current element
break;
}
}
// Go to the next element (and search again ...).
elementStart++;
}
}
/**
* @brief second pass of the hightlight
*
*/
2013-10-25 22:12:34 +02:00
void appl::Highlight::parse2(int32_t start,
2011-08-07 22:34:27 +02:00
int32_t stop,
2013-10-23 21:19:30 +02:00
etk::Vector<appl::ColorInfo> &metaData,
2013-10-09 22:00:24 +02:00
etk::Buffer &buffer) {
2013-10-07 22:04:21 +02:00
//APPL_DEBUG("Parse element 0 => " << m_listHighlightPass2.size() << " == > position search: (" << start << "," << stop << ")" );
int32_t elementStart = start;
int32_t elementStop = stop;
2013-10-23 21:19:30 +02:00
appl::ColorInfo resultat;
while (elementStart<elementStop) {
2012-04-23 10:15:43 +02:00
//APPL_DEBUG("Parse element in the buffer id=" << elementStart);
//try to fond the HL in ALL of we have
int32_t jjj;
2013-10-07 22:04:21 +02:00
for (jjj=0; jjj<m_listHighlightPass2.size(); jjj++){
resultFind_te ret = HLP_FIND_OK;
2013-10-07 22:04:21 +02:00
//APPL_DEBUG("Parse HL id=" << jjj << " position search: (" << start << "," << buffer.size() << ")" );
// Stop the search to the end (to get the end of the pattern)
2013-10-07 22:04:21 +02:00
ret = m_listHighlightPass2[jjj]->find(elementStart, elementStop, resultat, buffer);
if (HLP_FIND_ERROR != ret) {
2012-04-23 10:15:43 +02:00
//APPL_INFO("Find Pattern in the Buffer : (" << resultat.beginStart << "," << resultat.endStop << ")" );
2013-10-07 22:04:21 +02:00
// add curent element in the list ...
metaData.pushBack(resultat);
elementStart = resultat.endStop-1;
// Exit current cycle
break;
}
}
// Go to the next element (and search again ...).
elementStart++;
}
}
2013-10-25 22:12:34 +02:00
appl::Highlight* appl::Highlight::keep(const etk::UString& _filename) {
//EWOL_INFO("KEEP : appl::Highlight : file : \"" << _filename << "\"");
appl::Highlight* object = static_cast<appl::Highlight*>(getManager().localKeep(_filename));
if (NULL != object) {
return object;
}
EWOL_INFO("CREATE : appl::Highlight : file : \"" << _filename << "\"");
// this element create a new one every time ....
object = new appl::Highlight(_filename, "THEME:COLOR:textViewer.json");
if (NULL == object) {
EWOL_ERROR("allocation error of a resource : ??Highlight??");
return NULL;
}
getManager().localAdd(object);
return object;
}
void appl::Highlight::release(appl::Highlight*& _object) {
if (NULL == _object) {
return;
}
ewol::Resource* object2 = static_cast<ewol::Resource*>(_object);
getManager().release(object2);
_object = NULL;
}