LosaDev source snapshot 2019
This commit is contained in:
commit
c198676e97
20588 files changed
+8812878
No files matched your search
@@ -0,0 +1,214 @@
|
||||
|
||||
|
||||
#include "stdafx.h"
|
||||
|
||||
#include "ioDataChunk.h"
|
||||
|
||||
ioDataChunk::ioDataChunk()
|
||||
{
|
||||
m_pData = NULL;
|
||||
m_pPos = NULL;
|
||||
m_pEnd = NULL;
|
||||
m_iSize = 0;
|
||||
}
|
||||
|
||||
ioDataChunk::~ioDataChunk()
|
||||
{
|
||||
SAFEDELETEARRAY(m_pData);
|
||||
}
|
||||
|
||||
BYTE* ioDataChunk::Allocate( int iSize, const BYTE *pPtr )
|
||||
{
|
||||
assert( iSize > 0 );
|
||||
|
||||
SAFEDELETEARRAY( m_pData );
|
||||
m_pPos = NULL;
|
||||
m_pEnd = NULL;
|
||||
m_iSize = 0;
|
||||
|
||||
m_pData = new BYTE[ iSize + 1 ]; // +1 is for null
|
||||
if( m_pData )
|
||||
{
|
||||
m_pData[iSize] = '\0';
|
||||
|
||||
m_pPos = m_pData;
|
||||
m_pEnd = m_pData + iSize;
|
||||
m_iSize = iSize;
|
||||
|
||||
if( pPtr )
|
||||
{
|
||||
memcpy( m_pData, pPtr, iSize );
|
||||
}
|
||||
}
|
||||
|
||||
return m_pData;
|
||||
}
|
||||
|
||||
bool ioDataChunk::AllocateFromFile( const char *szName )
|
||||
{
|
||||
Clear();
|
||||
|
||||
HANDLE hFile = CreateFile( szName,
|
||||
GENERIC_READ,
|
||||
0,
|
||||
NULL,
|
||||
OPEN_EXISTING,
|
||||
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
|
||||
NULL );
|
||||
|
||||
if( INVALID_HANDLE_VALUE == hFile )
|
||||
return false;
|
||||
|
||||
DWORD dwFileSize = GetFileSize(hFile, NULL);
|
||||
if( INVALID_FILE_SIZE == dwFileSize )
|
||||
{
|
||||
CloseHandle( hFile );
|
||||
return false;
|
||||
}
|
||||
|
||||
m_pData = new BYTE[ dwFileSize + 1 ]; // +1 is for null
|
||||
m_pData[dwFileSize] = '\0';
|
||||
|
||||
DWORD dwReadedSize = 0;
|
||||
BOOL bRet = ReadFile( hFile,
|
||||
m_pData,
|
||||
dwFileSize,
|
||||
&dwReadedSize,
|
||||
NULL );
|
||||
|
||||
if( FALSE == bRet )
|
||||
{
|
||||
SAFEDELETEARRAY(m_pData);
|
||||
CloseHandle( hFile );
|
||||
return false;
|
||||
}
|
||||
|
||||
m_iSize = dwFileSize;
|
||||
m_pPos = m_pData;
|
||||
m_pEnd = m_pData + dwFileSize;
|
||||
|
||||
CloseHandle( hFile );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void ioDataChunk::Clear()
|
||||
{
|
||||
SAFEDELETEARRAY( m_pData );
|
||||
m_iSize = 0;
|
||||
}
|
||||
|
||||
int ioDataChunk::Read( void *pBuf, int iSize )
|
||||
{
|
||||
int iCnt = iSize;
|
||||
if( m_pPos + iSize > m_pEnd )
|
||||
iCnt = m_pEnd - m_pPos;
|
||||
|
||||
if( iCnt > 0 )
|
||||
{
|
||||
memcpy( pBuf, (const void*)m_pPos, iCnt );
|
||||
m_pPos += iCnt;
|
||||
}
|
||||
|
||||
return iCnt;
|
||||
}
|
||||
|
||||
void ioDataChunk::Seek( int iPos )
|
||||
{
|
||||
if( iPos <= m_iSize )
|
||||
{
|
||||
m_pPos = m_pData + iPos;
|
||||
}
|
||||
}
|
||||
|
||||
bool ioDataChunk::IsEOF() const
|
||||
{
|
||||
if( m_pPos >= m_pEnd )
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
int ioDataChunk::ReadUpTo( void *pBuf, int iSize, const char* pDelim )
|
||||
{
|
||||
int iPos = strcspn( (const char*)m_pPos, pDelim );
|
||||
if( iPos > iSize )
|
||||
iPos = iSize;
|
||||
|
||||
if( m_pPos + iPos > m_pEnd )
|
||||
iPos = m_pEnd - m_pPos;
|
||||
|
||||
if( iPos > 0 )
|
||||
{
|
||||
memcpy( pBuf, (const void*)m_pPos, iPos );
|
||||
}
|
||||
|
||||
m_pPos += iPos + 1;
|
||||
|
||||
return iPos;
|
||||
}
|
||||
|
||||
int ioDataChunk::SkipUpTo( const char *pDelim )
|
||||
{
|
||||
int iPos = strcspn( (const char*)m_pPos, pDelim );
|
||||
|
||||
if( m_pPos + iPos > m_pEnd )
|
||||
iPos = m_pEnd - m_pPos;
|
||||
|
||||
m_pPos += iPos + 1;
|
||||
|
||||
return iPos;
|
||||
}
|
||||
|
||||
void ioDataChunk::GetLine( std::string &line, bool bTrim )
|
||||
{
|
||||
char pBuf[512];
|
||||
|
||||
int iCount = ReadUpTo( pBuf, 511 );
|
||||
pBuf[iCount] = '\0';
|
||||
|
||||
line = pBuf;
|
||||
|
||||
if( bTrim )
|
||||
{
|
||||
int iLen = line.length();
|
||||
|
||||
int lSpace, rSpace;
|
||||
lSpace = rSpace = 0;
|
||||
|
||||
int i=0;
|
||||
for(int i=0 ;
|
||||
i<iLen && ( line.at(i) == ' ' || line.at(i) == '\t' || line.at(i) == '\r' ) ;
|
||||
++lSpace, i++ );
|
||||
|
||||
if( lSpace < iLen )
|
||||
{
|
||||
for(int i= iLen-1 ;
|
||||
i>=0 && ( line.at(i) == ' ' || line.at(i) == '\t' || line.at(i) == '\r' ) ;
|
||||
rSpace++, i-- );
|
||||
}
|
||||
|
||||
line = line.substr( lSpace, iLen - lSpace - rSpace );
|
||||
}
|
||||
}
|
||||
|
||||
void ioDataChunk::SkipToNextOpenBrace()
|
||||
{
|
||||
std::string line = "";
|
||||
|
||||
while( !IsEOF() && line !="{" )
|
||||
{
|
||||
GetLine( line );
|
||||
}
|
||||
}
|
||||
|
||||
void ioDataChunk::SkipToNextCloseBrace()
|
||||
{
|
||||
std::string line = "";
|
||||
|
||||
while( !IsEOF() && line.substr(0,1) != "}" )
|
||||
{
|
||||
GetLine( line );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
|
||||
|
||||
#ifndef _ioDataChunk_h_
|
||||
#define _ioDataChunk_h_
|
||||
|
||||
class ioDataChunk
|
||||
{
|
||||
private:
|
||||
BYTE *m_pData;
|
||||
BYTE *m_pPos;
|
||||
BYTE *m_pEnd;
|
||||
|
||||
int m_iSize;
|
||||
|
||||
public:
|
||||
BYTE* Allocate( int iSize, const BYTE *pPtr = NULL );
|
||||
bool AllocateFromFile( const char *szName );
|
||||
int Read( void *pBuf, int iSize );
|
||||
void Seek( int iPos );
|
||||
void Clear();
|
||||
|
||||
public:
|
||||
int ReadUpTo( void *pBuf, int iSize, const char* pDelim = "\n" );
|
||||
int SkipUpTo( const char* pDelim = "\n" );
|
||||
|
||||
void SkipToNextOpenBrace();
|
||||
void SkipToNextCloseBrace();
|
||||
|
||||
public:
|
||||
void GetLine( std::string &line, bool bTrim = true );
|
||||
bool IsEOF() const;
|
||||
|
||||
public:
|
||||
inline const BYTE* GetPtr() const { return m_pData; }
|
||||
inline BYTE* GetPtr() { return m_pData; }
|
||||
|
||||
inline int GetSize() const { return m_iSize; }
|
||||
|
||||
public:
|
||||
ioDataChunk();
|
||||
~ioDataChunk();
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,298 @@
|
||||
|
||||
|
||||
#include "stdafx.h"
|
||||
|
||||
#include "ioINIParser.h"
|
||||
#include "ioINILoader.h"
|
||||
#include <strsafe.h>
|
||||
|
||||
ioINILoader::ioINILoader()
|
||||
{
|
||||
ZeroMemory( m_szFileName, MAX_PATH );
|
||||
ZeroMemory( m_szTitle, MAX_PATH );
|
||||
m_pParser = NULL;
|
||||
}
|
||||
|
||||
ioINILoader::ioINILoader( const char *szFileName , bool bMemoryParsing )
|
||||
{
|
||||
ZeroMemory( m_szFileName, MAX_PATH );
|
||||
ZeroMemory( m_szTitle, MAX_PATH );
|
||||
|
||||
GetCurrentDirectory( MAX_PATH, m_szFileName );
|
||||
StringCbCat( m_szFileName, sizeof( m_szFileName ), "\\" );
|
||||
StringCbCat( m_szFileName, sizeof( m_szFileName ), szFileName );
|
||||
|
||||
m_pParser = NULL;
|
||||
|
||||
if(bMemoryParsing)
|
||||
{
|
||||
DoMemoryParsing();
|
||||
}
|
||||
}
|
||||
|
||||
ioINILoader::ioINILoader( const char *szPath, const char *szFileName , bool bMemoryParsing )
|
||||
{
|
||||
ZeroMemory( m_szFileName, MAX_PATH );
|
||||
ZeroMemory( m_szTitle, MAX_PATH );
|
||||
|
||||
StringCbPrintf( m_szFileName, sizeof( m_szFileName ), "%s/%s", szPath, szFileName );
|
||||
m_pParser = NULL;
|
||||
|
||||
if(bMemoryParsing)
|
||||
{
|
||||
DoMemoryParsing();
|
||||
}
|
||||
}
|
||||
|
||||
ioINILoader::~ioINILoader()
|
||||
{
|
||||
SAFEDELETE( m_pParser );
|
||||
}
|
||||
|
||||
void ioINILoader::SetFileName( const char *szFileName , bool bMemoryParsing )
|
||||
{
|
||||
StringCbCopy( m_szFileName, sizeof( m_szFileName ), szFileName );
|
||||
SAFEDELETE( m_pParser );
|
||||
|
||||
if(bMemoryParsing)
|
||||
{
|
||||
DoMemoryParsing();
|
||||
}
|
||||
}
|
||||
|
||||
void ioINILoader::SetTitle( const char *szTitle )
|
||||
{
|
||||
StringCbCopy( m_szTitle, sizeof( m_szTitle ), szTitle );
|
||||
}
|
||||
|
||||
bool ioINILoader::DoMemoryParsing()
|
||||
{
|
||||
SAFEDELETE( m_pParser );
|
||||
|
||||
m_pParser = new ioINIParser;
|
||||
if( m_pParser->ParsingFile( m_szFileName ) )
|
||||
return true;
|
||||
|
||||
SAFEDELETE( m_pParser );
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ioINILoader::DoMemoryParsingFromMemory( ioTextStream &rkStream )
|
||||
{
|
||||
SAFEDELETE( m_pParser );
|
||||
m_pParser = new ioINIParser;
|
||||
m_pParser->ParseINI( rkStream );
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ioINILoader::LoadBool( const char *szTitle, const char *szKeyName, bool bDefault )
|
||||
{
|
||||
int iValue = 0;
|
||||
if( bDefault )
|
||||
{
|
||||
iValue = 1;
|
||||
}
|
||||
|
||||
if( m_pParser )
|
||||
{
|
||||
const char *szValue = m_pParser->GetValue( szTitle, szKeyName );
|
||||
if( szValue )
|
||||
{
|
||||
iValue = atoi( szValue );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
iValue = GetPrivateProfileInt( szTitle, szKeyName, iValue, m_szFileName );
|
||||
}
|
||||
|
||||
if( iValue != 0 )
|
||||
return true;
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
int ioINILoader::LoadInt( const char *szTitle, const char *szKeyName, int iDefault )
|
||||
{
|
||||
int iValue = iDefault;
|
||||
if( m_pParser )
|
||||
{
|
||||
const char *szValue = m_pParser->GetValue( szTitle, szKeyName );
|
||||
if( szValue )
|
||||
{
|
||||
iValue = atoi( szValue );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
iValue = GetPrivateProfileInt( szTitle, szKeyName, iDefault, m_szFileName );
|
||||
}
|
||||
|
||||
return iValue;
|
||||
}
|
||||
|
||||
float ioINILoader::LoadFloat( const char *szTitle, const char *szKeyName, float fDefault )
|
||||
{
|
||||
float fValue = fDefault;
|
||||
|
||||
if( m_pParser )
|
||||
{
|
||||
const char *szValue = m_pParser->GetValue( szTitle, szKeyName );
|
||||
if( szValue )
|
||||
fValue = atof( szValue );
|
||||
else
|
||||
fValue = fDefault;
|
||||
}
|
||||
else
|
||||
{
|
||||
char szBuf[MAX_PATH];
|
||||
GetPrivateProfileString( szTitle, szKeyName, "", szBuf, MAX_PATH, m_szFileName );
|
||||
if( strcmp( szBuf, "" ) )
|
||||
fValue = (float)atof( szBuf );
|
||||
else
|
||||
fValue = fDefault;
|
||||
}
|
||||
|
||||
return fValue;
|
||||
}
|
||||
|
||||
void ioINILoader::LoadVector( const char *szTitle, Vector3 &vVec )
|
||||
{
|
||||
vVec.x = LoadFloat( szTitle, "X", 0.0f );
|
||||
vVec.y = LoadFloat( szTitle, "Y", 0.0f );
|
||||
vVec.z = LoadFloat( szTitle, "Z", 0.0f );
|
||||
}
|
||||
|
||||
void ioINILoader::LoadString( const char *szTitle,
|
||||
const char *szKeyName,
|
||||
const char *szDefault,
|
||||
char *szBuf,
|
||||
int iBufLen )
|
||||
{
|
||||
if( m_pParser )
|
||||
{
|
||||
const char *szValue = m_pParser->GetValue( szTitle, szKeyName );
|
||||
if( szValue )
|
||||
{
|
||||
StringCbCopyN( szBuf, iBufLen, szValue, iBufLen - 1 );
|
||||
}
|
||||
else
|
||||
{
|
||||
StringCbCopyN( szBuf, iBufLen, szDefault, iBufLen - 1 );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
GetPrivateProfileString( szTitle, szKeyName, szDefault, szBuf, iBufLen, m_szFileName );
|
||||
}
|
||||
}
|
||||
|
||||
bool ioINILoader::LoadBool( const char *szKeyName, bool bDefault )
|
||||
{
|
||||
return LoadBool( m_szTitle, szKeyName, bDefault );
|
||||
}
|
||||
|
||||
int ioINILoader::LoadInt( const char *szKeyName, int iDefault )
|
||||
{
|
||||
return LoadInt( m_szTitle, szKeyName, iDefault );
|
||||
}
|
||||
|
||||
float ioINILoader::LoadFloat( const char *szKeyName, float fDefault )
|
||||
{
|
||||
return LoadFloat( m_szTitle, szKeyName, fDefault );
|
||||
}
|
||||
|
||||
void ioINILoader::LoadVector( Vector3 &vVec )
|
||||
{
|
||||
LoadVector( m_szTitle, vVec );
|
||||
}
|
||||
|
||||
void ioINILoader::LoadString( const char *szKeyName,
|
||||
const char *szDefault,
|
||||
char *szBuf,
|
||||
int iBufLen )
|
||||
{
|
||||
LoadString( m_szTitle, szKeyName, szDefault, szBuf, iBufLen );
|
||||
}
|
||||
|
||||
void ioINILoader::SaveString( const char *szTitle, const char *szKeyName, const char *szBuf )
|
||||
{
|
||||
WritePrivateProfileString( szTitle, szKeyName, szBuf, m_szFileName );
|
||||
}
|
||||
|
||||
void ioINILoader::SaveInt( const char *szTitle, const char *szKeyName, int iValue )
|
||||
{
|
||||
char szBuf[MAX_PATH]="";
|
||||
StringCbPrintf( szBuf, sizeof( szBuf ), "%d", iValue );
|
||||
|
||||
WritePrivateProfileString( szTitle, szKeyName, szBuf, m_szFileName );
|
||||
}
|
||||
|
||||
void ioINILoader::SaveFloat( const char *szTitle, const char *szKeyName, float fValue, bool bNoLimit )
|
||||
{
|
||||
char szBuf[MAX_PATH]="";
|
||||
|
||||
if( bNoLimit )
|
||||
{
|
||||
StringCbPrintf( szBuf, sizeof( szBuf ), "%f", fValue );
|
||||
}
|
||||
else
|
||||
{
|
||||
StringCbPrintf( szBuf, sizeof( szBuf ), "%.2f", fValue );
|
||||
}
|
||||
|
||||
WritePrivateProfileString( szTitle, szKeyName, szBuf, m_szFileName );
|
||||
}
|
||||
|
||||
void ioINILoader::SaveBool( const char *szTitle, const char *szKeyName, bool bValue )
|
||||
{
|
||||
if( bValue )
|
||||
{
|
||||
WritePrivateProfileString( szTitle, szKeyName, "1", m_szFileName );
|
||||
}
|
||||
else
|
||||
{
|
||||
WritePrivateProfileString( szTitle, szKeyName, "0", m_szFileName );
|
||||
}
|
||||
}
|
||||
|
||||
void ioINILoader::SaveVector( const char *szTitle, const Vector3 &vVec )
|
||||
{
|
||||
char szBuf[MAX_PATH]="";
|
||||
|
||||
StringCbPrintf( szBuf, sizeof( szBuf ), "%.2f", vVec.x );
|
||||
WritePrivateProfileString( szTitle, "X", szBuf, m_szFileName );
|
||||
|
||||
StringCbPrintf( szBuf, sizeof( szBuf ), "%.2f", vVec.y );
|
||||
WritePrivateProfileString( szTitle, "Y", szBuf, m_szFileName );
|
||||
|
||||
StringCbPrintf( szBuf, sizeof( szBuf ), "%.2f", vVec.z );
|
||||
WritePrivateProfileString( szTitle, "Z", szBuf, m_szFileName );
|
||||
}
|
||||
|
||||
void ioINILoader::SaveString( const char *szKeyName, const char *szBuf )
|
||||
{
|
||||
SaveString( m_szTitle, szKeyName, szBuf );
|
||||
}
|
||||
|
||||
void ioINILoader::SaveInt( const char *szKeyName, int iValue )
|
||||
{
|
||||
SaveInt( m_szTitle, szKeyName, iValue );
|
||||
}
|
||||
|
||||
void ioINILoader::SaveFloat( const char *szKeyName, float fValue, bool bNoLimit )
|
||||
{
|
||||
SaveFloat( m_szTitle, szKeyName, fValue, bNoLimit );
|
||||
}
|
||||
|
||||
void ioINILoader::SaveBool( const char *szKeyName, bool bValue )
|
||||
{
|
||||
SaveBool( m_szTitle, szKeyName, bValue );
|
||||
}
|
||||
|
||||
void ioINILoader::SaveVector( const Vector3 &vVec )
|
||||
{
|
||||
SaveVector( m_szTitle, vVec );
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
|
||||
|
||||
#ifndef _ioINILoader_h_
|
||||
#define _ioINILoader_h_
|
||||
|
||||
class ioINIParser;
|
||||
class ioTextStream;
|
||||
|
||||
class ioINILoader
|
||||
{
|
||||
private:
|
||||
char m_szFileName[MAX_PATH];
|
||||
char m_szTitle[MAX_PATH];
|
||||
|
||||
ioINIParser *m_pParser;
|
||||
|
||||
public:
|
||||
void SetFileName( const char *szFileName , bool bMemoryParsing = true);
|
||||
void SetTitle( const char *szTitle );
|
||||
|
||||
bool DoMemoryParsing();
|
||||
bool DoMemoryParsingFromMemory( ioTextStream &rkStream );
|
||||
|
||||
const char* GetFileName() const { return m_szFileName; }
|
||||
const char* GetTitle() const { return m_szTitle; }
|
||||
|
||||
public:
|
||||
bool LoadBool( const char *szTitle, const char *szKeyName, bool bDefault );
|
||||
int LoadInt( const char *szTitle, const char *szKeyName, int iDefault );
|
||||
float LoadFloat( const char *szTitle, const char *szKeyName, float fDefault );
|
||||
void LoadVector( const char *szTitle, Vector3 &vVec );
|
||||
void LoadString( const char *szTitle,
|
||||
const char *szKeyName,
|
||||
const char *szDefault,
|
||||
char *szBuf,
|
||||
int iBufLen );
|
||||
|
||||
public:
|
||||
bool LoadBool( const char *szKeyName, bool bDefault );
|
||||
int LoadInt( const char *szKeyName, int iDefault );
|
||||
float LoadFloat( const char *szKeyName, float fDefault );
|
||||
void LoadVector( Vector3 &vVec );
|
||||
void LoadString( const char *szKeyName,
|
||||
const char *szDefault,
|
||||
char *szBuf,
|
||||
int iBufLen );
|
||||
|
||||
public:
|
||||
void SaveString( const char *szTitle, const char *szKeyName, const char *szBuf );
|
||||
void SaveInt( const char *szTitle, const char *szKeyName, int iValue );
|
||||
void SaveFloat( const char *szTitle, const char *szKeyName, float fValue, bool bNoLimit = false );
|
||||
void SaveBool( const char *szTitle, const char *szKeyName, bool bValue );
|
||||
void SaveVector( const char *szTitle, const Vector3 &vVec );
|
||||
|
||||
public:
|
||||
void SaveString( const char *szKeyName, const char *szBuf );
|
||||
void SaveInt( const char *szKeyName, int iValue );
|
||||
void SaveFloat( const char *szKeyName, float fValue, bool bNoLimit = false );
|
||||
void SaveBool( const char *szKeyName, bool bValue );
|
||||
void SaveVector( const Vector3 &vVec );
|
||||
|
||||
public:
|
||||
ioINILoader();
|
||||
ioINILoader( const char *szFileName , bool bMemoryParsing = true );
|
||||
ioINILoader( const char *szPath, const char *szFileName , bool bMemoryParsing = true );
|
||||
~ioINILoader();
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,245 @@
|
||||
|
||||
|
||||
#include "stdafx.h"
|
||||
|
||||
#include "ioINIParser.h"
|
||||
|
||||
#include "ioStream.h"
|
||||
//#include "../Window.h"
|
||||
//#include "../MainProcess.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
ioINIParser::ioINIParser()
|
||||
{
|
||||
m_pPreList = NULL;
|
||||
}
|
||||
|
||||
ioINIParser::~ioINIParser()
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
|
||||
bool ioINIParser::ParsingFile( const char *szFileName )
|
||||
{
|
||||
Clear();
|
||||
|
||||
ioTextStream kStream;
|
||||
if( !kStream.OpenFile( szFileName ) )
|
||||
{
|
||||
//LOG.PrintTimeAndLog( LOG_DEBUG_LEVEL, "ioINIParser::Parsing - %s Load Failed", szFileName );
|
||||
return false;
|
||||
}
|
||||
|
||||
ParseINI( kStream );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void ioINIParser::ParseINI( ioTextStream &rkStream )
|
||||
{
|
||||
std::string line, lastline;
|
||||
|
||||
while( !rkStream.IsEOF() )
|
||||
{
|
||||
if( lastline.empty() || lastline.at(0) != '[' )
|
||||
{
|
||||
rkStream.GetLine( line );
|
||||
}
|
||||
else
|
||||
{
|
||||
line = lastline;
|
||||
lastline.at(0) = '\0';
|
||||
}
|
||||
|
||||
if( line.length() == 0 || line.at(0) != '[' )
|
||||
continue;
|
||||
|
||||
lastline = ParseTitle( line, rkStream );
|
||||
}
|
||||
}
|
||||
|
||||
std::string ioINIParser::ParseTitle( const std::string &szTitle, ioTextStream &rkStream )
|
||||
{
|
||||
string line;
|
||||
|
||||
KeyList *pKeyList = new KeyList;
|
||||
if( !pKeyList )
|
||||
return line;
|
||||
|
||||
while( !rkStream.IsEOF() )
|
||||
{
|
||||
rkStream.GetLine( line );
|
||||
|
||||
if( line.length() == 0 || line.at(0) == ';' )
|
||||
continue;
|
||||
|
||||
if( line.at(0) == '[' ) // New Title
|
||||
break;
|
||||
|
||||
ParseKey( line, pKeyList );
|
||||
}
|
||||
|
||||
if( !pKeyList->empty() )
|
||||
{
|
||||
string szTitleText = szTitle.substr( 1, szTitle.length() - 2 );
|
||||
|
||||
ioStringConverter::toLowerCase( szTitleText );
|
||||
|
||||
m_TitleList.insert( TitleList::value_type( ioHashString(szTitleText.c_str()), pKeyList ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
delete pKeyList;
|
||||
}
|
||||
|
||||
return line;
|
||||
}
|
||||
|
||||
void ioINIParser::ParseKey( const std::string &line, KeyList *pKeyList )
|
||||
{
|
||||
const StringVector &vecparams = ioStringConverter::Split( line, "=", 1 );
|
||||
if( vecparams.size() != 2 || vecparams[0].empty() )
|
||||
return;
|
||||
|
||||
string szKeyName, szValue;
|
||||
|
||||
string::size_type i, tSize;
|
||||
tSize = vecparams[0].length();
|
||||
for(i=tSize-1 ; i>=0 ; i-- )
|
||||
{
|
||||
if( vecparams[0].at(i) != ' ' &&
|
||||
vecparams[0].at(i) != '\t' &&
|
||||
vecparams[0].at(i) != '\r' )
|
||||
{
|
||||
szKeyName = vecparams[0].substr( 0, i+1 );
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
tSize = vecparams[1].length();
|
||||
for(i=0 ; i<tSize ; i++ )
|
||||
{
|
||||
if( vecparams[1].at(i) != ' ' &&
|
||||
vecparams[1].at(i) != '\t' &&
|
||||
vecparams[1].at(i) != '\r' )
|
||||
{
|
||||
szValue = vecparams[1].substr( i );
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if( !szKeyName.empty() && !szValue.empty() )
|
||||
{
|
||||
ioStringConverter::toLowerCase( szKeyName );
|
||||
pKeyList->insert( KeyList::value_type( ioHashString(szKeyName.c_str()), szValue ) );
|
||||
}
|
||||
}
|
||||
|
||||
void ioINIParser::Clear()
|
||||
{
|
||||
TitleList::iterator iter;
|
||||
for( iter=m_TitleList.begin() ; iter!=m_TitleList.end() ; ++iter )
|
||||
{
|
||||
delete iter->second;
|
||||
}
|
||||
m_TitleList.clear();
|
||||
|
||||
m_PreTitle.Clear();
|
||||
m_pPreList = NULL;
|
||||
}
|
||||
|
||||
int ioINIParser::GetNumTotalTitle() const
|
||||
{
|
||||
return m_TitleList.size();
|
||||
}
|
||||
|
||||
int ioINIParser::GetNumTotalKey( int iTitleIdx ) const
|
||||
{
|
||||
if( COMPARE( iTitleIdx, 0, GetNumTotalTitle() ) )
|
||||
{
|
||||
TitleList::const_iterator iter = m_TitleList.begin();
|
||||
std::advance( iter, iTitleIdx );
|
||||
return iter->second->size();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char* ioINIParser::GetTitle( int iIdx ) const
|
||||
{
|
||||
if( COMPARE( iIdx, 0, GetNumTotalTitle() ) )
|
||||
{
|
||||
TitleList::const_iterator iter = m_TitleList.begin();
|
||||
std::advance( iter, iIdx );
|
||||
return iter->first.c_str();
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
const char* ioINIParser::GetKey( int iTitle, int iKey ) const
|
||||
{
|
||||
if( COMPARE( iTitle, 0, GetNumTotalTitle() ) )
|
||||
{
|
||||
TitleList::const_iterator iter = m_TitleList.begin();
|
||||
std::advance( iter, iTitle );
|
||||
|
||||
int iNumKey = iter->second->size();
|
||||
if( COMPARE( iKey, 0, iNumKey ) )
|
||||
{
|
||||
KeyList::const_iterator iKeyIter = iter->second->begin();
|
||||
std::advance( iKeyIter, iKey );
|
||||
return iKeyIter->first.c_str();
|
||||
}
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
const char* ioINIParser::GetValue( int iTitle, int iKey ) const
|
||||
{
|
||||
const char *pTitle = GetTitle( iTitle );
|
||||
const char *pKey = GetKey( iTitle, iKey );
|
||||
|
||||
return GetValue( pTitle, pKey );
|
||||
}
|
||||
|
||||
const char* ioINIParser::GetValue( const char *szTitle, const char *szKeyName ) const
|
||||
{
|
||||
char szLowTitle[MAX_PATH];
|
||||
strcpy_s( szLowTitle, szTitle );
|
||||
_strlwr_s( szLowTitle, sizeof(szLowTitle) );
|
||||
m_szTitleBuf = szLowTitle;
|
||||
|
||||
KeyList *pCurList = NULL;
|
||||
if( m_pPreList && m_PreTitle == m_szTitleBuf )
|
||||
{
|
||||
pCurList = m_pPreList;
|
||||
}
|
||||
else
|
||||
{
|
||||
TitleList::const_iterator iter = m_TitleList.find( m_szTitleBuf );
|
||||
if( iter != m_TitleList.end() )
|
||||
{
|
||||
pCurList = iter->second;
|
||||
|
||||
m_PreTitle = m_szTitleBuf;
|
||||
m_pPreList = pCurList;
|
||||
}
|
||||
}
|
||||
|
||||
if( pCurList )
|
||||
{
|
||||
char szLowKey[MAX_PATH];
|
||||
strcpy_s( szLowKey, szKeyName );
|
||||
_strlwr_s( szLowKey, sizeof(szLowKey) );
|
||||
m_szKeyNameBuf = szLowKey;
|
||||
|
||||
KeyList::const_iterator iKey = pCurList->find( m_szKeyNameBuf );
|
||||
if( iKey != pCurList->end() )
|
||||
return iKey->second.c_str();
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
|
||||
|
||||
#ifndef _ioINIParser_h_
|
||||
#define _ioINIParser_h_
|
||||
|
||||
|
||||
class ioTextStream;
|
||||
|
||||
class ioINIParser
|
||||
{
|
||||
private:
|
||||
typedef std::map< ioHashString, std::string > KeyList;
|
||||
typedef std::map< ioHashString, KeyList* > TitleList;
|
||||
|
||||
TitleList m_TitleList;
|
||||
|
||||
mutable ioHashString m_PreTitle; // 이전에 사용한 타이틀 캐시용도
|
||||
mutable KeyList *m_pPreList;
|
||||
|
||||
mutable ioHashString m_szTitleBuf;
|
||||
mutable ioHashString m_szKeyNameBuf;
|
||||
|
||||
public:
|
||||
bool ParsingFile( const char *szFileName );
|
||||
void ParseINI( ioTextStream &rkStream );
|
||||
void Clear();
|
||||
|
||||
private:
|
||||
std::string ParseTitle( const std::string &szTitle, ioTextStream &rkStream );
|
||||
void ParseKey( const std::string &line, KeyList *pKeyList );
|
||||
|
||||
public:
|
||||
int GetNumTotalTitle() const;
|
||||
int GetNumTotalKey( int iTitle ) const;
|
||||
|
||||
public: // 없으면 ""
|
||||
const char* GetTitle( int iIdx ) const;
|
||||
const char* GetKey( int iTitle, int iKey ) const;
|
||||
|
||||
public: // 없으면 NULL
|
||||
const char* GetValue( int iTitle, int iKey ) const;
|
||||
const char* GetValue( const char *szTitle, const char *szKeyName ) const;
|
||||
|
||||
public:
|
||||
ioINIParser();
|
||||
~ioINIParser();
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,74 @@
|
||||
|
||||
|
||||
#include "stdafx.h"
|
||||
#include "ioStream.h"
|
||||
|
||||
ioTextStream::ioTextStream()
|
||||
{
|
||||
}
|
||||
|
||||
ioTextStream::~ioTextStream()
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
|
||||
bool ioTextStream::OpenFile( const char *szFileName )
|
||||
{
|
||||
return m_DataChunk.AllocateFromFile( szFileName );
|
||||
}
|
||||
|
||||
BYTE* ioTextStream::OpenMemory( int iSize, BYTE *pBuf )
|
||||
{
|
||||
return m_DataChunk.Allocate( iSize, pBuf );
|
||||
}
|
||||
|
||||
void ioTextStream::Clear()
|
||||
{
|
||||
m_DataChunk.Clear();
|
||||
}
|
||||
|
||||
const BYTE* ioTextStream::GetPtr() const
|
||||
{
|
||||
return m_DataChunk.GetPtr();
|
||||
}
|
||||
|
||||
BYTE* ioTextStream::GetPtr()
|
||||
{
|
||||
return m_DataChunk.GetPtr();
|
||||
}
|
||||
|
||||
int ioTextStream::GetSize() const
|
||||
{
|
||||
return m_DataChunk.GetSize();
|
||||
}
|
||||
|
||||
int ioTextStream::ReadUpTo( void *pBuf, int iSize, const char* pDelim )
|
||||
{
|
||||
return m_DataChunk.ReadUpTo( pBuf, iSize, pDelim );
|
||||
}
|
||||
|
||||
int ioTextStream::SkipUpTo( const char* pDelim )
|
||||
{
|
||||
return m_DataChunk.SkipUpTo( pDelim );
|
||||
}
|
||||
|
||||
void ioTextStream::SkipToNextOpenBrace()
|
||||
{
|
||||
m_DataChunk.SkipToNextOpenBrace();
|
||||
}
|
||||
|
||||
void ioTextStream::SkipToNextCloseBrace()
|
||||
{
|
||||
m_DataChunk.SkipToNextCloseBrace();
|
||||
}
|
||||
|
||||
void ioTextStream::GetLine( std::string &line, bool bTrim )
|
||||
{
|
||||
m_DataChunk.GetLine( line, bTrim );
|
||||
}
|
||||
|
||||
bool ioTextStream::IsEOF() const
|
||||
{
|
||||
return m_DataChunk.IsEOF();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
|
||||
|
||||
#ifndef _ioStream_h_
|
||||
#define _ioStream_h_
|
||||
|
||||
#include "ioDataChunk.h"
|
||||
|
||||
class ioStream
|
||||
{
|
||||
public:
|
||||
virtual bool OpenFile( const char *szFileName ) = 0;
|
||||
virtual BYTE* OpenMemory( int iSize, BYTE *pBuf = NULL ) = 0;
|
||||
virtual void Clear() = 0;
|
||||
|
||||
public:
|
||||
virtual const BYTE* GetPtr() const = 0;
|
||||
virtual BYTE* GetPtr() = 0;
|
||||
virtual int GetSize() const = 0;
|
||||
|
||||
public:
|
||||
ioStream(){}
|
||||
virtual ~ioStream(){}
|
||||
};
|
||||
|
||||
|
||||
class ioTextStream : public ioStream
|
||||
{
|
||||
protected:
|
||||
ioDataChunk m_DataChunk;
|
||||
|
||||
public:
|
||||
virtual bool OpenFile( const char *szFileName );
|
||||
virtual BYTE* OpenMemory( int iSize, BYTE *pBuf = NULL );
|
||||
virtual void Clear();
|
||||
|
||||
public:
|
||||
virtual const BYTE* GetPtr() const;
|
||||
virtual BYTE* GetPtr();
|
||||
virtual int GetSize() const;
|
||||
|
||||
public:
|
||||
int ReadUpTo( void *pBuf, int iSize, const char* pDelim = "\n" );
|
||||
int SkipUpTo( const char* pDelim = "\n" );
|
||||
|
||||
void SkipToNextOpenBrace();
|
||||
void SkipToNextCloseBrace();
|
||||
|
||||
public:
|
||||
void GetLine( std::string &line, bool bTrim = true );
|
||||
bool IsEOF() const;
|
||||
|
||||
|
||||
public:
|
||||
ioTextStream();
|
||||
virtual ~ioTextStream();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user