DGen/SDL
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Macros | Typedefs | Functions
system.h File Reference
#include <stddef.h>
#include <stdio.h>
#include <limits.h>
#include <stdint.h>
#include <string.h>

Go to the source code of this file.

Macros

#define SYSTEM_H_BEGIN_
#define SYSTEM_H_END_
#define DGEN_BASEDIR   ".dgen"
#define DGEN_RC   "dgenrc"
#define DGEN_AUTORC   "dgenrc.auto"
#define DGEN_DIRSEP   "/"
#define elemof(a)   (sizeof(a) / sizeof((a)[0]))
#define containerof(p, s, m)   (s *)((uintptr_t)(p) - offsetof(s, m))
#define BITS_TO_BYTES(v)   ((((v) + 7u) & ~7u) >> 3)
#define DGEN_READ   0x1
#define DGEN_WRITE   0x2
#define DGEN_APPEND   0x4
#define DGEN_CURRENT   0x8
#define DGEN_TEXT   0x10
#define dgen_fopen_rc(mode)   dgen_fopen(NULL, DGEN_RC, ((mode) | DGEN_TEXT))
#define dgen_fopen_autorc(mode)   dgen_fopen(NULL, DGEN_AUTORC, ((mode) | DGEN_TEXT))
#define le2h16(v)   h2le16(v)
#define be2h16(v)   h2be16(v)
#define le2h32(v)   h2le32(v)
#define be2h32(v)   h2be32(v)
#define BACKSLASHIFY_NOQUOTES   0x0001

Typedefs

typedef uint8_t uint24_t [3]

Functions

FILE * dgen_fopen (const char *relative, const char *file, unsigned int mode)
 Open a file relative to DGen's home directory (when "relative" is NULL or path_type(relative) returns PATH_TYPE_UNSPECIFIED) and create the directory hierarchy if necessary, unless the file name is already relative to something or found in the current directory if mode contains DGEN_CURRENT.
FILE * dgen_freopen (const char *relative, const char *file, unsigned int mode, FILE *f)
const char * dgen_basename (const char *path)
 Return the base name in path, like basename() but without allocating anything nor modifying the "path" argument.
char * dgen_dir (char *buf, size_t *size, const char *sub)
 Return DGen's home directory with an optional subdirectory (or file).
char * dgen_userdir (char *buf, size_t *size)
 Return user's home directory.
static uint16_t h2le16 (uint16_t v)
static uint16_t h2be16 (uint16_t v)
static uint32_t h2le32 (uint32_t v)
static uint32_t h2be32 (uint32_t v)
static uint24_tu24cpy (uint24_t *dst, const uint24_t *src)
uint8_t * load (void **context, size_t *file_size, FILE *file, size_t max_size)
 Allocate a buffer and stuff the file inside using transparent decompression if libarchive is available.
void load_finish (void **context)
 Call this when you're done with your file.
void unload (uint8_t *data)
 Unload pointer returned by load().
char ** complete_path (const char *prefix, size_t len, const char *relative)
 Return a list of pathnames that match "len" characters of "prefix" on the file system, or NULL if none was found or if an error occured.
void complete_path_free (char **cp)
 Free return value of complete*() functions.
static size_t strcommon (const char *s1, const char *s2)
char * backslashify (const uint8_t *src, size_t size, unsigned int flags, size_t *pos)
 Create an escaped version of a string.
size_t utf8u32 (uint32_t *u32, const uint8_t *u8)
 Convert a UTF-8 character to its 32 bit representation.
size_t utf32u8 (uint8_t *u8, uint32_t u32)
 The opposite of utf8u32().
int prefix_casematch (const char *str, const char *argv[])
 Look for the longest common prefix between a string and an array of strings while ignoring case.
size_t prefix_getuint (const char *str, unsigned int *num)
 Read number from initial portion of a string and convert it.

Macro Definition Documentation

#define BACKSLASHIFY_NOQUOTES   0x0001
#define be2h16 (   v)    h2be16(v)
#define be2h32 (   v)    h2be32(v)
#define BITS_TO_BYTES (   v)    ((((v) + 7u) & ~7u) >> 3)
#define containerof (   p,
  s,
 
)    (s *)((uintptr_t)(p) - offsetof(s, m))
#define DGEN_APPEND   0x4
#define DGEN_AUTORC   "dgenrc.auto"
#define DGEN_BASEDIR   ".dgen"
#define DGEN_CURRENT   0x8
#define DGEN_DIRSEP   "/"
#define dgen_fopen_autorc (   mode)    dgen_fopen(NULL, DGEN_AUTORC, ((mode) | DGEN_TEXT))
#define dgen_fopen_rc (   mode)    dgen_fopen(NULL, DGEN_RC, ((mode) | DGEN_TEXT))
#define DGEN_RC   "dgenrc"
#define DGEN_READ   0x1
#define DGEN_TEXT   0x10
#define DGEN_WRITE   0x2
#define elemof (   a)    (sizeof(a) / sizeof((a)[0]))
#define le2h16 (   v)    h2le16(v)
#define le2h32 (   v)    h2le32(v)
#define SYSTEM_H_BEGIN_
#define SYSTEM_H_END_

Typedef Documentation

typedef uint8_t uint24_t[3]

Function Documentation

char* backslashify ( const uint8_t *  src,
size_t  size,
unsigned int  flags,
size_t *  pos 
)

Create an escaped version of a string.

When not NULL, "pos" refers to an offset in string "src". It is updated with its new offset value in the escaped string.

