File.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #ifndef _File_hpp_
  2. #define _File_hpp_
  3. #include <FpConfig.hpp>
  4. namespace Os {
  5. // This class encapsulates a very simple file interface that has the most often-used features
  6. class File {
  7. public:
  8. typedef enum {
  9. OPEN_NO_MODE, //!< File mode not yet selected
  10. OPEN_READ, //!< Open file for reading
  11. OPEN_WRITE, //!< Open file for writing
  12. OPEN_SYNC_WRITE, //!< Open file for writing; writes don't return until data is on disk
  13. OPEN_SYNC_DIRECT_WRITE, //!< Open file for writing, bypassing all caching. Requires data alignment
  14. OPEN_CREATE, //!< Open file for writing and truncates file if it exists, ie same flags as creat()
  15. OPEN_APPEND, //!< Open file for appending
  16. } Mode;
  17. typedef enum {
  18. OP_OK, //!< Operation was successful
  19. DOESNT_EXIST, //!< File doesn't exist (for read)
  20. NO_SPACE, //!< No space left
  21. NO_PERMISSION, //!< No permission to read/write file
  22. BAD_SIZE, //!< Invalid size parameter
  23. NOT_OPENED, //!< file hasn't been opened yet
  24. FILE_EXISTS, //!< file already exist (for CREATE with O_EXCL enabled)
  25. OTHER_ERROR, //!< A catch-all for other errors. Have to look in implementation-specific code
  26. } Status;
  27. File(); //!< Constructor
  28. virtual ~File(); //!< Destructor. Will close file if still open
  29. Status prealloc(NATIVE_INT_TYPE offset, NATIVE_INT_TYPE len);
  30. Status open(const char* fileName, Mode mode); //!< open file. Writing creates file if it doesn't exist
  31. Status open(const char* fileName, Mode mode, bool include_excl); //!< open file. Writing creates file if it doesn't exist
  32. bool isOpen(); //!< check if file descriptor is open or not.
  33. Status seek(NATIVE_INT_TYPE offset, bool absolute = true); //!< seek to location. If absolute = true, absolute from beginning of file
  34. Status flush(); //!< flush data to disk. No-op on systems that do not support.
  35. Status read(void * buffer, NATIVE_INT_TYPE &size, bool waitForFull = true); //!< read data from file; returns amount read or errno.
  36. //!< waitForFull = true to wait for all bytes to be read
  37. // size is modified to actual read size
  38. Status write(const void * buffer, NATIVE_INT_TYPE &size, bool waitForDone = true); //!< write size; will return amount written or errno
  39. Status bulkWrite(const void * buffer, NATIVE_UINT_TYPE &totalSize, NATIVE_INT_TYPE chunkSize); //!< write size; will return amount written or errno
  40. void close(); //!< close file
  41. NATIVE_INT_TYPE getLastError(); //!< read back last error code (typically errno)
  42. const char* getLastErrorString(); //!< get a string of the last error (typically from strerror)
  43. Status calculateCRC32(U32 &crc); //!< calculates the CRC32 of the file
  44. static Status niceCRC32(U32 &crc, const char* fileName); //!< Calculates CRC32 of file, not burdening FS
  45. private:
  46. NATIVE_INT_TYPE m_fd; //!< Stored file descriptor
  47. Mode m_mode; //!< Stores mode for error checking
  48. NATIVE_INT_TYPE m_lastError; //!< stores last error
  49. };
  50. }
  51. #endif