﻿#include "ProFile.h"
#include <iostream>     // std::cout
#include <fstream>      // std::ifstream
#include "Utf8_16.h"


// WORD wSignature = 0xFEFF;
// WORD wSr = 0x000D;
// WORD wSn = 0x000A;

CSection::CSection(void){
}
CSection::~CSection(void){
	/*
	int i;

	std::map<std::wstring, std::wstring>::iterator it = m_pcField.begin();
	if (;m_pcField!= m.end();it++) { 
		it->second.

	}

	for(i=0;i<m_pcField.size();i++){
		if(m_pcField[i]!=NULL)
			delete m_pcField[i];
	}
	m_pcField.RemoveAll();
	*/
}
CProFile::CProFile(void)
{
	nOutInFlg=0;
	m_iFileType=0; // unicode
	m_iIndexOfSection=-1;
	m_Ch=L'=';
	m_pSection = NULL;
	// this->AddSection(L"default");
}


CProFile::~CProFile(void)
{
	// int i,j;
	// BOOL l_bRet;
	std::wstring sLine;

	// not for write
	if ( 2!=nOutInFlg ){
		Clear();
		return;
	}
	FILE *fp;
	fp=_wfopen(m_FileName.c_str(), L"wb" );
	Utf16_Write writer;
	writer.setfile(fp);
	writer.setEncoding(Utf8_16::eUtf8_bom);
	SectionType::iterator it = m_apSection.begin();

	std::wstring line;
	for( ;it!= m_apSection.end();it++){ 
		if ( it->first == L"default"){
			continue;
		}
		// line+=L"["+ it->first + L"]\r\n";
		line =L"["+ it->first + L"]\r\n";
		writer.fwrite(line.c_str(), line.length());

		FieldType&	Field=it->second->GetField();
		FieldType::iterator fit = Field.begin();
		for (  ;fit!= Field.end();fit++) { 
			// line +=fit->first + L"=" + fit->second + L"\r\n";
			line =fit->first + L"=" + fit->second + L"\r\n";
			writer.fwrite(line.c_str(), line.length());
			
		}
		delete it->second;
	}
	// writer.fwrite(line.c_str(), line.length());

}
BOOL CProFile::OpenForRead(const wchar_t* lpszFileName)
{
	nOutInFlg=1; // in
	int ret =Open(lpszFileName);
	if ( ret> 0){
		nOutInFlg=-1; // error
		return false;
	}
	return true;
}
BOOL CProFile::OpenForWrite(const wchar_t* lpszFileName)
{
	
	nOutInFlg=2; // out
	m_FileName=lpszFileName;
	FILE *fp=_wfopen(m_FileName.c_str(), L"wb" );
	if (!fp){
		nOutInFlg=-1; // error
		return false;
	}
	fclose(fp);

	return true;

}
int CProFile::Open(const wchar_t* lpszFileName)
{

	Utf16_Read convert;
	if (!convert.readFile(lpszFileName)) {
		return 1;
	}

	if(convert.m_nFileSize <2 ){
		return 2;
	}

	// not for read
	if ( 1!=nOutInFlg ){
		return 0;
	}

	wchar_t szTemp[500];
	while(convert.getline(szTemp,sizeof(szTemp)/sizeof(wchar_t)))
	{
		if( szTemp[0]!=L'\0' ){
			this->Analysis(szTemp);
		}
	}
	return 0;

}

void CProFile::Analysis(std::wstring sLine)
{
	int l_iLen,i;
	std::wstring l_sSection,l_sEntry,l_sValue;
	wchar_t l_tCharStart,l_tCharEnd;
	//strtools::ltrim(sLine);

	strtools::trim(sLine);
	l_iLen=sLine.size();
	i=0;

	l_tCharStart=sLine[0];
	l_tCharEnd=sLine[l_iLen-1];
	if((l_tCharStart==L'['||l_tCharStart==L'［')&&
		(l_tCharEnd==L']'||l_tCharEnd==L'］'))
	{
		l_sSection=sLine.substr(1,l_iLen-2);
		strtools::ltrim(l_sSection);
		strtools::rtrim(l_sSection);
		this->AddSection(l_sSection);
	}else
	{
		while(i<l_iLen){
			if(sLine[i]==this->m_Ch)
			{
				break;
			}
			i++;
		}
		if(i<l_iLen)
		{
			l_sEntry=sLine.substr(0, i);
			l_sValue=sLine.substr(i+1);
			//strtools::ltrim(l_sEntry);
			//strtools::rtrim(l_sEntry);
			strtools::trim(l_sEntry);
			//strtools::ltrim(l_sValue);
			//strtools::rtrim(l_sValue);
			strtools::trim(l_sValue);


			// this->m_apSection[m_iIndexOfSection]->AddField(l_sEntry,l_sValue);
			if ( m_pSection )
				m_pSection->AddField(l_sEntry,l_sValue);
		}

	}

}

BOOL CProFile::test()
{
	Open(L"C:\\Documents and Settings\\Administrator.NDCPC16-2\\僨僗僋僩僢僾\\嬥丵work\\test.txt");
	// int sss=m_Section.GetSize();
	//this->clear();
	//sss=m_Section.GetSize();
	//this->AddSection(L"kkkkkkkk") ;

	
	//std::wstring s1 = L"ddddddd";
	//std::wstring v1 = L"!!!!!!!!";
	//m_Section[m_iIndexOfSection]->AddField( s1,v1);
//	s1 = L"222";
//	 v1 = L"333333333";
//	m_Section[m_iIndexOfSection]->AddField(s1,v1);
	/*
  std::wstring sss = "ddddddd";
  AddSection(sss) ;
  m_Section[0]->AddField("11111","tttttt");
  */
	return true;
}

void CProFile::Clear()
{
	SectionType::iterator it = m_apSection.begin();
	for (;it!= m_apSection.end();it++) { 
		delete it->second;
	}
	// this->AddSection(L"default");
}


void CProFile::SelectSection( const std::wstring& sSection)
{
	SectionType::iterator it = m_apSection.begin();
	for (;it!= m_apSection.end();it++) { 
		if ( it->first == sSection){
			m_pSection=m_apSection[sSection];
			break;
		}

	}
}

std::wstring CProFile::GetValue(std::wstring& sSection, std::wstring& sEntry) 
{
	// int i;
	SelectSection(sSection);
	if(!m_pSection)
		return L"";

	FieldType&	Field=m_pSection->GetField();
	FieldType::iterator fit = Field.begin();
	for (;fit!= Field.end();fit++) { 
		if (fit->first == sEntry ){
			return fit->second;
		}
	}
	return L"";
}