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

2020-10-09 Thread Jean SUZINEAU via fpc-pascal


Le 09/10/2020 à 10:15, Santiago A. via fpc-pascal a écrit :

Just nitpicking.
Shouldn't "try" be after the "reset" and after the "rewrite"?
Why don't you allow to write an empty string?


Yes, you're right, the Reset/Rewrite should be after the try.
I admit this code is not general, just tailored to my own needs.
In my use-cases, writing a file of size 0 as placeholder is not useful.

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


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

2020-10-09 Thread Ryan Joseph via fpc-pascal


> On Oct 9, 2020, at 7:02 AM, Benito van der Zander via fpc-pascal 
>  wrote:
> 
> 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.

I've seen in some API's they have an "atomic" flag for write file functions 
which do the swap you're describing. I agree it's a smart thing to include in 
the function but it's best to be an option since there is an additional 
operation being performed and affects performance.

Regards,
Ryan Joseph

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Are record fields zeroed on declaration of a record?

2020-10-09 Thread Ryan Joseph via fpc-pascal


> On Oct 8, 2020, at 4:58 AM, Bo Berglund via fpc-pascal 
>  wrote:
> 
> OK,
> is it possible to define the TMyRecord with default values so that
> when I do Default(TMyRecord) it will be non-zero defaults written
> instead of zeros?

You need to make a method for tha. You can't even use constructors because they 
must have parameters for some reason but you can use a static class function 
(this requires advanced records mode switch of course). There is also an 
initialize operator that is in 3.2.0 I believe.

Sven did say one time that he MAY consider allowing a record initialization 
syntax inside code blocks (see below). That would be a nice option but they 
require all fields to be initialized. Having default values like C++ would be 
best though.

type
 TmyRec = record
   a, b, c: integer;
   d, e: double;
   class function Create: TMyRec; static;  // this syntax works.

   class operator Initialize(var self: TMyRec); // init operator gets called 
when the record is declared
 end;

var
 charlie: TmyRec = (a:2; b:3; e:3.1415);
begin
 charlie := TmyRec(a:2; b:3; e:3.1415); // this would be a nice addition to 
avoid constructors

Regards,
Ryan Joseph

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


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

2020-10-09 Thread Benito van der Zander via fpc-pascal






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@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


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

2020-10-09 Thread Santiago A. via fpc-pascal

El 06/10/2020 a las 01:08, Jean SUZINEAU via fpc-pascal escribió:


In my own code I use BlockRead/BlockWrite, but I'm wondering if I've 
not seen this somewhere in RTL.


https://github.com/jsuzineau/pascal_o_r_mapping/blob/TjsDataContexte/pascal_o_r_mapping/02_Units/uuStrings.pas 




Just nitpicking.
Shouldn't "try" be after the "reset" and after the "rewrite"?
Why don't you allow to write an empty string?


function String_from_File( _FileName: String): String;
var
    F: File;
    Longueur: Integer;
begin
  Result:= '';
  if not FileExists( _FileName) then exit;
  AssignFile( F, _FileName);
  try
     Reset( F, 1);
     Longueur:= FileSize( F);
     if 0 = Longueur then exit;
     SetLength( Result, Longueur);
     BlockRead( F, Result[1], Longueur);
  finally
     CloseFile( F);
     end;
end;
procedure String_to_File( _FileName: String; _S: String);
var
    F: File;
begin
  if '' = _S then exit;
  AssignFile( F, _FileName);
  try
     ReWrite( F, 1);
     BlockWrite( F, _S[1], Length( _S));
  finally
     CloseFile( F);
     end;
end;



___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal



--
Saludos

Santiago A.

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Are record fields zeroed on declaration of a record?

2020-10-09 Thread Michael Van Canneyt via fpc-pascal



On Fri, 9 Oct 2020, Bo Berglund via fpc-pascal wrote:


On Thu, 8 Oct 2020 23:00:32 +0200 (CEST), Michael Van Canneyt via
fpc-pascal
 wrote:




On Thu, 8 Oct 2020, Ryan Joseph via fpc-pascal wrote:





On Oct 8, 2020, at 5:14 AM, gabor via fpc-pascal 
 wrote:

You can define typed constant and assign it to variable.

type
 TMyRecord = record
   X, Y: Integer;
   S: String;
 end;

const
 MYDEFREC: TMyRecord = (X: 1; Y: 2; S: 'ABC');


I never understood why we can't initialize fields at init time for
records.  Pascal seems to be the only language that doesn't support this
in 2020.  Thinking of C++, C#, PHP, Swift and many more all let you do
this.  Not even Delphi supports this AKAIK.


Of course you can:

var
  x : record a,b : integer; end = (a:1;b:3);


begin
end.

This is identical to how C++ does it. From cppreference.com:

typedef struct { int k; int l; int a[2]; } T;
T x = {.l = 43, .k = 42, .a[1] = 19, .a[0] = 18 };



If you do that can you be selective and initialize only some fields or
must you list all or none?


You can do selected fields.
Fields you didn't specify are undefined.

But the code you posted is wrong.

Michael.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal