PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/openFrameworks/utils/ofFileUtils.h

https://github.com/damiannz/openFrameworks
C Header | 278 lines | 167 code | 78 blank | 33 comment | 0 complexity | e543ae3adae0d5860af1e15eaf2ff052 MD5 | raw file
  1. #pragma once
  2. #include "ofConstants.h"
  3. #include "Poco/File.h"
  4. //----------------------------------------------------------
  5. // ofBuffer
  6. //----------------------------------------------------------
  7. class ofBuffer{
  8. public:
  9. ofBuffer();
  10. ofBuffer(const char * buffer, int size);
  11. ofBuffer(istream & stream);
  12. ofBuffer(const ofBuffer & buffer_);
  13. ~ofBuffer();
  14. void set(const char * _buffer, int _size);
  15. bool set(istream & stream);
  16. bool writeTo(ostream & stream) const;
  17. void clear();
  18. void allocate(long _size);
  19. char * getBinaryBuffer();
  20. const char * getBinaryBuffer() const;
  21. string getText() const;
  22. operator string() const; // cast to string, to use a buffer as a string
  23. long size() const;
  24. string getNextLine();
  25. string getFirstLine();
  26. bool isLastLine();
  27. void resetLineReader();
  28. friend ostream & operator<<(ostream & ostr, const ofBuffer & buf);
  29. friend istream & operator>>(istream & istr, ofBuffer & buf);
  30. private:
  31. vector<char> buffer;
  32. long nextLinePos;
  33. };
  34. //--------------------------------------------------
  35. ofBuffer ofBufferFromFile(const string & path, bool binary=false);
  36. //--------------------------------------------------
  37. bool ofBufferToFile(const string & path, ofBuffer & buffer, bool binary=false);
  38. //--------------------------------------------------
  39. class ofFilePath{
  40. public:
  41. static string getFileExt(string filename);
  42. static string removeExt(string filename);
  43. static string addLeadingSlash(string path);
  44. static string addTrailingSlash(string path);
  45. static string removeTrailingSlash(string path);
  46. static string getPathForDirectory(string path);
  47. static string getAbsolutePath(string path, bool bRelativeToData = true);
  48. static bool isAbsolute(string path);
  49. static string getFileName(string filePath, bool bRelativeToData = true);
  50. static string getBaseName(string filePath); // filename without extension
  51. static string getEnclosingDirectory(string filePath, bool bRelativeToData = true);
  52. static string getCurrentWorkingDirectory();
  53. static string join(string path1,string path2);
  54. static string getCurrentExePath();
  55. static string getCurrentExeDir();
  56. static string getUserHomeDir();
  57. };
  58. class ofFile: public fstream{
  59. public:
  60. enum Mode{
  61. Reference,
  62. ReadOnly,
  63. WriteOnly,
  64. ReadWrite,
  65. Append
  66. };
  67. ofFile();
  68. ofFile(string filePath, Mode mode=ReadOnly, bool binary=false);
  69. ofFile(const ofFile & mom);
  70. ofFile & operator= (const ofFile & mom);
  71. ~ofFile();
  72. bool open(string path, Mode mode=ReadOnly, bool binary=false);
  73. bool changeMode(Mode mode, bool binary=false); // reopens a file to the same path with a different mode;
  74. void close();
  75. bool create();
  76. bool exists() const;
  77. string path() const;
  78. string getExtension() const;
  79. string getFileName() const;
  80. string getBaseName() const; // filename without extension
  81. string getEnclosingDirectory() const;
  82. string getAbsolutePath() const;
  83. bool canRead() const;
  84. bool canWrite() const;
  85. bool canExecute() const;
  86. bool isFile() const;
  87. bool isLink() const;
  88. bool isDirectory() const;
  89. bool isDevice() const;
  90. bool isHidden() const;
  91. void setWriteable(bool writeable);
  92. void setReadOnly(bool readable);
  93. void setExecutable(bool executable);
  94. //these all work for files and directories
  95. bool copyTo(string path, bool bRelativeToData = true, bool overwrite = false);
  96. bool moveTo(string path, bool bRelativeToData = true, bool overwrite = false);
  97. bool renameTo(string path, bool bRelativeToData = true, bool overwrite = false);
  98. //be careful! this deletes a file or folder :)
  99. bool remove(bool recursive=false);
  100. uint64_t getSize() const;
  101. //if you want access to a few other things
  102. Poco::File & getPocoFile();
  103. //this allows to compare files by their paths, also provides sorting and use as key in stl containers
  104. bool operator==(const ofFile & file) const;
  105. bool operator!=(const ofFile & file) const;
  106. bool operator<(const ofFile & file) const;
  107. bool operator<=(const ofFile & file) const;
  108. bool operator>(const ofFile & file) const;
  109. bool operator>=(const ofFile & file) const;
  110. //------------------
  111. // stream operations
  112. //------------------
  113. // since this class inherits from fstream it can be used as a r/w stream:
  114. // http://www.cplusplus.com/reference/iostream/fstream/
  115. //helper functions to read/write a whole file to/from an ofBuffer
  116. ofBuffer readToBuffer();
  117. bool writeFromBuffer(ofBuffer & buffer);
  118. // this can be used to read the whole stream into an output stream. ie:
  119. // it's equivalent to rdbuf() just here to make it easier to use
  120. // cout << file.getFileBuffer() << endl;
  121. // write_file << file.getFileBuffer();
  122. filebuf * getFileBuffer() const;
  123. //-------
  124. //static helpers
  125. //-------
  126. static bool copyFromTo(string pathSrc, string pathDst, bool bRelativeToData = true, bool overwrite = false);
  127. //be careful with slashes here - appending a slash when moving a folder will causes mad headaches in osx
  128. static bool moveFromTo(string pathSrc, string pathDst, bool bRelativeToData = true, bool overwrite = false);
  129. static bool doesFileExist(string fPath, bool bRelativeToData = true);
  130. static bool removeFile(string path, bool bRelativeToData = true);
  131. private:
  132. bool isWriteMode();
  133. bool openStream(Mode _mode, bool binary);
  134. void copyFrom(const ofFile & mom);
  135. Poco::File myFile;
  136. Mode mode;
  137. };
  138. class ofDirectory{
  139. public:
  140. ofDirectory();
  141. ofDirectory(string path);
  142. void open(string path);
  143. void close();
  144. bool create(bool recursive = false);
  145. bool exists() const;
  146. string path() const;
  147. bool canRead() const;
  148. bool canWrite() const;
  149. bool canExecute() const;
  150. bool isDirectory() const;
  151. bool isHidden() const;
  152. void setWriteable(bool writeable);
  153. void setReadOnly(bool readable);
  154. void setExecutable(bool executable);
  155. void setShowHidden(bool showHidden);
  156. bool copyTo(string path, bool bRelativeToData = true, bool overwrite = false);
  157. bool moveTo(string path, bool bRelativeToData = true, bool overwrite = false);
  158. bool renameTo(string path, bool bRelativeToData = true, bool overwrite = false);
  159. //be careful! this deletes a file or folder :)
  160. bool remove(bool recursive);
  161. //-------------------
  162. // dirList operations
  163. //-------------------
  164. void allowExt(string extension);
  165. int listDir(string path);
  166. int listDir();
  167. string getOriginalDirectory();
  168. string getName(unsigned int position); // e.g., "image.png"
  169. string getPath(unsigned int position);
  170. ofFile getFile(unsigned int position, ofFile::Mode mode=ofFile::Reference, bool binary=false);
  171. vector<ofFile> getFiles();
  172. ofFile operator[](unsigned int position);
  173. bool getShowHidden();
  174. void reset(); //equivalent to close, just here for bw compatibility with ofxDirList
  175. void sort();
  176. unsigned int size();
  177. int numFiles(); // numFiles is deprecated, use size()
  178. //if you want access to a few other things
  179. Poco::File & getPocoFile();
  180. //this allows to compare dirs by their paths, also provides sorting and use as key in stl containers
  181. bool operator==(const ofDirectory & dir);
  182. bool operator!=(const ofDirectory & dir);
  183. bool operator<(const ofDirectory & dir);
  184. bool operator<=(const ofDirectory & dir);
  185. bool operator>(const ofDirectory & dir);
  186. bool operator>=(const ofDirectory & dir);
  187. //-------
  188. //static helpers
  189. //-------
  190. static bool createDirectory(string dirPath, bool bRelativeToData = true, bool recursive = false);
  191. static bool isDirectoryEmpty(string dirPath, bool bRelativeToData = true );
  192. static bool doesDirectoryExist(string dirPath, bool bRelativeToData = true);
  193. static bool removeDirectory(string path, bool deleteIfNotEmpty, bool bRelativeToData = true);
  194. private:
  195. Poco::File myDir;
  196. string originalDirectory;
  197. vector <string> extensions;
  198. vector <ofFile> files;
  199. bool showHidden;
  200. };