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.
27 #define MKDIR(s) mkdir(s)
29 #define MKDIR(s) mkdir(s, 0755)
35 Directory::Directory ( const string
& name_
)
41 Directory::Add ( const char* subdir
)
44 string s1
= string ( subdir
);
45 if ( ( i
= s1
.find ( '$' ) ) != string::npos
)
47 throw InvalidOperationException ( __FILE__
,
49 "No environment variables can be used here. Path was %s",
53 const char* p
= strpbrk ( subdir
, "/\\" );
55 p
= subdir
+ strlen(subdir
);
56 string
s ( subdir
, p
-subdir
);
57 if ( subdirs
.find(s
) == subdirs
.end() )
58 subdirs
[s
] = new Directory(s
);
60 subdirs
[s
]->Add ( p
);
64 Directory::mkdir_p ( const char* path
)
68 directory
= opendir ( path
);
69 if ( directory
!= NULL
)
71 closedir ( directory
);
76 if ( MKDIR ( path
) != 0 )
79 if ( errno
== EEXIST
)
82 throw AccessDeniedException ( string ( path
) );
88 Directory::CreateDirectory ( string path
)
92 if ( isalpha ( path
[0] ) && path
[1] == ':' && path
[2] == cSep
)
94 nextIndex
= path
.find ( cSep
, 3);
97 nextIndex
= path
.find ( cSep
);
99 bool directoryWasCreated
= false;
100 while ( nextIndex
!= string::npos
)
102 nextIndex
= path
.find ( cSep
, index
+ 1 );
103 directoryWasCreated
= mkdir_p ( path
.substr ( 0, nextIndex
).c_str () );
106 return directoryWasCreated
;
110 Directory::ReplaceVariable ( string name
,
114 size_t i
= path
.find ( name
);
115 if ( i
!= string::npos
)
116 return path
.replace ( i
, name
.length (), value
);
122 Directory::ResolveVariablesInPath ( char* buf
,
125 string s
= ReplaceVariable ( "$(INTERMEDIATE)", Environment::GetIntermediatePath (), path
);
126 s
= ReplaceVariable ( "$(OUTPUT)", Environment::GetOutputPath (), s
);
127 s
= ReplaceVariable ( "$(INSTALL)", Environment::GetInstallPath (), s
);
128 strcpy ( buf
, s
.c_str () );
132 Directory::GenerateTree ( const string
& parent
,
137 if ( parent
.size () > 0 )
141 path
= parent
+ sSep
+ name
;
142 ResolveVariablesInPath ( buf
, path
);
143 if ( CreateDirectory ( buf
) && verbose
)
144 printf ( "Created %s\n", buf
);
149 for ( directory_map::iterator i
= subdirs
.begin ();
153 i
->second
->GenerateTree ( path
, verbose
);
158 Directory::EscapeSpaces ( string path
)
165 newpath
= newpath
+ "\\ ";
167 newpath
= newpath
+ *p
;
174 Directory::CreateRule ( FILE* f
,
175 const string
& parent
)
179 if ( parent
.size() > 0 )
181 string escapedParent
= EscapeSpaces ( parent
);
184 escapedParent
.c_str (),
186 EscapeSpaces ( name
).c_str (),
187 escapedParent
.c_str () );
190 "\t$(ECHO_MKDIR)\n" );
195 path
= parent
+ sSep
+ name
;
200 for ( directory_map::iterator i
= subdirs
.begin();
204 i
->second
->CreateRule ( f
, path
);