Re: [fpc-devel] Method for write string into TStream

2014-07-22 Thread LacaK

Recap:

Way 1: Introduce new method(s), leave Write/ReadAnsiString as is.
1.1
 procedure WriteRawString(const s: string);
 function ReadRawString(Len: integer)

1.2 (ready for future when string will be UnicodeString)
 procedure WriteString(const s: string);
 function ReadString(Len: integer)

1.3 (create superior of Write/ReadAnsiString)
 type TStringLengthPrefix = (lenNone, lenWord, lenDWord);
 procedure WriteString(const s: string; WriteLength: 
TStringLengthPrefix = lenNone);
 function ReadString(ReadLength: TStringLengthPrefix=lenNone; Len: 
integer=0);


Way 2: Extend existing Write/ReadAnsiString (keeping backward compatibility)
2.1
 type TStringLengthPrefix = (lenNone, lenWord, lenDWord);
 procedure WriteAnsiString(const s: string; WriteLength: 
TStringLengthPrefix = lenDWord);
 function ReadAnsiString(ReadLength: TStringLengthPrefix=lenDWord; Len: 
integer=0);
(but IMO in future, when string becomes unicodestring, then Ansi in 
name will sound strange if parameter does not change from string to 
ansistring)


Way 3: Do nothing ;-)
If nobody else consider it as useful. ;-)

-Laco.

___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel


Re: [fpc-devel] Method for write string into TStream

2014-07-22 Thread tha...@thaddy.com

You can always use KOL for that ;)
But anyway: we implemented that at the cost of read-back speed and 
looking for zero termination, which is flawed anyway for object pascal 
strings, since it can contain zero's.
It can be done through RTTI with my usual complaints about that sort of 
shit.


A (abstract) stream can not know if the length is stored or 
not...without rtti.


If you accept polymorhism this is not a good idea. I may be wrong (again)

On 21-7-2014 10:27, LacaK wrote:

Hi,
When I work with f.e. TFileStream I need often write string into stream.
Now I use:
 Stream1.WriteBuffer(s[1], length(s));

