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
, "/\\" );
57 p
= subdir
+ strlen(subdir
);
58 string
s ( subdir
, p
-subdir
);
59 if ( subdirs
.find(s
) == subdirs
.end() )
60 subdirs
[s
] = new Directory(s
);
62 subdirs
[s
]->Add ( p
);
66 Directory::mkdir_p ( const char* path
)
70 directory
= opendir ( path
);
71 if ( directory
!= NULL
)
73 closedir ( directory
);
78 if ( MKDIR ( path
) != 0 )
81 if ( errno
== EEXIST
)
84 throw AccessDeniedException ( string ( path
) );
90 Directory::CreateDirectory ( string path
)
94 if ( isalpha ( path
[0] ) && path
[1] == ':' && path
[2] == cSep
)
96 nextIndex
= path
.find ( cSep
, 3);
99 nextIndex
= path
.find ( cSep
);
101 bool directoryWasCreated
= false;
102 while ( nextIndex
!= string::npos
)
104 nextIndex
= path
.find ( cSep
, index
+ 1 );
105 directoryWasCreated
= mkdir_p ( path
.substr ( 0, nextIndex
).c_str () );
108 return directoryWasCreated
;
112 Directory::ReplaceVariable ( string name
,
116 size_t i
= path
.find ( name
);
117 if ( i
!= string::npos
)
118 return path
.replace ( i
, name
.length (), value
);
124 Directory::ResolveVariablesInPath ( char* buf
,
127 string s
= ReplaceVariable ( "$(INTERMEDIATE)", Environment::GetIntermediatePath (), path
);
128 s
= ReplaceVariable ( "$(OUTPUT)", Environment::GetOutputPath (), s
);
129 s
= ReplaceVariable ( "$(INSTALL)", Environment::GetInstallPath (), s
);
130 strcpy ( buf
, s
.c_str () );
134 Directory::GenerateTree ( const string
& parent
,
139 if ( parent
.size () > 0 )
143 path
= parent
+ sSep
+ name
;
144 ResolveVariablesInPath ( buf
, path
);
145 if ( CreateDirectory ( buf
) && verbose
)
146 printf ( "Created %s\n", buf
);
151 for ( directory_map::iterator i
= subdirs
.begin ();
155 i
->second
->GenerateTree ( path
, verbose
);
160 Directory::EscapeSpaces ( string path
)
167 newpath
= newpath
+ "\\ ";
169 newpath
= newpath
+ *p
;
176 Directory::CreateRule ( FILE* f
,
177 const string
& parent
)
181 if ( parent
.size() > 0 )
183 string escapedParent
= EscapeSpaces ( parent
);
186 escapedParent
.c_str (),
188 EscapeSpaces ( name
).c_str (),
189 escapedParent
.c_str () );
192 "\t$(ECHO_MKDIR)\n" );
197 path
= parent
+ sSep
+ name
;
202 for ( directory_map::iterator i
= subdirs
.begin();
206 i
->second
->CreateRule ( f
, path
);