2000-08-29 23:26:29 +04:00
|
|
|
#include "cmSystemTools.h"
|
|
|
|
#include "errno.h"
|
2000-09-12 13:30:35 +04:00
|
|
|
#include <sys/stat.h>
|
2000-09-27 23:01:19 +04:00
|
|
|
#include "cmRegularExpression.h"
|
2000-09-12 13:30:35 +04:00
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
2000-08-29 23:26:29 +04:00
|
|
|
#include <windows.h>
|
2000-09-12 13:30:35 +04:00
|
|
|
#include <direct.h>
|
|
|
|
inline int Mkdir(const char* dir)
|
|
|
|
{
|
|
|
|
return _mkdir(dir);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
inline int Mkdir(const char* dir)
|
|
|
|
{
|
|
|
|
return mkdir(dir, 00700);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// remove extra spaces and the "\" character from the name
|
|
|
|
// of the class as it is in the CMakeLists.txt
|
|
|
|
std::string cmSystemTools::CleanUpName(const char* name)
|
|
|
|
{
|
|
|
|
std::string className = name;
|
|
|
|
size_t i =0;
|
|
|
|
while(className[i] == ' ')
|
|
|
|
{
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
if(i)
|
|
|
|
{
|
|
|
|
className = className.substr(i, className.size());
|
|
|
|
}
|
|
|
|
size_t pos = className.find('\\');
|
|
|
|
if(pos != std::string::npos)
|
|
|
|
{
|
|
|
|
className = className.substr(0, pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = className.find(' ');
|
|
|
|
if(pos != std::string::npos)
|
|
|
|
{
|
|
|
|
className = className.substr(0, pos);
|
|
|
|
}
|
|
|
|
return className;
|
|
|
|
}
|
|
|
|
|
2000-08-29 23:26:29 +04:00
|
|
|
|
|
|
|
bool cmSystemTools::MakeDirectory(const char* path)
|
|
|
|
{
|
|
|
|
std::string dir = path;
|
|
|
|
// replace all of the \ with /
|
|
|
|
size_t pos = 0;
|
|
|
|
while((pos = dir.find('\\', pos)) != std::string::npos)
|
|
|
|
{
|
|
|
|
dir[pos] = '/';
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
pos = dir.find(':');
|
|
|
|
if(pos == std::string::npos)
|
|
|
|
{
|
|
|
|
pos = 0;
|
|
|
|
}
|
|
|
|
while((pos = dir.find('/', pos)) != std::string::npos)
|
|
|
|
{
|
|
|
|
std::string topdir = dir.substr(0, pos);
|
2000-09-12 13:30:35 +04:00
|
|
|
Mkdir(topdir.c_str());
|
2000-08-29 23:26:29 +04:00
|
|
|
pos++;
|
|
|
|
}
|
2000-09-12 13:30:35 +04:00
|
|
|
if(Mkdir(path) != 0)
|
2000-08-29 23:26:29 +04:00
|
|
|
{
|
|
|
|
// if it is some other error besides directory exists
|
|
|
|
// then return false
|
|
|
|
if(errno != EEXIST)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2000-09-12 13:30:35 +04:00
|
|
|
|
|
|
|
// replace replace with with as many times as it shows up in source.
|
|
|
|
// write the result into source.
|
2000-08-29 23:26:29 +04:00
|
|
|
void cmSystemTools::ReplaceString(std::string& source,
|
|
|
|
const char* replace,
|
|
|
|
const char* with)
|
|
|
|
{
|
2000-09-01 18:43:10 +04:00
|
|
|
int lengthReplace = strlen(replace);
|
|
|
|
std::string rest;
|
|
|
|
size_t start = source.find(replace);
|
2000-08-29 23:26:29 +04:00
|
|
|
while(start != std::string::npos)
|
|
|
|
{
|
2000-09-01 18:43:10 +04:00
|
|
|
rest = source.substr(start+lengthReplace);
|
|
|
|
source = source.substr(0, start);
|
2000-08-29 23:26:29 +04:00
|
|
|
source += with;
|
2000-09-01 18:43:10 +04:00
|
|
|
source += rest;
|
|
|
|
start = source.find(replace, start + lengthReplace );
|
2000-08-29 23:26:29 +04:00
|
|
|
}
|
|
|
|
}
|
2000-09-12 13:30:35 +04:00
|
|
|
|
|
|
|
// return true if the file exists
|
|
|
|
bool cmSystemTools::FileExists(const char* filename)
|
|
|
|
{
|
|
|
|
struct stat fs;
|
|
|
|
if (stat(filename, &fs) != 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read a list from a CMakeLists.txt file open stream.
|
|
|
|
// assume the stream has just read "VAR = \"
|
|
|
|
// read until there is not a "\" at the end of the line.
|
|
|
|
void cmSystemTools::ReadList(std::vector<std::string>& stringList,
|
|
|
|
std::ifstream& fin)
|
|
|
|
{
|
|
|
|
char inbuffer[2048];
|
|
|
|
bool done = false;
|
|
|
|
while ( !done )
|
|
|
|
{
|
2000-09-27 23:01:19 +04:00
|
|
|
fin.getline(inbuffer, sizeof(inbuffer) );
|
2000-09-12 13:30:35 +04:00
|
|
|
std::string inname = inbuffer;
|
|
|
|
if(inname.find('\\') == std::string::npos)
|
|
|
|
{
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
if(inname.size())
|
|
|
|
{
|
|
|
|
stringList.push_back(cmSystemTools::CleanUpName(inname.c_str()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// convert windows slashes to unix slashes \ with /
|
|
|
|
void cmSystemTools::ConvertToUnixSlashes(std::string& path)
|
|
|
|
{
|
|
|
|
std::string::size_type pos = path.find('\\');
|
|
|
|
while(pos != std::string::npos)
|
|
|
|
{
|
|
|
|
path[pos] = '/';
|
|
|
|
pos = path.find('\\');
|
|
|
|
}
|
|
|
|
// remove any trailing slash
|
|
|
|
if(path[path.size()-1] == '/')
|
|
|
|
{
|
|
|
|
path = path.substr(0, path.size()-1);
|
|
|
|
}
|
|
|
|
}
|
2000-09-27 23:01:19 +04:00
|
|
|
|
|
|
|
|
|
|
|
int cmSystemTools::Grep(const char* dir, const char* file, const char* expression)
|
|
|
|
{
|
|
|
|
std::string path = dir;
|
|
|
|
path += "/";
|
|
|
|
path += file;
|
|
|
|
std::ifstream fin(path.c_str());
|
|
|
|
char buffer[2056];
|
|
|
|
int count = 0;
|
|
|
|
cmRegularExpression reg(expression);
|
|
|
|
while(fin)
|
|
|
|
{
|
|
|
|
fin.getline(buffer, sizeof(buffer));
|
|
|
|
count += reg.find(buffer);
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string cmSystemTools::ExtractVariable(const char* variable,
|
|
|
|
const char* l)
|
|
|
|
{
|
|
|
|
std::string line = l;
|
|
|
|
size_t varstart = line.find(variable);
|
|
|
|
size_t start = line.find("=");
|
|
|
|
if(start != std::string::npos && start > varstart )
|
|
|
|
{
|
|
|
|
start++;
|
|
|
|
while(line[start] == ' ' && start < line.size())
|
|
|
|
{
|
|
|
|
start++;
|
|
|
|
}
|
|
|
|
size_t end = line.size()-1;
|
|
|
|
while(line[end] == ' ' && end > start)
|
|
|
|
{
|
|
|
|
end--;
|
|
|
|
}
|
|
|
|
return line.substr(start, end).c_str();
|
|
|
|
}
|
|
|
|
return std::string("");
|
|
|
|
}
|
|
|
|
|