LibGame  v0.4.0
The LG Game Engine - Copyright (C) 2024 ETMSoftware
lg_file_ops.c File Reference

Functions

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

Function Documentation

◆ lg_open_file()

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

Open file in specified mode (see fopen modes)

Parameters
pathPath 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 *  path)

Get file size

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

◆ lg_read_from_bfile()

int lg_read_from_bfile ( const char *  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
pathPath 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 *  path,
void *  data,
size_t  size 
)

Write to binary file

Parameters
pathPath 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 *  path,
const char *  unused 
)

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

Parameters
pathPath to file
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 *  path)

Get Android asset size

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

◆ lg_read_from_android_asset()

int lg_read_from_android_asset ( const char *  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
pathPath to file
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_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