I think, that for convenience it will be useful having something like:
procedure TStream.WriteString(const s: string);
 begin
   WriteBuffer(s[1], length(s)*sizeof(char);
 end;

In FPC we now have for user convenience also WriteByte, WriteWord, 
WriteQWord (which all calls WriteBuffer)
and Delphi has lot of overloaded WriteData (but none of them takes as 
parameter string):


http://docwiki.embarcadero.com/Libraries/XE3/en/System.Classes.TStream.WriteData 



Do you think, that something similar can be added also in FPC ?
Thanks
-Laco.
___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel


___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel


[fpc-devel] Method for write string into TStream

2014-07-21 Thread LacaK

Hi,
When I work with f.e. TFileStream I need often write string into stream.
Now I use:
 Stream1.WriteBuffer(s[1], length(s));

I think, that for convenience it will be useful having something like:
procedure TStream.WriteString(const s: string);
 begin
   WriteBuffer(s[1], length(s)*sizeof(char);
 end;

In FPC we now have for user convenience also WriteByte, WriteWord, 
WriteQWord (which all calls WriteBuffer)
and Delphi has lot of overloaded WriteData (but none of them takes as 
parameter string):
 
http://docwiki.embarcadero.com/Libraries/XE3/en/System.Classes.TStream.WriteData


Do you think, that something similar can be added also in FPC ?
Thanks
-Laco.
___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel


Re: [fpc-devel] Method for write string into TStream

2014-07-21 Thread Sven Barth
Am 21.07.2014 10:29 schrieb LacaK la...@zoznam.sk:

 Hi,
 When I work with f.e. TFileStream I need often write string into stream.
 Now I use:
  Stream1.WriteBuffer(s[1], length(s));

 I think, that for convenience it will be useful having something like:
 procedure TStream.WriteString(const s: string);
  begin
WriteBuffer(s[1], length(s)*sizeof(char);
  end;

 In FPC we now have for user convenience also WriteByte, WriteWord,
WriteQWord (which all calls WriteBuffer)
 and Delphi has lot of overloaded WriteData (but none of them takes as
parameter string):

http://docwiki.embarcadero.com/Libraries/XE3/en/System.Classes.TStream.WriteData

 Do you think, that something similar can be added also in FPC ?

There is already a WriteAnsiString (
http://www.freepascal.org/docs-html/rtl/classes/tstream.writeansistring.html
) with the difference that it also writes out the length. Adding an
additional overload (especially with a name like WriteString) would IMHO be
confusing because of this.

Nothing stops you though from writing a class helper that adds a
WriteString method to TStream.

Regards,
Sven
___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel


Re: [fpc-devel] Method for write string into TStream

2014-07-21 Thread Michael Van Canneyt



On Mon, 21 Jul 2014, LacaK wrote:


Sven Barth  wrote / napísal(a):

  Am 21.07.2014 10:29 schrieb LacaK la...@zoznam.sk:
  
   Hi,
   When I work with f.e. TFileStream I need often write string into stream.
   Now I use:
    Stream1.WriteBuffer(s[1], length(s));
  
   I think, that for convenience it will be useful having something like:
   procedure TStream.WriteString(const s: string);
    begin
      WriteBuffer(s[1], length(s)*sizeof(char);
    end;
  
   In FPC we now have for user convenience also WriteByte, WriteWord, 
WriteQWord (which all calls WriteBuffer)
   and Delphi has lot of overloaded WriteData (but none of them takes as 
parameter string):
    
http://docwiki.embarcadero.com/Libraries/XE3/en/System.Classes.TStream.WriteData
  
   Do you think, that something similar can be added also in FPC ?

  There is already a WriteAnsiString ( 
http://www.freepascal.org/docs-html/rtl/classes/tstream.writeansistring.html ) 
with the difference that it also writes out the length.

Yes I know, and I do not want write length only content of string

  Adding an additional overload (especially with a name like WriteString) 
would IMHO be confusing because of this.

Yes without looking into documentation at first look it can by confusable
(what about WriteRawString then) ?

  Nothing stops you though from writing a class helper that adds a 
WriteString method to TStream.

Yes, I can create also own descendant etc. ... but I am asking for general 
solution, which will be useful for all.


Sure:
We can simply add a boolean parameter 'WriteLength : Boolean = True' to the existing call. 
It keeps backwards compatibility, and allows you to write the strings with or without length.


Another option is to add a WriteLn() with various overloaded variants. It 
should be clear then that this will write a textual representation.

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


Re: [fpc-devel] Method for write string into TStream

2014-07-21 Thread Marco van de Voort
In our previous episode, Michael Van Canneyt said:
Nothing stops you though from writing a class helper that adds a 
  WriteString method to TStream.
  
  Yes, I can create also own descendant etc. ... but I am asking for general 
  solution, which will be useful for all.
 
 Sure:
 We can simply add a boolean parameter 'WriteLength : Boolean = True' to the 
 existing call. 
 It keeps backwards compatibility, and allows you to write the strings with or 
 without length.

(If so I would make it an enum selecting 0,2,4 bytes length or so)
___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel


Re: [fpc-devel] Method for write string into TStream

2014-07-21 Thread LacaK

Marco van de Voort  wrote / napísal(a):

In our previous episode, Michael Van Canneyt said:
  

  Nothing stops you though from writing a class helper that adds a 
WriteString method to TStream.

Yes, I can create also own descendant etc. ... but I am asking for general 
solution, which will be useful for all.
  

Sure:
We can simply add a boolean parameter 'WriteLength : Boolean = True' to the existing call. 
It keeps backwards compatibility, and allows you to write the strings with or without length.



(If so I would make it an enum selecting 0,2,4 bytes length or so)
  

Both variants are Ok for me.
But If we will extend WriteAnsiString, then we must IMO also extend 
ReadAnsiString, and in case where length is not a part of streamed data 
then it must be explicitly given as parameter to method call (not only enum)


So IMO simpler will be introduce new methods (like):
 procedure WriteRawString(const s: string);
 function ReadRawString(Len: integer)
or alter existing AnsiString methods to something like:
 type TLengthPrefix = (lenNone, lenWord, lenDWord);
 procedure WriteAnsiString(const s: string; WriteLength: TLengthPrefix 
= lenDWord);
 function ReadAnsiString(ReadLength: TLengthPrefix=lenDWord; Len: 
integer=0);


-Laco.

___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel