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

Benito van der Zander benito at benibela.de
Fri Oct 9 15:02:36 CEST 2020




>
> They cannot be used on handles that do not support FileSeek() 
> (sockets, pipes, stdin/stdout etc.).


Well, it would be better if it could

You can just incrementally resize the return array, when reading 
succeeds after seeking fails.

I have a string load function doing that:

https://github.com/benibela/bbutils/blob/master/bbutils.pas#L3652


>
>
>   Is it possible to extend this same set of functions to writing? 
> Naturally I need to write back to the file now and the same problem
> presents itself.  I have to search through other code bases to find a
> function or Google and find 


Writing a file should write the data in a temporary file and then rename 
the temporary file to replace the target file. Otherwise it might 
destroy the target file, without writing the new content, when there is 
an error. Although not even renaming is always safe, some people say you 
need a filesystem-specific transaction log.



Benito

On 06.10.2020 10:12, Michael Van Canneyt via fpc-pascal wrote:
>
>
>
> 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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20201009/2b3a7044/attachment.htm>


More information about the fpc-pascal mailing list