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