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 .
21 #include <astmodule.hxx>
22 #include <rtl/strbuf.hxx>
23 #include <osl/file.hxx>
24 #include <osl/thread.h>
40 using namespace ::osl
;
42 static std::list
< OString
>* pCreatedDirectories
= nullptr;
44 static bool checkOutputPath(const OString
& completeName
)
46 OString sysPathName
= convertToAbsoluteSystemPath(completeName
);
47 OStringBuffer
buffer(sysPathName
.getLength()+16);
49 if ( sysPathName
.indexOf( SEPARATOR
) == -1 )
53 OString
token(sysPathName
.getToken(0, SEPARATOR
, nIndex
));
54 if (token
.startsWith("..")
55 || (token
.getLength() >= 2 && token
[1] == ':')
56 || token
.startsWith("."))
59 buffer
.append(SEPARATOR
);
66 buffer
.append(sysPathName
.getToken(0, SEPARATOR
, nIndex
));
68 if ( !buffer
.isEmpty() && nIndex
!= -1 )
71 if (mkdir(buffer
.getStr(), 0777) == -1)
73 if (mkdir(buffer
.getStr()) == -1)
78 fprintf(stderr
, "%s: cannot create directory '%s'\n",
79 idlc()->getOptions()->getProgramName().getStr(), buffer
.getStr());
84 if ( !pCreatedDirectories
)
85 pCreatedDirectories
= new std::list
< OString
>;
86 pCreatedDirectories
->push_front(buffer
.getStr());
89 buffer
.append(SEPARATOR
);
90 } while( nIndex
!= -1 );
94 static bool cleanPath()
96 if ( pCreatedDirectories
)
98 for (auto const& createdDirectory
: *pCreatedDirectories
)
101 // if (rmdir((char*)createdDirectory.getStr(), 0777) == -1)
103 if (rmdir(createdDirectory
.getStr()) == -1)
106 fprintf(stderr
, "%s: cannot remove directory '%s'\n",
107 idlc()->getOptions()->getProgramName().getStr(), createdDirectory
.getStr());
111 delete pCreatedDirectories
;
116 void removeIfExists(const OString
& pathname
)
118 osl::File::remove(OStringToOUString(pathname
, RTL_TEXTENCODING_UTF8
));
122 produceFile(const OString
& regFileName
, sPair_t
const*const pDepFile
)
124 Options
* pOptions
= idlc()->getOptions();
126 OString regTmpName
= regFileName
.replaceAt(regFileName
.getLength() -3, 3, "_idlc_");
128 if ( !checkOutputPath(regFileName
) )
130 fprintf(stderr
, "%s: could not create path of registry file '%s'.\n",
131 pOptions
->getProgramName().getStr(), regFileName
.getStr());
138 depTmpName
= pDepFile
->first
.replaceAt(
139 regFileName
.getLength() -3, 3, "_idlc_");
140 if ( !checkOutputPath(depTmpName
) )
142 fprintf(stderr
, "%s: could not create path of dep file '%s'.\n",
143 pOptions
->getProgramName().getStr(), pDepFile
->first
.getStr());
146 removeIfExists(depTmpName
);
149 removeIfExists(regTmpName
);
150 OString urlRegTmpName
= convertToFileUrl(regTmpName
);
153 if ( regFile
.create(OStringToOUString(urlRegTmpName
, RTL_TEXTENCODING_UTF8
)) != RegError::NO_ERROR
)
155 fprintf(stderr
, "%s: could not create registry file '%s'\n",
156 pOptions
->getProgramName().getStr(), regTmpName
.getStr());
157 removeIfExists(regTmpName
);
158 removeIfExists(regFileName
);
164 if ( regFile
.openRootKey(rootKey
) != RegError::NO_ERROR
)
166 fprintf(stderr
, "%s: could not open root of registry file '%s'\n",
167 pOptions
->getProgramName().getStr(), regFileName
.getStr());
168 removeIfExists(regTmpName
);
169 removeIfExists(regFileName
);
174 // produce registry file
175 if ( !idlc()->getRoot()->dump(rootKey
) )
177 rootKey
.releaseKey();
178 if (regFile
.close() != RegError::NO_ERROR
)
180 fprintf(stderr
, "%s: could not close registry file '%s'\n",
181 pOptions
->getProgramName().getStr(), regFileName
.getStr());
183 regFile
.destroy(OStringToOUString(regFileName
, RTL_TEXTENCODING_UTF8
));
184 removeIfExists(regFileName
);
189 rootKey
.releaseKey();
190 if ( regFile
.close() != RegError::NO_ERROR
)
192 fprintf(stderr
, "%s: could not close registry file '%s'\n",
193 pOptions
->getProgramName().getStr(), regFileName
.getStr());
194 removeIfExists(regTmpName
);
195 removeIfExists(regFileName
);
200 if (pDepFile
&& !idlc()->dumpDeps(depTmpName
, pDepFile
->second
))
202 fprintf(stderr
, "%s: could not write dep file '%s'\n",
203 pOptions
->getProgramName().getStr(), pDepFile
->first
.getStr());
204 removeIfExists(depTmpName
);
205 removeIfExists(pDepFile
->first
);
206 removeIfExists(regTmpName
);
207 removeIfExists(regFileName
);
212 removeIfExists(regFileName
);
214 if ( File::move(OStringToOUString(regTmpName
, osl_getThreadTextEncoding()),
215 OStringToOUString(regFileName
, osl_getThreadTextEncoding())) != FileBase::E_None
) {
216 fprintf(stderr
, "%s: cannot rename temporary registry '%s' to '%s'\n",
217 idlc()->getOptions()->getProgramName().getStr(),
218 regTmpName
.getStr(), regFileName
.getStr());
219 removeIfExists(regTmpName
);
223 removeIfExists(regTmpName
);
227 removeIfExists(pDepFile
->first
);
228 if ( File::move(OStringToOUString(depTmpName
, osl_getThreadTextEncoding()),
229 OStringToOUString(pDepFile
->first
, osl_getThreadTextEncoding())) != FileBase::E_None
) {
230 fprintf(stderr
, "%s: cannot rename dep file '%s' to '%s'\n",
231 idlc()->getOptions()->getProgramName().getStr(),
232 depTmpName
.getStr(), pDepFile
->first
.getStr());
233 removeIfExists(depTmpName
);
234 removeIfExists(pDepFile
->first
);
235 removeIfExists(regFileName
);
239 removeIfExists(depTmpName
);
245 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */