6. File input output library

The FIO module exposes C++ FILE * API, file mapping, directory and file stat manipulation routines to Daslang.

All functions and symbols are in “fio” module, use require to get access to it.

require fio

6.1. Type aliases

file = FILE const?

alias for the FILE const?; its there since most file functions expect exactly this type

6.2. Constants

seek_set = 0

constant for fseek which sets the file pointer to the beginning of the file plus the offset.

seek_cur = 1

constant for fseek which sets the file pointer to the current position of the file plus the offset.

seek_end = 2

constant for fseek which sets the file pointer to the end of the file plus the offset.

df_magic = 0x12345678

obsolete. magic number for binary_save and binary_load.

6.3. Structures

df_header

obsolete. header for the fsave and fload which internally use binary_save and binary_load.

Fields
  • magic : uint - magic bits, to identify the file type.

  • size : int - total size of the saved data (not including this header)

6.4. Handled structures

FStat
FStat implicit.size() : uint64()

Returns the size of the file represented by the given FStat object.

FStat implicit.atime() : clock()

Returns the atime of the file represented by the given FStat object.

FStat implicit.ctime() : clock()

Returns the ctime of the file represented by the given FStat object.

FStat implicit.mtime() : clock()

Returns the mtime of the file represented by the given FStat object.

FStat implicit.is_reg() : bool()

Returns true if the file represented by the given FStat object is a regular file.

FStat implicit.is_dir() : bool()

Returns true if the file represented by the given FStat object is a directory.

Properties
  • size : uint64

  • atime : clock

  • ctime : clock

  • mtime : clock

  • is_reg : bool

  • is_dir : bool

Fields
  • is_valid : bool - stat and fstat return file information in this structure.

6.5. Handled types

FILE

Holds system specific FILE type.

6.6. File manipulation

remove(name: string implicit) : bool()

deletes file specified by name

Arguments
  • name : string implicit

rename(old_name: string implicit; new_name: string implicit) : bool()

renames file.

Arguments
  • old_name : string implicit

  • new_name : string implicit

fopen(name: string implicit; mode: string implicit) : FILE const?()

equivalent to C fopen. Opens file in different modes.

Arguments
  • name : string implicit

  • mode : string implicit

fclose(file: FILE const? implicit)

equivalent to C fclose. Closes file.

Arguments
  • file : FILE ? implicit

fflush(file: FILE const? implicit)

equivalent to C fflush. Flushes FILE buffers.

Arguments
  • file : FILE ? implicit

fprint(file: FILE const? implicit; text: string implicit)

same as print but outputs to file.

Arguments
  • file : FILE ? implicit

  • text : string implicit

fread(file: FILE const? implicit) : string()

reads data from file.

Arguments
  • file : FILE ? implicit

