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