You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
6.9 KiB

  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #ifndef cmStringCommand_h
  14. #define cmStringCommand_h
  15. #include "cmCommand.h"
  16. class cmMakefile;
  17. namespace cmsys
  18. {
  19. class RegularExpression;
  20. }
  21. /** \class cmStringCommand
  22. * \brief Common string operations
  23. *
  24. */
  25. class cmStringCommand : public cmCommand
  26. {
  27. public:
  28. /**
  29. * This is a virtual constructor for the command.
  30. */
  31. virtual cmCommand* Clone()
  32. {
  33. return new cmStringCommand;
  34. }
  35. /**
  36. * This is called when the command is first encountered in
  37. * the CMakeLists.txt file.
  38. */
  39. virtual bool InitialPass(std::vector<std::string> const& args);
  40. /**
  41. * This determines if the command is invoked when in script mode.
  42. */
  43. virtual bool IsScriptable() { return true; }
  44. /**
  45. * The name of the command as specified in CMakeList.txt.
  46. */
  47. virtual const char* GetName() { return "string";}
  48. /**
  49. * Succinct documentation.
  50. */
  51. virtual const char* GetTerseDocumentation()
  52. {
  53. return "String operations.";
  54. }
  55. /**
  56. * More documentation.
  57. */
  58. virtual const char* GetFullDocumentation()
  59. {
  60. return
  61. " string(REGEX MATCH <regular_expression>\n"
  62. " <output variable> <input> [<input>...])\n"
  63. " string(REGEX MATCHALL <regular_expression>\n"
  64. " <output variable> <input> [<input>...])\n"
  65. " string(REGEX REPLACE <regular_expression>\n"
  66. " <replace_expression> <output variable>\n"
  67. " <input> [<input>...])\n"
  68. " string(REPLACE <match_string>\n"
  69. " <replace_string> <output variable>\n"
  70. " <input> [<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. " <output variable>)\n"
  85. "REGEX MATCH will match the regular expression once and store the "
  86. "match in the output variable.\n"
  87. "REGEX MATCHALL will match the regular expression as many times as "
  88. "possible and store the matches in the output variable as a list.\n"
  89. "REGEX REPLACE will match the regular expression as many times as "
  90. "possible and substitute the replacement expression for the match "
  91. "in the output. The replace expression may refer to paren-delimited "
  92. "subexpressions of the match using \\1, \\2, ..., \\9. Note that "
  93. "two backslashes (\\\\1) are required in CMake code to get a "
  94. "backslash through argument parsing.\n"
  95. "REPLACE will replace all occurences of match_string in the input with "
  96. "replace_string and store the result in the output.\n"
  97. "COMPARE EQUAL/NOTEQUAL/LESS/GREATER will compare the strings and "
  98. "store true or false in the output variable.\n"
  99. "ASCII will convert all numbers into corresponding ASCII characters.\n"
  100. "CONFIGURE will transform a string like CONFIGURE_FILE transforms "
  101. "a file.\n"
  102. "TOUPPER/TOLOWER will convert string to upper/lower characters.\n"
  103. "LENGTH will return a given string's length.\n"
  104. "SUBSTRING will return a substring of a given string.\n"
  105. "STRIP will return a substring of a given string with leading "
  106. "and trailing spaces removed.\n"
  107. "RANDOM will return a random string of given length consisting of "
  108. "characters from the given alphabet. Default length is 5 "
  109. "characters and default alphabet is all numbers and upper and "
  110. "lower case letters.\n"
  111. "The following characters have special meaning in regular expressions:\n"
  112. " ^ Matches at beginning of a line\n"
  113. " $ Matches at end of a line\n"
  114. " . Matches any single character\n"
  115. " [ ] Matches any character(s) inside the brackets\n"
  116. " [^ ] Matches any character(s) not inside the brackets\n"
  117. " - Matches any character in range on either side of a dash\n"
  118. " * Matches preceding pattern zero or more times\n"
  119. " + Matches preceding pattern one or more times\n"
  120. " ? Matches preceding pattern zero or once only\n"
  121. " | Matches a pattern on either side of the |\n"
  122. " () Saves a matched subexpression, which can be referenced in "
  123. "the REGEX REPLACE operation. Additionally it is saved in the special "
  124. "CMake variables CMAKE_MATCH_(0..9).";
  125. }
  126. cmTypeMacro(cmStringCommand, cmCommand);
  127. static void ClearMatches(cmMakefile* mf);
  128. static void StoreMatches(cmMakefile* mf, cmsys::RegularExpression& re);
  129. protected:
  130. bool HandleConfigureCommand(std::vector<std::string> const& args);
  131. bool HandleAsciiCommand(std::vector<std::string> const& args);
  132. bool HandleRegexCommand(std::vector<std::string> const& args);
  133. bool RegexMatch(std::vector<std::string> const& args);
  134. bool RegexMatchAll(std::vector<std::string> const& args);
  135. bool RegexReplace(std::vector<std::string> const& args);
  136. bool HandleToUpperLowerCommand(std::vector<std::string> const& args,
  137. bool toUpper);
  138. bool HandleCompareCommand(std::vector<std::string> const& args);
  139. bool HandleReplaceCommand(std::vector<std::string> const& args);
  140. bool HandleLengthCommand(std::vector<std::string> const& args);
  141. bool HandleSubstringCommand(std::vector<std::string> const& args);
  142. bool HandleStripCommand(std::vector<std::string> const& args);
  143. bool HandleRandomCommand(std::vector<std::string> const& args);
  144. class RegexReplacement
  145. {
  146. public:
  147. RegexReplacement(const char* s): number(-1), value(s) {}
  148. RegexReplacement(const std::string& s): number(-1), value(s) {}
  149. RegexReplacement(int n): number(n), value() {}
  150. RegexReplacement() {};
  151. int number;
  152. std::string value;
  153. };
  154. };
  155. #endif