filename.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright (C) 2010 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. // $Id$
  15. #include <iostream>
  16. #include <algorithm>
  17. #include <string>
  18. #include <ctype.h>
  19. #include <log/filename.h>
  20. #include <log/strutil.h>
  21. using namespace std;
  22. namespace isc {
  23. namespace log {
  24. // Split string into components. Any backslashes are assumed to have
  25. // been replaced by forward slashes.
  26. void
  27. Filename::split(const string& full_name, string& directory,
  28. string& name, string& extension) const
  29. {
  30. directory = name = extension = "";
  31. bool dir_present = false;
  32. if (!full_name.empty()) {
  33. // Find the directory.
  34. size_t last_slash = full_name.find_last_of('/');
  35. if (last_slash != string::npos) {
  36. // Found the last slash, so extract directory component and
  37. // set where the scan for the last_dot should terminate.
  38. directory = full_name.substr(0, last_slash + 1);
  39. if (last_slash == full_name.size()) {
  40. // The entire string was a directory, so exit not and don't
  41. // do any more searching.
  42. return;
  43. }
  44. // Found a directory so note the fact.
  45. dir_present = true;
  46. }
  47. // Now search backwards for the last ".".
  48. size_t last_dot = full_name.find_last_of('.');
  49. if ((last_dot == string::npos) ||
  50. (dir_present && (last_dot < last_slash))) {
  51. // Last "." either not found or it occurs to the left of the last
  52. // slash if a directory was present (so it is part of a directory
  53. // name). In this case, the remainder of the string after the slash
  54. // is the name part.
  55. name = full_name.substr(last_slash + 1);
  56. return;
  57. }
  58. // Did find a valid dot, so it and everything to the right is the
  59. // extension...
  60. extension = full_name.substr(last_dot);
  61. // ... and the name of the file is everything in between.
  62. if ((last_dot - last_slash) > 1) {
  63. name = full_name.substr(last_slash + 1, last_dot - last_slash - 1);
  64. }
  65. }
  66. }
  67. // Expand the stored filename with the default.
  68. string
  69. Filename::expandWithDefault(const string& defname) const {
  70. string def_directory("");
  71. string def_name("");
  72. string def_extension("");
  73. // Normalize the input string.
  74. string copy_defname = isc::strutil::trim(defname);
  75. #ifdef WIN32
  76. isc::strutil::normalizeSlash(copy_defname);
  77. #endif
  78. // Split into the components
  79. split(copy_defname, def_directory, def_name, def_extension);
  80. // Now construct the result.
  81. string retstring =
  82. (directory_.empty() ? def_directory : directory_) +
  83. (name_.empty() ? def_name : name_) +
  84. (extension_.empty() ? def_extension : extension_);
  85. return retstring;
  86. }
  87. // Use the stored name as default for a given name
  88. string
  89. Filename::useAsDefault(const string& name) const {
  90. string name_directory("");
  91. string name_name("");
  92. string name_extension("");
  93. // Normalize the input string.
  94. string copy_name = isc::strutil::trim(name);
  95. #ifdef WIN32
  96. isc::strutil::normalizeSlash(copy_name);
  97. #endif
  98. // Split into the components
  99. split(copy_name, name_directory, name_name, name_extension);
  100. // Now construct the result.
  101. string retstring =
  102. (name_directory.empty() ? directory_ : name_directory) +
  103. (name_name.empty() ? name_ : name_name) +
  104. (name_extension.empty() ? extension_ : name_extension);
  105. return retstring;
  106. }
  107. } // namespace log
  108. } // namespace isc