1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <idlc/idlc.hxx>
21 #include <idlc/astmodule.hxx>
22 #include <rtl/strbuf.hxx>
23 #include <osl/file.hxx>
24 #include <osl/thread.h>
40 using namespace ::osl
;
42 StringList
* pCreatedDirectories
= NULL
;
44 static bool checkOutputPath(const OString
& completeName
)
46 OString sysPathName
= convertToAbsoluteSystemPath(completeName
);
47 OStringBuffer
buffer(sysPathName
.getLength());
49 if ( sysPathName
.indexOf( SEPARATOR
) == -1 )
53 OString
token(sysPathName
.getToken(0, SEPARATOR
, nIndex
));
54 const sal_Char
* p
= token
.getStr();
55 if (strcmp(p
, "..") == 0
57 || strcmp(p
, ".") == 0)
60 buffer
.append(SEPARATOR
);
67 buffer
.append(sysPathName
.getToken(0, SEPARATOR
, nIndex
));
69 if ( !buffer
.isEmpty() && nIndex
!= -1 )
72 if (mkdir(buffer
.getStr(), 0777) == -1)
74 if (mkdir((char*)buffer
.getStr()) == -1)
79 fprintf(stderr
, "%s: cannot create directory '%s'\n",
80 idlc()->getOptions()->getProgramName().getStr(), buffer
.getStr());
85 if ( !pCreatedDirectories
)
86 pCreatedDirectories
= new StringList();
87 pCreatedDirectories
->push_front(buffer
.getStr());
90 buffer
.append(SEPARATOR
);
91 } while( nIndex
!= -1 );
95 static bool cleanPath()
97 if ( pCreatedDirectories
)
99 StringList::iterator iter
= pCreatedDirectories
->begin();
100 StringList::iterator end
= pCreatedDirectories
->end();
101 while ( iter
!= end
)
104 // if (rmdir((char*)(*iter).getStr(), 0777) == -1)
106 if (rmdir((*iter
).getStr()) == -1)
109 fprintf(stderr
, "%s: cannot remove directory '%s'\n",
110 idlc()->getOptions()->getProgramName().getStr(), (*iter
).getStr());
115 delete pCreatedDirectories
;
120 void removeIfExists(const OString
& pathname
)
122 osl::File::remove(OStringToOUString(pathname
, RTL_TEXTENCODING_UTF8
));
126 produceFile(const OString
& regFileName
, sPair_t
const*const pDepFile
)
128 Options
* pOptions
= idlc()->getOptions();
130 OString regTmpName
= regFileName
.replaceAt(regFileName
.getLength() -3, 3, "_idlc_");
132 if ( !checkOutputPath(regFileName
) )
134 fprintf(stderr
, "%s: could not create path of registry file '%s'.\n",
135 pOptions
->getProgramName().getStr(), regFileName
.getStr());
142 depTmpName
= pDepFile
->first
.replaceAt(
143 regFileName
.getLength() -3, 3, "_idlc_");
144 if ( !checkOutputPath(depTmpName
) )
146 fprintf(stderr
, "%s: could not create path of dep file '%s'.\n",
147 pOptions
->getProgramName().getStr(), pDepFile
->first
.getStr());
150 removeIfExists(depTmpName
);
153 removeIfExists(regTmpName
);
154 OString urlRegTmpName
= convertToFileUrl(regTmpName
);
157 if ( regFile
.create(OStringToOUString(urlRegTmpName
, RTL_TEXTENCODING_UTF8
)) != RegError::NO_ERROR
)
159 fprintf(stderr
, "%s: could not create registry file '%s'\n",
160 pOptions
->getProgramName().getStr(), regTmpName
.getStr());
161 removeIfExists(regTmpName
);
162 removeIfExists(regFileName
);
168 if ( regFile
.openRootKey(rootKey
) != RegError::NO_ERROR
)
170 fprintf(stderr
, "%s: could not open root of registry file '%s'\n",
171 pOptions
->getProgramName().getStr(), regFileName
.getStr());
172 removeIfExists(regTmpName
);
173 removeIfExists(regFileName
);
178 // produce registry file
179 if ( !idlc()->getRoot()->dump(rootKey
) )
181 rootKey
.releaseKey();
182 if (regFile
.close() != RegError::NO_ERROR
)
184 fprintf(stderr
, "%s: could not close registry file '%s'\n",
185 pOptions
->getProgramName().getStr(), regFileName
.getStr());
187 regFile
.destroy(OStringToOUString(regFileName
, RTL_TEXTENCODING_UTF8
));
188 removeIfExists(regFileName
);
193 rootKey
.releaseKey();
194 if ( regFile
.close() != RegError::NO_ERROR
)
196 fprintf(stderr
, "%s: could not close registry file '%s'\n",
197 pOptions
->getProgramName().getStr(), regFileName
.getStr());
198 removeIfExists(regTmpName
);
199 removeIfExists(regFileName
);
204 if (pDepFile
&& !idlc()->dumpDeps(depTmpName
, pDepFile
->second
))
206 fprintf(stderr
, "%s: could not write dep file '%s'\n",
207 pOptions
->getProgramName().getStr(), pDepFile
->first
.getStr());
208 removeIfExists(depTmpName
);
209 removeIfExists(pDepFile
->first
);
210 removeIfExists(regTmpName
);
211 removeIfExists(regFileName
);
216 removeIfExists(regFileName
);
218 if ( File::move(OStringToOUString(regTmpName
, osl_getThreadTextEncoding()),
219 OStringToOUString(regFileName
, osl_getThreadTextEncoding())) != FileBase::E_None
) {
220 fprintf(stderr
, "%s: cannot rename temporary registry '%s' to '%s'\n",
221 idlc()->getOptions()->getProgramName().getStr(),
222 regTmpName
.getStr(), regFileName
.getStr());
223 removeIfExists(regTmpName
);
227 removeIfExists(regTmpName
);
231 removeIfExists(pDepFile
->first
);
232 if ( File::move(OStringToOUString(depTmpName
, osl_getThreadTextEncoding()),
233 OStringToOUString(pDepFile
->first
, osl_getThreadTextEncoding())) != FileBase::E_None
) {
234 fprintf(stderr
, "%s: cannot rename dep file '%s' to '%s'\n",
235 idlc()->getOptions()->getProgramName().getStr(),
236 depTmpName
.getStr(), pDepFile
->first
.getStr());
237 removeIfExists(depTmpName
);
238 removeIfExists(pDepFile
->first
);
239 removeIfExists(regFileName
);
243 removeIfExists(depTmpName
);
249 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */