Current location - Health Preservation Learning Network - Healthy weight loss - How to write a class that reads files in C++? Find the password ... kneel down
How to write a class that reads files in C++? Find the password ... kneel down
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

#ifndef? INIFILE_H_

# Definition? INIFILE_H_

# If? _MSC_VER? & gt? 1000

#pragma? once

#endif? //? _MSC_VER? & gt? 1000

# If? _MSC_VER? & gt? 1000

#pragma? Warning (Disabled: 4786)

#endif

# Contains? & ltvector & gt

# Contains? & lt string & gt

# Contains? & ltstring.h & gt

//TOFIX? Use? Some? Kind? od? Map<& gt? Where to? Accelerate? Search?

Use? Namespace? std

Class? IniFile

{

//Ini? Documents? key

Class? IniKey

{

Public:

IniKey(){ };

IniKey(const? IniKey? & that) {? Operator? = (that); ? }

Invalid? Operator? =? (const? IniKey? & that)

{

If (this? ! =? & that) {//? No? Copy? yourself

m_name? =? that.m _ name

m_value? =? that.m _ value

}

}

//Need? For what? Search? Keys? catalogue

Bull? Operator? ==? (const? IniKey? & that)? {? Return? 0? ==? strcmp(m_name.c_str(),? that . m _ name . c _ str()); ? };

String? M _ name// string

String? m _ value

};

//ini? Documents? part

Class? IniSection

{

Public:

IniSection(){ };

IniSection(const? Department? & that) {? Operator? = (that); ? }

Invalid? Operator? =? (const? Department? & that)

{

If (this? ! =? & that) {//? No? Copy? yourself

m_name? =? that.m _ name

m_Keys? =? that.m _ Keys

}

}

//Need? For what? Search? Festival? catalogue

Bull? Operator? ==? (const? Department? & that)? {? Return? 0? ==? strcmp(m_name.c_str(),? that . m _ name . c _ str()); ? };

String? m _ name

Vector & ltIniKey & gt? m _ Keys

};

typedef? Vector & ltinikey & gt * iteratorinikeyeerator;

typedef? Vector & ltinisation & gt * iterator;

Public:

IniFile();

Virtual? ~ IniFile();

Bull? Load(const? Charles? * SZ file);

Bull? save();

Embedded? boolGetDirty()? const? {? Return? m _ bDirty? }

Embedded? String? GetPath()? const? {? Return? m _ strPath? }

Invalid? SetPath(const? Charles? *szFile)? {? m_strPath? =? szFile? }

//

Bull? clear all();

//Read? Values? Are you online? Different? format

Bull? GetValue(const? Charles? *szSection,? const? Charles? *szKey,? String? & value,? const? Charles? * SZ default);

Bull? GetValue(const? Charles? *szSection,? const? Charles? *szKey,? int? & ampn value,? int? ndedefault? =? 0);

Bull? GetValue(const? Charles? *szSection,? const? Charles? *szKey,? Not signed? Short? & ampn value,? int? ndedefault? =? 0);

Bull? GetValue(const? Charles? *szSection,? const? Charles? *szKey,? Bull? & ampb value,? int? ndedefault? =? 0);

//TOFIX? Support? Comments?

Bull? SetValue(const? Charles? *szSection,? const? Charles? *szKey,? const? Charles? * SZ val);

Bull? SetValue(const? Charles? *szSection,? const? Charles? *szKey,? const? int? nValue);

//bool? SetValue(const? Charles? *szSection,? const? Charles? *szKey,? const? Bull? & ampb value);

Bull? KeyExists(const? Charles? *szSection,? const? Charles? * szKey);

Bull? SectionExists(const? Charles? * SZ section);

Bull? AddSection(const? Charles? * SZ section);

Bull? RemoveSection (constant? Charles? * SZ section);

Bull? RemoveKey(const? Charles? *szSection,? const? Charles? * szKey);

//TOFIX? Support? Section/key? Enumeration? -& gt; ? GetXXXCount,? GetXXXName (index)

Vector & lt string & gt? get sections();

Protected:

Bull? FindKey(IniKeyIterator? & it,? const? Charles? *szSection,? const? Charles? * szKey);

Bull? FindSection(IniSectionIterator? & it,? const? Charles? * SZ section);

Vector & ltIniSection & gt? M _ content;

String? m _ strPath

Boolm _ bDirty// Yes? Content? Changed? No? save

};

