#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_t * | u24cpy (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. |
#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, | |||
m | |||
) | (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 uint8_t uint24_t[3] |
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.
[in] | src | String to escape. |
size | Number of characters from "src" to process. | |
flags | BACKSLASHIFY_* flags. | |
[in,out] | pos | Offset in string "src" to update. |
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.
[in] | prefix | Path name to match. |
len | Number of characters to match in "path". | |
[in] | relative | If non-NULL, consider path relative to this. |
void complete_path_free | ( | char ** | cp | ) |
Free return value of complete*() functions.
[in,out] | cp | Buffer 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.
[in] | path | Path to extract the last component from. |
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).
[in,out] | buf | Buffer to store result in. If NULL, memory is allocated. |
[in,out] | size | Size of "buf" when provided, then the returned path size. |
[in] | sub | NUL-terminated string to append to the path. |
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.
[in] | relative | Subdirectory to look in. |
[in] | file | File name to open. |
mode | Mode flags to use (DGEN_READ, DGEN_WRITE and others). |
FILE* dgen_freopen | ( | const char * | relative, |
const char * | file, | ||
unsigned int | mode, | ||
FILE * | f | ||
) |
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).
[in,out] | buf | Used to store path in. If NULL, memory is allocated. |
[in,out] | size | Size of "buf" when provided, then the returned path size. |
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
|
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.
[in,out] | context | On first call of load() this should point to NULL. |
[out] | file_size | Final size. |
[in] | file | File pointer to load data from. |
max_size | If nonzero, refuse to load anything larger. |
void load_finish | ( | void ** | context | ) |
Call this when you're done with your file.
[in,out] | context | Context 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.
[in] | str | String to compare argv entries to. |
[in] | argv | NULL-terminated array of prefixes to match. |
size_t prefix_getuint | ( | const char * | str, |
unsigned int * | num | ||
) |
Read number from initial portion of a string and convert it.
[in] | str | String to read from. |
[out] | num | If not NULL, stores the converted number. |
|
inlinestatic |
void unload | ( | uint8_t * | data | ) |
Unload pointer returned by load().
[in] | data | Pointer to unload. |
size_t utf32u8 | ( | uint8_t * | u8, |
uint32_t | u32 | ||
) |
The opposite of utf8u32().
[out] | u8 | Converted character. |
u32 | Character to convert. |
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.
[out] | u32 | Converted character, (uint32_t)-1 on error. |
[in] | u8 | Multibyte character to convert. |