¿Cómo escribir una clase en C para leer un archivo? Solicitud de código... .ORZ
/************************ archivo de encabezado ************** * *********/
#ifndef?INIFILE_H_
#define?INIFILE_H_
#if?_MSC_VER ?gt;?1000 p>
#pragma?once
#endif?//?_MSC_VER ?gt;?1000
#if?_MSC_VER ?gt;?1000 #pragma?warning( desactivar : 4786)
#endif
#include?lt;vectorgt;
#include?lt;stringgt;
#include ? lt; string.hgt;
//TOFIX?maplt;gt; para acelerar la búsqueda
usando el espacio de nombres std;
class?IniFile
{
//Ini?key
clase?IniKey
{
público:
IniKey(){};
IniKey(const?IniKey?amp;eso){?operator?=(eso);?
void?(const? IniKey ?&that)
{
If(this! =? search?key?list
bool?operator?==?(const?IniKey ?amp; that)?{Return 0==?strcmp(m_name.c_str(),?that.m_name.c_str());?};String m_name;//String
String m_value;
};
//ini?section
clase?IniSection
{
público:
IniSection(){};
IniSection(const?IniSection?amp;that){?
void?(const?IniSection?amp;that)
{
If(this!=?amp;that){//?don't?copy?self
m_name?=?that.m_name ;
m_Keys?=?that.m_Keys;
}
}}
/needed?for?searching?section?list
bool?operador?==?(const?IniSection?amp;eso)? {return 0==?strcmp(m_name.c_str(),?that.m_name.c_str());?};
cadena?m_name;
vectorlt;IniKeygt;? m_Keys;
};
typedef?vectorlt;IniKeygt;::iteratorIniKeyItera
tor;
typedef?vectorlt;IniSectiongt;::iteratorIniSectionIterator;
público:
IniFile();
virtual?~IniFile ();
bool?Load(const?char?*szFile);
bool?Save();
inline?boolGetDirty()?const? {?return?m_bDirty;?}
en línea?
en línea?string?GetPath()?const?{?return?m_strPath;?}
void ?{?m_strPath?=?szFile;?}
//
bool?ClearAll();
//Leer valores en diferentes formatos
p>bool?GetValue(const?char?*szSection,?const?char?*szKey,?string?amp;value,?const?char?*szDefault);
bool?char ?*szSection, const?char?*szKey, int?amp;nValue, int?nDefault?=?0);
bool?GetValue(const?char?*szSection, const?char ?*szKey , unsigned?short?amp;nValue, int?nDefault?=?0);
bool?GetValue(const?char?*szSection, const?char?*szKey,?bool?amp ;bValue, ?int?nDefault?=?0);
//TOFIX?*szVal);
bool?SetValue(const?char?*szSection,?const?char ?*szKey ,?const?int?nValue);
//bool?SetValue(const?char?*szSection,?const?char?*szKey,?const?bool?amp;bValue);
bool?KeyExists(const?char?*szSection,?const?char?*szKey);
bool?RemoveSection(const?char?*szSection);
bool?RemoveKey(const?char?*szSection,?const?char?*szKey)
//TOFIX?gt;?GetXXXCount,?GetXXXName(index) vectorlt;stringgt;?GetSections ();
protegido:
bool?FindKey(IniKeyIterator?amp;It,?const?char?*szSection,?const?char?*szKey);
bool?FindSection(IniSectionIterator?amp;It,?const?c
har?*szSection);
vectorlt;IniSectiongt;?m_contents;
string?m_strPath;
boolm _bDirty; //¿se ha modificado el contenido?sin ?guardando
};
#endif?//?INIFILE_H_
/***************** ***** ****archivo cpp************************ **********/
/* *?IniFile.cpp * */
#include?"stdafx.h"
#include?"IniFile.h"
#include?lt; algoritmo gt; //find()? función
#include?lt;stringgt;
#include?lt;fstreamgt;
#ifndef?_snprintf #endif
# endif
IniFile::IniFile()
{
m_bDirty?=?false;
}
IniFile::~ IniFile()
{
}
bool?IniFile:: Cargar (const?char?*szFile)
{
m_bDirty?=?false;//init?flag
m_contents .clear();?// Borrar almacenamiento
SetPath(szFile);
string?line;
/Abrir archivo INI para leer
fstream?iniFile(szFile) ,?ios::in/*|ios.: in/*|ios::nocreate*/);
if(!iniFile.is_open())
return?false ;
IniKeyIterator?It;
if(FindKey(It,?szSection,?szKey)){
bValue?=?atoi(It-gt; m_value.c_str()?gt;?0;
return?false;
}
bool?IniFile::SetValue(const?char?*szSection , const?char?*szKey, const?int?nValue)
{
char?szNumber[30]?=?"";
snprintf( szNumber,?sizeof(szNumber),?" d" ,?nValue);
return?SetValue(szSection,?szKey,?szNumber);
}
bool?IniFile::SetValue(const?char ?*szSection, const?char?*szKey, const?char?*szVal)
{
m_bDirty?=?true ; //contenido?Cambiar
IniKeyIterator?It;
if(FindKey(It,?szSection,?szKey))
{
It-gt;m_value?=? szVal; //Actualizar clave existente
return?true;
}
if(AddSection( szSection))//ensure?section?exists
{
IniSectionIterator?SecIt;
if(FindSection(SecIt,?szSection))
{
IniKey? clave;
key.m_name?=?szKey;
key.m_value?=?szVal;
SecIt-gt;m_Keys.push_back(clave);
return?}
}
return?false;
}
bool?IniFile: :AddSection (const?char?*szSection)
{
m_bDirty?=?true; //contenido ?
IniSectionIterator?It;
if(!FindSection(It,?szSection))
{
IniSection?section;
sección.m_name ?m_name ?=?szSection ; p>
m_contents.push_back(sección);
retorno?true;
}
retorno?true; //sección?szSection) )
m_contents.erase(It);
return?true;
}
bool?IniFile:: FindSection( IniSectionIterator?amp; It,?
{
IniSection?section;
section.m_name?=?szSection;
It? =?find(m_contents.begin(), ?m_contents.end(), ?sección);
return(It!=?m_contents.end());
}
bool?IniFile::FindKey(IniKeyIterator?amp;It,?
{
IniSectionIterator?SecIt;
if( FindSection( SecIt,?szSection))?//section?exists
{
IniKey?key;
key.m_name?=?szKey;
p>
¿Es?=?find(SecIt-gt;m_Keys.begin(),?SecIt-gt;m_Keys.end(),?key);
¿regresar?(Es !=? SecIt-gt;m_Keys.end());
<p>}
return?false;
}
vectorlt;stringgt;?IniFile::GetSections()
{ vector lt ;stringgt;?strVector; for(vectorlt;IniSectiongt;::iterator?itor?=?m_contents.begin();itor!=m_contents.end(); itor) {
IniSection*?sec?=?( IniSection*)(itor); strVector.push_back(sec-gt; m_name); } return?strVector;}
/********** ***** ******************* *************************/