minGlue.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /* Glue functions for the minIni library, based on the C/C++ stdio library
  2. *
  3. * Or better said: this file contains macros that maps the function interface
  4. * used by minIni to the standard C/C++ file I/O functions.
  5. *
  6. * By CompuPhase, 2008-2014
  7. * This "glue file" is in the public domain. It is distributed without
  8. * warranties or conditions of any kind, either express or implied.
  9. */
  10. /* map required file I/O types and functions to the standard C library */
  11. #include <dfs_posix.h>
  12. #define INI_BUFFERSIZE 256 /* maximum line length, maximum path length */
  13. #define INI_FILETYPE int
  14. #define ini_openread(filename,file) ((*(file) = open((filename),O_RDONLY, 0)) >= 0)
  15. #define ini_openwrite(filename,file) ((*(file) = open((filename),O_WRONLY | O_TRUNC, 0)) >= 0)
  16. #define ini_close(file) (close(*(file)) == 0)
  17. //#define ini_read(buffer,size,file) (read(*(file), (buffer),(size)) > 0)
  18. #define ini_write(buffer,file) write(*(file), (buffer), strlen(buffer))
  19. #define ini_rename(source,dest) rename((source), (dest))
  20. #define ini_remove(filename) unlink(filename)
  21. #define INI_FILEPOS off_t
  22. #define ini_tell(file,pos) (*(pos) = lseek(*(file), 0, SEEK_CUR))
  23. #define ini_seek(file,pos) lseek(*(file), *(pos), SEEK_SET)
  24. static int ini_read(char *buffer, int size, INI_FILETYPE *file)
  25. {
  26. int numread = size;
  27. char *eol;
  28. if ((numread = read(*file, buffer, size)) == 0) {
  29. return 0; /* at EOF */
  30. }
  31. if ((eol = strchr(buffer, '\n')) == NULL)
  32. eol = strchr(buffer, '\r');
  33. if (eol != NULL) {
  34. /* terminate the buffer */
  35. *++eol = '\0';
  36. /* "unread" the data that was read too much */
  37. lseek(*file, - (int)(numread - (size_t)(eol - buffer)), SEEK_CUR);
  38. } /* if */
  39. return 1;
  40. }