Merge pull request #3011 from opentx/projectkk2glider/lua_io_issues

Projectkk2glider/lua io issues
This commit is contained in:
Bertrand Songis
2015-10-31 18:48:53 +01:00
3 changed files with 31 additions and 16 deletions
+15 -2
View File
@@ -819,10 +819,10 @@ FRESULT f_open (FIL * fil, const TCHAR *name, BYTE flag)
fil->fsize = tmp.st_size; fil->fsize = tmp.st_size;
fil->fptr = 0; 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; fil->fptr = 0;
if (fil->fs) { 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; return FR_OK;
} }
TRACE("f_open(%s) = error %d (%s)", path, errno, strerror(errno)); 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; 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) FRESULT f_close (FIL * fil)
{ {
if (fil && fil->fs) { if (fil && fil->fs) {
+5 -1
View File
@@ -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_eof(fp) ((int)((fp)->fptr == (fp)->fsize))
#define f_error(fp) ((fp)->err) #define f_error(fp) ((fp)->err)
#define f_tell(fp) ((fp)->fptr) #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 #ifndef EOF
#define EOF (-1) #define EOF (-1)
+11 -13
View File
@@ -19,6 +19,7 @@
#include <errno.h> #include <errno.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdio.h>
#define liolib_c #define liolib_c
@@ -183,14 +184,10 @@ static LStream *newprefile (lua_State *L) {
#if !defined(USE_FATFS) #if !defined(USE_FATFS)
static int aux_close (lua_State *L) { static int aux_close (lua_State *L) {
#if !defined(USE_FATFS)
LStream *p = tolstream(L); LStream *p = tolstream(L);
lua_CFunction cf = p->closef; lua_CFunction cf = p->closef;
p->closef = NULL; /* mark stream as closed */ p->closef = NULL; /* mark stream as closed */
return (*cf)(L); /* close it */ return (*cf)(L); /* close it */
#else
return 0;
#endif
} }
#endif #endif
@@ -249,14 +246,14 @@ static int io_open (lua_State *L) {
const char *md = luaL_optstring(L, 2, "r"); const char *md = luaL_optstring(L, 2, "r");
LStream *p = newfile(L); LStream *p = newfile(L);
#if defined(USE_FATFS) #if defined(USE_FATFS)
BYTE mode; BYTE mode = FA_READ;
if (strchr(md, 'w') || strchr(md, 'a')) if (*md == 'w')
mode = FA_WRITE; mode = FA_WRITE | FA_CREATE_ALWAYS; // always create file and truncate it
else else if (*md == 'a')
mode = FA_READ; mode = FA_WRITE | FA_OPEN_ALWAYS; // always open file (create it if necessary)
FRESULT result = f_open(&p->f, filename, mode); FRESULT result = f_open(&p->f, filename, mode);
if (result == FR_OK && strchr(md, 'a')) if (result == FR_OK && *md == 'a')
result = f_lseek(&p->f, f_size(&p->f)); result = f_lseek(&p->f, f_size(&p->f)); // seek to the end of the file
return result == FR_OK ? 1 : 0; return result == FR_OK ? 1 : 0;
#else #else
const char *mode = md; /* to traverse/check mode */ 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++) { for (; nargs--; arg++) {
if (lua_type(L, arg) == LUA_TNUMBER) { if (lua_type(L, arg) == LUA_TNUMBER) {
/* optimization: could be done exactly as for strings */ /* optimization: could be done exactly as for strings */
status = status && char s[LUAI_MAXNUMBER2STR];
f_printf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0; sprintf(s, LUA_NUMBER_FMT, lua_tonumber(L, arg));
status = status && f_puts(s, f) > 0;
} }
else { else {
size_t l; size_t l;