2011-07-20 10:33:24 +02:00
/**
* @ author Edouard DUPIN
2012-11-25 11:55:06 +01:00
*
* @ copyright 2010 , Edouard DUPIN , all right reserved
*
* @ license GPL v3 ( see license file )
2011-07-20 10:33:24 +02:00
*/
2012-04-23 10:15:43 +02:00
# include <appl/Debug.h>
2012-04-24 09:42:14 +02:00
# include <appl/global.h>
2012-01-11 15:26:53 +01:00
# include <Highlight.h>
# include <tinyXML/tinyxml.h>
2011-07-20 10:33:24 +02:00
# undef __class__
# define __class__ "Highlight"
2012-08-14 16:11:09 +02:00
void Highlight : : ParseRules ( TiXmlNode * child , etk : : Vector < HighlightPattern * > & mListPatern , int32_t level )
2011-07-20 10:33:24 +02:00
{
// Create the patern ...
HighlightPattern * myPattern = new HighlightPattern ( ) ;
// parse under Element
myPattern - > ParseRules ( child , level ) ;
// add element in the list
2012-08-09 14:26:30 +02:00
mListPatern . PushBack ( myPattern ) ;
2011-07-20 10:33:24 +02:00
}
2012-02-15 16:23:20 +01:00
Highlight : : Highlight ( etk : : UString & xmlFilename )
2011-07-20 10:33:24 +02:00
{
TiXmlDocument XmlDocument ;
2012-01-31 18:26:04 +01:00
2012-11-01 10:47:36 +01:00
etk : : FSNode fileName ( etk : : UString ( " DATA: " ) + xmlFilename ) ;
2012-01-31 18:26:04 +01:00
if ( false = = fileName . Exist ( ) ) {
2012-04-23 10:15:43 +02:00
APPL_ERROR ( " File Does not exist : " < < fileName ) ;
2012-01-31 18:26:04 +01:00
return ;
}
2012-11-01 10:47:36 +01:00
int32_t fileSize = fileName . FileSize ( ) ;
2012-01-31 18:26:04 +01:00
if ( 0 = = fileSize ) {
2012-04-23 10:15:43 +02:00
APPL_ERROR ( " This file is empty : " < < fileName ) ;
2012-01-31 18:26:04 +01:00
return ;
}
2012-11-01 10:47:36 +01:00
if ( false = = fileName . FileOpenRead ( ) ) {
2012-04-23 10:15:43 +02:00
APPL_ERROR ( " Can not open the file : " < < fileName ) ;
2012-01-31 18:26:04 +01:00
return ;
}
// allocate data
2012-02-07 18:04:15 +01:00
char * fileBuffer = new char [ fileSize + 5 ] ;
2012-01-31 18:26:04 +01:00
if ( NULL = = fileBuffer ) {
2012-04-23 10:15:43 +02:00
APPL_ERROR ( " Error Memory allocation size= " < < fileSize ) ;
2012-01-31 18:26:04 +01:00
return ;
}
2012-02-07 18:04:15 +01:00
memset ( fileBuffer , 0 , ( fileSize + 5 ) * sizeof ( char ) ) ;
2012-01-31 18:26:04 +01:00
// load data from the file :
2012-11-01 10:47:36 +01:00
fileName . FileRead ( fileBuffer , 1 , fileSize ) ;
2012-01-31 18:26:04 +01:00
// close the file:
2012-11-01 10:47:36 +01:00
fileName . FileClose ( ) ;
2012-01-31 18:26:04 +01:00
// load the XML from the memory
bool loadError = XmlDocument . Parse ( ( const char * ) fileBuffer , 0 , TIXML_ENCODING_UTF8 ) ;
2011-07-20 10:33:24 +02:00
if ( false = = loadError ) {
2012-04-23 10:15:43 +02:00
APPL_ERROR ( " can not load Hightlight XML: PARSING error: \" " < < xmlFilename < < " \" " ) ;
2011-07-20 10:33:24 +02:00
return ;
}
2012-01-31 18:26:04 +01:00
2011-07-20 10:33:24 +02:00
TiXmlElement * root = XmlDocument . FirstChildElement ( " EdnLang " ) ;
if ( NULL = = root ) {
2012-04-23 10:15:43 +02:00
APPL_ERROR ( " can not load Hightlight XML: main node not find: \" EdnLang \" " ) ;
2011-07-20 10:33:24 +02:00
return ;
}
int32_t level1 = 0 ;
int32_t level2 = 0 ;
TiXmlNode * child = root - > FirstChild ( ) ;
while ( NULL ! = child )
{
if ( child - > Type ( ) = = TiXmlNode : : TINYXML_COMMENT ) {
// nothing to do ...
} else if ( ! strcmp ( child - > Value ( ) , " ext " ) ) {
const char * myData = child - > ToElement ( ) - > GetText ( ) ;
if ( NULL ! = myData ) {
2012-04-23 10:15:43 +02:00
//APPL_INFO(PFX"(l %d) node fined : %s=\"%s\"", child->Row(), child->Value() , myData);
2012-02-15 16:23:20 +01:00
etk : : UString * myEdnData = new etk : : UString ( myData ) ;
2012-08-09 14:26:30 +02:00
m_listExtentions . PushBack ( myEdnData ) ;
2011-07-20 10:33:24 +02:00
}
} else if ( ! strcmp ( child - > Value ( ) , " pass1 " ) ) {
// Get sub Nodes ...
TiXmlNode * passChild = child - > FirstChild ( ) ;
while ( NULL ! = passChild ) {
if ( passChild - > Type ( ) = = TiXmlNode : : TINYXML_COMMENT ) {
// nothing to do ...
} else if ( ! strcmp ( passChild - > Value ( ) , " rule " ) ) {
ParseRules ( passChild , m_listHighlightPass1 , level1 + + ) ;
} else {
2012-04-23 10:15:43 +02:00
APPL_ERROR ( " (l " < < passChild - > Row ( ) < < " ) node not suported : \" " < < passChild - > Value ( ) < < " \" must be [rule] " ) ;
2011-07-20 10:33:24 +02:00
}
// get the next node element :
passChild = passChild - > NextSibling ( ) ;
}
} else if ( ! strcmp ( child - > Value ( ) , " pass2 " ) ) {
// Get sub Nodes ...
TiXmlNode * passChild = child - > FirstChild ( ) ;
while ( NULL ! = passChild ) {
if ( passChild - > Type ( ) = = TiXmlNode : : TINYXML_COMMENT ) {
// nothing to do ...
} else if ( ! strcmp ( passChild - > Value ( ) , " rule " ) ) {
ParseRules ( passChild , m_listHighlightPass2 , level2 + + ) ;
} else {
2012-04-23 10:15:43 +02:00
APPL_ERROR ( " (l " < < passChild - > Row ( ) < < " ) node not suported : \" " < < passChild - > Value ( ) < < " \" must be [rule] " ) ;
2011-07-20 10:33:24 +02:00
}
// get the next node element :
passChild = passChild - > NextSibling ( ) ;
}
} else {
2012-04-23 10:15:43 +02:00
APPL_ERROR ( " (l " < < child - > Row ( ) < < " ) node not suported : \" " < < child - > Value ( ) < < " \" must be [ext,pass1,pass2] " ) ;
2011-07-20 10:33:24 +02:00
}
// get the next node element :
child = child - > NextSibling ( ) ;
}
2012-01-31 18:26:04 +01:00
if ( NULL ! = fileBuffer ) {
delete [ ] fileBuffer ;
}
2011-07-20 10:33:24 +02:00
}
Highlight : : ~ Highlight ( void )
{
2011-08-08 14:06:11 +02:00
int32_t i ;
2011-07-20 10:33:24 +02:00
// clean all Element
2012-08-09 14:26:30 +02:00
for ( i = 0 ; i < m_listHighlightPass1 . Size ( ) ; i + + ) {
2011-08-08 14:06:11 +02:00
if ( NULL ! = m_listHighlightPass1 [ i ] ) {
delete ( m_listHighlightPass1 [ i ] ) ;
m_listHighlightPass1 [ i ] = NULL ;
}
}
// clear the compleate list
2012-08-09 14:26:30 +02:00
m_listHighlightPass1 . Clear ( ) ;
2011-08-08 14:06:11 +02:00
// clean all Element
2012-08-09 14:26:30 +02:00
for ( i = 0 ; i < m_listExtentions . Size ( ) ; i + + ) {
2011-08-08 14:06:11 +02:00
if ( NULL ! = m_listExtentions [ i ] ) {
delete ( m_listExtentions [ i ] ) ;
m_listExtentions [ i ] = NULL ;
}
2011-07-20 10:33:24 +02:00
}
// clear the compleate list
2012-08-09 14:26:30 +02:00
m_listExtentions . Clear ( ) ;
2011-07-20 10:33:24 +02:00
}
2011-08-19 16:09:58 +02:00
void Highlight : : ReloadColor ( void )
{
int32_t i ;
2012-08-09 14:26:30 +02:00
for ( i = 0 ; i < m_listHighlightPass1 . Size ( ) ; i + + ) {
2011-08-19 16:09:58 +02:00
if ( NULL ! = m_listHighlightPass1 [ i ] ) {
m_listHighlightPass1 [ i ] - > ReloadColor ( ) ;
}
}
2012-08-09 14:26:30 +02:00
for ( i = 0 ; i < m_listHighlightPass2 . Size ( ) ; i + + ) {
2011-08-19 16:09:58 +02:00
if ( NULL ! = m_listHighlightPass2 [ i ] ) {
m_listHighlightPass2 [ i ] - > ReloadColor ( ) ;
}
}
}
2011-07-20 10:33:24 +02:00
2012-02-15 16:23:20 +01:00
bool Highlight : : HasExtention ( etk : : UString & ext )
2011-07-20 10:33:24 +02:00
{
2011-08-08 14:06:11 +02:00
int32_t i ;
2012-08-09 14:26:30 +02:00
for ( i = 0 ; i < m_listExtentions . Size ( ) ; i + + ) {
2011-08-08 14:06:11 +02:00
if ( ext = = * m_listExtentions [ i ] ) {
2011-07-20 10:33:24 +02:00
return true ;
}
}
return false ;
}
2012-11-01 10:47:36 +01:00
bool Highlight : : FileNameCompatible ( etk : : FSNode & fileName )
2011-07-20 10:33:24 +02:00
{
2011-08-08 14:06:11 +02:00
int32_t i ;
2012-02-15 16:23:20 +01:00
etk : : UString extention ;
2012-11-01 10:47:36 +01:00
if ( true = = fileName . FileHasExtention ( ) ) {
2011-08-07 10:47:06 +02:00
extention = " *. " ;
2012-11-01 10:47:36 +01:00
extention + = fileName . FileGetExtention ( ) ;
2011-07-20 10:33:24 +02:00
} else {
2012-11-01 10:47:36 +01:00
extention = fileName . GetNameFile ( ) ;
2011-07-20 10:33:24 +02:00
}
2012-04-23 10:15:43 +02:00
APPL_DEBUG ( " try to find : in \" " < < fileName < < " \" extention: \" " < < extention < < " \" " ) ;
2011-07-20 10:33:24 +02:00
2012-08-09 14:26:30 +02:00
for ( i = 0 ; i < m_listExtentions . Size ( ) ; i + + ) {
2011-08-08 14:06:11 +02:00
if ( extention = = * m_listExtentions [ i ] ) {
2011-07-20 10:33:24 +02:00
return true ;
}
}
return false ;
}
void Highlight : : Display ( void )
{
2011-08-08 14:06:11 +02:00
int32_t i ;
2012-04-23 10:15:43 +02:00
APPL_INFO ( " List of ALL Highlight : " ) ;
2012-08-09 14:26:30 +02:00
for ( i = 0 ; i < m_listExtentions . Size ( ) ; i + + ) {
2012-04-23 10:15:43 +02:00
APPL_INFO ( " Extention : " < < i < < " : " < < * m_listExtentions [ i ] ) ;
2011-07-20 10:33:24 +02:00
}
// Display all elements
2012-08-09 14:26:30 +02:00
for ( i = 0 ; i < m_listHighlightPass1 . Size ( ) ; i + + ) {
2012-04-23 10:15:43 +02:00
APPL_INFO ( " " < < i < < " Pass 1 : " < < m_listHighlightPass1 [ i ] - > GetName ( ) ) ;
2011-07-20 10:33:24 +02:00
//m_listHighlightPass1[i]->Display();
}
// Display all elements
2012-08-09 14:26:30 +02:00
for ( i = 0 ; i < m_listHighlightPass2 . Size ( ) ; i + + ) {
2012-04-23 10:15:43 +02:00
APPL_INFO ( " " < < i < < " Pass 2 : " < < m_listHighlightPass2 [ i ] - > GetName ( ) ) ;
2011-07-20 10:33:24 +02:00
//m_listHighlightPass2[i]->Display();
}
}
2011-08-09 14:28:58 +02:00
// 13h 46min 22s | (l= 214) Highlight::Parse | [II] Find Pattern in the Buffer : (2457,2479)
2011-07-20 10:33:24 +02:00
2011-08-09 14:28:58 +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...
2011-08-07 22:34:27 +02:00
void Highlight : : Parse ( int32_t start ,
int32_t stop ,
2012-08-14 16:11:09 +02:00
etk : : Vector < colorInformation_ts > & metaData ,
2011-08-08 10:24:24 +02:00
int32_t addingPos ,
2012-11-19 22:45:55 +01:00
etk : : Buffer & buffer )
2011-07-20 10:33:24 +02:00
{
if ( 0 > addingPos ) {
addingPos = 0 ;
}
2012-04-23 10:15:43 +02:00
//APPL_DEBUG("Parse element 0 => " << m_listHighlightPass1.Size() << " ==> position search: (" << start << "," << stop << ")" );
2011-07-20 10:33:24 +02:00
int32_t elementStart = start ;
int32_t elementStop = stop ;
colorInformation_ts resultat ;
2011-08-08 10:24:24 +02:00
while ( elementStart < elementStop ) {
2012-04-23 10:15:43 +02:00
//APPL_DEBUG("Parse element in the buffer id=" << elementStart);
2011-08-08 10:24:24 +02:00
//try to fond the HL in ALL of we have
2012-08-09 14:26:30 +02:00
for ( int32_t jjj = 0 ; jjj < m_listHighlightPass1 . Size ( ) ; jjj + + ) {
2011-08-08 10:24:24 +02:00
resultFind_te ret = HLP_FIND_OK ;
2012-04-23 10:15:43 +02:00
//APPL_DEBUG("Parse HL id=" << jjj << " position search: (" << start << "," << buffer.Size() << ")" );
2011-08-08 10:24:24 +02:00
// Stop the search to the end (to get the end of the pattern)
2012-08-09 14:26:30 +02:00
ret = m_listHighlightPass1 [ jjj ] - > Find ( elementStart , buffer . Size ( ) , resultat , buffer ) ;
2011-08-08 10:24:24 +02:00
if ( HLP_FIND_ERROR ! = ret ) {
2012-04-23 10:15:43 +02:00
//APPL_INFO("Find Pattern in the Buffer : (" << resultat.beginStart << "," << resultat.endStop << ")" );
2011-08-08 10:24:24 +02:00
// Remove element in the current List where the current Element have a end inside the next...
int32_t kkk = addingPos ;
2012-08-09 14:26:30 +02:00
while ( kkk < metaData . Size ( ) ) {
2011-08-08 10:24:24 +02:00
if ( metaData [ kkk ] . beginStart < = resultat . endStop ) {
// Remove element
2012-04-23 10:15:43 +02:00
//APPL_INFO("Erase element=" << kkk);
2012-08-09 14:26:30 +02:00
metaData . EraseLen ( kkk , kkk + 1 ) ;
2011-08-08 10:24:24 +02:00
// Increase the end of search
2012-08-09 14:26:30 +02:00
if ( kkk < metaData . Size ( ) ) {
2011-08-08 10:24:24 +02:00
// just befor the end of the next element
elementStop = metaData [ kkk ] . beginStart - 1 ;
} else {
// end of the buffer
2012-08-09 14:26:30 +02:00
elementStop = buffer . Size ( ) ;
2011-08-08 10:24:24 +02:00
}
2011-07-20 10:33:24 +02:00
} else {
2011-08-08 10:24:24 +02:00
// Not find ==> exit the cycle :
break ;
2011-07-20 10:33:24 +02:00
}
}
2011-08-08 10:24:24 +02:00
// Add curent element in the list ...
2012-08-09 14:26:30 +02:00
metaData . Insert ( addingPos , resultat ) ;
2012-04-23 10:15:43 +02:00
//APPL_DEBUG("INSERT at "<< addingPos << " S=" << resultat.beginStart << " E=" << resultat.endStop );
2011-08-08 10:24:24 +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 + + ;
// We find a pattern ==> Stop search for the current element
break ;
2011-07-20 10:33:24 +02:00
}
}
2011-08-08 10:24:24 +02:00
// Go to the next element (and search again ...).
elementStart + + ;
2011-07-20 10:33:24 +02:00
}
}
2011-08-08 10:24:24 +02:00
2011-07-20 10:33:24 +02:00
/**
* @ brief second pass of the hightlight
*
*/
2011-08-07 22:34:27 +02:00
void Highlight : : Parse2 ( int32_t start ,
int32_t stop ,
2012-08-14 16:11:09 +02:00
etk : : Vector < colorInformation_ts > & metaData ,
2012-11-19 22:45:55 +01:00
etk : : Buffer & buffer )
2011-07-20 10:33:24 +02:00
{
2012-04-23 10:15:43 +02:00
//APPL_DEBUG("Parse element 0 => " << m_listHighlightPass2.size() << " ==> position search: (" << start << "," << stop << ")" );
2011-07-20 10:33:24 +02:00
int32_t elementStart = start ;
int32_t elementStop = stop ;
colorInformation_ts resultat ;
2011-08-08 10:24:24 +02:00
while ( elementStart < elementStop ) {
2012-04-23 10:15:43 +02:00
//APPL_DEBUG("Parse element in the buffer id=" << elementStart);
2011-08-08 10:24:24 +02:00
//try to fond the HL in ALL of we have
int32_t jjj ;
2012-08-09 14:26:30 +02:00
for ( jjj = 0 ; jjj < m_listHighlightPass2 . Size ( ) ; jjj + + ) {
2011-08-08 10:24:24 +02:00
resultFind_te ret = HLP_FIND_OK ;
2012-04-23 10:15:43 +02:00
//APPL_DEBUG("Parse HL id=" << jjj << " position search: (" << start << "," << buffer.Size() << ")" );
2011-08-08 10:24:24 +02:00
// Stop the search to the end (to get the end of the pattern)
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 << ")" );
2011-08-08 10:24:24 +02:00
// Add curent element in the list ...
2012-08-09 14:26:30 +02:00
metaData . PushBack ( resultat ) ;
2011-08-16 10:01:28 +02:00
elementStart = resultat . endStop - 1 ;
2011-08-08 10:24:24 +02:00
// Exit current cycle
break ;
}
2011-07-20 10:33:24 +02:00
}
2011-08-08 10:24:24 +02:00
// Go to the next element (and search again ...).
elementStart + + ;
2011-07-20 10:33:24 +02:00
}
}