Null characters in strings

Morton, John John.Morton at navcanada.ca
Tue Sep 7 23:21:28 CEST 2010


My thanks for confirming that writeln is not implemented using C's
printf function (I can't remember where I got that idea) and that gpc's
writeln can send all 256 characters (absolutely necessary to handle
binary time as a string).

On reflection I realize I have misstated our problem.  But I think I
also have a work-around for it. 

The issue is that the supplied library returns the time as a 32 bit
binary value.  The problem is essentially how to coerce the 32 bits into
a corresponding 4 character gpc string.  (BTW, they are the first 4
bytes of every message sent by our application).

I had hoped that the coercion (effectively a type cast) would be easy to
accomplish within Pascal.  I then thought about variant records but that
did not seem feasible.  
I have since realized that the conversion could be done where we define
the interface to the external routine.  In the Pascal function
definition use a 4 character fixed length string for the time field and
have the linker map that string to the corresponding 32 bit value field
used by the library (probably by using pass-by-address, possibly
pass-by-value).  
Not a pure Pascal approach but a practical one - as long as you are well
aware of the pitfalls.  (In the "pure" approach the Pascal interface
parameter would be a 32 bit unsigned value that gets converted/mapped by
Pascal to the corresponding 4 byte string).

Anyone have any thoughts on this?  Have I have missed a simpler
approach?

John.

-----Original Message-----
From: gpc-owner at gnu.de [mailto:gpc-owner at gnu.de] On Behalf Of Frank
Heckenbach
Sent: September 3, 2010 05:16 PM
To: gpc at gnu.de
Subject: Re: Null characters (& string

Morton, John wrote:

> We have encountered an unexpected implementation issue with gpc, one
> that probably affects all languages implemented using C/C++:  The
> mapping of Pascal writeln (etc.) statements to "equivalent" C/C++
string
> handling code.  

Others have replied already, but I can confirm that GPC does not use
printf etc. internally to implement WriteLn, if that's what you
mean.

> Stated more generally, is there a pure gpc Pascal way to send
> ("writeln") strings that can contain any/all of the 256 8 bit
characters
> (especially the null) - an approach that does not require retyping our
> string definitions?  

Of course, any such way is not pure Pascal in the sense of ISO
Pascal or so. Text files (WriteLn) are not really meant for binary
data, though it works with GPC, and is as portable as GPC is, and
probably works with some other compilers as well. The official
Pascal way would be a "file of Char" (though ISO Pascal doesn't even
guarantee that the NUL character is part of the character set),
where you'd need to read/write every character individually.

Frank

-- 
Frank Heckenbach, f.heckenbach at fh-soft.de, http://fjf.gnu.de/, 7977168E
GPC To-Do list, latest features, fixed bugs:
http://www.gnu-pascal.de/todo.html
GPC download signing key: ACB3 79B2 7EB2 B7A7 EFDE  D101 CD02 4C9D 0FE0
E5E8







More information about the Gpc mailing list