Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi INI readstring limitation

Tags:

limit

ini

delphi

I'm storing my application's settings in an INI file. I read that there is a limitation of 2kb for a binary entry so I encoded the binary into a string and stored the value as string (writestring). When checking the file, it seems that all the string was stored as expected.

When trying to read it back, it seems that only 2047 characters were read so when decoding it back to a stream, it fails.

Apparently it seems there is a 2kb limitation for string as well but I was wondering if that is that or maybe I did something wrong. If there's such a limitation, any idea how can I bypass it?

Thanks

EDIT: silly me, I went to system.inifiles and it says in the code

function TIniFile.ReadString(const Section, Ident, Default: string): string;
var
  Buffer: array[0..2047] of Char; <<<<<<<<<<<<<<<<
begin

  SetString(Result, Buffer, GetPrivateProfileString(MarshaledString(Section),
    MarshaledString(Ident), MarshaledString(Default), Buffer, Length(Buffer),
    MarshaledString(FFileName)));
end;
like image 862
Amos Avatar asked Jul 02 '16 13:07

Amos


People also ask

How big can an INI file be?

INI files have a 64KB limit of the data they can store.

How do I read data from INI file in Delphi?

Reading From INI The TIniFile class has several "read" methods. The ReadString reads a string value from a key, ReadInteger. ReadFloat and similar are used to read a number from a key. All "read" methods have a default value that can be used if the entry does not exist.


1 Answers

The solution is easy.

Extend TInifile and insert your own version of ReadString.

TMyIniFile = class(TInifile)
      function ReadString(const Section, Ident, Default: string): string; override;
end;

function TMyIniFile.ReadString(const Section, Ident, Default: string): string;
var
  Buffer: array[0..largenumber] of Char;
begin                                
  SetString(Result, Buffer, GetPrivateProfileString(MarshaledString(Section),
    MarshaledString(Ident), MarshaledString(Default), Buffer, Length(Buffer),
    MarshaledString(FFileName)));
end;
like image 140
Johan Avatar answered Sep 29 '22 07:09

Johan