Lenguaje de programación Borland Delphi
Con esta función podremos leer valores de ficheros parecidos (o iguales) a los ficheros INI, con la estructura:
[CLAVE]
Valor:dato
o también:
|Clave|
Valor=dato
function obtenerValorClave (fichero : String; clave : string;
subclave : string; caracterAsignacion : string; caracterClaveAb : string;
caracterClaveCer : string) : string;
var
lFichero : TStringList;
i, j : integer;
begin
if FileExists(fichero) then
begin
lFichero := TStringList.Create;
lFichero.LoadFromFile(fichero);
for i := 0 to lFichero.Count - 1 do
begin
if Pos (caracterClaveAb + clave + caracterClaveCer, lFichero[i]) <> 0 then
begin
for j := i + 1 to lFichero.Count - 1 do
begin
if Pos (subclave + caracterAsignacion, lFichero[j]) <> 0 then
begin
Result := Trim (copy (lFichero[j], length (subclave) + 2,
length (lFichero[j]) - (length (subclave))));
Break;
end
else
if j = lFichero.Count then
begin
Result := '';
Break;
end;
end;
end;
end;
end
else
Result := '';
end;
Ejemplo de utilización:
ShowMessage (obtenerValorClave('C:\Organizar\Zetafax.ini',
'DOCUMENT', 'Comment', '=', '[', ']'));
Este ejemplo lee el valor Comment de la clave DOCUMENT del fichero Zetafax.ini, con la siguiente estructura:
[ZETAFAX]
Type: ReceiveControl
Revision: A
[DOCUMENT]
File: ~RECD547.G3N
Comment: From: PRUEBA
[MESSAGE]
AssocFile:
Format=TIFF-NORMAL
Accepted: 05-11-09 07:36:41
Publicado el: 2006-02-11