#include "stdafx.h"
#include "inifile.h"
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
// Only valid if C++
#ifndef __cplusplus
#error C++ compiler required.
#endif
// In the event you want to trace the calls can define _TRACE_CINIFILE
#ifdef _TRACE_CINIFILE
#define _CINIFILE_DEBUG
#endif
// _CRLF is used in the Save() function
// The class will read the correct data regardless of how the file linefeeds are defined <CRLF> or <CR>
// It is best to use the linefeed that is default to the system. This reduces issues if needing to modify
// the file with ie. notepad.exe which doesn't recognize unix linefeeds.
#ifdef _WIN32 // Windows default is \r\n
#ifdef _FORCE_UNIX_LINEFEED
#define _CRLFA "\n"
#define _CRLFW L"\n"
#else
#define _CRLFA "\r\n"
#define _CRLFW L"\r\n"
#endif
#else // Standard format is \n for unix
#ifdef _FORCE_WINDOWS_LINEFEED
#define _CRLFA "\r\n"
#define _CRLFW L"\r\n"
#else
#define _CRLFA "\n"
#define _CRLFW L"\n"
#endif
#endif
// Convert wstring to string
std::string wstr_to_str(const std::wstring& arg)
{
std::string res( arg.length(), '\0' );
size_t cot;
wcstombs_s(&cot, const_cast<char*>(res.data()) , arg.length(),arg.c_str(), arg.length());
return res;
}
// Convert string to wstring
std::wstring str_to_wstr(const std::string& arg)
{
std::wstring res(arg.length(), L'\0');
size_t cot;
mbstowcs_s(&cot, const_cast<wchar_t*>(res.data()), arg.length(), arg.c_str(), arg.length());
return res;
}
// Helper Functions
void RTrim(std::string &str, const std::string& chars = " \t")
{
#ifdef _CINIFILE_DEBUG
std::cout << "RTrim()" << std::endl;
#endif
str.erase(str.find_last_not_of(chars)+1);
}
void LTrim(std::string &str, const std::string& chars = " \t" )
{
#ifdef _CINIFILE_DEBUG
std::cout << "LTrim()" << std::endl;
#endif
str.erase(0, str.find_first_not_of(chars));
}
void Trim( std::string& str , const std::string& chars = " \t" )
{
#ifdef _CINIFILE_DEBUG
std::cout << "Trim()" << std::endl;
#endif
str.erase(str.find_last_not_of(chars)+1);
str.erase(0, str.find_first_not_of(chars));
}
// Stream Helpers
std::ostream& operator<<(std::ostream& output, CIniFileA& obj)
{
obj.Save( output );
return output;
}
std::istream& operator>>(std::istream& input, CIniFileA& obj)
{
obj.Load( input );
return input;
}
std::istream& operator>>(std::istream& input, CIniMergeA merger)
{
return merger(input);
}
// CIniFileA class methods
CIniFileA::CIniFileA()
{
#ifdef _CINIFILE_DEBUG
std::cout << "CIniFileA::CIniFileA()" << std::endl;
#endif
}
CIniFileA::~CIniFileA()
{
#ifdef _CINIFILE_DEBUG
std::cout << "CIniFileA::~CIniFileA()" << std::endl;
#endif
RemoveAllSections( );
}
const char* const CIniFileA::LF = _CRLFA;
void CIniFileA::Save( std::ostream& output )
{
std::string sSection;
for( SecIndexA::iterator itr = m_sections.begin() ; itr != m_sections.end() ; ++itr )
{
sSection = "[" + (*itr)->GetSectionName() + "]";
#ifdef _CINIFILE_DEBUG
std::cout << "Writing Section " << sSection << std::endl;
#endif
output << sSection << _CRLFA;
for( KeyIndexA::iterator klitr = (*itr)->m_keys.begin() ; klitr != (*itr)->m_keys.end() ; ++klitr )
{
std::string sKey = (*klitr)->GetKeyName() + "=" + (*klitr)->GetValue();
#ifdef _CINIFILE_DEBUG
std::cout << "Writing Key [" << sKey << "]" << std::endl;
#endif
output << sKey << _CRLFA;
}
}
}
bool CIniFileA::Save( const std::string& fileName )
{
#ifdef _CINIFILE_DEBUG
std::cout << "CIniFileA::Save() - " << fileName << std::endl;
#endif
std::ofstream output;
output.open( fileName.c_str() , std::ios::binary );
if( !output.is_open() )
return false;
Save(output);
output.close();
return true;
}
void CIniFileA::Create(const std::string& fileName)
{
FILE* file = NULL;
fopen_s(&file,fileName.c_str(),"a+");
fclose(file);
}
void CIniFileA::Load( std::istream& input , bool bMerge )
{
if( !bMerge )
RemoveAllSections();
CIniSectionA* pSection = NULL;
std::string sRead;
enum { KEY , SECTION , COMMENT , OTHER };
while( std::getline( input , sRead ) )
{
// Trim all whitespace on the left
LTrim( sRead );
// Trim any returns
RTrim( sRead , "\n\r");
if( !sRead.empty() )
{
unsigned int nType = ( sRead.find_first_of("[") == 0 && ( sRead[sRead.find_last_not_of(" \t\r\n")] == ']' ) ) ? SECTION : OTHER ;
nType = ( (nType == OTHER) && ( sRead.find_first_of("=") != std::string::npos && sRead.find_first_of("=") > 0 ) )? KEY : nType ;
nType = ( (nType == OTHER) && ( sRead.find_first_of("#") == 0) ) ? COMMENT : nType ;
switch( nType )
{
case SECTION:
#ifdef _CINIFILE_DEBUG
std::cout << "Parsing: Secton - " << sRead << std::endl;
#endif
pSection = AddSection( sRead.substr( 1 , sRead.size() - 2 ) );
break;
case KEY:
{
#ifdef _CINIFILE_DEBUG
std::cout << "Parsing: Key - " << sRead << std::endl;
#endif
// Check to ensure valid section... or drop the keys listed
if( pSection )
{
size_t iFind = sRead.find_first_of("=");
std::string sKey = sRead.substr(0,iFind);
std::string sValue = sRead.substr(iFind + 1);
CIniKeyA* pKey = pSection->AddKey( sKey );
if( pKey )
{
#ifdef _CINIFILE_DEBUG
std::cout << "Parsing: Key Value - " << sValue << std::endl;
#endif
pKey->SetValue( sValue );
}
}
}
break;
case COMMENT:
#ifdef _CINIFILE_DEBUG
std::cout << "Parsing: Comment - " << sRead << std::endl;
#endif
break;
case OTHER:
#ifdef _CINIFILE_DEBUG
std::cout << "Parsing: Other - " << sRead << std::endl;
#endif
break;
}
}
}
}
bool CIniFileA::Load(const std::string& fileName , bool bMerge )
{
#ifdef _CINIFILE_DEBUG
std::cout << "CIniFileA::Load() - " << fileName << std::endl;
#endif
std::ifstream input;
input.open( fileName.c_str() , std::ios::binary);
if( !input.is_open() )
return false;
Load( input , bMerge );
input.close();
return true;
}
const SecIndexA& CIniFileA::GetSections() const
{
#ifdef _CINIFILE_DEBUG
std::cout << "CIniFileA::GetSections()" << std::endl;
#endif
return m_sections;
}
CIniSectionA* CIniFileA::GetSection( std::string sSection ) const
{
#ifdef _CINIFILE_DEBUG
std::cout << "CIniFileA::GetSection()" << std::endl;
#endif
Trim(sSection);
SecIndexA::const_iterator itr = _find_sec( sSection );
if( itr != m_sections.end() )
return *itr;
return NULL;
}
CIniSectionA* CIniFileA::AddSection( std::string sSection )
{
#ifdef _CINIFILE_DEBUG
std::cout << "CIniFileA::AddSection()" << std::endl;
#endif
Trim(sSection);
SecIndexA::const_iterator itr = _find_sec( sSection );
if( itr == m_sections.end() )
{
// Note constuctor doesn't trim the string so it is trimmed above
CIniSectionA* pSection = new CIniSectionA( this , sSection );
m_sections.insert(pSection);
return pSection;
}
else
return *itr;
}
std::string CIniFileA::intTostr(int nDes)
{
std::string str;
std::stringstream s;
s<<nDes;
s>>str;
return str;
}
int CIniFileA::strToint(const std::string& str)
{
int n = 0;
std::stringstream s;
s<<str;
s>>n;
return n;
}
std::string CIniFileA::GetKeyValue( const std::string& sSection, const std::string& sKey ) const
{
#ifdef _CINIFILE_DEBUG
std::cout << "CIniFileA::GetKeyValue()" << std::endl;
#endif
st
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
inifile.rar (2个子文件)
inifile
inifile.h 21KB
inifile.cpp 34KB
共 2 条
- 1
资源评论
- xdfuse2021-12-13不太好,使用比较麻烦
sizeof("me")
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功