728b1bc5fcf94de8accedf8beca1a2ac8b13b270
[platform/upstream/cmake.git] / Source / cmStringCommand.h
1 /*============================================================================
2   CMake - Cross Platform Makefile Generator
3   Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
4
5   Distributed under the OSI-approved BSD License (the "License");
6   see accompanying file Copyright.txt for details.
7
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
14
15 #include "cmCommand.h"
16
17 class cmMakefile;
18 namespace cmsys
19 {
20   class RegularExpression;
21 }
22
23 /** \class cmStringCommand
24  * \brief Common string operations
25  *
26  */
27 class cmStringCommand : public cmCommand
28 {
29 public:
30   /**
31    * This is a virtual constructor for the command.
32    */
33   virtual cmCommand* Clone()
34     {
35     return new cmStringCommand;
36     }
37
38   /**
39    * This is called when the command is first encountered in
40    * the CMakeLists.txt file.
41    */
42   virtual bool InitialPass(std::vector<std::string> const& args,
43                            cmExecutionStatus &status);
44
45   /**
46    * This determines if the command is invoked when in script mode.
47    */
48   virtual bool IsScriptable() const { return true; }
49
50   /**
51    * The name of the command as specified in CMakeList.txt.
52    */
53   virtual const char* GetName() const { return "string";}
54
55   /**
56    * Succinct documentation.
57    */
58   virtual const char* GetTerseDocumentation() const
59     {
60     return "String operations.";
61     }
62
63   /**
64    * More documentation.
65    */
66   virtual const char* GetFullDocumentation() const
67     {
68     return
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 "
114       "a file.\n"
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       "    -        Inside brackets, specifies an inclusive range between\n"
137       "             characters on either side e.g. [a-f] is [abcdef]\n"
138       "   *         Matches preceding pattern zero or more times\n"
139       "   +         Matches preceding pattern one or more times\n"
140       "   ?         Matches preceding pattern zero or once only\n"
141       "   |         Matches a pattern on either side of the |\n"
142       "   ()        Saves a matched subexpression, which can be referenced \n"
143       "             in the REGEX REPLACE operation. Additionally it is saved\n"
144       "             by all regular expression-related commands, including \n"
145       "             e.g. if( MATCHES ), in the variables CMAKE_MATCH_(0..9).";
146     }
147
148   cmTypeMacro(cmStringCommand, cmCommand);
149   static void ClearMatches(cmMakefile* mf);
150   static void StoreMatches(cmMakefile* mf, cmsys::RegularExpression& re);
151 protected:
152   bool HandleConfigureCommand(std::vector<std::string> const& args);
153   bool HandleAsciiCommand(std::vector<std::string> const& args);
154   bool HandleRegexCommand(std::vector<std::string> const& args);
155   bool RegexMatch(std::vector<std::string> const& args);
156   bool RegexMatchAll(std::vector<std::string> const& args);
157   bool RegexReplace(std::vector<std::string> const& args);
158   bool HandleHashCommand(std::vector<std::string> const& args);
159   bool HandleToUpperLowerCommand(std::vector<std::string> const& args,
160                                  bool toUpper);
161   bool HandleCompareCommand(std::vector<std::string> const& args);
162   bool HandleReplaceCommand(std::vector<std::string> const& args);
163   bool HandleLengthCommand(std::vector<std::string> const& args);
164   bool HandleSubstringCommand(std::vector<std::string> const& args);
165   bool HandleStripCommand(std::vector<std::string> const& args);
166   bool HandleRandomCommand(std::vector<std::string> const& args);
167   bool HandleFindCommand(std::vector<std::string> const& args);
168
169   class RegexReplacement
170   {
171   public:
172     RegexReplacement(const char* s): number(-1), value(s) {}
173     RegexReplacement(const std::string& s): number(-1), value(s) {}
174     RegexReplacement(int n): number(n), value() {}
175     RegexReplacement() {};
176     int number;
177     std::string value;
178   };
179 };
180
181
182 #endif