fileman.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #ifndef FILEMAN_H
  2. #define FILEMAN_H
  3. #include <fstream>
  4. #include <string>
  5. #include <vector>
  6. #define BLOCKSIZE 8
  7. /**
  8. * @class FileMan
  9. *
  10. * Provides File I/O abstraction
  11. */
  12. class FileMan {
  13. private:
  14. /**
  15. * Internal state
  16. *
  17. * Filestreams for put and get
  18. * Vector for holding received filenames from listing
  19. * Names for put and get
  20. * Size for internal read
  21. * Total and Remaining chunks for put, get and list
  22. * Boolean replacement for filestreams being open for list
  23. *
  24. */
  25. std::ifstream putfile;
  26. std::ofstream getfile;
  27. std::vector<std::string> listdata;
  28. std::string getname, putname;
  29. const unsigned int max_read_len = 8;
  30. int putsize;
  31. int putchunks;
  32. int putchunksRemaining;
  33. int getchunks;
  34. int getchunksRemaining;
  35. int listchunks;
  36. int listchunksRemaining;
  37. bool islisting;
  38. public:
  39. /**
  40. * Constructor and Destructor
  41. */
  42. FileMan();
  43. ~FileMan();
  44. /**
  45. * Query internal state
  46. *
  47. * Return true if the corresponding action is being performed, false otherwise
  48. */
  49. bool isGetting();
  50. bool isPutting();
  51. bool isListing();
  52. /**
  53. * Check for and prepare state and streams for reading/writing
  54. *
  55. * Return true if successful, false otherwise
  56. */
  57. bool openPut(const std::string &name);
  58. bool openGet(const std::string &name);
  59. bool openList();
  60. /**
  61. * Close the respective filestream
  62. */
  63. void closePut();
  64. void closeGet();
  65. void closeList();
  66. /**
  67. * Query the names of the file currently being put or get
  68. */
  69. std::string getPutName();
  70. std::string getGetName();
  71. /**
  72. * Cancel a put, get or list, depreparing internal state (closing streams if
  73. * required)
  74. */
  75. void cancelPut();
  76. void cancelGet();
  77. void cancelList();
  78. /**
  79. * Read max_rea_len bytes from the current file opened for put
  80. */
  81. std::vector<char> readPut();
  82. /**
  83. * Write the provided vector to the current file opened for get
  84. */
  85. void writeGet(std::vector<char> data);
  86. /**
  87. * Wrapper methods for reading and writing base64 encoded data instead of raw
  88. * bytes
  89. */
  90. std::string readBase64();
  91. void writeBase64(std::string data);
  92. /**
  93. * read and write emulating methods for list
  94. */
  95. void putListData(std::vector<std::string> names);
  96. std::vector<std::string> getListData();
  97. /**
  98. * Query internal state, requesting the corresponding size
  99. */
  100. int getPutChunks();
  101. int getGetChunks();
  102. int getListChunks();
  103. int getPutRemainingChunks();
  104. int getGetRemainingChunks();
  105. int getListRemainingChunks();
  106. int getPutSize();
  107. /**
  108. * Set internal state, adjusting the chunks as well as chunks remaining for
  109. * get and list
  110. */
  111. void setGetChunks(int chunks);
  112. void setListChunks(int chunks);
  113. };
  114. #endif