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

Michael Van Canneyt michael at freepascal.org
Mon Oct 5 23:23:40 CEST 2020



On Mon, 5 Oct 2020, Ryan Joseph via fpc-pascal wrote:

> I often need to use a function which reads a file into a string, as is so common in so many scripting languages. Can it be considered to add something like this to the RTL? Since we have a refcounted Ansistring type it's natural for this to be a one-liner. Not saying we should use TStringList but that's the closest thing I found in the RTL.
>
> function ReadFile(path: Ansistring): Ansistring;
> var
>  list: TStringList;
> begin
>  list := TStringList.Create;
>  list.LoadFromFile(ExpandFileName(path));
>  result := list.Text;
>  list.Free;
> end;

I am inclined to agree, but there are some complications.

This will not work without some form of passing on the expected encoding. 
You will also need overloads for UnicodeString. Some people will want a
PChar or a buffer or a TBytes. Then you must allow the filename to be
specified as a single-byte string or 2-byte string.

So I think you're looking at 6 or even 8 versions of this hypothetical 
function...

Michael.



More information about the fpc-pascal mailing list