Group PJ_FILE_IO

group PJ_FILE_IO

This file contains functionalities to perform file I/O. The file I/O can be implemented with various back-end, either using native file API or ANSI stream.

Size Limits

There may be limitation on the size that can be handled by the pj_file_setpos() or pj_file_getpos() functions. The API itself uses 64-bit integer for the file offset/position (where available); however some backends (such as ANSI) may only support signed 32-bit offset resolution.

Reading and writing operation uses signed 32-bit integer to indicate the size.

Enums

enum pj_file_access

These enumerations are used when opening file. Values PJ_O_RDONLY, PJ_O_WRONLY, and PJ_O_RDWR are mutually exclusive. Value PJ_O_APPEND can only be used when the file is opened for writing.

Values:

enumerator PJ_O_RDONLY

Open file for reading.

enumerator PJ_O_WRONLY

Open file for writing.

enumerator PJ_O_RDWR

Open file for reading and writing. File will be truncated.

enumerator PJ_O_APPEND

Append to existing file.

enumerator PJ_O_CLOEXEC

Enable unix close-on-exec flag.

enum pj_file_seek_type

The seek directive when setting the file position with pj_file_setpos.

Values:

enumerator PJ_SEEK_SET

Offset from beginning of the file.

enumerator PJ_SEEK_CUR

Offset from current position.

enumerator PJ_SEEK_END

Size of the file plus offset.

Functions

pj_status_t pj_file_open(pj_pool_t *pool, const char *pathname, unsigned flags, pj_oshandle_t *fd)

Open the file as specified in pathname with the specified mode, and return the handle in fd. All files will be opened as binary.

Parameters:
  • pool – Pool to allocate memory for the new file descriptor.

  • pathname – The file name to open.

  • flags – Open flags, which is bitmask combination of pj_file_access enum. The flag must be either PJ_O_RDONLY, PJ_O_WRONLY, or PJ_O_RDWR. When file writing is specified, existing file will be truncated unless PJ_O_APPEND is specified.

  • fd – The returned descriptor.

Returns:

PJ_SUCCESS or the appropriate error code on error.

pj_status_t pj_file_close(pj_oshandle_t fd)

Close an opened file descriptor.

Parameters:

fd – The file descriptor.

Returns:

PJ_SUCCESS or the appropriate error code on error.

pj_status_t pj_file_write(pj_oshandle_t fd, const void *data, pj_ssize_t *size)

Write data with the specified size to an opened file.

Parameters:
  • fd – The file descriptor.

  • data – Data to be written to the file.

  • size – On input, specifies the size of data to be written. On return, it contains the number of data actually written to the file.

Returns:

PJ_SUCCESS or the appropriate error code on error.

pj_status_t pj_file_read(pj_oshandle_t fd, void *data, pj_ssize_t *size)

Read data from the specified file. When end-of-file condition is set, this function will return PJ_SUCCESS but the size will contain zero.

Parameters:
  • fd – The file descriptor.

  • data – Pointer to buffer to receive the data.

  • size – On input, specifies the maximum number of data to read from the file. On output, it contains the size of data actually read from the file. It will contain zero when EOF occurs.

Returns:

PJ_SUCCESS or the appropriate error code on error. When EOF occurs, the return is PJ_SUCCESS but size will report zero.

pj_status_t pj_file_setpos(pj_oshandle_t fd, pj_off_t offset, enum pj_file_seek_type whence)

Set file position to new offset according to directive whence.

Parameters:
  • fd – The file descriptor.

  • offset – The new file position to set.

  • whence – The directive.

Returns:

PJ_SUCCESS or the appropriate error code on error.

pj_status_t pj_file_getpos(pj_oshandle_t fd, pj_off_t *pos)

Get current file position.

Parameters:
  • fd – The file descriptor.

  • pos – On return contains the file position as measured from the beginning of the file.

Returns:

PJ_SUCCESS or the appropriate error code on error.

pj_status_t pj_file_flush(pj_oshandle_t fd)

Flush file buffers.

Parameters:

fd – The file descriptor.

Returns:

PJ_SUCCESS or the appropriate error code on error.