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)
 
long long lg_get_file_size (const char *full_path)
 
int lg_read_from_bfile (const char *full_path, void *data, size_t size)
 
int lg_write_to_bfile (const char *full_path, void *data, size_t size)
 
void lg_close_file (FILE *fp)
 
void * lg_open_android_asset (const char *relative_path, const char *unused)
 
long long lg_get_android_asset_size (const char *relative_path)
 
int lg_read_from_android_asset (const char *relative_path, void *data, size_t size)
 
void lg_close_android_asset (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

On Android, assets are included in the APK, and very often compressed.

So now, when trying to read assets, we can fallback to *_android_asset_*() funcs which use SDL_RWops funcs.

See these macros in lg_file_ops.h, and DO use them whenever you need to access ASSETS:

 OPEN_ASSET_FILE
 GET_ASSET_SIZE
 READ_ASSET_FILE
 CLOSE_ASSET_FILE

(Or we'll try android_fopen.c/h ?)

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()

long long 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

 To read full content, just get file size first with lg_get_file_size()
Parameters
full_pathFull path to file
dataA pointer to a buffer
sizeNum of bytes to read
Returns
LG_OK if OK

◆ lg_write_to_bfile()

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

Write to binary file

Parameters
full_pathFull path to file
dataA pointer 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_android_asset()

void * lg_open_android_asset ( const char *  relative_path,
const char *  unused 
)

Open Android asset in "rb" mode (see fopen modes)

Parameters
relative_pathRelative path to file in Android app's assets
unusedUnused, only there for compatibility with lg_open_file()
Returns
Opened stream if OK, NULL on error

◆ lg_get_android_asset_size()

long long lg_get_android_asset_size ( const char *  relative_path)

Get Android asset size

Parameters
relative_pathRelative path to file in Android app's assets
Returns
Size of file if OK, -1 on error

◆ lg_read_from_android_asset()

int lg_read_from_android_asset ( const char *  relative_path,
void *  data,
size_t  size 
)

Read from Android asset

 To read full content, just get file size first with lg_get_android_asset_size()
Parameters
relative_pathRelative path to file in Android app's assets
dataA pointer to a buffer
sizeNum of bytes to read
Returns
LG_OK if OK

◆ lg_close_android_asset()

void lg_close_android_asset ( void *  rwops)

Close opened Android asset

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