cmStringCommand.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 cmStringCommand
  17. * \brief Common string operations
  18. *
  19. */
  20. class cmStringCommand : public cmCommand
  21. {
  22. public:
  23. /**
  24. * This is a virtual constructor for the command.
  25. */
  26. virtual cmCommand* Clone()
  27. {
  28. return new cmStringCommand;
  29. }
  30. /**
  31. * This is called when the command is first encountered in
  32. * the CMakeLists.txt file.
  33. */
  34. virtual bool InitialPass(std::vector<std::string> const& args);
  35. /**
  36. * This determines if the command is invoked when in script mode.
  37. */
  38. virtual bool IsScriptable() { return true; }
  39. /**
  40. * The name of the command as specified in CMakeList.txt.
  41. */
  42. virtual const char* GetName() { return "STRING";}
  43. /**
  44. * Succinct documentation.
  45. */
  46. virtual const char* GetTerseDocumentation()
  47. {
  48. return "String operations.";
  49. }
  50. /**
  51. * More documentation.
  52. */
  53. virtual const char* GetFullDocumentation()
  54. {
  55. return
  56. " STRING(REGEX MATCH <regular_expression>\n"
  57. " <output variable> <input> [<input>...])\n"
  58. " STRING(REGEX MATCHALL <regular_expression>\n"
  59. " <output variable> <input> [<input>...])\n"
  60. " STRING(REGEX REPLACE <regular_expression>\n"
  61. " <replace_expression> <output variable>\n"
  62. " <input> [<input>...])\n"
  63. " STRING(REPLACE <match_expression>\n"
  64. " <replace_expression> <output variable>\n"
  65. " <input> [<input>...])\n"
  66. " STRING(COMPARE EQUAL <string1> <string2> <output variable>)\n"
  67. " STRING(COMPARE NOTEQUAL <string1> <string2> <output variable>)\n"
  68. " STRING(COMPARE LESS <string1> <string2> <output variable>)\n"
  69. " STRING(COMPARE GREATER <string1> <string2> <output variable>)\n"
  70. " STRING(ASCII <number> [<number> ...] <output variable>)\n"
  71. " STRING(CONFIGURE <string1> <output variable>\n"
  72. " [@ONLY] [ESCAPE_QUOTES])\n"
  73. " STRING(TOUPPER <string1> <output variable>)\n"
  74. " STRING(TOLOWER <string1> <output variable>)\n"
  75. " STRING(LENGTH <string> <output variable>)\n"
  76. " STRING(SUBSTRING <string> <begin> <length> <output variable>)\n"
  77. " STRING(STRIP <string> <output variable>)\n"
  78. " STRING(RANDOM [LENGTH <length>] [ALPHABET <alphabet>]\n"
  79. " <output variable>)\n"
  80. "REGEX MATCH will match the regular expression once and store the "
  81. "match in the output variable.\n"
  82. "REGEX MATCHALL will match the regular expression as many times as "
  83. "possible and store the matches in the output variable as a list.\n"
  84. "REGEX REPLACE will match the regular expression as many times as "
  85. "possible and substitute the replacement expression for the match "
  86. "in the output. The replace expression may refer to paren-delimited "
  87. "subexpressions of the match using \\1, \\2, ..., \\9. Note that "
  88. "two backslashes (\\\\1) are required in CMake code to get a "
  89. "backslash through argument parsing.\n"
  90. "REPLACE will match the given expression "
  91. "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. "COMPARE EQUAL/NOTEQUAL/LESS/GREATER will compare the strings and "
  97. "store true or false in the output variable.\n"
  98. "ASCII will convert all numbers into corresponding ASCII characters.\n"
  99. "CONFIGURE will transform a string like CONFIGURE_FILE transforms "
  100. "a file.\n"
  101. "TOUPPER/TOLOWER will convert string to upper/lower characters.\n"
  102. "LENGTH will return a given string's length.\n"
  103. "SUBSTRING will return a substring of a given string.\n"
  104. "STRIP will return a substring of a given string with leading "
  105. "and trailing spaces removed.\n"
  106. "RANDOM will return a random string of given length consisting of "
  107. "characters from the given alphabet. Default length is 5 "
  108. "characters and default alphabet is all numbers and upper and "
  109. "lower case letters.";
  110. }
  111. cmTypeMacro(cmStringCommand, cmCommand);
  112. protected:
  113. bool HandleConfigureCommand(std::vector<std::string> const& args);
  114. bool HandleAsciiCommand(std::vector<std::string> const& args);
  115. bool HandleRegexCommand(std::vector<std::string> const& args);
  116. bool RegexMatch(std::vector<std::string> const& args);
  117. bool RegexMatchAll(std::vector<std::string> const& args);
  118. bool RegexReplace(std::vector<std::string> const& args);
  119. bool HandleToUpperLowerCommand(std::vector<std::string> const& args,
  120. bool toUpper);
  121. bool HandleCompareCommand(std::vector<std::string> const& args);
  122. bool HandleReplaceCommand(std::vector<std::string> const& args);
  123. bool HandleLengthCommand(std::vector<std::string> const& args);
  124. bool HandleSubstringCommand(std::vector<std::string> const& args);
  125. bool HandleStripCommand(std::vector<std::string> const& args);
  126. bool HandleRandomCommand(std::vector<std::string> const& args);
  127. class RegexReplacement
  128. {
  129. public:
  130. RegexReplacement(const char* s): number(-1), value(s) {}
  131. RegexReplacement(const std::string& s): number(-1), value(s) {}
  132. RegexReplacement(int n): number(n), value() {}
  133. RegexReplacement() {};
  134. int number;
  135. std::string value;
  136. };
  137. };
  138. #endif