Move Directory class to its own file

svn path=/trunk/; revision=19469
This commit is contained in:
Casper Hornstrup 2005-11-22 21:58:50 +00:00
parent fee8a937ff
commit dc1f52977d
5 changed files with 241 additions and 212 deletions

View file

@ -19,20 +19,8 @@
#include "mingw.h"
#include <assert.h>
#ifdef _MSC_VER
#define popen _popen
#define pclose _pclose
#else
#include <dirent.h>
#endif//_MSC_VER
#include "modulehandler.h"
#ifdef WIN32
#define MKDIR(s) mkdir(s)
#else
#define MKDIR(s) mkdir(s, 0755)
#endif
using std::string;
using std::vector;
using std::set;
@ -62,180 +50,6 @@ v2s ( const string_list& v, int wrap_at )
}
Directory::Directory ( const string& name_ )
: name(name_)
{
}
void
Directory::Add ( const char* subdir )
{
size_t i;
string s1 = string ( subdir );
if ( ( i = s1.find ( '$' ) ) != string::npos )
{
throw InvalidOperationException ( __FILE__,
__LINE__,
"No environment variables can be used here. Path was %s",
subdir );
}
const char* p = strpbrk ( subdir, "/\\" );
if ( !p )
p = subdir + strlen(subdir);
string s ( subdir, p-subdir );
if ( subdirs.find(s) == subdirs.end() )
subdirs[s] = new Directory(s);
if ( *p && *++p )
subdirs[s]->Add ( p );
}
bool
Directory::mkdir_p ( const char* path )
{
#ifndef _MSC_VER
DIR *directory;
directory = opendir ( path );
if ( directory != NULL )
{
closedir ( directory );
return false;
}
#endif//_MSC_VER
if ( MKDIR ( path ) != 0 )
{
#ifdef _MSC_VER
if ( errno == EEXIST )
return false;
#endif//_MSC_VER
throw AccessDeniedException ( string ( path ) );
}
return true;
}
bool
Directory::CreateDirectory ( string path )
{
size_t index = 0;
size_t nextIndex;
if ( isalpha ( path[0] ) && path[1] == ':' && path[2] == cSep )
{
nextIndex = path.find ( cSep, 3);
}
else
nextIndex = path.find ( cSep );
bool directoryWasCreated = false;
while ( nextIndex != string::npos )
{
nextIndex = path.find ( cSep, index + 1 );
directoryWasCreated = mkdir_p ( path.substr ( 0, nextIndex ).c_str () );
index = nextIndex;
}
return directoryWasCreated;
}
string
Directory::ReplaceVariable ( string name,
string value,
string path )
{
size_t i = path.find ( name );
if ( i != string::npos )
return path.replace ( i, name.length (), value );
else
return path;
}
void
Directory::ResolveVariablesInPath ( char* buf,
string path )
{
string s = ReplaceVariable ( "$(INTERMEDIATE)", Environment::GetIntermediatePath (), path );
s = ReplaceVariable ( "$(OUTPUT)", Environment::GetOutputPath (), s );
s = ReplaceVariable ( "$(INSTALL)", Environment::GetInstallPath (), s );
strcpy ( buf, s.c_str () );
}
void
Directory::GenerateTree ( const string& parent,
bool verbose )
{
string path;
if ( parent.size () > 0 )
{
char buf[256];
path = parent + sSep + name;
ResolveVariablesInPath ( buf, path );
if ( CreateDirectory ( buf ) && verbose )
printf ( "Created %s\n", buf );
}
else
path = name;
for ( directory_map::iterator i = subdirs.begin ();
i != subdirs.end ();
++i )
{
i->second->GenerateTree ( path, verbose );
}
}
string
Directory::EscapeSpaces ( string path )
{
string newpath;
char* p = &path[0];
while ( *p != 0 )
{
if ( *p == ' ' )
newpath = newpath + "\\ ";
else
newpath = newpath + *p;
*p++;
}
return newpath;
}
void
Directory::CreateRule ( FILE* f,
const string& parent )
{
string path;
if ( parent.size() > 0 )
{
string escapedParent = EscapeSpaces ( parent );
fprintf ( f,
"%s%c%s: | %s\n",
escapedParent.c_str (),
cSep,
EscapeSpaces ( name ).c_str (),
escapedParent.c_str () );
fprintf ( f,
"\t$(ECHO_MKDIR)\n" );
fprintf ( f,
"\t${mkdir} $@\n" );
path = parent + sSep + name;
}
else
path = name;
for ( directory_map::iterator i = subdirs.begin();
i != subdirs.end();
++i )
{
i->second->CreateRule ( f, path );
}
}
static class MingwFactory : public Backend::Factory
{
public:

View file

@ -32,32 +32,6 @@ class MingwModuleHandler;
extern std::string
v2s ( const string_list& v, int wrap_at );
typedef std::map<std::string,Directory*> directory_map;
class Directory
{
public:
std::string name;
directory_map subdirs;
Directory ( const std::string& name );
void Add ( const char* subdir );
void GenerateTree ( const std::string& parent,
bool verbose );
std::string EscapeSpaces ( std::string path );
void CreateRule ( FILE* f,
const std::string& parent );
private:
bool mkdir_p ( const char* path );
std::string ReplaceVariable ( std::string name,
std::string value,
std::string path );
std::string GetEnvironmentVariable ( const std::string& name );
void ResolveVariablesInPath ( char* buf,
std::string path );
bool CreateDirectory ( std::string path );
};
class MingwBackend : public Backend
{

View file

@ -0,0 +1,209 @@
/*
* Copyright (C) 2005 Casper S. Hornstrup
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "pch.h"
#include <assert.h>
#include "rbuild.h"
#ifdef _MSC_VER
#define popen _popen
#define pclose _pclose
#else
#include <dirent.h>
#endif//_MSC_VER
#ifdef WIN32
#define MKDIR(s) mkdir(s)
#else
#define MKDIR(s) mkdir(s, 0755)
#endif
using std::string;
using std::vector;
Directory::Directory ( const string& name_ )
: name(name_)
{
}
void
Directory::Add ( const char* subdir )
{
size_t i;
string s1 = string ( subdir );
if ( ( i = s1.find ( '$' ) ) != string::npos )
{
throw InvalidOperationException ( __FILE__,
__LINE__,
"No environment variables can be used here. Path was %s",
subdir );
}
const char* p = strpbrk ( subdir, "/\\" );
if ( !p )
p = subdir + strlen(subdir);
string s ( subdir, p-subdir );
if ( subdirs.find(s) == subdirs.end() )
subdirs[s] = new Directory(s);
if ( *p && *++p )
subdirs[s]->Add ( p );
}
bool
Directory::mkdir_p ( const char* path )
{
#ifndef _MSC_VER
DIR *directory;
directory = opendir ( path );
if ( directory != NULL )
{
closedir ( directory );
return false;
}
#endif//_MSC_VER
if ( MKDIR ( path ) != 0 )
{
#ifdef _MSC_VER
if ( errno == EEXIST )
return false;
#endif//_MSC_VER
throw AccessDeniedException ( string ( path ) );
}
return true;
}
bool
Directory::CreateDirectory ( string path )
{
size_t index = 0;
size_t nextIndex;
if ( isalpha ( path[0] ) && path[1] == ':' && path[2] == cSep )
{
nextIndex = path.find ( cSep, 3);
}
else
nextIndex = path.find ( cSep );
bool directoryWasCreated = false;
while ( nextIndex != string::npos )
{
nextIndex = path.find ( cSep, index + 1 );
directoryWasCreated = mkdir_p ( path.substr ( 0, nextIndex ).c_str () );
index = nextIndex;
}
return directoryWasCreated;
}
string
Directory::ReplaceVariable ( string name,
string value,
string path )
{
size_t i = path.find ( name );
if ( i != string::npos )
return path.replace ( i, name.length (), value );
else
return path;
}
void
Directory::ResolveVariablesInPath ( char* buf,
string path )
{
string s = ReplaceVariable ( "$(INTERMEDIATE)", Environment::GetIntermediatePath (), path );
s = ReplaceVariable ( "$(OUTPUT)", Environment::GetOutputPath (), s );
s = ReplaceVariable ( "$(INSTALL)", Environment::GetInstallPath (), s );
strcpy ( buf, s.c_str () );
}
void
Directory::GenerateTree ( const string& parent,
bool verbose )
{
string path;
if ( parent.size () > 0 )
{
char buf[256];
path = parent + sSep + name;
ResolveVariablesInPath ( buf, path );
if ( CreateDirectory ( buf ) && verbose )
printf ( "Created %s\n", buf );
}
else
path = name;
for ( directory_map::iterator i = subdirs.begin ();
i != subdirs.end ();
++i )
{
i->second->GenerateTree ( path, verbose );
}
}
string
Directory::EscapeSpaces ( string path )
{
string newpath;
char* p = &path[0];
while ( *p != 0 )
{
if ( *p == ' ' )
newpath = newpath + "\\ ";
else
newpath = newpath + *p;
*p++;
}
return newpath;
}
void
Directory::CreateRule ( FILE* f,
const string& parent )
{
string path;
if ( parent.size() > 0 )
{
string escapedParent = EscapeSpaces ( parent );
fprintf ( f,
"%s%c%s: | %s\n",
escapedParent.c_str (),
cSep,
EscapeSpaces ( name ).c_str (),
escapedParent.c_str () );
fprintf ( f,
"\t$(ECHO_MKDIR)\n" );
fprintf ( f,
"\t${mkdir} $@\n" );
path = parent + sSep + name;
}
else
path = name;
for ( directory_map::iterator i = subdirs.begin();
i != subdirs.end();
++i )
{
i->second->CreateRule ( f, path );
}
}

View file

@ -66,6 +66,7 @@ extern char cBadSep;
#define MS_VS_DEF_VERSION "7.10"
class Directory;
class Project;
class IfableData;
class Module;
@ -96,6 +97,32 @@ class CompilationUnit;
class SourceFileTest;
typedef std::map<std::string,Directory*> directory_map;
class Directory
{
public:
std::string name;
directory_map subdirs;
Directory ( const std::string& name );
void Add ( const char* subdir );
void GenerateTree ( const std::string& parent,
bool verbose );
std::string EscapeSpaces ( std::string path );
void CreateRule ( FILE* f,
const std::string& parent );
private:
bool mkdir_p ( const char* path );
std::string ReplaceVariable ( std::string name,
std::string value,
std::string path );
std::string GetEnvironmentVariable ( const std::string& name );
void ResolveVariablesInPath ( char* buf,
std::string path );
bool CreateDirectory ( std::string path );
};
class Configuration
{
public:

View file

@ -160,6 +160,7 @@ RBUILD_COMMON_SOURCES = \
compilerflag.cpp \
configuration.cpp \
define.cpp \
directory.cpp \
exception.cpp \
filesupportcode.cpp \
include.cpp \
@ -291,6 +292,10 @@ $(RBUILD_INT_)define.o: $(RBUILD_BASE_)define.cpp $(RBUILD_HEADERS) | $(RBUILD_I
$(ECHO_CC)
${host_gpp} $(RBUILD_HOST_CXXFLAGS) -c $< -o $@
$(RBUILD_INT_)directory.o: $(RBUILD_BASE_)directory.cpp $(RBUILD_HEADERS) | $(RBUILD_INT)
$(ECHO_CC)
${host_gpp} $(RBUILD_HOST_CXXFLAGS) -c $< -o $@
$(RBUILD_INT_)exception.o: $(RBUILD_BASE_)exception.cpp $(RBUILD_HEADERS) | $(RBUILD_INT)
$(ECHO_CC)
${host_gpp} $(RBUILD_HOST_CXXFLAGS) -c $< -o $@