fmap(file: FILE const? implicit; block: block<(array<uint8>#):void>)

create map view of file, i.e. maps file contents to memory. Data is available as array<uint8> inside the block.

Arguments
  • file : FILE ? implicit

  • block : block<(array<uint8>#):void> implicit

fgets(file: FILE const? implicit) : string()

equivalent to C fgets. Reads and returns new string from the line.

Arguments
  • file : FILE ? implicit

fwrite(file: FILE const? implicit; text: string implicit)

writes data fo file.

Arguments
  • file : FILE ? implicit

  • text : string implicit

feof(file: FILE const? implicit) : bool()

equivalent to C feof. Returns true if end of file has been reached.

Arguments
  • file : FILE ? implicit

fseek(file: FILE const? implicit; offset: int64; mode: int) : int64()

equivalent to C fseek. Rewinds position of the current FILE pointer.

Arguments
  • file : FILE ? implicit

  • offset : int64

  • mode : int

ftell(file: FILE const? implicit) : int64()

equivalent to C ftell. Returns current FILE pointer position.

Arguments
  • file : FILE ? implicit

fstat(file: FILE const? implicit; stat: FStat implicit) : bool()

equivalent to C fstat. Returns information about file, such as file size, timestamp, etc.

Arguments
  • file : FILE ? implicit

  • stat : FStat implicit

stat(file: string implicit; stat: FStat implicit) : bool()

same as fstat, but file is specified by file name.

Arguments
  • file : string implicit

  • stat : FStat implicit

fstdin() : FILE const?()

returns FILE pointer to standard input.

fstdout() : FILE const?()

returns FILE pointer to standard output.

fstderr() : FILE const?()

returns FILE pointer to standard error.

getchar() : int()

equivalent to C getchar. Reads and returns next character from standard input.

fload(file: file; size: int; blk: block<(data:array<uint8>):void>)

obsolete. saves data to file.

Arguments
  • file : file

  • size : int

  • blk : block<(data:array<uint8>):void>

fopen(name: string; mode: string; blk: block<(f:file):void>) : auto()

equivalent to C fopen. Opens file in different modes.

Arguments
  • name : string

  • mode : string

  • blk : block<(f: file ):void>

stat(path: string) : FStat()

same as fstat, but file is specified by file name.

Arguments
  • path : string

fstat(f: file) : FStat()

equivalent to C fstat. Returns information about file, such as file size, timestamp, etc.

Arguments
fread(f: file; blk: block<(data:string#):auto>) : auto()

reads data from file.

Arguments
fload(f: file; buf: auto(BufType)) : auto()

obsolete. saves data to file.

Arguments
  • f : file

  • buf : auto(BufType)

fsave(f: file; buf: auto(BufType)) : auto()

obsolete. loads data from file.

Arguments
  • f : file

  • buf : auto(BufType)

fread(f: file; buf: auto(BufType) implicit) : auto()

reads data from file.

Arguments
  • f : file

  • buf : auto(BufType) implicit

fread(f: file; buf: array<auto(BufType)>) : auto()

reads data from file.

Arguments
  • f : file

  • buf : array<auto(BufType)> implicit

fwrite(f: file; buf: auto(BufType) implicit) : auto()

writes data fo file.

Arguments
  • f : file

  • buf : auto(BufType) implicit

fwrite(f: file; buf: array<auto(BufType)>) : auto()

writes data fo file.

Arguments
  • f : file

  • buf : array<auto(BufType)> implicit

6.7. Path manipulation

dir_name(name: string implicit) : string()

equivalent to linux dirname. Splits path and returns the component preceding the final ‘/’. Trailing ‘/’ characters are not counted as part of the pathname.

Arguments
  • name : string implicit

base_name(name: string implicit) : string()

equivalent to linux basename. Splits path and returns the string up to, but not including, the final ‘/’.

Arguments
  • name : string implicit

get_full_file_name(path: string implicit) : string()

returns full name of the file in normalized form.

Arguments
  • path : string implicit

6.8. Directory manipulation

mkdir(path: string implicit) : bool()

makes directory.

Arguments
  • path : string implicit

chdir(path: string implicit) : bool()

changes current directory.

Arguments
  • path : string implicit

getcwd() : string()

returns current working directory.

dir(path: string; blk: block<(filename:string):void>) : auto()

iterates through all files in the specified path.

Arguments
  • path : string

  • blk : block<(filename:string):void>

6.9. OS specific routines

sleep(msec: uint)

sleeps for specified number of milliseconds.

Arguments
  • msec : uint

exit(exitCode: int)

Warning

This is unsafe operation.

equivalent to C exit. Terminates program.

Arguments
  • exitCode : int

popen(command: string implicit; scope: block<(FILE const?):void>) : int()

Warning

This is unsafe operation.

equivalent to linux popen. Opens pipe to command.

Arguments
  • command : string implicit

  • scope : block<( FILE ?):void> implicit

popen_binary(command: string implicit; scope: block<(FILE const?):void>) : int()

Warning

This is unsafe operation.

opens pipe to command and returns FILE pointer to it, in binary mode.

Arguments
  • command : string implicit

  • scope : block<( FILE ?):void> implicit

get_env_variable(var: string implicit) : string()

returns value of the environment variable.

Arguments
  • var : string implicit

has_env_variable(var: string implicit) : bool()

returns true if the environment variable is defined.

Arguments
  • var : string implicit

sanitize_command_line(var: string implicit) : string()

sanitizes command line arguments.

Arguments
  • var : string implicit