Ferreteria/v0.6/clade/Sys/FileSys/Mode/fopen modes/drafts

From Woozle Writes Code
< Ferreteria‎ | v0.6‎ | clade‎ | Sys‎ | FileSys‎ | Mode‎ | fopen modes
Jump to navigation Jump to search
earlier drafts of fopen() Modes

Take 2a

  • R = readable?
  • W = writable?
  • S = seekable (fseek() works)?
    • R = only for reads
  • C = create file if not found
  • E = what to do if file already exists:
    • f = fail (return with error)
    • t = erase (truncate to zero length)
  • P = initial pointer location
    • < = beginning of file
    • > = end of file
mode | R | W | (S) | C | E | P | documentation says...
-----+---+---+-----+---+---+---+----------------------
`r`  | 1 | 0 | (1) | . |   | < | ...reading only; start of file.
`r+` | 1 | 1 | (1) | . |   | < | ...reading and writing; place the file pointer at the beginning of the file.
`w`  | 0 | 1 | (1) | 1 | t | < | ...writing only; start of file; truncate to zero length. If the file does not exist, attempt to create it.
`w+` | 1 | 1 | (1) | 1 | t | < | ...reading and writing; otherwise it has the same behavior as 'w'.
`a`  | 0 | 1 | (0) | 1 |   | > | ...writing only; file pointer at end of file. Create if not found. fseek() has no effect, writes are always appended.
`a+` | 1 | 1 | (R) | 1 |   | > | ...reading and writing; file pointer at end of file. Create if not found. fseek() only affects reading, writes appended.
`x`  | 0 | 1 | (1) | 1 | f | < | ...writing only; file pointer at start of file. FAIL if found; create if not found.
`x+` | 1 | 1 | (1) | 1 | f | < | ...reading and writing; otherwise it has the same behavior as 'x'.
`c`  | 0 | 1 | (1) | 1 |   | < | ...writing only. Create if not found. No action if found. File pointer at start of file.
`c+` | 1 | 1 | (1) | 1 |   | < | ...Open the file for reading and writing; otherwise it has the same behavior as 'c'.

Thinking

I'm thinking that S (seekability) should not be part of the request because it's tightly constrained by P. The only cases where S isn't 1 are when the file-pointer is set to EOF -- so asking for EOF implies seekability limitations. It looks like we can also condense P into E ("what to do if file already exists"), since you can't meaningfully seek EOF on a file that you've truncated or bailed out of accessing because it isn't supposed to be there.

This leaves us with the binary flags `RWC` plus `E` with 3 possible values when not zero, leading to Take 2b.

Take 1

  • R = readable?
  • W = writable?
  • A = appendable?
  • S = seekable (fseek() works)?
    • R = only for reads
  • E = what to do if the file already exists:
    • C = create it
    • F = fail/error
  • P = initial pointer location
    • < = beginning of file
    • > = end of file
  • T = truncate to zero length?
mode | R | W | A | S | E | P | T | documentation says...
-----+---+---+---+---+---+---+---+------------------
`r`  | Y | . | - | Y | . | < | . | ...reading only; place the file pointer at the beginning of the file.
`r+` | Y | Y | Y | Y | . | < | . | ...reading and writing; place the file pointer at the beginning of the file.
`w`  | . | Y | Y | Y | C | < | Y | ...writing only; file pointer at start of file, truncate the file to zero length. If the file does not exist, attempt to create it.
`w+` | Y | Y | Y | Y | C | < | Y | ...reading and writing; otherwise it has the same behavior as 'w'.
`a`  | . | Y | Y | . | C | > | . | ...writing only; file pointer at end of file. Create if not found. fseek() has no effect, writes are always appended.
`a+` | Y | Y | Y | R | C | > | . | ...reading and writing; file pointer at end of file. Create if not found. fseek() only affects reading, writes appended.
`x`  | . | Y | Y | Y | F | < | . | ...writing only; file pointer at start of file. FAIL if found; create if not found.
`x+` | Y | Y | Y | Y | F | < | . | ...reading and writing; otherwise it has the same behavior as 'x'.
`c`  | . | Y | Y | Y | C | < | . | ...writing only. Create if not found. No action if found. File pointer at start of file.
`c+` | Y | Y | Y | Y | C | < | . | ...Open the file for reading and writing; otherwise it has the same behavior as 'c'.