leer archivos ini

SMK
26 de Diciembre del 2005
buenoo..
como se pueden leer los inis sin el uso de la api de WindowS.
..digo..leer linea por linea buscando los "[" y "]" y asi encontrar los valores y devolverlos en la funcion..

gracias de antemano....

azeballes
26 de Diciembre del 2005
Te paso un código, para que te compile le vas a tener que hacer algunos toques ya que no es c puro. Pero el algoritmo funciona bien:

#include <stdio.i>
#include <inifile.ie>

char buffer[MAX_LONG_FILE];
unsigned filelength;

int
readfile(char(255) filename)
{
int handle;
int error;

filelength = 0;
error = 0;

if (handle = _fopen(filename, "r") ) <= 0
{
return setmsg( makestring("No se puede abrir ", filename) );
}

while ( error >= 0 )
{
error = fgetc(handle);
if (error < 255 && error > 0)
{
if (filelength == MAX_LONG_FILE - 1)
{
(void) _fclose(handle);
return setmsg(makestring("El archivo ",filename," es demasiado grande"));
}
buffer[filelength] = ((char) error);
filelength += 1;
}
}
(void) _fclose(handle);
return 0;
}

int
getPosNextCharIs(int start, char caracter)
{
int pos, i;
pos = -1;
i = start;
while (i < filelength)
{
if (buffer[i] == caracter)
{
pos = i;
break;
}
i += 1;
}
return pos;
}

int
getPosSection(char(MAX_LONG_SECTION) section)
{
int inicio, fin;
inicio = 0;
while (1)
{
inicio = getPosNextCharIs(inicio,'[');
if (inicio == -1)
{
break;
}
fin = getPosNextCharIs(inicio,']');
if (fin == -1)
{
break;
}
if ( buffer[ inicio + 1, fin - inicio - 1] == section)
{
inicio = inicio + 2;
break;
}
inicio = fin;
}

if (inicio != -1)
{
inicio -= 1;
}
return inicio;
}

char(MAX_LONG_SECTION)
getNextSection(int & start)
{
int inicio, fin;
char(MAX_LONG_SECTION) auxl;

inicio = start;

auxl = "";

if (inicio >= filelength or inicio < 0)
{
return auxl;
}

inicio = getPosNextCharIs(inicio,'[');
if (inicio == -1)
{
return auxl;
}

fin = getPosNextCharIs(inicio,']');
if (fin == -1)
{
return auxl;
}

auxl = buffer[inicio + 1, fin - inicio - 1];

start = inicio;

return auxl;
}

char(MAX_LONG_VALUE)
read_string(int &pos)
{
char(MAX_LONG_VALUE) aux;
int i;
i = pos;
aux = "";

while ( buffer[i] != '=' and //Para devolver claves
buffer[i] != ((char) 13) and //enter
buffer[i] != ((char) 10) and //eol
i < filelength)
{
aux[i - pos] = buffer[i];
i += 1;
}

if (i != pos)
{
aux[i - pos] = 0;
pos = i - 1;
}
return aux;
}

int
getPosKey(char(MAX_LONG_SECTION)name, int start)
{
int inicio, fin;
inicio = start;
//Controlar inicio???
while (1)
{
inicio = getPosNextCharIs(inicio,'n');
if (inicio == -1)
{
break;
}

fin = getPosNextCharIs(inicio,'=');
if (fin == -1)
{
break;
}

if ( buffer[ inicio + 1, fin - inicio - 1] == name)
{
inicio += 1;
break;
}
inicio = fin;
}

if (fin == -1)
{
inicio = fin;
}

return inicio;
}

char(MAX_LONG_SECTION)
getNextKey(int & start)
{
int inicio, fin;
char(MAX_LONG_SECTION) auxl;

auxl = "";
inicio = start;

if (inicio >= filelength or inicio < 0)
{
return auxl;
}

inicio = getPosNextCharIs(inicio,'n');
if (inicio == -1)
{
return auxl;
}

fin = getPosNextCharIs(inicio,'=');
if (fin == -1)
{
return auxl;
}

auxl = buffer[ inicio + 1, fin - inicio - 1];

start = inicio;

return auxl;
}

void
readString( char(MAX_LONG_SECTION) section, char(MAX_LONG_SECTION) key, char(MAX_LONG_VALUE) def, char(MAX_LONG_VALUE) value )
{
int pos_section, pos_key, next_section, pos_value, next_key;

value = def;

pos_section = getPosSection(section); //Traigo la posición de la seccion
if (pos_section == -1)
{
return;
}

pos_key = getPosKey(key,pos_section); //Traigo la posición de la clave
if (pos_key == -1)
{
return;
}

//Traigo la posición de la siguiente seccion
next_section = pos_section;
if ( getNextSection(next_section) != "" and pos_key > next_section)
{
return;
}

pos_value = getPosNextCharIs(pos_key, '='); //Traigo la posición del sig. valor
if (pos_value == -1)
{
return;
}

//Traigo la posición de la siguiente clave
next_key = pos_key;
if ( getNextKey(next_key) != "" and pos_value > next_key)
{
return;
}

pos_value += 1;
value = read_string(pos_value); //Traigo el valor de la clave
return;
}

void
inifile( char(255) file
, char(MAX_LONG_SECTION) section
, char(MAX_LONG_SECTION) key
, char(MAX_LONG_VALUE) value
, char(MAX_LONG_VALUE) def )
{
value = def;
if ( readfile(file) != 0 )
{
return;
}
readString( section, key, def, value );
return ;
}