Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: open(): Is there a simple way to map between numeric open modes and "symbol modes"?

by BrowserUk (Patriarch)
on Feb 07, 2013 at 23:49 UTC ( [id://1017735]=note: print w/replies, xml ) Need Help??


in reply to open(): Is there a simple way to map between numeric open modes and "symbol modes"? (solved)

See Fmode.pm


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"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: open(): Is there a simple way to map between numeric open modes and "symbol modes"?
by Anonymous Monk on Feb 08, 2013 at 02:39 UTC
    From Fmode:
    my %modes; @modes{ qw < > >> +< +> +>>  } = ( 1, 2, 2, 128, 128, 128 );
    Are these the official constants? If so, how do I un-OR OR results, against the other flags, to get back to these basic constants?
      how do I un-OR OR results, against the other flags, to get back to these basic constants?

      (From MSVC header files:

      #define _O_RDONLY 0x0000 /* open for reading only */ #define _O_WRONLY 0x0001 /* open for writing only */ #define _O_RDWR 0x0002 /* open for reading and writing */ #define _O_APPEND 0x0008 /* writes done at eof */ #define _O_CREAT 0x0100 /* create and open file */ #define _O_TRUNC 0x0200 /* open and truncate */ #define _O_EXCL 0x0400 /* open only if file doesn't already e +xist */ #define _O_TEXT 0x4000 /* file mode is text (translated) */ #define _O_BINARY 0x8000 /* file mode is binary (untranslated) +*/ #define _O_WTEXT 0x10000 /* file mode is UTF16 (translated) */ #define _O_U16TEXT 0x20000 /* file mode is UTF16 no BOM (translat +ed) */ #define _O_U8TEXT 0x40000 /* file mode is UTF8 no BOM (translat +ed) */

      So, assuming the variable $mode contains the OR'd values; you can check which bits are set something like:

      if( $mode & 1 ) { ## _O_WRONLY say "File is opend WRITE only }

      But note: not all of the bits that can be set when opening a file are retained in FILE*->_flag.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Thumbs up for this load of code!! Thank you BrowserUk!

      As I remember, I "discovered" them by writing a small C program that open files in each of the modes and then queried the numeric value using this code (from Fmode.xs):

      int xs_fmode( FILE *stream ) { return stream->_flag; }

      And I discovered that incantation by finding the struct FILE definition in the C header files.

      NOTE: This was done using MSVC header files; YMMV on other platforms.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      See also FileHandle::Fmode and Fcntl

      #!/usr/bin/perl -- use strict; use warnings; use Data::Dump; use Fcntl(); my %mode2symbol = ( #~ Fcntl::F_DUPFD() => '&', Fcntl::O_RDONLY() => '<', Fcntl::O_RDWR() => '+<', Fcntl::O_TRUNC() => '>', Fcntl::O_WRONLY() => '>', Fcntl::O_APPEND() => '>>', Fcntl::O_CREAT() => '>', ); my %name2mode = ( #~ F_DUPFD => Fcntl::F_DUPFD(), O_RDONLY => Fcntl::O_RDONLY(), O_RDWR => Fcntl::O_RDWR(), O_TRUNC => Fcntl::O_TRUNC(), O_WRONLY => Fcntl::O_WRONLY(), O_APPEND => Fcntl::O_APPEND(), O_CREAT => Fcntl::O_CREAT(), ); $mode2symbol{ $name2mode{O_RDWR} | $name2mode{O_APPEND} } = '+>>'; $mode2symbol{ $name2mode{O_RDONLY} | $name2mode{O_APPEND} } = '+>>'; $mode2symbol{ $name2mode{O_WRONLY} | $name2mode{O_APPEND} } = '+>>'; dd \%mode2symbol; dd \%name2mode; __END__ { "0" => "<", "1" => ">", "2" => "+<", "8" => "+>>", "9" => "+>>", "10" => "+>>", "256" => ">", "512" => ">", } { O_APPEND => 8, O_CREAT => 256, O_RDONLY => 0, O_RDWR => 2, O_TRUNC => 512, O_WRONLY => 1, }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1017735]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (7)
As of 2024-03-19 08:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found