2 * Copyright (C) 2005 Casper S. Hornstrup
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #define MKDIR(s) mkdir(s)
31 #define MKDIR(s) mkdir(s, 0755)
37 Directory::Directory ( const string
& name_
)
43 Directory::Add ( const char* subdir
)
46 string s1
= string ( subdir
);
47 if ( ( i
= s1
.find ( '$' ) ) != string::npos
)
49 throw InvalidOperationException ( __FILE__
,
51 "No environment variables can be used here. Path was %s",
55 const char* p
= strpbrk ( subdir
, "/\\" );
56 if ( subdir
== p
|| ( *subdir
&& subdir
[1] == ':' ) )
58 throw InvalidOperationException ( __FILE__
,
60 "Invalid relative path '%s'",
65 p
= subdir
+ strlen(subdir
);
66 string
s ( subdir
, p
-subdir
);
67 if ( subdirs
.find(s
) == subdirs
.end() )
68 subdirs
[s
] = new Directory(s
);
70 subdirs
[s
]->Add ( p
);
74 Directory::mkdir_p ( const char* path
)
78 directory
= opendir ( path
);
79 if ( directory
!= NULL
)
81 closedir ( directory
);
86 if ( MKDIR ( path
) != 0 )
89 if ( errno
== EEXIST
)
92 throw AccessDeniedException ( string ( path
) );
98 Directory::CreateDirectory ( string path
)
102 if ( isalpha ( path
[0] ) && path
[1] == ':' && path
[2] == cSep
)
104 nextIndex
= path
.find ( cSep
, 3);
107 nextIndex
= path
.find ( cSep
);
109 bool directoryWasCreated
= false;
110 while ( nextIndex
!= string::npos
)
112 nextIndex
= path
.find ( cSep
, index
+ 1 );
113 directoryWasCreated
= mkdir_p ( path
.substr ( 0, nextIndex
).c_str () );
116 return directoryWasCreated
;
120 Directory::ReplaceVariable ( const string
& name
,
124 size_t i
= path
.find ( name
);
125 if ( i
!= string::npos
)
126 return path
.replace ( i
, name
.length (), value
);
132 Directory::ResolveVariablesInPath ( char* buf
,
135 string s
= ReplaceVariable ( "$(INTERMEDIATE)", Environment::GetIntermediatePath (), path
);
136 s
= ReplaceVariable ( "$(OUTPUT)", Environment::GetOutputPath (), s
);
137 s
= ReplaceVariable ( "$(INSTALL)", Environment::GetInstallPath (), s
);
138 strcpy ( buf
, s
.c_str () );
142 Directory::GenerateTree ( const string
& parent
,
147 if ( parent
.size () > 0 )
151 if ( name
.size () > 0 )
152 path
= parent
+ sSep
+ name
;
155 ResolveVariablesInPath ( buf
, path
);
156 if ( CreateDirectory ( buf
) && verbose
)
157 printf ( "Created %s\n", buf
);
162 for ( directory_map::iterator i
= subdirs
.begin ();
166 i
->second
->GenerateTree ( path
, verbose
);
171 Directory::EscapeSpaces ( string path
)
178 newpath
= newpath
+ "\\ ";
180 newpath
= newpath
+ *p
;
187 Directory::CreateRule ( FILE* f
,
188 const string
& parent
)
192 if ( parent
.size() > 0 )
194 string escapedParent
= EscapeSpaces ( parent
);
197 escapedParent
.c_str (),
199 EscapeSpaces ( name
).c_str (),
200 escapedParent
.c_str () );
203 "\t$(ECHO_MKDIR)\n" );
208 path
= parent
+ sSep
+ name
;
213 for ( directory_map::iterator i
= subdirs
.begin();
217 i
->second
->CreateRule ( f
, path
);