cmStringCommand.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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(COMPARE EQUAL <string1> <string2> <output variable>)\n"
  70. " string(COMPARE NOTEQUAL <string1> <string2> <output variable>)\n"
  71. " string(COMPARE LESS <string1> <string2> <output variable>)\n"
  72. " string(COMPARE GREATER <string1> <string2> <output variable>)\n"
  73. " string(ASCII <number> [<number> ...] <output variable>)\n"
  74. " string(CONFIGURE <string1> <output variable>\n"
  75. " [@ONLY] [ESCAPE_QUOTES])\n"
  76. " string(TOUPPER <string1> <output variable>)\n"
  77. " string(TOLOWER <string1> <output variable>)\n"
  78. " string(LENGTH <string> <output variable>)\n"
  79. " string(SUBSTRING <string> <begin> <length> <output variable>)\n"
  80. " string(STRIP <string> <output variable>)\n"
  81. " string(RANDOM [LENGTH <length>] [ALPHABET <alphabet>]\n"
  82. " <output variable>)\n"
  83. "REGEX MATCH will match the regular expression once and store the "
  84. "match in the output variable.\n"
  85. "REGEX MATCHALL will match the regular expression as many times as "
  86. "possible and store the matches in the output variable as a list.\n"
  87. "REGEX REPLACE will match the regular expression as many times as "
  88. "possible and substitute the replacement expression for the match "
  89. "in the output. The replace expression may refer to paren-delimited "
  90. "subexpressions of the match using \\1, \\2, ..., \\9. Note that "
  91. "two backslashes (\\\\1) are required in CMake code to get a "
  92. "backslash through argument parsing.\n"
  93. "REPLACE will replace all occurrences of match_string in the input with "
  94. "replace_string and store the result in the output.\n"
  95. "COMPARE EQUAL/NOTEQUAL/LESS/GREATER will compare the strings and "
  96. "store true or false in the output variable.\n"
  97. "ASCII will convert all numbers into corresponding ASCII characters.\n"
  98. "CONFIGURE will transform a string like CONFIGURE_FILE transforms "
  99. "a file.\n"
  100. "TOUPPER/TOLOWER will convert string to upper/lower characters.\n"
  101. "LENGTH will return a given string's length.\n"
  102. "SUBSTRING will return a substring of a given string.\n"
  103. "STRIP will return a substring of a given string with leading "
  104. "and trailing spaces removed.\n"
  105. "RANDOM will return a random string of given length consisting of "
  106. "characters from the given alphabet. Default length is 5 "
  107. "characters and default alphabet is all numbers and upper and "
  108. "lower case letters.\n"
  109. "The following characters have special meaning in regular expressions:\n"
  110. " ^ Matches at beginning of a line\n"
  111. " $ Matches at end of a line\n"
  112. " . Matches any single character\n"
  113. " [ ] Matches any character(s) inside the brackets\n"
  114. " [^ ] Matches any character(s) not inside the brackets\n"
  115. " - Matches any character in range on either side of a dash\n"
  116. " * Matches preceding pattern zero or more times\n"
  117. " + Matches preceding pattern one or more times\n"
  118. " ? Matches preceding pattern zero or once only\n"
  119. " | Matches a pattern on either side of the |\n"
  120. " () Saves a matched subexpression, which can be referenced \n"
  121. " in the REGEX REPLACE operation. Additionally it is saved\n"
  122. " by all regular expression-related commands, including \n"
  123. " e.g. if( MATCHES ), in the variables CMAKE_MATCH_(0..9).";
  124. }
  125. cmTypeMacro(cmStringCommand, cmCommand);
  126. static void ClearMatches(cmMakefile* mf);
  127. static void StoreMatches(cmMakefile* mf, cmsys::RegularExpression& re);
  128. protected:
  129. bool HandleConfigureCommand(std::vector<std::string> const& args);
  130. bool HandleAsciiCommand(std::vector<std::string> const& args);
  131. bool HandleRegexCommand(std::vector<std::string> const& args);
  132. bool RegexMatch(std::vector<std::string> const& args);
  133. bool RegexMatchAll(std::vector<std::string> const& args);
  134. bool RegexReplace(std::vector<std::string> const& args);
  135. bool HandleToUpperLowerCommand(std::vector<std::string> const& args,
  136. bool toUpper);
  137. bool HandleCompareCommand(std::vector<std::string> const& args);
  138. bool HandleReplaceCommand(std::vector<std::string> const& args);
  139. bool HandleLengthCommand(std::vector<std::string> const& args);
  140. bool HandleSubstringCommand(std::vector<std::string> const& args);
  141. bool HandleStripCommand(std::vector<std::string> const& args);
  142. bool HandleRandomCommand(std::vector<std::string> const& args);
  143. class RegexReplacement
  144. {
  145. public:
  146. RegexReplacement(const char* s): number(-1), value(s) {}
  147. RegexReplacement(const std::string& s): number(-1), value(s) {}
  148. RegexReplacement(int n): number(n), value() {}
  149. RegexReplacement() {};
  150. int number;
  151. std::string value;
  152. };
  153. };
  154. #endif