safeSave.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. ///////////////////////////////////////////
  2. //do not remove this notice
  3. //(c) Luta Vlad
  4. //
  5. // safeSave 1.0.0
  6. //
  7. ///////////////////////////////////////////
  8. #pragma once
  9. #include <fstream>
  10. #include <vector>
  11. #ifdef _MSC_VER
  12. #pragma warning( disable : 26812 )
  13. #endif
  14. namespace sfs
  15. {
  16. enum Errors : int
  17. {
  18. noError,
  19. couldNotOpenFinle,
  20. fileSizeDitNotMatch,
  21. checkSumFailed,
  22. couldNotMakeBackup,
  23. readBackup,
  24. };
  25. const char* getErrorString(Errors e);
  26. //can return error: couldNotOpenFinle
  27. Errors readEntireFile(std::vector<char>& data, const char* name);
  28. //can return error: couldNotOpenFinle
  29. Errors readEntireFile(void* data, size_t size, const char* name, bool shouldMatchSize, int *bytesRead = nullptr);
  30. //can return error: couldNotOpenFinle, fileSizeDitNotMatch, checkSumFailed
  31. Errors readEntireFileWithCheckSum(void* data, size_t size, const char* name);
  32. //can return error: couldNotOpenFinle
  33. Errors writeEntireFileWithCheckSum(const void* data, size_t size, const char* name);
  34. //can return error: couldNotOpenFinle
  35. Errors writeEntireFile(const std::vector<char>& data, const char* name);
  36. //can return error: couldNotOpenFinle
  37. Errors writeEntireFile(const void*data, size_t size, const char* name);
  38. //can return error: couldNotOpenFinle,
  39. // couldNotMakeBackup (if reportnotMakingBackupAsAnError is true, but will still save the first file)
  40. Errors safeSave(const void* data, size_t size, const char* nameWithoutExtension, bool reportnotMakingBackupAsAnError);
  41. //can return error: couldNotOpenFinle, fileSizeDitNotMatch, checkSumFailed,
  42. // readBackup (if reportLoadingBackupAsAnError but data will still be loaded with the backup)
  43. Errors safeLoad(void* data, size_t size, const char* nameWithoutExtension, bool reportLoadingBackupAsAnError);
  44. //same as safeLoad but only loads the backup file.
  45. //can return error: couldNotOpenFinle, fileSizeDitNotMatch, checkSumFailed
  46. Errors safeLoadBackup(void* data, size_t size, const char* nameWithoutExtension);
  47. #if defined WIN32 || defined _WIN32 || defined __WIN32__ || defined __NT__
  48. struct FileMapping
  49. {
  50. void* pointer = {};
  51. size_t size = 0;
  52. struct
  53. {
  54. void* fileHandle = 0;
  55. void* fileMapping = 0;
  56. }internal = {};
  57. };
  58. #elif defined __linux__
  59. struct FileMapping
  60. {
  61. void* pointer = {};
  62. size_t size = 0;
  63. struct
  64. {
  65. int fd = 0;
  66. }internal = {};
  67. };
  68. #endif
  69. //can return error: couldNotOpenFinle
  70. Errors openFileMapping(FileMapping& fileMapping, const char* name, size_t size, bool createIfNotExisting);
  71. void closeFileMapping(FileMapping& fileMapping);
  72. };