filename.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef __FILENAME_H
  15. #define __FILENAME_H
  16. #include <string>
  17. #include <util/strutil.h>
  18. namespace isc {
  19. namespace util {
  20. /// \brief Class to Manipulate Filenames
  21. ///
  22. /// This is a utility class to manipulate filenames. It repeats some of the
  23. /// features found in the Boost filename class, but is self-contained so avoids
  24. /// the need to link in the Boost library.
  25. ///
  26. /// A Unix-style filename comprises three parts:
  27. ///
  28. /// Directory - everything up to and including the last "/". If there is no
  29. /// "/" in the string, there is no directory component. Note that the
  30. /// requirement of a trailing slash eliminates the ambiguity of whether a
  31. /// component is a directory or not, e.g. in /alpha/beta", "beta" could be the
  32. /// name of a directory or is could be a file. The interpretation here is that
  33. /// "beta" is the name of a file (although that file could be a directory).
  34. ///
  35. /// Note: Under Windows, the drive letter is considered to be part of the
  36. /// directory specification. Unless this class becomes more widely-used on
  37. /// Windows, there is no point in adding redundant code.
  38. ///
  39. /// Name - everthing from the character after the last "/" up to but not
  40. /// including the last ".".
  41. ///
  42. /// Extension - everthing from the right-most "." (after the right-most "/") to
  43. /// the end of the string. If there is no "." after the last "/", there is
  44. /// no file extension.
  45. ///
  46. /// (Note that on Windows, this function will replace all "\" characters
  47. /// with "/" characters on input strings.)
  48. ///
  49. /// This class provides functions for extracting the components and for
  50. /// substituting components.
  51. class Filename {
  52. public:
  53. /// \brief Constructor
  54. Filename(const std::string& name) :
  55. full_name_(""), directory_(""), name_(""), extension_("")
  56. {
  57. setName(name);
  58. }
  59. /// \brief Sets Stored Filename
  60. ///
  61. /// \param name New name to replaced currently stored name
  62. void setName(const std::string& name) {
  63. full_name_ = isc::util::str::trim(name);
  64. #ifdef WIN32
  65. isc::util::str::normalizeSlash(full_name_);
  66. #endif
  67. split(full_name_, directory_, name_, extension_);
  68. }
  69. /// \return Stored Filename
  70. std::string fullName() const {
  71. return (full_name_);
  72. }
  73. /// \return Directory of Given File Name
  74. std::string directory() const {
  75. return (directory_);
  76. }
  77. /// \brief Set directory for the file
  78. ///
  79. /// \param new_directory The directory to set. If this is an empty
  80. /// string, the directory this filename object currently
  81. /// has will be removed.
  82. void setDirectory(const std::string& new_directory);
  83. /// \return Name of Given File Name
  84. std::string name() const {
  85. return (name_);
  86. }
  87. /// \return Extension of Given File Name
  88. std::string extension() const {
  89. return (extension_);
  90. }
  91. /// \return Name + extension of Given File Name
  92. std::string nameAndExtension() const {
  93. return (name_ + extension_);
  94. }
  95. /// \brief Expand Name with Default
  96. ///
  97. /// A default file specified is supplied and used to fill in any missing
  98. /// fields. For example, if the name stored is "/a/b" and the supplied
  99. /// name is "c.d", the result is "/a/b.d": the only field missing from the
  100. /// stored name is the extension, which is supplied by the default.
  101. /// Another example would be to store "a.b" and to supply a default of
  102. /// "/c/d/" - the result is "/c/d/a.b". (Note that if the supplied default
  103. /// was "/c/d", the result would be "/c/a.b", even if "/c/d" were actually
  104. /// a directory.)
  105. ///
  106. /// \param defname Default name
  107. ///
  108. /// \return Name expanded with defname.
  109. std::string expandWithDefault(const std::string& defname) const;
  110. /// \brief Use as Default and Substitute into String
  111. ///
  112. /// Does essentially the inverse of expand(); that filled in the stored
  113. /// name with a default and returned the result. This treats the stored
  114. /// name as the default and uses it to fill in a given name. In essence,
  115. /// the code:
  116. /// \code
  117. /// Filename f("/a/b");
  118. /// result = f.expandWithdefault("c.d");
  119. /// \endcode
  120. /// gives as a result "/a/b.d". This is the same as:
  121. /// \code
  122. /// Filename f("c.d");
  123. /// result = f.useAsDefault("/a/b");
  124. /// \endcode
  125. ///
  126. /// \param name Name to expand
  127. ///
  128. /// \return Name expanded with stored name
  129. std::string useAsDefault(const std::string& name) const;
  130. private:
  131. /// \brief Split Name into Components
  132. ///
  133. /// Splits the file name into the directory, name and extension parts.
  134. /// The name is assumed to have had back slashes replaced by forward
  135. /// slashes (if appropriate).
  136. ///
  137. /// \param full_name Name to split
  138. /// \param directory Returned directory part
  139. /// \param name Returned name part
  140. /// \param extension Returned extension part
  141. void split(const std::string& full_name, std::string& directory,
  142. std::string& name, std::string& extension) const;
  143. // Members
  144. std::string full_name_; ///< Given name
  145. std::string directory_; ///< Directory part
  146. std::string name_; ///< Name part
  147. std::string extension_; ///< Extension part
  148. };
  149. } // namespace util
  150. } // namespace isc
  151. #endif // __FILENAME_H