Browse Source

Add glue for minIni to write to ini files.

Eric Helgeson 4 months ago
parent
commit
9342cf9084
3 changed files with 60 additions and 8 deletions
  1. 7 1
      lib/minIni/minGlue.h
  2. 2 2
      lib/minIni/minIni.cpp
  3. 51 5
      lib/minIni/minIni_cache.cpp

+ 7 - 1
lib/minIni/minGlue.h

@@ -2,7 +2,6 @@
 
 #include <SdFat.h>
 
-#define INI_READONLY 1
 #define INI_FILETYPE                    FsFile
 #define INI_FILEPOS                     fspos_t
 
@@ -11,3 +10,10 @@ bool ini_close(INI_FILETYPE *fp);
 bool ini_read(char *buffer, int size, INI_FILETYPE *fp);
 void ini_tell(INI_FILETYPE *fp, INI_FILEPOS *pos);
 void ini_seek(INI_FILETYPE *fp, INI_FILEPOS *pos);
+
+bool ini_openwrite(const char *filename, INI_FILETYPE *fp);
+void ini_rename(const char *source, const char *destination);
+void ini_write(char *buffer, INI_FILETYPE *fp);
+// void ini_remove(const char *filename);
+#define INI_OPENREWRITE
+bool ini_openrewrite(const char *filename, INI_FILETYPE *fp);

+ 2 - 2
lib/minIni/minIni.cpp

@@ -663,7 +663,7 @@ static int close_rename(INI_FILETYPE *rfp, INI_FILETYPE *wfp, const TCHAR *filen
   (void)ini_tempname(buffer, filename, INI_BUFFERSIZE);
   #if defined ini_remove || defined INI_REMOVE
     (void)ini_remove(filename);
-  #endif
+ #endif
   (void)ini_rename(buffer, filename);
   return 1;
 }
@@ -721,7 +721,7 @@ int ini_puts(const TCHAR *Section, const TCHAR *Key, const TCHAR *Value, const T
         ini_tell(&rfp, &tail);
         /* create new buffer (without writing it to file) */
         writekey(LocalBuffer, Key, Value, NULL);
-        if (_tcslen(LocalBuffer) == (size_t)(tail - head)) {
+        if (_tcslen(LocalBuffer) == (size_t)(tail.position - head.position)) {
           /* length matches, close the file & re-open for read/write, then
            * write at the correct position
            */

+ 51 - 5
lib/minIni/minIni_cache.cpp

@@ -54,11 +54,13 @@ void reload_ini_cache(const char *filename)
 #endif
 }
 
+
 // Open .ini file either from cache or from SD card
 bool ini_openread(const char *filename, INI_FILETYPE *fp)
 {
 #if INI_CACHE_SIZE > 0
-    if (g_ini_cache.valid &&
+    if (SD.exists(filename) &&
+        g_ini_cache.valid &&
         (filename == g_ini_cache.filename || strcmp(filename, g_ini_cache.filename) == 0))
     {
         fp->close();
@@ -66,21 +68,35 @@ bool ini_openread(const char *filename, INI_FILETYPE *fp)
         g_ini_cache.current_pos.position = 0;
         return true;
     }
+    invalidate_ini_cache(); // new file?
 #endif
 
     return fp->open(SD.vol(), filename, O_RDONLY);
 }
 
+bool ini_openwrite(const char *filename, INI_FILETYPE *fp) {
+#if INI_CACHE_SIZE > 0
+    invalidate_ini_cache(); // We'll be writing so just use the actual fp.
+#endif
+    return fp->open(SD.vol(), filename, O_WRITE | O_CREAT);
+}
+
+bool ini_openrewrite(const char *filename, INI_FILETYPE *fp) {
+#if INI_CACHE_SIZE > 0
+    invalidate_ini_cache(); // We'll be writing so just use the actual fp.
+#endif
+    return fp->open(SD.vol(), filename, O_RDWR | O_CREAT);
+}
+
 // Close previously opened file
 bool ini_close(INI_FILETYPE *fp)
 {
 #if INI_CACHE_SIZE > 0
-    if (g_ini_cache.fp == fp)
+    if (g_ini_cache.valid && g_ini_cache.fp == fp)
     {
         g_ini_cache.fp = NULL;
         return true;
     }
-    else
 #endif
     {
         return fp->close();
@@ -91,7 +107,11 @@ bool ini_close(INI_FILETYPE *fp)
 bool ini_read(char *buffer, int size, INI_FILETYPE *fp)
 {
 #if INI_CACHE_SIZE > 0
-    if (g_ini_cache.fp == fp)
+    if (!g_ini_cache.valid && g_ini_cache.fp == fp)
+    {
+        reload_ini_cache(g_ini_cache.filename);
+    }
+    else if (g_ini_cache.valid && g_ini_cache.fp == fp)
     {
         // Read one line from cache
         uint32_t srcpos = g_ini_cache.current_pos.position;
@@ -108,7 +128,6 @@ bool ini_read(char *buffer, int size, INI_FILETYPE *fp)
         g_ini_cache.current_pos.position = srcpos;
         return dstpos > 0;
     }
-    else
 #endif
     {
         // Read from SD card
@@ -145,3 +164,30 @@ void ini_seek(INI_FILETYPE *fp, INI_FILEPOS *pos)
         fp->fsetpos(pos);
     }
 }
+
+
+
+void ini_rename(const char *source, const char *destination) {
+    if (SD.exists(destination)) {
+        SD.remove(destination);
+    }
+    SD.rename(source, destination);
+#if INI_CACHE_SIZE > 0
+    invalidate_ini_cache();
+#endif
+}
+
+void ini_write(char *buffer, INI_FILETYPE *fp) {
+    fp->write(buffer);
+    // invalidate_ini_cache(); // for next open
+}
+
+
+const char* ini_get_cache_ptr(uint32_t* out_len) {
+    if (g_ini_cache.valid) {
+        if (out_len) *out_len = g_ini_cache.filelen;
+        return g_ini_cache.cachedata;
+    }
+    if (out_len) *out_len = 0;
+    return nullptr;
+}