|
/********************************************************************\
created: 2001/03/04
created: 4:3:2001 13:17
filename: Parser\prsSingle.cpp
file path: .\Parser
file base: prsSingle
file ext: cpp
author: Alex Kucherenko
purpose:
\********************************************************************/
#include "stdafx.h"
// Disable some warnings while using STL
#pragma warning (disable : 4800 4290 4786 4503 )
#include <algorithm>
#include "prsExceptions.h"
#include "prsBase.h"
#include "prsSingle.h"
//////////////////////////////////////////////////////////////////////////
// Default constructor
CToDoSingleParse::CToDoSingleParse() :
CToDoBaseParser(),
m_lCounter(0)
{
}
//////////////////////////////////////////////////////////////////////////
// Constructor which set file name
CToDoSingleParse::CToDoSingleParse( const string FileName ) :
CToDoBaseParser( FileName ),
m_lCounter(0)
{
}
//////////////////////////////////////////////////////////////////////////
// Constructor which set file name and search word
CToDoSingleParse::CToDoSingleParse( const string FileName, const string SearchWord ) :
CToDoBaseParser( FileName ),
m_strSearchWord( SearchWord ),
m_lCounter(0)
{
}
//////////////////////////////////////////////////////////////////////////
// Destructor
CToDoSingleParse::~CToDoSingleParse()
{
ClearAllElements();
}
//////////////////////////////////////////////////////////////////////////
// Iterator function which point map to first element
TLineMapperIter * CToDoSingleParse::First() throw( CToDoNoElements )
{
if( m_lCounter == 0 )
throw CToDoNoElements();
m_mapIter = m_mapLines.begin();
return &m_mapIter;
}
//////////////////////////////////////////////////////////////////////////
// Function point map to last element
// WARNING: Last element in doesn't have any data and have no valid pointers
TLineMapperIter * CToDoSingleParse::Last() throw( CToDoNoElements )
{
if( m_lCounter == 0 ) throw CToDoNoElements();
m_mapIter = m_mapLines.end();
return &m_mapIter;
}
//////////////////////////////////////////////////////////////////////////
// Function move pointer in map to next element
TLineMapperIter * CToDoSingleParse::Next() throw( CToDoNoElements, CToDoLastRiched )
{
if( m_lCounter == 0 ) throw CToDoNoElements();
if( m_mapIter == m_mapLines.end() ) throw CToDoLastRiched();
m_mapIter++;
return &m_mapIter;
}
//////////////////////////////////////////////////////////////////////////
// Function move pointer in map to prev element
TLineMapperIter * CToDoSingleParse::Prev() throw( CToDoNoElements, CToDoFirstRiched )
{
if( m_lCounter == 0 ) throw CToDoNoElements();
if( m_mapIter == m_mapLines.begin() ) throw CToDoFirstRiched();
m_mapIter--;
return &m_mapIter;
}
//////////////////////////////////////////////////////////////////////////
// Engine of our class which do main work for us
long CToDoSingleParse::ParseEngine() throw( CToDoFileWork )
{
std::ifstream flParse( GetFileName().c_str() );
if( !flParse ) throw CToDoFileWork( );
long lCounter = 0;
char MainStr[ TODO_STRING_RESERVE ];
char FindedStr[ TODO_STRING_RESERVE ];
do
{
flParse.getline( MainStr, sizeof( MainStr ) );
lCounter++;
char *tmp = strstr( MainStr, m_strSearchWord.c_str() );
if( tmp != NULL )
{
strcpy( FindedStr, tmp + m_strSearchWord.length() );
m_mapLines.insert( TLineMapper::value_type( lCounter, FindedStr ) );
++m_lCounter;
FindedStr[0] = 0x0;
}
} while( !flParse.eof() );
flParse.close( );
return m_lCounter;
}
//////////////////////////////////////////////////////////////////////////
// Operator merge two classes
CToDoSingleParse &CToDoSingleParse::operator+( CToDoSingleParse &twice )
{
TLineMapperIter i;
for( i = m_mapLines.begin() ; i != m_mapLines.end() ; i++ )
{
long tmpLong = i->first;
string tmpString = i->second;
TLineMapperIter tmpIter = twice.m_mapLines.find( tmpLong );
// if nothing find then continue
if( tmpIter == twice.m_mapLines.end( ) ) continue;
long lLast = tmpLong, lNext;
do
{
tmpIter++;
// if end of map riched
if( tmpIter == twice.m_mapLines.end() ) break;
lNext = tmpIter->first;
if( lNext - lLast > 1 ) break;
tmpString += tmpIter->second;
lLast = lNext;
} while( 1 );
i->second = tmpString;
}
return *this;
};
//////////////////////////////////////////////////////////////////////////
//
void CToDoSingleParse::Merge( CToDoSingleParse &twice )
{
for( TLineMapperIter k = twice.m_mapLines.begin();
k != twice.m_mapLines.end(); k++ )
{
try
{
m_mapLines.insert( TLineMapper::value_type( k->first, k->second ) );
}
catch(...)
{
continue;
}
}
m_lCounter = m_mapLines.size();
};
//:> End of file
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.