FileName.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #ifndef FILENAME_H
  2. #define FILENAME_H
  3. #include <string>
  4. namespace NICE {
  5. /**
  6. * Represents a file name.
  7. * Supports special string and file operations.
  8. *
  9. * @author Ferid Bajramovic (ferid [dot] bajramovic [at] informatik [dot] uni-jena [dot] de)
  10. */
  11. class FileName {
  12. public:
  13. /**
  14. * Constructor: empty filename.
  15. */
  16. FileName();
  17. /**
  18. * Constructor.
  19. * @param _fileName initial file name
  20. */
  21. FileName(const std::string& _fileName);
  22. /**
  23. * Constructor.
  24. * @param _fileName initial file name
  25. */
  26. FileName(const char* _fileName);
  27. /**
  28. * Copy-Constructor.
  29. */
  30. FileName(const FileName& _fileName);
  31. /**
  32. * Assignment.
  33. */
  34. FileName& operator=(const FileName& _fileName);
  35. /**
  36. * Assignment.
  37. */
  38. FileName& operator=(const std::string& _fileName);
  39. /**
  40. * Assignment.
  41. */
  42. FileName& operator=(const char* _fileName);
  43. virtual ~FileName();
  44. /**
  45. * Convert to \c string.
  46. * @return the file name (as \c string)
  47. */
  48. const std::string& str() const;
  49. /**
  50. * Convert (cast) to <tt>char *</tt>.
  51. * @return the file name (as <tt>char *</tt>)
  52. */
  53. operator const char*() const;
  54. /**
  55. * Convert (cast) to \c string.
  56. * @return the file name (as \c string)
  57. */
  58. operator const std::string&() const;
  59. /**
  60. * Set the FileName to \c s.
  61. * @param s new filename
  62. */
  63. inline void set(const std::string& s) { fileName = s; }
  64. /**
  65. * Extract the path (including the last slash '/').
  66. * Example: <tt> FileName("/a/b/c/file.txt").extractPath()</tt>
  67. * returns "/a/b/c/"
  68. * @return the path
  69. */
  70. FileName extractPath() const;
  71. /**
  72. * Extract the file name (name and extension, no path)
  73. * Example: <tt>FileName("/a/b/c/file.txt").extractFileName()</tt>
  74. * returns "file.txt"
  75. * @return file name without path
  76. */
  77. FileName extractFileName() const;
  78. /**
  79. * Extract the extension (everything from the last dot '.', inclusive)
  80. * Example: <tt>FileName("/a/b/c/file.txt").extractExtension()</tt>
  81. * returns ".txt"
  82. * @return the file extension
  83. */
  84. FileName extractExtension() const;
  85. /**
  86. * Remove the extension if one exists (everything from the last dot '.', inclusive)
  87. */
  88. void removeExtension();
  89. /**
  90. * Add a slash '/' at the end if there is non
  91. * (except when the FileName is empty "")
  92. */
  93. void addSlash();
  94. /**
  95. * Remove a slash '/' at the end if there is one
  96. */
  97. void removeSlash();
  98. /**
  99. * Add \c extension as file name extension,
  100. * except if the file name extension is already \c extension.
  101. * @param extension The new file name extension
  102. * (should contain a leading dot ".")
  103. */
  104. void setExtension(const std::string& extension);
  105. /**
  106. * Create a directory named as this FileName.
  107. */
  108. void createDirectory() const;
  109. /**
  110. * Does this filename exist (i.e. a file with this name)?
  111. * @return true if the file exists
  112. */
  113. bool fileExists() const;
  114. /**
  115. * Is this filename a directory?
  116. * @return true if it's directory
  117. */
  118. bool isDirectory() const;
  119. /**
  120. * Delete the file (does not work on directories).
  121. */
  122. void deleteFile() const;
  123. private:
  124. //! stores the file name
  125. std::string fileName;
  126. };
  127. } // namespace
  128. #endif // FILENAME_H