cmStringCommand.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #ifndef cmStringCommand_h
  11. #define cmStringCommand_h
  12. #include "cmCommand.h"
  13. class cmMakefile;
  14. namespace cmsys
  15. {
  16. class RegularExpression;
  17. }
  18. /** \class cmStringCommand
  19. * \brief Common string operations
  20. *
  21. */
  22. class cmStringCommand : public cmCommand
  23. {
  24. public:
  25. /**
  26. * This is a virtual constructor for the command.
  27. */
  28. virtual cmCommand* Clone()
  29. {
  30. return new cmStringCommand;
  31. }
  32. /**
  33. * This is called when the command is first encountered in
  34. * the CMakeLists.txt file.
  35. */
  36. virtual bool InitialPass(std::vector<std::string> const& args,
  37. cmExecutionStatus &status);
  38. /**
  39. * This determines if the command is invoked when in script mode.
  40. */
  41. virtual bool IsScriptable() const { return true; }
  42. /**
  43. * The name of the command as specified in CMakeList.txt.
  44. */
  45. virtual const char* GetName() const { return "string";}
  46. /**
  47. * Succinct documentation.
  48. */
  49. virtual const char* GetTerseDocumentation() const
  50. {
  51. return "String operations.";
  52. }
  53. /**
  54. * More documentation.
  55. */
  56. virtual const char* GetFullDocumentation() const
  57. {
  58. return
  59. " string(REGEX MATCH <regular_expression>\n"
  60. " <output variable> <input> [<input>...])\n"
  61. " string(REGEX MATCHALL <regular_expression>\n"
  62. " <output variable> <input> [<input>...])\n"
  63. " string(REGEX REPLACE <regular_expression>\n"
  64. " <replace_expression> <output variable>\n"
  65. " <input> [<input>...])\n"
  66. " string(REPLACE <match_string>\n"
  67. " <replace_string> <output variable>\n"
  68. " <input> [<input>...])\n"
  69. " string(<MD5|SHA1|SHA224|SHA256|SHA384|SHA512>\n"
  70. " <output variable> <input>)\n"
  71. " string(COMPARE EQUAL <string1> <string2> <output variable>)\n"
  72. " string(COMPARE NOTEQUAL <string1> <string2> <output variable>)\n"
  73. " string(COMPARE LESS <string1> <string2> <output variable>)\n"
  74. " string(COMPARE GREATER <string1> <string2> <output variable>)\n"
  75. " string(ASCII <number> [<number> ...] <output variable>)\n"
  76. " string(CONFIGURE <string1> <output variable>\n"
  77. " [@ONLY] [ESCAPE_QUOTES])\n"
  78. " string(TOUPPER <string1> <output variable>)\n"
  79. " string(TOLOWER <string1> <output variable>)\n"
  80. " string(LENGTH <string> <output variable>)\n"
  81. " string(SUBSTRING <string> <begin> <length> <output variable>)\n"
  82. " string(STRIP <string> <output variable>)\n"
  83. " string(RANDOM [LENGTH <length>] [ALPHABET <alphabet>]\n"
  84. " [RANDOM_SEED <seed>] <output variable>)\n"
  85. " string(FIND <string> <substring> <output variable> [REVERSE])\n"
  86. " string(TIMESTAMP <output variable> [<format string>] [UTC])\n"
  87. "REGEX MATCH will match the regular expression once and store the "
  88. "match in the output variable.\n"
  89. "REGEX MATCHALL will match the regular expression as many times as "
  90. "possible and store the matches in the output variable as a list.\n"
  91. "REGEX REPLACE will match the regular expression as many times as "
  92. "possible and substitute the replacement expression for the match "
  93. "in the output. The replace expression may refer to paren-delimited "
  94. "subexpressions of the match using \\1, \\2, ..., \\9. Note that "
  95. "two backslashes (\\\\1) are required in CMake code to get a "
  96. "backslash through argument parsing.\n"
  97. "REPLACE will replace all occurrences of match_string in the input with "
  98. "replace_string and store the result in the output.\n"
  99. "MD5, SHA1, SHA224, SHA256, SHA384, and SHA512 "
  100. "will compute a cryptographic hash of the input string.\n"
  101. "COMPARE EQUAL/NOTEQUAL/LESS/GREATER will compare the strings and "
  102. "store true or false in the output variable.\n"
  103. "ASCII will convert all numbers into corresponding ASCII characters.\n"
  104. "CONFIGURE will transform a string like CONFIGURE_FILE transforms "
  105. "a file.\n"
  106. "TOUPPER/TOLOWER will convert string to upper/lower characters.\n"
  107. "LENGTH will return a given string's length.\n"
  108. "SUBSTRING will return a substring of a given string. If length is "
  109. "-1 the remainder of the string starting at begin will be returned.\n"
  110. "STRIP will return a substring of a given string with leading "
  111. "and trailing spaces removed.\n"
  112. "RANDOM will return a random string of given length consisting of "
  113. "characters from the given alphabet. Default length is 5 "
  114. "characters and default alphabet is all numbers and upper and "
  115. "lower case letters. If an integer RANDOM_SEED is given, its "
  116. "value will be used to seed the random number generator.\n"
  117. "FIND will return the position where the given substring was found "
  118. "in the supplied string. If the REVERSE flag was used, the command "
  119. "will search for the position of the last occurrence of the "
  120. "specified substring.\n"
  121. "The following characters have special meaning in regular expressions:\n"
  122. " ^ Matches at beginning of a line\n"
  123. " $ Matches at end of a line\n"
  124. " . Matches any single character\n"
  125. " [ ] Matches any character(s) inside the brackets\n"
  126. " [^ ] Matches any character(s) not inside the brackets\n"
  127. " - Inside brackets, specifies an inclusive range between\n"
  128. " characters on either side e.g. [a-f] is [abcdef]\n"
  129. " * Matches preceding pattern zero or more times\n"
  130. " + Matches preceding pattern one or more times\n"
  131. " ? Matches preceding pattern zero or once only\n"
  132. " | Matches a pattern on either side of the |\n"
  133. " () Saves a matched subexpression, which can be referenced \n"
  134. " in the REGEX REPLACE operation. Additionally it is saved\n"
  135. " by all regular expression-related commands, including \n"
  136. " e.g. if( MATCHES ), in the variables CMAKE_MATCH_(0..9).\n"
  137. "TIMESTAMP will write a string representation of "
  138. "the current date and/or time to <output variable>.\n"
  139. "Should the command be unable to obtain a timestamp "
  140. "<output variable> will be set to the empty string \"\".\n"
  141. "The optional UTC flag requests the current date/time "
  142. "representation to be in Coordinated Universal Time (UTC) "
  143. "rather than local time.\n"
  144. "The optional <format string> may contain the following "
  145. "format specifiers: \n"
  146. " %d The day of the current month (01-31).\n"
  147. " %H The hour on a 24-hour clock (00-23).\n"
  148. " %I The hour on a 12-hour clock (01-12).\n"
  149. " %j The day of the current year (001-366).\n"
  150. " %m The month of the current year (01-12).\n"
  151. " %M The minute of the current hour (00-59).\n"
  152. " %S The second of the current minute.\n"
  153. " 60 represents a leap second. (00-60)\n"
  154. " %U The week number of the current year (00-53).\n"
  155. " %w The day of the current week. 0 is Sunday. (0-6)\n"
  156. " %y The last two digits of the current year (00-99)\n"
  157. " %Y The current year. \n"
  158. "Unknown format specifiers will be ignored "
  159. "and copied to the output as-is.\n"
  160. "If no explicit <format string> is given it will default to:\n"
  161. " %Y-%m-%dT%H:%M:%S for local time.\n"
  162. " %Y-%m-%dT%H:%M:%SZ for UTC.";
  163. }
  164. cmTypeMacro(cmStringCommand, cmCommand);
  165. static void ClearMatches(cmMakefile* mf);
  166. static void StoreMatches(cmMakefile* mf, cmsys::RegularExpression& re);
  167. protected:
  168. bool HandleConfigureCommand(std::vector<std::string> const& args);
  169. bool HandleAsciiCommand(std::vector<std::string> const& args);
  170. bool HandleRegexCommand(std::vector<std::string> const& args);
  171. bool RegexMatch(std::vector<std::string> const& args);
  172. bool RegexMatchAll(std::vector<std::string> const& args);
  173. bool RegexReplace(std::vector<std::string> const& args);
  174. bool HandleHashCommand(std::vector<std::string> const& args);
  175. bool HandleToUpperLowerCommand(std::vector<std::string> const& args,
  176. bool toUpper);
  177. bool HandleCompareCommand(std::vector<std::string> const& args);
  178. bool HandleReplaceCommand(std::vector<std::string> const& args);
  179. bool HandleLengthCommand(std::vector<std::string> const& args);
  180. bool HandleSubstringCommand(std::vector<std::string> const& args);
  181. bool HandleStripCommand(std::vector<std::string> const& args);
  182. bool HandleRandomCommand(std::vector<std::string> const& args);
  183. bool HandleFindCommand(std::vector<std::string> const& args);
  184. bool HandleTimestampCommand(std::vector<std::string> const& args);
  185. class RegexReplacement
  186. {
  187. public:
  188. RegexReplacement(const char* s): number(-1), value(s) {}
  189. RegexReplacement(const std::string& s): number(-1), value(s) {}
  190. RegexReplacement(int n): number(n), value() {}
  191. RegexReplacement() {};
  192. int number;
  193. std::string value;
  194. };
  195. };
  196. #endif