AnimaEngine  1.0.0
Nintendo DS Pokemon Black, White, Black 2, and White 2 sprite extraction and preview toolchain
Macros | Functions
file_util.c File Reference

Implementations of filesystem utility functions. More...

#include "file_util.h"
#include <errno.h>
#include <ftw.h>
#include <sys/stat.h>
#include <sys/types.h>

Macros

#define _XOPEN_SOURCE   500
 

Functions

static int File_MkdirOne (const char *path)
 Creates a single directory. More...
 
int File_ReadAll (const char *path, u8 **out_data, size_t *out_size)
 Reads the entire contents of a file into a newly allocated buffer. More...
 
int File_WriteAll (const char *path, const u8 *data, size_t size)
 Writes a buffer to a file, creating or overwriting it. More...
 
int File_MkdirRecursive (const char *path)
 Creates a directory structure recursively (equivalent to mkdir -p). More...
 
static int File_RemoveRecursiveCallback (const char *path, const struct stat *st, int type_flag, struct FTW *ftw_buffer)
 Callback function used by nftw to remove individual files and directories. More...
 
int File_RemoveRecursive (const char *path)
 Deletes a file or recursively deletes a directory. More...
 
u16 ReadU16LE (const u8 *p)
 Helper to read a little-endian unsigned 16-bit value. More...
 
int CopyBytes (const u8 *data, size_t size, u8 **out_data, size_t *out_size)
 Allocates and duplicates a byte array. More...
 
u32 ReadU32LE (const u8 *p)
 Helper to read a little-endian unsigned 32-bit value. More...
 

Detailed Description

Implementations of filesystem utility functions.

Macro Definition Documentation

◆ _XOPEN_SOURCE

#define _XOPEN_SOURCE   500

Function Documentation

◆ CopyBytes()

int CopyBytes ( const u8 data,
size_t  size,
u8 **  out_data,
size_t *  out_size 
)

Allocates and duplicates a byte array.

Parameters
dataSource data to copy.
sizeNumber of bytes to copy.
out_dataPointer to the destination buffer pointer.
out_sizePointer to store the size of the duplicated buffer.
Returns
0 on success; negative value on memory failure.

◆ File_MkdirOne()

static int File_MkdirOne ( const char *  path)
static

Creates a single directory.

Parameters
pathDirectory path.
Returns
0 on success; non-zero on failure.

◆ File_MkdirRecursive()

int File_MkdirRecursive ( const char *  path)

Creates a directory structure recursively (equivalent to mkdir -p).

Parameters
pathPath of directory structure to create.
Returns
0 on success; negative value on failure.

◆ File_ReadAll()

int File_ReadAll ( const char *  path,
u8 **  out_data,
size_t *  out_size 
)

Reads the entire contents of a file into a newly allocated buffer.

Parameters
pathPath to the file to read.
out_dataPointer to the destination buffer pointer, allocated inside this function.
out_sizePointer to store the size of the read file in bytes.
Returns
0 on success; negative value on read failure.

◆ File_RemoveRecursive()

int File_RemoveRecursive ( const char *  path)

Deletes a file or recursively deletes a directory.

Parameters
pathPath of target file or directory.
Returns
0 on success; negative value on deletion failure.

◆ File_RemoveRecursiveCallback()

static int File_RemoveRecursiveCallback ( const char *  path,
const struct stat *  st,
int  type_flag,
struct FTW *  ftw_buffer 
)
static

Callback function used by nftw to remove individual files and directories.

Parameters
pathSystem path.
stFile stat pointer (unused).
type_flagFile type flags (unused).
ftw_bufferFTW struct details (unused).
Returns
0 on success; non-zero on failure.

◆ File_WriteAll()

int File_WriteAll ( const char *  path,
const u8 data,
size_t  size 
)

Writes a buffer to a file, creating or overwriting it.

Parameters
pathPath to the file to write.
dataPointer to the buffer containing data to write.
sizeNumber of bytes to write.
Returns
0 on success; negative value on write failure.

◆ ReadU16LE()

u16 ReadU16LE ( const u8 p)

Helper to read a little-endian unsigned 16-bit value.

Parameters
pPointer to the start bytes.
Returns
The parsed u16 value.

◆ ReadU32LE()

u32 ReadU32LE ( const u8 p)

Helper to read a little-endian unsigned 32-bit value.

Parameters
pPointer to the start bytes.
Returns
The parsed u32 value.