LibGame v0.4.0
The LG Game Engine - Copyright (C) 2024-2026 ETMSoftware
Loading...
Searching...
No Matches
lg_file_ops.c File Reference

Functions

void * lg_open_file (const char *full_path, const char *mode_str)
 
ssize_t lg_get_file_size (const char *full_path)
 
int lg_read_from_bfile (const char *full_path, void *data, size_t size)
 
int lg_read_from_bfile2 (const char *full_path, void *data, int64_t *size2)
 
int lg_write_to_bfile (const char *full_path, const void *data, size_t size)
 
void lg_close_file (FILE *fp)
 
void * lg_open_asset_file (const char *relative_path_in_assets, const char *unused)
 
int64_t lg_get_asset_file_size (const char *relative_path_in_assets)
 
int lg_read_from_asset_file (const char *relative_path_in_assets, void *data, size_t size)
 
int lg_read_from_asset_file2 (const char *relative_path_in_assets, void *data, int64_t *size2)
 
void lg_close_asset_file (void *rwops)
 
zboolean lg_copy_file_from_app_wr_to_assets (const char *file_name, const char *src_sub_path, const char *dest_sub_path)
 
zboolean lg_copy_file_from_cache_to_assets (const char *file_name, const char *src_sub_path, const char *dest_sub_path)
 
char * lg_getline_from_str (const char *str)
 

Detailed Description

NOTE: The SDL_RWops API doesn't deal with compressed assets so make sure to put this in APP_NAME/app/build.gradle:

 androidResources {
    noCompress "scene", "txt", "obj", "fbx", "bmesh", "png", "jpg", "pkm", "ttf", "ogg", "wav"
 }

NEW CONVENTIONS FOR PATHS / FILE NAMES AS FUNC ARGS:

- full_path -> absolute path, include file name
- relative_path -> relative path, include file name = sub_dir1/sub_dir2/.../file_name
- file_name -> basename only, always with extension

Function Documentation

◆ lg_open_file()

void * lg_open_file ( const char *  full_path,
const char *  mode_str 
)

Open file in specified mode (see fopen modes)

Parameters
full_pathFull path to file
mode_strOpening mode
Returns
Opened stream if OK, NULL on error

◆ lg_get_file_size()

ssize_t lg_get_file_size ( const char *  full_path)

Get file size

Parameters
full_pathFull path to file
Returns
Size of file if OK, -1 on error

◆ lg_read_from_bfile()

int lg_read_from_bfile ( const char *  full_path,
void *  data,
size_t  size 
)

Read from binary file - need data size

To read full content, just get file size first with lg_get_file_size()

Parameters
full_pathFull path to file
dataPointer to a buffer
sizeNum of bytes to read
Returns
LG_OK if OK

◆ lg_read_from_bfile2()

int lg_read_from_bfile2 ( const char *  full_path,
void *  data,
int64_t *  size2 
)

Read from binary file - variant 2: no need to get file size first

Parameters
full_pathFull path to file
dataPointer to a buffer
size2Pointer to an int64_t which will get the num of bytes read
Returns
LG_OK if OK

◆ lg_write_to_bfile()

int lg_write_to_bfile ( const char *  full_path,
const void *  data,
size_t  size 
)

Write to binary file

Parameters
full_pathFull path to file
dataPointer to a buffer
sizeNum of bytes to write
Returns
LG_OK if OK

◆ lg_close_file()

void lg_close_file ( FILE *  fp)

Close opened stream

◆ lg_open_asset_file()

void * lg_open_asset_file ( const char *  relative_path_in_assets,
const char *  unused 
)

Open file in app's ASSETS dir in "rb" mode (see fopen modes)

WARNING: the SDL_RWops API doesn't deal with compressed assets so make sure to put this in APP_NAME/app/build.gradle:

androidResources {
noCompress "scene", "txt", "obj", "fbx", "bmesh", "png", "jpg", "pkm", "ttf", "ogg", "wav"
}
Parameters
relative_path_in_assetsRelative path to file in app's ASSETS dir
unusedUnused, only there for compatibility with lg_open_file()
Returns
Opened stream if OK, NULL on error

◆ lg_get_asset_file_size()

int64_t lg_get_asset_file_size ( const char *  relative_path_in_assets)

Get size of file in app's ASSETS dir

Parameters
relative_path_in_assetsRelative path to file in app's ASSETS dir
Returns
Size of file if OK, -1 on error

◆ lg_read_from_asset_file()

int lg_read_from_asset_file ( const char *  relative_path_in_assets,
void *  data,
size_t  size 
)

Read from file in app's ASSETS dir - need data size

To read full content, just get file size first with lg_get_asset_file_size()

Parameters
relative_path_in_assetsRelative path to file in app's ASSETS dir
dataPointer to a buffer
sizeNum of bytes to read
Returns
LG_OK if OK

◆ lg_read_from_asset_file2()

int lg_read_from_asset_file2 ( const char *  relative_path_in_assets,
void *  data,
int64_t *  size2 
)

Read from file in app's ASSETS dir - variant 2: no need to get file size first

Parameters
relative_path_in_assetsRelative path to file in app's ASSETS dir
dataPointer to a buffer
size2Pointer to an int64_t which will get the num of bytes read, or -1 on error
Returns
LG_OK if OK

◆ lg_close_asset_file()

void lg_close_asset_file ( void *  rwops)

Close opened file in app's ASSETS dir

Parameters
rwops

◆ lg_copy_file_from_app_wr_to_assets()

zboolean lg_copy_file_from_app_wr_to_assets ( const char *  file_name,
const char *  src_sub_path,
const char *  dest_sub_path 
)

Copy file from the SRC SUB PATH folder in the APP WRITABLE folder to the DEST SUB PATH folder in assets

NO-OP on Android

Parameters
file_nameShould be file basename
src_sub_pathSub path int the assets folder
dest_sub_pathSub path int the APP WRITABLE folder
Returns
TRUE if OK, FALSE on error

◆ lg_copy_file_from_cache_to_assets()

zboolean lg_copy_file_from_cache_to_assets ( const char *  file_name,
const char *  src_sub_path,
const char *  dest_sub_path 
)

Copy file from the SUB PATH folder in the LG_APP_CACHE folder to the SUB PATH folder in assets

NO-OP on Android

Parameters
file_nameShould be file basename
src_sub_pathSub path int the assets folder
dest_sub_pathSub path int the APP WRITABLE folder
Returns
TRUE if OK, FALSE on error

◆ lg_getline_from_str()

char * lg_getline_from_str ( const char *  str)

Read an entire line from a string, ie until reaching end of line char ('
')

 Behave almost like getline() but:
 - Read from a null-terminated string
 - Returned string must be freed after each call
Parameters
strA null-terminated string
Returns
A null-terminated line if OK, NULL if EOF or error