1 /*============================================================================
2 CMake - Cross Platform Makefile Generator
3 Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
5 Distributed under the OSI-approved BSD License (the "License");
6 see accompanying file Copyright.txt for details.
8 This software is distributed WITHOUT ANY WARRANTY; without even the
9 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License for more information.
11 ============================================================================*/
12 #ifndef cmStringCommand_h
13 #define cmStringCommand_h
15 #include "cmCommand.h"
20 class RegularExpression;
23 /** \class cmStringCommand
24 * \brief Common string operations
27 class cmStringCommand : public cmCommand
31 * This is a virtual constructor for the command.
33 virtual cmCommand* Clone()
35 return new cmStringCommand;
39 * This is called when the command is first encountered in
40 * the CMakeLists.txt file.
42 virtual bool InitialPass(std::vector<std::string> const& args,
43 cmExecutionStatus &status);
46 * This determines if the command is invoked when in script mode.
48 virtual bool IsScriptable() const { return true; }
51 * The name of the command as specified in CMakeList.txt.
53 virtual const char* GetName() const { return "string";}
56 * Succinct documentation.
58 virtual const char* GetTerseDocumentation() const
60 return "String operations.";
66 virtual const char* GetFullDocumentation() const
69 " string(REGEX MATCH <regular_expression>\n"
70 " <output variable> <input> [<input>...])\n"
71 " string(REGEX MATCHALL <regular_expression>\n"
72 " <output variable> <input> [<input>...])\n"
73 " string(REGEX REPLACE <regular_expression>\n"
74 " <replace_expression> <output variable>\n"
75 " <input> [<input>...])\n"
76 " string(REPLACE <match_string>\n"
77 " <replace_string> <output variable>\n"
78 " <input> [<input>...])\n"
79 " string(<MD5|SHA1|SHA224|SHA256|SHA384|SHA512>\n"
80 " <output variable> <input>)\n"
81 " string(COMPARE EQUAL <string1> <string2> <output variable>)\n"
82 " string(COMPARE NOTEQUAL <string1> <string2> <output variable>)\n"
83 " string(COMPARE LESS <string1> <string2> <output variable>)\n"
84 " string(COMPARE GREATER <string1> <string2> <output variable>)\n"
85 " string(ASCII <number> [<number> ...] <output variable>)\n"
86 " string(CONFIGURE <string1> <output variable>\n"
87 " [@ONLY] [ESCAPE_QUOTES])\n"
88 " string(TOUPPER <string1> <output variable>)\n"
89 " string(TOLOWER <string1> <output variable>)\n"
90 " string(LENGTH <string> <output variable>)\n"
91 " string(SUBSTRING <string> <begin> <length> <output variable>)\n"
92 " string(STRIP <string> <output variable>)\n"
93 " string(RANDOM [LENGTH <length>] [ALPHABET <alphabet>]\n"
94 " [RANDOM_SEED <seed>] <output variable>)\n"
95 " string(FIND <string> <substring> <output variable> [REVERSE])\n"
96 "REGEX MATCH will match the regular expression once and store the "
97 "match in the output variable.\n"
98 "REGEX MATCHALL will match the regular expression as many times as "
99 "possible and store the matches in the output variable as a list.\n"
100 "REGEX REPLACE will match the regular expression as many times as "
101 "possible and substitute the replacement expression for the match "
102 "in the output. The replace expression may refer to paren-delimited "
103 "subexpressions of the match using \\1, \\2, ..., \\9. Note that "
104 "two backslashes (\\\\1) are required in CMake code to get a "
105 "backslash through argument parsing.\n"
106 "REPLACE will replace all occurrences of match_string in the input with "
107 "replace_string and store the result in the output.\n"
108 "MD5, SHA1, SHA224, SHA256, SHA384, and SHA512 "
109 "will compute a cryptographic hash of the input string.\n"
110 "COMPARE EQUAL/NOTEQUAL/LESS/GREATER will compare the strings and "
111 "store true or false in the output variable.\n"
112 "ASCII will convert all numbers into corresponding ASCII characters.\n"
113 "CONFIGURE will transform a string like CONFIGURE_FILE transforms "
115 "TOUPPER/TOLOWER will convert string to upper/lower characters.\n"
116 "LENGTH will return a given string's length.\n"
117 "SUBSTRING will return a substring of a given string. If length is "
118 "-1 the remainder of the string starting at begin will be returned.\n"
119 "STRIP will return a substring of a given string with leading "
120 "and trailing spaces removed.\n"
121 "RANDOM will return a random string of given length consisting of "
122 "characters from the given alphabet. Default length is 5 "
123 "characters and default alphabet is all numbers and upper and "
124 "lower case letters. If an integer RANDOM_SEED is given, its "
125 "value will be used to seed the random number generator.\n"
126 "FIND will return the position where the given substring was found "
127 "in the supplied string. If the REVERSE flag was used, the command "
128 "will search for the position of the last occurrence of the "
129 "specified substring.\n"
130 "The following characters have special meaning in regular expressions:\n"
131 " ^ Matches at beginning of a line\n"
132 " $ Matches at end of a line\n"
133 " . Matches any single character\n"
134 " [ ] Matches any character(s) inside the brackets\n"
135 " [^ ] Matches any character(s) not inside the brackets\n"
136 " - Matches any character in range on either side of a dash\n"
137 " * Matches preceding pattern zero or more times\n"
138 " + Matches preceding pattern one or more times\n"
139 " ? Matches preceding pattern zero or once only\n"
140 " | Matches a pattern on either side of the |\n"
141 " () Saves a matched subexpression, which can be referenced \n"
142 " in the REGEX REPLACE operation. Additionally it is saved\n"
143 " by all regular expression-related commands, including \n"
144 " e.g. if( MATCHES ), in the variables CMAKE_MATCH_(0..9).";
147 cmTypeMacro(cmStringCommand, cmCommand);
148 static void ClearMatches(cmMakefile* mf);
149 static void StoreMatches(cmMakefile* mf, cmsys::RegularExpression& re);
151 bool HandleConfigureCommand(std::vector<std::string> const& args);
152 bool HandleAsciiCommand(std::vector<std::string> const& args);
153 bool HandleRegexCommand(std::vector<std::string> const& args);
154 bool RegexMatch(std::vector<std::string> const& args);
155 bool RegexMatchAll(std::vector<std::string> const& args);
156 bool RegexReplace(std::vector<std::string> const& args);
157 bool HandleHashCommand(std::vector<std::string> const& args);
158 bool HandleToUpperLowerCommand(std::vector<std::string> const& args,
160 bool HandleCompareCommand(std::vector<std::string> const& args);
161 bool HandleReplaceCommand(std::vector<std::string> const& args);
162 bool HandleLengthCommand(std::vector<std::string> const& args);
163 bool HandleSubstringCommand(std::vector<std::string> const& args);
164 bool HandleStripCommand(std::vector<std::string> const& args);
165 bool HandleRandomCommand(std::vector<std::string> const& args);
166 bool HandleFindCommand(std::vector<std::string> const& args);
168 class RegexReplacement
171 RegexReplacement(const char* s): number(-1), value(s) {}
172 RegexReplacement(const std::string& s): number(-1), value(s) {}
173 RegexReplacement(int n): number(n), value() {}
174 RegexReplacement() {};