ppFilenamePattern.cxx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // Filename: ppFilenamePattern.cxx
  2. // Created by: drose (25Sep00)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. #include "ppFilenamePattern.h"
  6. #include <assert.h>
  7. ////////////////////////////////////////////////////////////////////
  8. // Function: PPFilenamePattern::Constructor
  9. // Access: Public
  10. // Description:
  11. ////////////////////////////////////////////////////////////////////
  12. PPFilenamePattern::
  13. PPFilenamePattern(const string &pattern) {
  14. size_t pct = pattern.find(PATTERN_WILDCARD);
  15. _has_wildcard = (pct != string::npos);
  16. if (_has_wildcard) {
  17. _prefix = pattern.substr(0, pct);
  18. _suffix = pattern.substr(pct + 1);
  19. } else {
  20. _prefix = pattern;
  21. }
  22. }
  23. ////////////////////////////////////////////////////////////////////
  24. // Function: PPFilenamePattern::Copy Constructor
  25. // Access: Public
  26. // Description:
  27. ////////////////////////////////////////////////////////////////////
  28. PPFilenamePattern::
  29. PPFilenamePattern(const PPFilenamePattern &copy) :
  30. _has_wildcard(copy._has_wildcard),
  31. _prefix(copy._prefix),
  32. _suffix(copy._suffix)
  33. {
  34. }
  35. ////////////////////////////////////////////////////////////////////
  36. // Function: PPFilenamePattern::Copy Assignment Operator
  37. // Access: Public
  38. // Description:
  39. ////////////////////////////////////////////////////////////////////
  40. void PPFilenamePattern::
  41. operator = (const PPFilenamePattern &copy) {
  42. _has_wildcard = copy._has_wildcard;
  43. _prefix = copy._prefix;
  44. _suffix = copy._suffix;
  45. }
  46. ////////////////////////////////////////////////////////////////////
  47. // Function: PPFilenamePattern::has_wildcard
  48. // Access: Public
  49. // Description: Returns true if the filename pattern contained a
  50. // wildcard (and hence represents a pattern and not
  51. // just a single particular filename), or false if it
  52. // did not.
  53. ////////////////////////////////////////////////////////////////////
  54. bool PPFilenamePattern::
  55. has_wildcard() const {
  56. return _has_wildcard;
  57. }
  58. ////////////////////////////////////////////////////////////////////
  59. // Function: PPFilenamePattern::get_pattern
  60. // Access: Public
  61. // Description: Returns the filename pattern.
  62. ////////////////////////////////////////////////////////////////////
  63. string PPFilenamePattern::
  64. get_pattern() const {
  65. if (_has_wildcard) {
  66. return _prefix + PATTERN_WILDCARD + _suffix;
  67. } else {
  68. return _prefix;
  69. }
  70. }
  71. ////////////////////////////////////////////////////////////////////
  72. // Function: PPFilenamePattern::get_prefix
  73. // Access: Public
  74. // Description: Returns the part of the filename pattern before the
  75. // wildcard. If the filename did not contain a
  76. // wildcard, this returns the entire filename.
  77. ////////////////////////////////////////////////////////////////////
  78. const string &PPFilenamePattern::
  79. get_prefix() const {
  80. return _prefix;
  81. }
  82. ////////////////////////////////////////////////////////////////////
  83. // Function: PPFilenamePattern::get_suffix
  84. // Access: Public
  85. // Description: Returns the part of the filename pattern after the
  86. // wildcard. If the filename did not contain a
  87. // wildcard, this returns the empty string.
  88. ////////////////////////////////////////////////////////////////////
  89. const string &PPFilenamePattern::
  90. get_suffix() const {
  91. return _suffix;
  92. }
  93. ////////////////////////////////////////////////////////////////////
  94. // Function: PPFilenamePattern::matches_filename
  95. // Access: Public
  96. // Description: Returns true if the given filename matches the
  97. // pattern, false otherwise.
  98. ////////////////////////////////////////////////////////////////////
  99. bool PPFilenamePattern::
  100. matches(const string &filename) const {
  101. if (_has_wildcard) {
  102. return
  103. (filename.length() >= _prefix.length() + _suffix.length()) &&
  104. (filename.substr(0, _prefix.length()) == _prefix) &&
  105. (filename.substr(filename.length() - _suffix.length()) == _suffix);
  106. } else {
  107. return (filename == _prefix);
  108. }
  109. }
  110. ////////////////////////////////////////////////////////////////////
  111. // Function: PPFilenamePattern::extract_body
  112. // Access: Public
  113. // Description: If the filename matches the pattern
  114. // (e.g. matches_filename() returns true), return the
  115. // portion of the filename that corresponds to the
  116. // wildcard in the pattern: the part of the filename
  117. // between the prefix and the suffix. If the filename
  118. // does not match the pattern, or the pattern does not
  119. // contain a wildcard, returns empty string.
  120. ////////////////////////////////////////////////////////////////////
  121. string PPFilenamePattern::
  122. extract_body(const string &filename) const {
  123. if (_has_wildcard) {
  124. size_t outside_length = _prefix.length() + _suffix.length();
  125. if ((filename.length() >= outside_length) &&
  126. (filename.substr(0, _prefix.length()) == _prefix) &&
  127. (filename.substr(filename.length() - _suffix.length()) == _suffix)) {
  128. return filename.substr(_prefix.length(), filename.length() - outside_length);
  129. }
  130. }
  131. return string();
  132. }
  133. ////////////////////////////////////////////////////////////////////
  134. // Function: PPFilenamePattern::transform_filename
  135. // Access: Public
  136. // Description: Transforms a filename by replacing the parts of the
  137. // filename described in the pattern transform_from with
  138. // the corresponding parts of the filename described in
  139. // this pattern. If the filename does not match
  140. // transform_from, returns the untransformed filename.
  141. //
  142. // It is an error to call this unless both this pattern
  143. // and transform_from include a wildcard.
  144. ////////////////////////////////////////////////////////////////////
  145. string PPFilenamePattern::
  146. transform(const string &filename,
  147. const PPFilenamePattern &transform_from) const {
  148. assert(transform_from._has_wildcard);
  149. if (transform_from.matches(filename)) {
  150. if (!_has_wildcard) {
  151. return _prefix;
  152. } else {
  153. string body = transform_from.extract_body(filename);
  154. string result = _prefix + body;
  155. // Now the suffix might contain more % characters. Replace all
  156. // of them.
  157. size_t p = 0;
  158. size_t pct = _suffix.find(PATTERN_WILDCARD, p);
  159. while (pct != string::npos) {
  160. result += _suffix.substr(p, pct - p);
  161. result += body;
  162. p = pct + 1;
  163. pct = _suffix.find(PATTERN_WILDCARD, p);
  164. }
  165. result += _suffix.substr(p);
  166. return result;
  167. }
  168. }
  169. return filename;
  170. }