in reply to F_GETFL and Win32

This will return the open mode for a given Perl filehandle as a number.

#! perl -slw use strict; use Inline C => 'DATA', NAME => 'fmode', CLEAN_AFTER_BUILD => 0; open FILE, $ARGV[0] or die "'$ARGV[0]:$!"; printf "%x\n", fmode( \*FILE ); __DATA__ C:\test>fmode "<junk.dat" 1 C:\test>fmode ">junk.dat" 2 C:\test>fmode ">>junk.dat" 2 C:\test>fmode "+<junk.dat" 80 C:\test>fmode "+>junk.dat" 80 C:\test>fmode "+>>junk.dat" 80 __C__ int fmode( FILE *stream ) { return stream->_flag; }

In theory you could grab the XS code generated by Inline::C and form that into a module (Win32::Fmode?) for distribution--but I've never managed to get that to work. Everytime I've tried, xsubpp bellyaches about being unable to parse stuff:

C:\Perl\bin\perl.exe C:\Perl\lib\ExtUtils/xsubpp -typemap C:\Perl\lib\ExtUtils\typemap Fmode.xs > Fmode.xsc && C:\Perl\bin\perl.exe -MExtUtils::Command -e mv Fmode.xsc Fmode.c Error: Cannot parse function definition from 'fmode( FILE *stream ) {' in Fmode.xs, line 12

And since none {{censored}} ....


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: F_GETFL and Win32
by syphilis (Archbishop) on Mar 08, 2006 at 22:23 UTC
    That process you describe works fine for me - I don't know why you would be getting the error you reported. (I wonder if it was a deficiency in the typemaps at the time.) The XS file should look like this:
    #include "EXTERN.h" #include "perl.h" #include "XSUB.h" int fmode( FILE *stream ) { return stream->_flag; } MODULE = Win32::Fmode PACKAGE = Win32::Fmode PROTOTYPES: DISABLE int fmode (stream) FILE *stream
    If it were a portable cross-platform solution I'd put it on cpan (though not under the Win32 namespace, of course) .... pity ...

    Cheers,
    Rob

      I eventually got it to go(*) and I have placed it on cpan as Win32::Fmode.

      I'm not sure what I was doing wrong to be honest, but doing anything with XS seems to a bit of a trial and error process for me. I usually manage to track down my misunderstandings and once I know what they are, I can normally find the text in perlxs or perlxstut that would have allowed me to avoid the mistake had I recognised the significance of it.

      If it were a portable cross-platform solution, I'd put it on cpan ...

      Do the field names of the _iobuf struct vary by platform? I haven't verified my memory, but I seem to recall using a macro to obtain the file mode directly from the FILE* years ago under HP?

      (*)The docs appear to be quite thorough to me, but just rather dense and understated such that you (I) have to make the mistakes and then go looking for the breif mention of the cause before recognising it as such. I'm not sure what can be done about that?

      Have you ever read a graduate thesis on some deep Physics or Chemistry theory, and then later read a description of the same thing by a (good) professor. that really understands and has internalised the same material.

      The former usually touches on everything in precise detail--crosses every T and dots every I--but somehow at the end of it, whilst you may be familiar with all the terminology, and able to quickly find an accurate description of everything, you end up little the wiser because there is lacking an explantion of the underlying causes/concepts.

      Conversely, the latter will often skip much of the detail, by referring you to existing reference material, but it will explain, by simplification and/or analogy, the underlying concepts directly and clearly. You come away with a much better feeling of understanding, even if you need to go elsewhere for precise details.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Do the field names of the _iobuf struct vary by platform?

        All I know is that when I tried to build it on my linux box I got:

        Fmode.xs.6: structure has no member named `_flag'.

        (The leading underscore generally indicates that it's not ANSI standard.)

        I wonder if it would be better to pass a 'PerlIO*' (I think there is such a thing) rather than a 'FILE*'. If it could be made portable that would be soooo much better. There's already a 'FileHandle' namespace - so you could call it 'FileHandle::Fmode'.

        As for the perl internals documentation - if you know what you're doing, then you'll probably understand it, but if you don't know what you're doing, then you'll probably not understand it. It's far more valuable as a reference, than as a learning aid (imho).

        Cheers,
        Rob