cmStringCommand.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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() { return true; }
  42. /**
  43. * The name of the command as specified in CMakeList.txt.
  44. */
  45. virtual const char* GetName() { return "string";}
  46. /**
  47. * Succinct documentation.
  48. */
  49. virtual const char* GetTerseDocumentation()
  50. {
  51. return "String operations.";
  52. }
  53. /**
  54. * More documentation.
  55. */
  56. virtual const char* GetFullDocumentation()
  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. "REGEX MATCH will match the regular expression once and store the "
  87. "match in the output variable.\n"
  88. "REGEX MATCHALL will match the regular expression as many times as "
  89. "possible and store the matches in the output variable as a list.\n"
  90. "REGEX REPLACE will match the regular expression as many times as "
  91. "possible and substitute the replacement expression for the match "
  92. "in the output. The replace expression may refer to paren-delimited "
  93. "subexpressions of the match using \\1, \\2, ..., \\9. Note that "
  94. "two backslashes (\\\\1) are required in CMake code to get a "
  95. "backslash through argument parsing.\n"
  96. "REPLACE will replace all occurrences of match_string in the input with "
  97. "replace_string and store the result in the output.\n"
  98. "MD5, SHA1, SHA224, SHA256, SHA384, and SHA512 "
  99. "will compute a cryptographic hash of the input string.\n"
  100. "COMPARE EQUAL/NOTEQUAL/LESS/GREATER will compare the strings and "
  101. "store true or false in the output variable.\n"
  102. "ASCII will convert all numbers into corresponding ASCII characters.\n"
  103. "CONFIGURE will transform a string like CONFIGURE_FILE transforms "
  104. "a file.\n"
  105. "TOUPPER/TOLOWER will convert string to upper/lower characters.\n"
  106. "LENGTH will return a given string's length.\n"
  107. "SUBSTRING will return a substring of a given string. If length is "
  108. "-1 the remainder of the string starting at begin will be returned.\n"
  109. "STRIP will return a substring of a given string with leading "
  110. "and trailing spaces removed.\n"
  111. "RANDOM will return a random string of given length consisting of "
  112. "characters from the given alphabet. Default length is 5 "
  113. "characters and default alphabet is all numbers and upper and "
  114. "lower case letters. If an integer RANDOM_SEED is given, its "
  115. "value will be used to seed the random number generator.\n"
  116. "FIND will return the position where the given substring was found "
  117. "in the supplied string. If the REVERSE flag was used, the command "
  118. "will search for the position of the last occurrence of the "
  119. "specified substring.\n"
  120. "The following characters have special meaning in regular expressions:\n"
  121. " ^ Matches at beginning of a line\n"
  122. " $ Matches at end of a line\n"
  123. " . Matches any single character\n"
  124. " [ ] Matches any character(s) inside the brackets\n"
  125. " [^ ] Matches any character(s) not inside the brackets\n"
  126. " - Matches any character in range on either side of a dash\n"
  127. " * Matches preceding pattern zero or more times\n"
  128. " + Matches preceding pattern one or more times\n"
  129. " ? Matches preceding pattern zero or once only\n"
  130. " | Matches a pattern on either side of the |\n"
  131. " () Saves a matched subexpression, which can be referenced \n"
  132. " in the REGEX REPLACE operation. Additionally it is saved\n"
  133. " by all regular expression-related commands, including \n"
  134. " e.g. if( MATCHES ), in the variables CMAKE_MATCH_(0..9).";
  135. }
  136. cmTypeMacro(cmStringCommand, cmCommand);
  137. static void ClearMatches(cmMakefile* mf);
  138. static void StoreMatches(cmMakefile* mf, cmsys::RegularExpression& re);
  139. protected:
  140. bool HandleConfigureCommand(std::vector<std::string> const& args);
  141. bool HandleAsciiCommand(std::vector<std::string> const& args);
  142. bool HandleRegexCommand(std::vector<std::string> const& args);
  143. bool RegexMatch(std::vector<std::string> const& args);
  144. bool RegexMatchAll(std::vector<std::string> const& args);
  145. bool RegexReplace(std::vector<std::string> const& args);
  146. bool HandleHashCommand(std::vector<std::string> const& args);
  147. bool HandleToUpperLowerCommand(std::vector<std::string> const& args,
  148. bool toUpper);
  149. bool HandleCompareCommand(std::vector<std::string> const& args);
  150. bool HandleReplaceCommand(std::vector<std::string> const& args);
  151. bool HandleLengthCommand(std::vector<std::string> const& args);
  152. bool HandleSubstringCommand(std::vector<std::string> const& args);
  153. bool HandleStripCommand(std::vector<std::string> const& args);
  154. bool HandleRandomCommand(std::vector<std::string> const& args);
  155. bool HandleFindCommand(std::vector<std::string> const& args);
  156. class RegexReplacement
  157. {
  158. public:
  159. RegexReplacement(const char* s): number(-1), value(s) {}
  160. RegexReplacement(const std::string& s): number(-1), value(s) {}
  161. RegexReplacement(int n): number(n), value() {}
  162. RegexReplacement() {};
  163. int number;
  164. std::string value;
  165. };
  166. };
  167. #endif