Parameters:
[in]srcString to escape.
sizeNumber of characters from "src" to process.
flagsBACKSLASHIFY_* flags.
[in,out]posOffset in string "src" to update.
Returns:
Escaped version of "src", NULL on error.
char** complete_path ( const char *  prefix,
size_t  len,
const char *  relative 
)

Return a list of pathnames that match "len" characters of "prefix" on the file system, or NULL if none was found or if an error occured.

When "prefix" isn't explicitly relative nor absolute, if "relative" is non-NULL, then the path will be completed as if "prefix" was a subdirectory of "relative". If "relative" is NULL, DGen's home directory will be used.

If "relative" isn't explicitly relative nor absolute, it will be considered a subdirectory of DGen's home directory.

Parameters:
[in]prefixPath name to match.
lenNumber of characters to match in "path".
[in]relativeIf non-NULL, consider path relative to this.
Returns:
List of path names that match "len" characters of "prefix".
void complete_path_free ( char **  cp)

Free return value of complete*() functions.

Parameters:
[in,out]cpBuffer to pass to free_pppc().
const char* dgen_basename ( const char *  path)

Return the base name in path, like basename() but without allocating anything nor modifying the "path" argument.

Parameters:
[in]pathPath to extract the last component from.
Returns:
Last component from "path".
char* dgen_dir ( char *  buf,
size_t *  size,
const char *  sub 
)

Return DGen's home directory with an optional subdirectory (or file).

The returned string doesn't have a trailing '/' and must be freed using free() (unless "buf" is provided).

Parameters:
[in,out]bufBuffer to store result in. If NULL, memory is allocated.
[in,out]sizeSize of "buf" when provided, then the returned path size.
[in]subNUL-terminated string to append to the path.
Returns:
DGen's home directory (either as "buf" or a new buffer), NULL in case of error.
FILE* dgen_fopen ( const char *  relative,
const char *  file,
unsigned int  mode 
)

Open a file relative to DGen's home directory (when "relative" is NULL or path_type(relative) returns PATH_TYPE_UNSPECIFIED) and create the directory hierarchy if necessary, unless the file name is already relative to something or found in the current directory if mode contains DGEN_CURRENT.

Parameters:
[in]relativeSubdirectory to look in.
[in]fileFile name to open.
modeMode flags to use (DGEN_READ, DGEN_WRITE and others).
Returns:
File pointer, or NULL in case of error.
See also:
dgen_freopen()
system.h
FILE* dgen_freopen ( const char *  relative,
const char *  file,
unsigned int  mode,
FILE *  f 
)
See also:
dgen_fopen()
char* dgen_userdir ( char *  buf,
size_t *  size 
)

Return user's home directory.

The returned string doesn't have a trailing '/' and must be freed using free() (unless "buf" is provided).

Parameters:
[in,out]bufUsed to store path in. If NULL, memory is allocated.
[in,out]sizeSize of "buf" when provided, then the returned path size.
Returns:
User's home directory (either as "buf" or a new buffer), NULL in case of error.
static uint16_t h2be16 ( uint16_t  v)
inlinestatic
static uint32_t h2be32 ( uint32_t  v)
inlinestatic
static uint16_t h2le16 ( uint16_t  v)
inlinestatic
static uint32_t h2le32 ( uint32_t  v)
inlinestatic
uint8_t* load ( void **  context,
size_t *  file_size,
FILE *  file,
size_t  max_size 
)

Allocate a buffer and stuff the file inside using transparent decompression if libarchive is available.

If file_size is non-NULL, store the final size there. If max_size is nonzero, refuse to load anything larger. In case the returned value is NULL, errno should contain the error.

If an error is returned but errno is 0, EOF has been reached.

Parameters:
[in,out]contextOn first call of load() this should point to NULL.
[out]file_sizeFinal size.
[in]fileFile pointer to load data from.
max_sizeIf nonzero, refuse to load anything larger.
Returns:
Buffer containing loaded data.
void load_finish ( void **  context)

Call this when you're done with your file.

Parameters:
[in,out]contextContext returned by load().
int prefix_casematch ( const char *  str,
const char *  argv[] 
)

Look for the longest common prefix between a string and an array of strings while ignoring case.

Parameters:
[in]strString to compare argv entries to.
[in]argvNULL-terminated array of prefixes to match.
Returns:
Index in argv or -1 if nothing matches.
size_t prefix_getuint ( const char *  str,
unsigned int *  num 
)

Read number from initial portion of a string and convert it.

Parameters:
[in]strString to read from.
[out]numIf not NULL, stores the converted number.
Returns:
Length of the number in str, 0 on error.
static size_t strcommon ( const char *  s1,
const char *  s2 
)
inlinestatic
static uint24_t* u24cpy ( uint24_t dst,
const uint24_t src 
)
inlinestatic
void unload ( uint8_t *  data)

Unload pointer returned by load().

Parameters:
[in]dataPointer to unload.
size_t utf32u8 ( uint8_t *  u8,
uint32_t  u32 
)

The opposite of utf8u32().

Parameters:
[out]u8Converted character.
u32Character to convert.
Returns:
Number of characters written to "u8", 0 on error.
size_t utf8u32 ( uint32_t *  u32,
const uint8_t *  u8 
)

Convert a UTF-8 character to its 32 bit representation.

Return the number of valid bytes for this character. On error, u32 is set to (uint32_t)-1.

Parameters:
[out]u32Converted character, (uint32_t)-1 on error.
[in]u8Multibyte character to convert.
Returns:
Number of bytes read.