Difference between revisions of "Libs-dev"

From KolibriOS wiki
Jump to navigation Jump to search
(Initial documentation version, covering libio. Should probably be separated into separate subpages.)
 
m (Page interlinks, constants description separation)
Line 27: Line 27:
 
Result:
 
Result:
 
:<tt>eax</tt>
 
:<tt>eax</tt>
::0 (error) / matched file data pointer (acts as find descriptor) <FileInfo*>
+
::0 (error) / matched file data pointer (acts as find descriptor) <[[#FileInfoA|FileInfo]]*>
  
 
====file_find_next====
 
====file_find_next====
Line 37: Line 37:
 
Arguments:
 
Arguments:
 
:<tt>_findd</tt>
 
:<tt>_findd</tt>
::find descriptor (see [[#file_find_first|file_find_first]]) <FileInfo*>
+
::find descriptor (see [[#file_find_first|file_find_first]]) <[[#FileInfoA|FileInfo]]*>
  
 
Result:
 
Result:
 
:<tt>eax</tt>
 
:<tt>eax</tt>
::0 (error) / matched file data pointer (acts as find descriptor) <FileInfo*>
+
::0 (error) / matched file data pointer (acts as find descriptor) <[[#FileInfoA|FileInfo]]*>
  
 
====file_find_close====
 
====file_find_close====
Line 51: Line 51:
 
Arguments:
 
Arguments:
 
:<tt>_findd</tt>
 
:<tt>_findd</tt>
::find descriptor (see [[#file_find_first|file_find_first]]) <FileInfo*>
+
::find descriptor (see [[#file_find_first|file_find_first]]) <[[#FileInfoA|FileInfo]]*>
  
 
Result:
 
Result:
Line 86: Line 86:
 
::path to file (full or relative) <asciiz>
 
::path to file (full or relative) <asciiz>
 
:<tt>_mode</tt>
 
:<tt>_mode</tt>
::mode to open file in (combination of O_* constants) <dword>
+
::mode to open file in (combination of [[#file_open_mode|O_* constants]]) <dword>
::<tt>O_BINARY</tt>
 
:::don't change read/written data in any way (default)
 
::<tt>O_READ</tt>
 
:::open file for reading
 
::<tt>O_WRITE</tt>
 
:::open file for writing
 
::<tt>O_CREATE</tt>
 
:::create file if it doesn't exist, open otherwise
 
::<tt>O_SHARE</tt>
 
:::allow simultaneous access by using different file descriptors (not implemented)
 
::<tt>O_TEXT</tt>
 
:::replace newline chars with LF (overrides O_BINARY, not implemented)
 
  
 
Result:
 
Result:
Line 161: Line 149:
 
::position in file (in bytes) counted from specified origin <dword>
 
::position in file (in bytes) counted from specified origin <dword>
 
:<tt>_origin</tt>
 
:<tt>_origin</tt>
::origin from where to set the position (one of SEEK_* constants) <dword>
+
::origin from where to set the position (one of [[#file_seek_origin|SEEK_* constants]]) <dword>
::<tt>SEEK_SET</tt>
 
:::from beginning of file
 
::<tt>SEEK_CUR</tt>
 
:::from current pointer position
 
::<tt>SEEK_END</tt>
 
:::from end of file
 
  
 
Result:
 
Result:
Line 253: Line 235:
 
  O_SHARE  = 00001000b
 
  O_SHARE  = 00001000b
 
  O_TEXT  = 00010000b
 
  O_TEXT  = 00010000b
 +
 +
Detailed description:
 +
:<tt>O_BINARY</tt>
 +
::don't change read/written data in any way (default)
 +
:<tt>O_READ</tt>
 +
::open file for reading
 +
:<tt>O_WRITE</tt>
 +
::open file for writing
 +
:<tt>O_CREATE</tt>
 +
::create file if it doesn't exist, open otherwise
 +
:<tt>O_SHARE</tt>
 +
::allow simultaneous access by using different file descriptors (not implemented)
 +
:<tt>O_TEXT</tt>
 +
::replace newline chars with LF (overrides O_BINARY, not implemented)
  
 
====file seek origin====
 
====file seek origin====
Line 258: Line 254:
 
  SEEK_CUR = 1
 
  SEEK_CUR = 1
 
  SEEK_END = 2
 
  SEEK_END = 2
 +
 +
Detailed description:
 +
:<tt>SEEK_SET</tt>
 +
::from beginning of file
 +
:<tt>SEEK_CUR</tt>
 +
::from current pointer position
 +
:<tt>SEEK_END</tt>
 +
::from end of file
  
 
==== file attributes====
 
==== file attributes====
Line 321: Line 325:
 
   Attributes  dd ?
 
   Attributes  dd ?
 
   Flags        dd ?
 
   Flags        dd ?
   DateCreate  FileDateTime
+
   DateCreate  [[#FileDateTime|FileDateTime]]
   DateAccess  FileDateTime
+
   DateAccess  [[#FileDateTime|FileDateTime]]
   DateModify  FileDateTime
+
   DateModify  [[#FileDateTime|FileDateTime]]
 
   union
 
   union
 
     FileSize  dq ?
 
     FileSize  dq ?
Line 340: Line 344:
 
   Attributes  dd ?
 
   Attributes  dd ?
 
   Flags        dd ?
 
   Flags        dd ?
   DateCreate  FileDateTime
+
   DateCreate  [[#FileDateTime|FileDateTime]]
   DateAccess  FileDateTime
+
   DateAccess  [[#FileDateTime|FileDateTime]]
   DateModify  FileDateTime
+
   DateModify  [[#FileDateTime|FileDateTime]]
 
   union
 
   union
 
     FileSize  dq ?
 
     FileSize  dq ?

Revision as of 04:42, 23 July 2010

Introduction

Libs-dev is a system libraries collection targeting different problematic programming areas.

Libraries description

Libs-dev now consists of four common-use libraries: libio (input-output programming), libini (INI-like files support), libimg (graphics files manipulation) and libgfx (drawing convenience API).

libio

Right now, libio provides API to only work with files, although other communication types (like pipes) are also planned. By default, library is compiled to use OEM file names. Support for Unicode file names is also present but requires include file to be changed and library to be recompiled.

functions (enumerating files)

file_find_first

Find first file with matching attributes and mask in specified directory.

Prototype:

proc file.find_first _dir, _mask, _attr

Arguments:

_dir
directory path to search in <asciiz>
_mask
file mask, with use of wildcards <asciiz>
_attr
file attributes mask (combination of FA_* constants) <dword>

Result:

eax
0 (error) / matched file data pointer (acts as find descriptor) <FileInfo*>

file_find_next

Find next file matching criteria.

Prototype:

proc file.find_next _findd

Arguments:

_findd
find descriptor (see file_find_first) <FileInfo*>

Result:

eax
0 (error) / matched file data pointer (acts as find descriptor) <FileInfo*>

file_find_close

Close find descriptor and free memory.

Prototype:

proc file.find_close _findd

Arguments:

_findd
find descriptor (see file_find_first) <FileInfo*>

Result:

eax
result of memory freeing routine

functions (working with specific file)

file_size

Get file size.

Prototype:

proc file.size _name

Arguments:

_name
path to file (full or relative) <asciiz>

Result:

eax
-1 (error) / file size (in bytes, up to 2G) <dword>

Notes:

call file_err to obtain extended error information

file_open

Open file.

Prototype:

proc file.open _name, _mode

Arguments:

_name
path to file (full or relative) <asciiz>
_mode
mode to open file in (combination of O_* constants) <dword>

Result:

eax
0 (error) / file descriptor <InternalFileInfo*>

Notes:

call file_err to obtain extended error information

file_read

Read data from file.

Prototype:

proc file.read _filed, _buf, _buflen

Arguments:

_filed
file descriptor (see file_open) <InternalFileInfo*>
_buf
buffer to put read data to <byte*>
_buflen
buffer size (number of bytes to be read from file) <dword>

Result:

eax
-1 (error) / number of bytes read <dword>

Notes:

call file_err to obtain extended error information

file_write

Write data to file.

Prototype:

proc file.write _filed, _buf, _buflen

Arguments:

_filed
file descriptor (see file_open) <InternalFileInfo*>
_buf
buffer to get write data from <byte*>
_buflen
buffer size (number of bytes to be written to file) <dword>

Result:

eax
-1 (error) / number of bytes written <dword>

Notes:

call file_err to obtain extended error information

file_seek

Set file pointer position.

Prototype:

proc file.seek _filed, _where, _origin

Arguments:

_filed
file descriptor (see file_open) <InternalFileInfo*>
_where
position in file (in bytes) counted from specified origin <dword>
_origin
origin from where to set the position (one of SEEK_* constants) <dword>

Result:

eax
-1 (error) / 0 <dword>

Notes:

call file_err to obtain extended error information

file_iseof

Determine if file pointer is at the end of file.

Prototype:

proc file.eof? _filed

Arguments:

_filed
file descriptor (see file_open) <InternalFileInfo*>

Result:

eax
false / true <dword>

Notes:

call file_err to obtain extended error information

file_truncate (file_seteof)

Truncate file size to current file pointer position:

Prototype:

proc file.truncate _filed

Arguments:

_filed
file descriptor (see file_open) <InternalFileInfo*>

Result:

eax
-1 (error) / 0 <dword>

Notes:

call file_err to obtain extended error information

file_tell

Get current file pointer position.

Prototype:

proc file.tell _filed

Arguments:

_filed
file descriptor (see file_open) <InternalFileInfo*>

Result:

eax
-1 (error) / file pointer position <dword>

Notes:

call file_err to obtain extended error information

file_close

Close file.

Prototype:

proc file.close _filed

Arguments:

_filed
file descriptor (see file_open) <InternalFileInfo*>

Result:

eax
-1 (error) / file pointer position <dword>

Notes:

call file_err to obtain extended error information

constants

file open mode

O_BINARY = 00000000b
O_READ   = 00000001b
O_WRITE  = 00000010b
O_CREATE = 00000100b
O_SHARE  = 00001000b
O_TEXT   = 00010000b

Detailed description:

O_BINARY
don't change read/written data in any way (default)
O_READ
open file for reading
O_WRITE
open file for writing
O_CREATE
create file if it doesn't exist, open otherwise
O_SHARE
allow simultaneous access by using different file descriptors (not implemented)
O_TEXT
replace newline chars with LF (overrides O_BINARY, not implemented)

file seek origin

SEEK_SET = 0
SEEK_CUR = 1
SEEK_END = 2

Detailed description:

SEEK_SET
from beginning of file
SEEK_CUR
from current pointer position
SEEK_END
from end of file

file attributes

FA_READONLY = 00000001b
FA_HIDDEN   = 00000010b
FA_SYSTEM   = 00000100b
FA_LABEL    = 00001000b
FA_FOLDER   = 00010000b
FA_ARCHIVED = 00100000b
FA_ANY      = 00111111b

structures

FileDateTime

Date and time as returned by underlying system calls.

struct FileDateTime
  union
    time    dd ?
    struct
      sec   db ?
      min   db ?
      hour  db ?
    ends
  ends
  union
    date    dd ?
    struct
      day   db ?
      month db ?
      year  dw ?
    ends
  ends
ends

FileInfoBlock

File information block used by underlying system calls to identify function to be called and its basic arguments. Should generally not be used by programs.

struct FileInfoBlock
  Function   dd ?
  Position   dd ?
  Flags      dd ?
  Count      dd ?
  Buffer     dd ?
             db ?
  FileName   dd ?
ends

FileInfoHeader

File information header as returned by underlying call to 70.1 system function. Should generally not be used by programs.

struct FileInfoHeader
  Version    dd ?
  FilesRead  dd ?
  FilesCount dd ?
             rd 5
ends

FileInfoA

OEM version of FileInfo structure.

struct FileInfoA
  Attributes   dd ?
  Flags        dd ?
  DateCreate   FileDateTime
  DateAccess   FileDateTime
  DateModify   FileDateTime
  union
    FileSize   dq ?
    struct
      FileSizeLow  dd ?
      FileSizeHigh dd ?
    ends
  ends
  FileName     rb 252
ends

FileInfoW

Unicode version of FileInfo structure.

struct FileInfoW
  Attributes   dd ?
  Flags        dd ?
  DateCreate   FileDateTime
  DateAccess   FileDateTime
  DateModify   FileDateTime
  union
    FileSize   dq ?
    struct
      FileSizeLow  dd ?
      FileSizeHigh dd ?
    ends
  ends
  FileName     rw 260
ends

usage examples

Small piece of code illustrating file opening, reading 256 bytes, seeking to the beginning, writing same data back and closing file descriptor. Note that we could succeessfully read less than 256 bytes, so we remember the exact number.

include 'libio/libio.inc'

; ...

        stdcall file_open, filename, O_READ + O_WRITE
        or      eax, eax
        jz      .error

        mov     [fdesc], eax
 
        stdcall file_read, eax, buffer, 256
        mov     [bytes_read], eax
        inc     eax
        jz      .close

        stdcall file_seek, [fdesc], 0, SEEK_SET
        inc     eax
        jz      .close

        stdcall file_write, [fdesc], buffer, [bytes_read]

  .close:
        stdcall file_close, [fdesc]
 
  .error:

; ...

filename   db '/hd0/1/a.dat', 0
fdesc      dd ?
bytes_read dd ?
buffer     db 256 dup(?)

libini

libimg

libgfx

Libraries tests

There's a .test directory containing different libraries tests. They are designed to be independent and should generate (or contain pre-generated) test data which leads to same results disregarding the order tests are executed in.

Coding style

All the public functions are contained in <library name>.asm file, supporting public declarations in <library name>.inc file. All other library-specific (private and auxilary) core functions and declarations are separated into <library name>_p.asm and <library name>_p.inc files. There could also be separate files with self-describing names providing additional functionality.

Specific type of function description comments is being used allowing documentation to be generated automatically in the future.