#include <sys/stat.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <limits.h>
#include <errno.h>
#include <assert.h>
#include <string.h>
#include <dirent.h>
#include <ctype.h>
#include <sys/types.h>
#include <pwd.h>
#include <unistd.h>
#include "system.h"
Classes | |
struct | chunk |
Macros | |
#define | CHUNK_SIZE BUFSIZ |
#define | FTELL(f) ftell(f) |
#define | FSEEK(f, o, w) fseek((f), (o), (w)) |
#define | FOFFT long |
Enumerations | |
enum | path_type { PATH_TYPE_UNSPECIFIED, PATH_TYPE_RELATIVE, PATH_TYPE_ABSOLUTE } |
Functions | |
static const char * | fopen_mode (unsigned int mode) |
enum path_type | path_type (const char *path, size_t len) |
Check whether a path is absolute or relative. | |
char * | dgen_userdir (char *buf, size_t *size) |
Return user's home directory. | |
char * | dgen_dir (char *buf, size_t *size, const char *sub) |
Return DGen's home directory with an optional subdirectory (or file). | |
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. | |
void | unload (uint8_t *data) |
Unload pointer returned by load(). | |
void | load_finish (void **context) |
Call this when you're done with your file. | |
static size_t | load_size (FILE *file) |
Return the remaining file size from the current file offset. | |
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. | |
static void | free_pppc (char ***pppc, size_t skip) |
Free NULL-terminated list of strings and set source pointer to NULL. | |
static char ** | complete_path_simple (const char *path, size_t len) |
Return a list of path names that match "len" characters of "path" on the file system, or NULL if none was found or if an error occured. | |
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. | |
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 CHUNK_SIZE BUFSIZ |
#define FOFFT long |
#define FTELL | ( | f | ) | ftell(f) |
enum path_type |
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(). |
|
static |
Return a list of path names that match "len" characters of "path" on the file system, or NULL if none was found or if an error occured.
[in] | path | Path name to match. |
len | Number of characters in "path" to match. |
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. |
|
static |
|
static |
Free NULL-terminated list of strings and set source pointer to NULL.
This function can skip a given number of indices (starting from 0) which won't be freed.
[in,out] | pppc | Pointer to an array of strings. |
skip | Number of indices to skip in *pppc[]. |
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(). |
|
static |
Return the remaining file size from the current file offset.
[in] | file | File pointer. |
Check whether a path is absolute or relative.
Examples: /foo/bar, \foo\bar are absolute, ./foo/bar, ., .., are relative.
[in] | path | Path to parse. |
len | Length of path. |
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. |
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. |