[fpc-pascal] Adding file to string to the RTL

Michael Van Canneyt michael at freepascal.org
Tue Oct 6 19:20:55 CEST 2020


Well, the answer is twofold:

1. IOUtils does not exist yet. Now the OP has a solution.

2. I don't use IOUtils and don't plan to.
    Its .NET-like approach has no added value for me.
    Just like I don't use rtii unit, but always use the more low-level
    typinfo unit.

But once TFile will be implemented, it can obviously reuse the functions from
Sysutils. (like it presumably will for 99% of all other functions in it). 
If need be the API can be extended in SysUtils to match what IOUtils needs..

Michael.

On Tue, 6 Oct 2020, Nico Neumann wrote:

> Wouldn't it be better to implement it directly as *IOUtils.TFile* (like
> Deplhi)?
>
>
>
>
> *class function ReadAllBytes(const Path: string): TBytes; static;class
> function ReadAllLines(const Path: string): TStringDynArray;overload;
> static;class function ReadAllLines(const Path: string; const Encoding:
> TEncoding): TStringDynArray; overload; static;class function
> ReadAllText(const Path: string): string; overload; inline; static;class
> function ReadAllText(const Path: string; const Encoding: TEncoding):
> string; overload; inline; static;*
> Souce:
> http://docwiki.embarcadero.com/Libraries/Sydney/en/System.IOUtils.TFile_Methods
> Then there would be only one way in the RTL for this task and not two once
> *IOUtils* (foundation webpage says it's planned anyway) is implemented.
>
> Am Di., 6. Okt. 2020 um 10:12 Uhr schrieb Michael Van Canneyt via
> fpc-pascal <fpc-pascal at lists.freepascal.org>:
>
>>
>>
>> On Mon, 5 Oct 2020, Ryan Joseph via fpc-pascal wrote:
>>
>>>
>>>
>>>> On Oct 5, 2020, at 5:08 PM, Jean SUZINEAU via fpc-pascal <
>> fpc-pascal at lists.freepascal.org> wrote:
>>>>
>>>> In my own code I use BlockRead/BlockWrite, but I'm wondering if I've
>> not seen this somewhere in RTL.
>>>>
>>>>
>>>
>>> This looks good to me what about the concerns raised by Michael?  I don't
>>> know enough about text formats but using AnsiString always just seems to
>>> work for me (I assume the compiler did something magic behind the
>> scenes).
>>
>> No, we don't deal in magic, only bits and bytes :-)
>>
>> I added the following functions to the sysutils unit (rev 47056):
>>
>> // Read raw content as bytes
>>
>> Function GetFileContents(Const aFileName : RawByteString) : TBytes;
>> Function GetFileContents(Const aFileName : UnicodeString) : TBytes;
>> Function GetFileContents(Const aHandle : THandle) : TBytes;
>>
>> // Read content as string
>>
>> // Assume TEncoding.SystemEncoding
>> Function GetFileAsString(Const aFileName : RawByteString) : RawByteString;
>> // Specify encoding
>> Function GetFileAsString(Const aFileName : RawByteString; aEncoding :
>> TEncoding) : RawByteString;
>> // Assume TEncoding.Unicode contents
>> Function GetFileAsString(Const aFileName : UnicodeString) : UnicodeString;
>> // Specify encoding, return Unicode string.
>> Function GetFileAsString(Const aFileName : UnicodeString; aEncoding :
>> TEncoding) : UnicodeString;
>>
>> These functions will raise an exception if the file cannot be opened or
>> read.
>> They cannot be used on handles that do not support FileSeek() (sockets,
>> pipes, stdin/stdout etc.).
>>
>> I did some tests on encoding conversion but not extensively.
>> If you find any errors, please report them through the bugtracker.
>>
>> Michael.
>> _______________________________________________
>> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
>> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>>
>


More information about the fpc-pascal mailing list