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.
We have not found a pure (Pascal-only) way to use gpc writeln (etc.) statements to send strings that must include null characters (in our case as part of a 4 byte binary time). The problem is that the implementation of the mapped C/C++ function prematurely terminates a message at the null, preventing the message from being transmitted properly.
The impact on us is that whenever any byte of the time rolls over a service outage results - with the length of the outage depending upon which byte rolls over. We have since kluged a way around the issue but would much prefer a pure Pascal (=portable) solution.
In our case the message must be sent as one complete entity, meaning separating the string into multiple "writeln" statements is not possible. (This would be an awkward approach if/when (multiple) nulls can appear anywhere in the string).
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?
Any ideas out there about how to do this?
Thanks.
Le 03/09/2010 17:23, Morton, John a écrit :
We have not found a pure (Pascal-only) way to use gpc writeln (etc.) statements to send strings that must include null characters (in our case as part of a 4 byte binary time). The problem is that the implementation of the mapped C/C++ function prematurely terminates a message at the null, preventing the message from being transmitted properly.
I do not understand. The following test program works as expected:
program bug; var s: string[255] = 'hello >'+chr(0)+'< world'; begin writeln(s); end.
there is an unprintable character between the two > < and redirecting the output to a file which can be examined with a binary editor shows that this character is indeed a null.
Maurice
What do you mean by mapping, what role does C/C++ play in your case? Are you saying that the message is constructed in Pascal and read in C? In that case the termination at 0 is no surprise.
Bastiaan.
On 03.09.2010 17:23, 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.
We have not found a pure (Pascal-only) way to use gpc writeln (etc.) statements to send strings that must include null characters (in our case as part of a 4 byte binary time). The problem is that the implementation of the mapped C/C++ function prematurely terminates a message at the null, preventing the message from being transmitted properly.
The impact on us is that whenever any byte of the time rolls over a service outage results - with the length of the outage depending upon which byte rolls over. We have since kluged a way around the issue but would much prefer a pure Pascal (=portable) solution.
In our case the message must be sent as one complete entity, meaning separating the string into multiple "writeln" statements is not possible. (This would be an awkward approach if/when (multiple) nulls can appear anywhere in the string).
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?
Any ideas out there about how to do this?
Thanks.
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