port from perforce
This commit is contained in:
89
aiwaz/Aiwaz/FileSystem/CFileLoader/CFileLoader.cpp
Normal file
89
aiwaz/Aiwaz/FileSystem/CFileLoader/CFileLoader.cpp
Normal file
@@ -0,0 +1,89 @@
|
||||
#include "stdafx.h"
|
||||
#include "CFileLoader.h"
|
||||
|
||||
#include "IFileSystem.h"
|
||||
|
||||
|
||||
CFileLoader::~CFileLoader()
|
||||
{
|
||||
this->FreeFile();
|
||||
}
|
||||
|
||||
|
||||
bool CFileLoader::LoadFile(string16 argFilename)
|
||||
{
|
||||
m_File = m_Engine->get_FileSystem().Open(argFilename);
|
||||
if (m_File != NULL)
|
||||
{
|
||||
std::stringstream dataStream;
|
||||
dataStream.write(m_File->get_Buffer(), m_File->get_BufferLength());
|
||||
|
||||
while (!dataStream.eof())
|
||||
{
|
||||
m_CurrentLineOfCode++;
|
||||
|
||||
std::string currentReadLine;
|
||||
std::getline(dataStream, currentReadLine);
|
||||
if (currentReadLine.empty())
|
||||
continue;
|
||||
|
||||
std::vector<std::string> token = this->SplitString(currentReadLine);
|
||||
if (token.empty())
|
||||
continue;
|
||||
|
||||
m_CurrentSection = token[0];
|
||||
if (!this->OnSectionBegin(m_CurrentSection, token))
|
||||
{
|
||||
this->FreeFile();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (token[token.size() - 1] == "{{")
|
||||
m_EndSectionMarker = "}}";
|
||||
else
|
||||
m_EndSectionMarker = "}";
|
||||
|
||||
while (!dataStream.eof())
|
||||
{
|
||||
m_CurrentLineOfCode++;
|
||||
std::getline(dataStream, currentReadLine);
|
||||
if (currentReadLine.empty())
|
||||
continue;
|
||||
|
||||
std::vector<std::string> token = this->SplitString(currentReadLine, ", \t()\r");
|
||||
if (token.empty())
|
||||
continue;
|
||||
|
||||
if (token[0] == m_EndSectionMarker)
|
||||
{
|
||||
if (!this->OnSectionEnd(m_CurrentSection))
|
||||
{
|
||||
this->FreeFile();
|
||||
return false;
|
||||
}
|
||||
m_CurrentSection = "";
|
||||
break;
|
||||
}
|
||||
|
||||
if (!this->OnContentReceived(token, currentReadLine))
|
||||
{
|
||||
this->FreeFile();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this->FreeFile();
|
||||
return true;
|
||||
}
|
||||
this->FreeFile();
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void CFileLoader::FreeFile()
|
||||
{
|
||||
if (m_File != NULL)
|
||||
delete m_File;
|
||||
m_File = NULL;
|
||||
}
|
||||
73
aiwaz/Aiwaz/FileSystem/CFileLoader/CFileLoader.h
Normal file
73
aiwaz/Aiwaz/FileSystem/CFileLoader/CFileLoader.h
Normal file
@@ -0,0 +1,73 @@
|
||||
#pragma once
|
||||
|
||||
#include "IEngine.h"
|
||||
#include "IFileSystem.h"
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
class CFileLoader
|
||||
{
|
||||
public:
|
||||
CFileLoader(IEngine* ar_Engine_) : m_Engine(ar_Engine_),m_CurrentLineOfCode(0), m_File(NULL),m_EndSectionMarker("}") {}
|
||||
virtual ~CFileLoader();
|
||||
|
||||
bool LoadFile(string16 argFilename);
|
||||
const std::string& get_CurrentSection() const { return m_CurrentSection; }
|
||||
|
||||
virtual bool OnSectionBegin(const std::string& argContentName, const std::vector<std::string>& argContentToken) { return true; }
|
||||
virtual bool OnContentReceived(const std::vector<std::string>& argContentToken, const std::string& argContentData) { return true; }
|
||||
virtual bool OnSectionEnd(const std::string& argContentName) { return true; }
|
||||
|
||||
unsigned int get_CurrentLineNumber() const { return m_CurrentLineOfCode; }
|
||||
|
||||
protected:
|
||||
std::vector<std::string> SplitString(const std::string & argString, const std::string & argDelimiters=", \t\r")
|
||||
{
|
||||
// Skip delims at beginning, find start of first token
|
||||
std::string::size_type lastPos = argString.find_first_not_of(argDelimiters, 0);
|
||||
// Find next delimiter @ end of token
|
||||
std::string::size_type pos = argString.find_first_of(argDelimiters, lastPos);
|
||||
|
||||
// output vector
|
||||
std::vector<std::string> tokens;
|
||||
while (std::string::npos != pos || std::string::npos != lastPos)
|
||||
{
|
||||
// Found a token, add it to the vector.
|
||||
tokens.push_back(argString.substr(lastPos, pos - lastPos));
|
||||
// Skip delims. Note the "not_of". this is beginning of token
|
||||
lastPos = argString.find_first_not_of(argDelimiters, pos);
|
||||
// Find next delimiter at end of token.
|
||||
pos = argString.find_first_of(argDelimiters, lastPos);
|
||||
}
|
||||
|
||||
return tokens;
|
||||
}
|
||||
|
||||
std::wstring ConvertToString16(const std::string& argString) const
|
||||
{
|
||||
int codePage = 0;
|
||||
if (argString.size() == 0)
|
||||
return std::wstring();
|
||||
|
||||
int length = ::MultiByteToWideChar(codePage, 0, argString.c_str(), argString.size() + 1, NULL, 0);
|
||||
if (length == 0)
|
||||
return std::wstring();
|
||||
|
||||
wchar_t* newString = new wchar_t[length];
|
||||
::MultiByteToWideChar(codePage, 0, argString.c_str(), argString.size() + 1, newString, length);
|
||||
return std::wstring(newString);
|
||||
}
|
||||
|
||||
IEngine* m_Engine;
|
||||
|
||||
void FreeFile();
|
||||
|
||||
private:
|
||||
std::string m_CurrentSection;
|
||||
IFile* m_File;
|
||||
|
||||
std::string m_EndSectionMarker;
|
||||
unsigned int m_CurrentLineOfCode;
|
||||
};
|
||||
Reference in New Issue
Block a user