| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407 |
- /*=========================================================================
- Program: CMake - Cross-Platform Makefile Generator
- Module: $RCSfile$
- Language: C++
- Date: $Date$
- Version: $Revision$
- Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
- See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
- This software is distributed WITHOUT ANY WARRANTY; without even
- the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
- PURPOSE. See the above copyright notices for more information.
- =========================================================================*/
- #include "cmStringCommand.h"
- #include <cmsys/RegularExpression.hxx>
- #include <stdlib.h> // required for atoi
- //----------------------------------------------------------------------------
- bool cmStringCommand::InitialPass(std::vector<std::string> const& args)
- {
- if(args.size() < 1)
- {
- this->SetError("must be called with at least one argument.");
- return false;
- }
-
- std::string subCommand = args[0];
- if(subCommand == "REGEX")
- {
- return this->HandleRegexCommand(args);
- }
- else if(subCommand == "COMPARE")
- {
- return this->HandleCompareCommand(args);
- }
- else if(subCommand == "ASCII")
- {
- return this->HandleAsciiCommand(args);
- }
-
- std::string e = "does not recognize sub-command "+subCommand;
- this->SetError(e.c_str());
- return false;
- }
- //----------------------------------------------------------------------------
- bool cmStringCommand::HandleAsciiCommand(std::vector<std::string> const& args)
- {
- if ( args.size() <= 1 )
- {
- this->SetError("No output variable specified");
- return false;
- }
- std::string::size_type cc;
- std::string outvar = args[args.size()-1];
- std::string output = "";
- for ( cc = 1; cc < args.size()-1; cc ++ )
- {
- int ch = atoi(args[cc].c_str());
- if ( ch > 0 && ch < 256 )
- {
- output += static_cast<char>(ch);
- }
- else
- {
- std::string error = "Character with code ";
- error += ch;
- error += " does not exist.";
- this->SetError(error.c_str());
- return false;
- }
- }
- // Store the output in the provided variable.
- m_Makefile->AddDefinition(outvar.c_str(), output.c_str());
- return true;
- }
- //----------------------------------------------------------------------------
- bool cmStringCommand::HandleRegexCommand(std::vector<std::string> const& args)
- {
- if(args.size() < 2)
- {
- this->SetError("sub-command REGEX requires a mode to be specified.");
- return false;
- }
- std::string mode = args[1];
- if(mode == "MATCH")
- {
- if(args.size() < 5)
- {
- this->SetError("sub-command REGEX, mode MATCH needs "
- "at least 5 arguments total to command.");
- return false;
- }
- return this->RegexMatch(args);
- }
- else if(mode == "MATCHALL")
- {
- if(args.size() < 5)
- {
- this->SetError("sub-command REGEX, mode MATCHALL needs "
- "at least 5 arguments total to command.");
- return false;
- }
- return this->RegexMatchAll(args);
- }
- else if(mode == "REPLACE")
- {
- if(args.size() < 6)
- {
- this->SetError("sub-command REGEX, mode MATCH needs "
- "at least 6 arguments total to command.");
- return false;
- }
- return this->RegexReplace(args);
- }
-
- std::string e = "sub-command REGEX does not recognize mode "+mode;
- this->SetError(e.c_str());
- return false;
- }
- //----------------------------------------------------------------------------
- bool cmStringCommand::RegexMatch(std::vector<std::string> const& args)
- {
- //"STRING(REGEX MATCH <regular_expression> <output variable> <input> [<input>...])\n";
- std::string regex = args[2];
- std::string outvar = args[3];
-
- // Concatenate all the last arguments together.
- std::string input = args[4];
- for(unsigned int i=5; i < args.size(); ++i)
- {
- input += args[i];
- }
-
- // Compile the regular expression.
- cmsys::RegularExpression re;
- if(!re.compile(regex.c_str()))
- {
- std::string e = "sub-command REGEX, mode MATCH failed to compile regex \""+regex+"\".";
- this->SetError(e.c_str());
- return false;
- }
-
- // Scan through the input for all matches.
- std::string output;
- if(re.find(input.c_str()))
- {
- std::string::size_type l = re.start();
- std::string::size_type r = re.end();
- if(r-l == 0)
- {
- std::string e = "sub-command REGEX, mode MATCH regex \""+regex+"\" matched an empty string.";
- this->SetError(e.c_str());
- return false;
- }
- output = input.substr(l, r-l);
- }
-
- // Store the output in the provided variable.
- m_Makefile->AddDefinition(outvar.c_str(), output.c_str());
- return true;
- }
- //----------------------------------------------------------------------------
- bool cmStringCommand::RegexMatchAll(std::vector<std::string> const& args)
- {
- //"STRING(REGEX MATCHALL <regular_expression> <output variable> <input> [<input>...])\n";
- std::string regex = args[2];
- std::string outvar = args[3];
-
- // Concatenate all the last arguments together.
- std::string input = args[4];
- for(unsigned int i=5; i < args.size(); ++i)
- {
- input += args[i];
- }
-
- // Compile the regular expression.
- cmsys::RegularExpression re;
- if(!re.compile(regex.c_str()))
- {
- std::string e = "sub-command REGEX, mode MATCHALL failed to compile regex \""+regex+"\".";
- this->SetError(e.c_str());
- return false;
- }
-
- // Scan through the input for all matches.
- std::string output;
- const char* p = input.c_str();
- while(re.find(p))
- {
- std::string::size_type l = re.start();
- std::string::size_type r = re.end();
- if(r-l == 0)
- {
- std::string e = "sub-command REGEX, mode MATCHALL regex \""+regex+"\" matched an empty string.";
- this->SetError(e.c_str());
- return false;
- }
- if(output.length() > 0)
- {
- output += ";";
- }
- output += std::string(p+l, r-l);
- p += r;
- }
-
- // Store the output in the provided variable.
- m_Makefile->AddDefinition(outvar.c_str(), output.c_str());
- return true;
- }
- //----------------------------------------------------------------------------
- bool cmStringCommand::RegexReplace(std::vector<std::string> const& args)
- {
- //"STRING(REGEX REPLACE <regular_expression> <replace_expression> <output variable> <input> [<input>...])\n"
- std::string regex = args[2];
- std::string replace = args[3];
- std::string outvar = args[4];
-
- // Pull apart the replace expression to find the escaped [0-9] values.
- std::vector<RegexReplacement> replacement;
- std::string::size_type l = 0;
- while(l < replace.length())
- {
- std::string::size_type r = replace.find("\\", l);
- if(r == std::string::npos)
- {
- r = replace.length();
- replacement.push_back(replace.substr(l, r-l));
- }
- else
- {
- if(r-l > 0)
- {
- replacement.push_back(replace.substr(l, r-l));
- }
- if(r == (replace.length()-1))
- {
- this->SetError("sub-command REGEX, mode REPLACE: "
- "replace-expression ends in a backslash.");
- return false;
- }
- if((replace[r+1] >= '0') && (replace[r+1] <= '9'))
- {
- replacement.push_back(replace[r+1]-'0');
- }
- else if(replace[r+1] == 'n')
- {
- replacement.push_back("\n");
- }
- else if(replace[r+1] == '\\')
- {
- replacement.push_back("\\");
- }
- else
- {
- std::string e = "sub-command REGEX, mode REPLACE: Unknown escape \"";
- e += replace.substr(r, 2);
- e += "\"in replace-expression.";
- this->SetError(e.c_str());
- return false;
- }
- r += 2;
- }
- l = r;
- }
-
- // Concatenate all the last arguments together.
- std::string input = args[5];
- for(unsigned int i=6; i < args.size(); ++i)
- {
- input += args[i];
- }
-
- // Compile the regular expression.
- cmsys::RegularExpression re;
- if(!re.compile(regex.c_str()))
- {
- std::string e = "sub-command REGEX, mode REPLACE failed to compile regex \""+regex+"\".";
- this->SetError(e.c_str());
- return false;
- }
-
- // Scan through the input for all matches.
- std::string output;
- std::string::size_type base = 0;
- while(re.find(input.c_str()+base))
- {
- std::string::size_type l2 = re.start();
- std::string::size_type r = re.end();
-
- // Concatenate the part of the input that was not matched.
- output += input.substr(base, l2);
-
- // Make sure the match had some text.
- if(r-l2 == 0)
- {
- std::string e = "sub-command REGEX, mode REPLACE regex \""+regex+"\" matched an empty string.";
- this->SetError(e.c_str());
- return false;
- }
-
- // Concatenate the replacement for the match.
- for(unsigned int i=0; i < replacement.size(); ++i)
- {
- if(replacement[i].number < 0)
- {
- // This is just a plain-text part of the replacement.
- output += replacement[i].value;
- }
- else
- {
- // Replace with part of the match.
- int n = replacement[i].number;
- std::string::size_type start = re.start(n);
- std::string::size_type end = re.end(n);
- std::string::size_type len = input.length()-base;
- if((start != std::string::npos) && (end != std::string::npos) &&
- (start <= len) && (end <= len))
- {
- output += input.substr(base+start, end-start);
- }
- else
- {
- std::string e =
- "sub-command REGEX, mode REPLACE: replace expression \""+
- replace+"\" contains an out-of-range escape for regex \""+
- regex+"\".";
- this->SetError(e.c_str());
- return false;
- }
- }
- }
-
- // Move past the match.
- base += r;
- }
-
- // Concatenate the text after the last match.
- output += input.substr(base, input.length()-base);
-
- // Store the output in the provided variable.
- m_Makefile->AddDefinition(outvar.c_str(), output.c_str());
- return true;
- }
- //----------------------------------------------------------------------------
- bool cmStringCommand::HandleCompareCommand(std::vector<std::string> const& args)
- {
- if(args.size() < 2)
- {
- this->SetError("sub-command COMPARE requires a mode to be specified.");
- return false;
- }
- std::string mode = args[1];
- if((mode == "EQUAL") || (mode == "NOTEQUAL") ||
- (mode == "LESS") || (mode == "GREATER"))
- {
- if(args.size() < 5)
- {
- std::string e = "sub-command COMPARE, mode ";
- e += mode;
- e += " needs at least 5 arguments total to command.";
- this->SetError(e.c_str());
- return false;
- }
-
- const std::string& left = args[2];
- const std::string& right = args[3];
- const std::string& outvar = args[4];
- bool result;
- if(mode == "LESS")
- {
- result = (left < right);
- }
- else if(mode == "GREATER")
- {
- result = (left > right);
- }
- else if(mode == "EQUAL")
- {
- result = (left == right);
- }
- else // if(mode == "NOTEQUAL")
- {
- result = !(left == right);
- }
- if(result)
- {
- m_Makefile->AddDefinition(outvar.c_str(), "1");
- }
- else
- {
- m_Makefile->AddDefinition(outvar.c_str(), "0");
- }
- return true;
- }
- std::string e = "sub-command COMPARE does not recognize mode "+mode;
- this->SetError(e.c_str());
- return false;
- }
|