| 1 |
/* minIni glue functions for Microchip's "Memory Disk Drive" file system |
| 2 |
* library, as presented in Microchip application note AN1045. |
| 3 |
* |
| 4 |
* By CompuPhase, 2011-2014 |
| 5 |
* This "glue file" is in the public domain. It is distributed without |
| 6 |
* warranties or conditions of any kind, either express or implied. |
| 7 |
* |
| 8 |
* (The "Microchip Memory Disk Drive File System" is copyright (c) Microchip |
| 9 |
* Technology Incorporated, and licensed at its own terms.) |
| 10 |
*/ |
| 11 |
|
| 12 |
#define INI_BUFFERSIZE 256 /* maximum line length, maximum path length */ |
| 13 |
|
| 14 |
#include "MDD File System\fsio.h" |
| 15 |
#include <string.h> |
| 16 |
|
| 17 |
#define INI_FILETYPE FSFILE* |
| 18 |
#define ini_openread(filename,file) ((*(file) = FSfopen((filename),FS_READ)) != NULL) |
| 19 |
#define ini_openwrite(filename,file) ((*(file) = FSfopen((filename),FS_WRITE)) != NULL) |
| 20 |
#define ini_openrewrite(filename,file) ((*(file) = fopen((filename),FS_READPLUS)) != NULL) |
| 21 |
#define ini_close(file) (FSfclose(*(file)) == 0) |
| 22 |
#define ini_write(buffer,file) (FSfwrite((buffer), 1, strlen(buffer), (*file)) > 0) |
| 23 |
#define ini_remove(filename) (FSremove((filename)) == 0) |
| 24 |
|
| 25 |
#define INI_FILEPOS long int |
| 26 |
#define ini_tell(file,pos) (*(pos) = FSftell(*(file))) |
| 27 |
#define ini_seek(file,pos) (FSfseek(*(file), *(pos), SEEK_SET) == 0) |
| 28 |
|
| 29 |
/* Since the Memory Disk Drive file system library reads only blocks of files, |
| 30 |
* the function to read a text line does so by "over-reading" a block of the |
| 31 |
* of the maximum size and truncating it behind the end-of-line. |
| 32 |
*/ |
| 33 |
static int ini_read(char *buffer, int size, INI_FILETYPE *file) |
| 34 |
{ |
| 35 |
size_t numread = size; |
| 36 |
char *eol; |
| 37 |
|
| 38 |
if ((numread = FSfread(buffer, 1, size, *file)) == 0) |
| 39 |
return 0; /* at EOF */ |
| 40 |
if ((eol = strchr(buffer, '\n')) == NULL) |
| 41 |
eol = strchr(buffer, '\r'); |
| 42 |
if (eol != NULL) { |
| 43 |
/* terminate the buffer */ |
| 44 |
*++eol = '\0'; |
| 45 |
/* "unread" the data that was read too much */ |
| 46 |
FSfseek(*file, - (int)(numread - (size_t)(eol - buffer)), SEEK_CUR); |
| 47 |
} /* if */ |
| 48 |
return 1; |
| 49 |
} |
| 50 |
|
| 51 |
#ifndef INI_READONLY |
| 52 |
static int ini_rename(const char *source, const char *dest) |
| 53 |
{ |
| 54 |
FSFILE* ftmp = FSfopen((source), FS_READ); |
| 55 |
FSrename((dest), ftmp); |
| 56 |
return FSfclose(ftmp) == 0; |
| 57 |
} |
| 58 |
#endif |