#endif? //? INIFILE_H_

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

/*

*? IniFile.cpp

*

*/

# Contains? " stdafx.h "

# Contains? " IniFile.h "

# Contains? & lt algorithm & gt//find ()? function

# Contains? & lt string & gt

# Contains? & ltfstream & gt

#ifdef? _WIN32

#ifndef? __MINGW32__

# Definition? snprintf? _snprintf

#endif

#endif

IniFile::IniFile()

{

m_bDirty? =? Fake;

}

IniFile::~IniFile()

{

}

Bull? IniFile::Load(const? Charles? *szFile)

{

m_bDirty? =? Fake; //init? a kind of ancient banner

m _ contents . clear(); ? //Clear? Storage; stock

set path(SZ file);

String? Line;

//Open? That? INI? Documents? For what? read

fstream? iniFile(szFile,? IOs::in/* | IOs::no create */);

If (! iniFile.is_open())

Return? Fake;

//Read? From where? Documents? Line? By who? line

Bull? bDone? =? Fake;

Charles? SZ buffer[4000];

And (! bDone)

{

szBuffer[0]? =? '\0';

iniFile.getline(szBuffer,? sizeof(SZ buffer));

Line? =? szBuffer

//Yes? That? Answer? Festival? Are you online? That? buffer

if('['? ==? Line [0])

{

int? NPO? =? line . find _ first _ of(']');

if(- 1? ! =? NPO)

{

Department? Section;

section.m_name? =? line.substr( 1,? nPos- 1)。 c _ str();

//Add? New? Festival? Where to? That? End? Yes? Storage; stock

m _ contents . push _ back(section);

}

}

other

{

//Otherwise? Is it? That? Line? "Keyword = Value"

int? NPO? =? line . find _ first _ of(' = ');

If (NPO? & gt? 0)// There? Must? Is it? Space? For what? Keys? name

{

//Add? New? Keys? Where to? That? Finally? Festival? Are you online? That? Storage; stock

IniKey? Key;

key.m_name? =? line.substr(0,NPO)。 c _ str();

key.m_value? =? line.substr(nPos+ 1,? line.size()-nPos- 1)。 c _ str();

int? Size? =? m _ contents . size();

If (size? & gt? 0)

{

m_contents[size- 1]。 m _ keys . push _ back(key);

}

}

}

//Check? For what? export

bDone? =? iniFile.eof()? ||? iniFile.bad()? ||? inifile . fail();

}

Return? True;

}

Bull? IniFile::Save()

{

m_bDirty? =? Fake; //Reset? a kind of ancient banner

//Make sure? Directory? exist

//? String? Stedill? =? get parent dir(m _ strpath . c _ str());

//EnsureNotTerminated(strDir);

//EnsureDirExists(strDir); //TOFIX? Check? success

//Open? That? INI? Documents? For what? writing

fstream? iniFile(m_strPath.c_str(),? IOs::out | IOs::trunc);

If (! iniFile.is_open())

Return? Fake;

Charles? SZ buffer[4000];

int? Size? =? m _ contents . size();

for(int? I = 0; ? I< size; ? i++)

{

//Write? Line? With what? Festival? name

snprintf(szBuffer,? sizeof(szBuffer),? "[%s]\n ",m_contents[i]。 m _ name . c _ str());

iniFile.write(szBuffer,? strlen(SZ buffer));

int? Count? =? m_contents[i]。 m _ keys . size();

for(int? j = 0; ? J< counting; ? j++)

{

//Write? "The key? =? Value "

snprintf(szBuffer,? sizeof(szBuffer),? " %s=%s\n ",

m_contents[i]。 m_Keys[j]。 m_name.c_str(),

m_contents[i]。 m_Keys[j]。 m _ value . c _ str());

iniFile.write(szBuffer,? strlen(SZ buffer));

}

iniFile.write("\n ",? 1);

}

Return? True;

}

Bull? IniFile::ClearAll()

{

m_bDirty? =? True;

m _ contents . clear();

Return? True;

}

Bull? IniFile::SectionExists(const? Charles? *szSection)

{

IniSectionIterator? It;

Return? FindSection (it,? SZ section);

}

