DGen/SDL
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Classes | Macros | Enumerations | Functions
system.c File Reference
#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.

Macro Definition Documentation

#define CHUNK_SIZE   BUFSIZ
#define FOFFT   long
#define FSEEK (   f,
  o,
  w 
)    fseek((f), (o), (w))
#define FTELL (   f)    ftell(f)

Enumeration Type Documentation

enum path_type
Enumerator:
PATH_TYPE_UNSPECIFIED 
PATH_TYPE_RELATIVE 
PATH_TYPE_ABSOLUTE 

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().
static char** complete_path_simple ( const char *  path,
size_t  len 
)
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.

Parameters:
[in]pathPath name to match.
lenNumber of characters in "path" to match.
Returns:
List of matching path names or NULL.
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 const char* fopen_mode ( unsigned int  mode)
static
static void free_pppc ( char ***  pppc,
size_t  skip 
)
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.

Parameters:
[in,out]pppcPointer to an array of strings.
skipNumber 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.

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().
static size_t load_size ( FILE *  file)
static

Return the remaining file size from the current file offset.

Parameters:
[in]fileFile pointer.
enum path_type path_type ( const char *  path,
size_t  len 
)

Check whether a path is absolute or relative.

Examples: /foo/bar, \foo\bar are absolute, ./foo/bar, ., .., are relative.

Parameters:
[in]pathPath to parse.
lenLength of path.
Returns:
Path type (PATH_TYPE_ABSOLUTE, PATH_TYPE_RELATIVE or PATH_TYPE_UNSPECIFIED).
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.
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.