Tassilo von Parseval wrote:
On Thu, Oct 13, 2005 at 02:08:11PM +0200 Reinhard Pagitsch wrote:


I tryed the following in my XS module:

int Write1Byte(PerlIO* fh, long what)
{
        return PerlIO_write(fh, what, 1);
        // or return PerlIO_write(fh, (void*)what, 1);
}
With this I got a crash.


No wonder. It's horribly wrong. PerlIO_write receives a memory address
as second argument. When you pass it 'what', which is a long, this is
interpreted as a memory address. Is that really what you want? I assume
you want:

    return PerlIO_write(fh, (void*)&what, 1);

But this is then byte-order dependant: It will write the
least-significant byte on little-endian and most significant byte on
big-endian.

Oh, thanks, my mistake.



if I do this it works:
int Write1Byte(PerlIO* fh, long what)
{
        int i;
        char buf[1] = { '\0' };
        buf[0] = (char)what;
        i = PerlIO_write(fh, buf, 1);
        return i;
}

Can anyone tell me why?


I don't understand the purpose of Write1Byte(). Why is the second
argument a 'long'? Can you explain the semantics of this function first?
Is it supposed to write the least significant byte of an integer value?

The function will be used in a module I am writing to parse AFPDS files and, lets say, merge them.

This function is to write e.g. the control character (0x5A) to the file.
It will also be used to write CRLF and the triplex (e.g. 0xd3eeee: NOP record) to file if needed. I changed the type of the function parameter to short which shall be better, I think.

A record of an AFPDS contain (not all) 6 parts:
+ The control character (1 byte)
+ the length of the record (2 bytes)
+ the triplet (3 bytes)
+ flag (1 byte)
+ two bytes with 0x0000
+ data the rest
Whereas IBM says (manual) the control charater is not a part of the record.

So my intention was to use some set of general functions where I can pass the needed informations for writeing to file. Maybe there is a better way?

regards
REinhard

Reply via email to