Bull? IniFile::KeyExists(const? Charles? *szSection,? const? Charles? *szKey)

{

IniKeyIterator? It;

Return? FindKey (it,? szSection,? szKey);

}

Bull? IniFile::GetValue(const? Charles? *szSection,? const? Charles? *szKey,? String? & value,? const? Charles? *szDefault)

{

IniKeyIterator? It;

If(FindKey (it,? szSection,? szKey)){

Value? =? It-> m _ value

Return? True;

}

Value? =? szDefault

Return? Fake;

}

Bull? IniFile::GetValue(const? Charles? *szSection,? const? Charles? *szKey,? int? & ampn value,? int? NDE default)

{

IniKeyIterator? It;

If(FindKey (it,? szSection,? szKey)){

N value? =? atoi(It-& gt; m _ value . c _ str());

Return? True;

}

N value? =? ndedefault;

Return? Fake;

}

Bull? IniFile::GetValue(const? Charles? *szSection,? const? Charles? *szKey,? Not signed? Short? & ampn value,? int? NDE default)

{

IniKeyIterator? It;

If(FindKey (it,? szSection,? szKey)){

N value? =? atoi(It-& gt; m _ value . c _ str());

Return? True;

}

N value? =? ndedefault;

Return? Fake;

}

Bull? IniFile::GetValue(const? Charles? *szSection,? const? Charles? *szKey,? Bull? & ampb value,? int? NDE default)

{

IniKeyIterator? It;

If(FindKey (it,? szSection,? szKey)){

B value? =? atoi(It-& gt; m_value.c_str())? & gt? 0;

Return? True;

}

B value? =? ndedefault? & gt? 0;

Return? Fake;

}

Bull? IniFile::SetValue(const? Charles? *szSection,? const? Charles? *szKey,? const? int? nValue)

{

Charles? szNumber[30]? =? "";

snprintf(szNumber,sizeof(szNumber),? " %d ",nValue);

Return? SetValue(szSection,szKey,? SZ number);

}

Bull? IniFile::SetValue(const? Charles? *szSection,? const? Charles? *szKey,? const? Charles? *szVal)

{

m_bDirty? =? True; //content? change

IniKeyIterator? It;

If(FindKey (it,? szSection,? szKey))

{

It-> m_value? =? SzVal// update? Existing? key

Return? True;

}

If(add section(szSection))// Ensure? Festival? exist

{

IniSectionIterator? SecIt

if(FindSection(SecIt,szSection))

{

IniKey? Key;

key.m_name? =? szKey

key.m_value? =? szVal

SecIt-& gt; m _ keys . push _ back(key);

Return? True;

}

}

Return? Fake;

}

Bull? IniFile::AddSection(const? Charles? *szSection)

{

m_bDirty? =? True; //content? change

IniSectionIterator? It;

If (! FindSection (it,? szSection))

{

Department? Section;

section.m_name? =? szSection

m _ contents . push _ back(section);

Return? True;

}

Return? True; //section? Already? exist

}

Bull? IniFile::RemoveSection(const? Charles? *szSection)

{

IniSectionIterator? It;

if(FindSection(It,? szSection))

m _ contents . erase(It);

Return? True;

}

Bull? IniFile::find section(IniSectionIterator? & it,? const? Charles? *szSection)

{

Department? Section;

section.m_name? =? szSection

It? =? find(m_contents.begin(),? m_contents.end(),? Section);

Return? (it? ! =? m _ contents . end());

}

Bull? IniFile::find key(inikey iterator? & it,? const? Charles? *szSection,? const? Charles? *szKey)

{

IniSectionIterator? SecIt

if(FindSection(SecIt,szSection))? //section? exist

{

IniKey? Key;

key.m_name? =? szKey

It? =? Find (secit->; m_Keys.begin(),? SecIt-& gt; m_Keys.end(),? Key);

Return? (it? ! =? SecIt-& gt; m _ keys . end());

}

Return? Fake;

}

Vector & lt string & gt? IniFile::GetSections()

{

Vector & lt string & gt? strVector

for(vector & lt; Insection & gt * iterator? itor? =? m _ contents . begin(); Ito! = m _ contents . end(); ++itor)

{

IniSection*? sec? =? (I section *)(itor);

str vector . push _ back(sec-& gt; m _ name);

}

Return? strVector

}

/***************************************************/