Merge pull request #3011 from opentx/projectkk2glider/lua_io_issues
Projectkk2glider/lua io issues
This commit is contained in:
@@ -819,10 +819,10 @@ FRESULT f_open (FIL * fil, const TCHAR *name, BYTE flag)
|
||||
fil->fsize = tmp.st_size;
|
||||
fil->fptr = 0;
|
||||
}
|
||||
fil->fs = (FATFS*)fopen(realPath, (flag & FA_WRITE) ? "wb+" : "rb+");
|
||||
fil->fs = (FATFS*)fopen(realPath, (flag & FA_WRITE) ? ((flag & FA_CREATE_ALWAYS) ? "wb+" : "ab+") : "rb+");
|
||||
fil->fptr = 0;
|
||||
if (fil->fs) {
|
||||
TRACE("f_open(%s) = %p", path, (FILE*)fil->fs);
|
||||
TRACE("f_open(%s, %x) = %p", path, flag, (FILE*)fil->fs);
|
||||
return FR_OK;
|
||||
}
|
||||
TRACE("f_open(%s) = error %d (%s)", path, errno, strerror(errno));
|
||||
@@ -856,6 +856,19 @@ FRESULT f_lseek (FIL* fil, DWORD offset)
|
||||
return FR_OK;
|
||||
}
|
||||
|
||||
UINT f_size(FIL* fil)
|
||||
{
|
||||
if (fil && fil->fs) {
|
||||
long curr = ftell((FILE*)fil->fs);
|
||||
fseek((FILE*)fil->fs, 0, SEEK_END);
|
||||
long size = ftell((FILE*)fil->fs);
|
||||
fseek((FILE*)fil->fs, curr, SEEK_SET);
|
||||
TRACE("f_size(%p) %u", fil->fs, size);
|
||||
return size;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
FRESULT f_close (FIL * fil)
|
||||
{
|
||||
if (fil && fil->fs) {
|
||||
|
||||
Vendored
+5
-1
@@ -238,7 +238,11 @@ TCHAR* f_gets (TCHAR* buff, int len, FIL* fp); /* Get a string from the fil
|
||||
#define f_eof(fp) ((int)((fp)->fptr == (fp)->fsize))
|
||||
#define f_error(fp) ((fp)->err)
|
||||
#define f_tell(fp) ((fp)->fptr)
|
||||
#define f_size(fp) ((fp)->fsize)
|
||||
#if !defined(SIMU)
|
||||
#define f_size(fp) ((fp)->fsize)
|
||||
#else
|
||||
UINT f_size(FIL* fil);
|
||||
#endif
|
||||
|
||||
#ifndef EOF
|
||||
#define EOF (-1)
|
||||
|
||||
+11
-13
@@ -19,6 +19,7 @@
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define liolib_c
|
||||
|
||||
@@ -183,14 +184,10 @@ static LStream *newprefile (lua_State *L) {
|
||||
|
||||
#if !defined(USE_FATFS)
|
||||
static int aux_close (lua_State *L) {
|
||||
#if !defined(USE_FATFS)
|
||||
LStream *p = tolstream(L);
|
||||
lua_CFunction cf = p->closef;
|
||||
p->closef = NULL; /* mark stream as closed */
|
||||
return (*cf)(L); /* close it */
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -249,14 +246,14 @@ static int io_open (lua_State *L) {
|
||||
const char *md = luaL_optstring(L, 2, "r");
|
||||
LStream *p = newfile(L);
|
||||
#if defined(USE_FATFS)
|
||||
BYTE mode;
|
||||
if (strchr(md, 'w') || strchr(md, 'a'))
|
||||
mode = FA_WRITE;
|
||||
else
|
||||
mode = FA_READ;
|
||||
BYTE mode = FA_READ;
|
||||
if (*md == 'w')
|
||||
mode = FA_WRITE | FA_CREATE_ALWAYS; // always create file and truncate it
|
||||
else if (*md == 'a')
|
||||
mode = FA_WRITE | FA_OPEN_ALWAYS; // always open file (create it if necessary)
|
||||
FRESULT result = f_open(&p->f, filename, mode);
|
||||
if (result == FR_OK && strchr(md, 'a'))
|
||||
result = f_lseek(&p->f, f_size(&p->f));
|
||||
if (result == FR_OK && *md == 'a')
|
||||
result = f_lseek(&p->f, f_size(&p->f)); // seek to the end of the file
|
||||
return result == FR_OK ? 1 : 0;
|
||||
#else
|
||||
const char *mode = md; /* to traverse/check mode */
|
||||
@@ -559,8 +556,9 @@ static int g_write (lua_State *L, FILE *f, int arg) {
|
||||
for (; nargs--; arg++) {
|
||||
if (lua_type(L, arg) == LUA_TNUMBER) {
|
||||
/* optimization: could be done exactly as for strings */
|
||||
status = status &&
|
||||
f_printf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0;
|
||||
char s[LUAI_MAXNUMBER2STR];
|
||||
sprintf(s, LUA_NUMBER_FMT, lua_tonumber(L, arg));
|
||||
status = status && f_puts(s, f) > 0;
|
||||
}
|
||||
else {
|
||||
size_t l;
|
||||
|
||||
Reference in New Issue
Block a user