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 ::rtl
;
41 using namespace ::osl
;
43 StringList
* pCreatedDirectories
= NULL
;
45 static sal_Bool
checkOutputPath(const OString
& completeName
)
47 OString sysPathName
= convertToAbsoluteSystemPath(completeName
);
48 OStringBuffer
buffer(sysPathName
.getLength());
50 if ( sysPathName
.indexOf( SEPARATOR
) == -1 )
54 OString
token(sysPathName
.getToken(0, SEPARATOR
, nIndex
));
55 const sal_Char
* p
= token
.getStr();
56 if (strcmp(p
, "..") == 0
58 || strcmp(p
, ".") == 0)
61 buffer
.append(SEPARATOR
);
68 buffer
.append(sysPathName
.getToken(0, SEPARATOR
, nIndex
));
70 if ( buffer
.getLength() > 0 && nIndex
!= -1 )
73 if (mkdir((char*)buffer
.getStr(), 0777) == -1)
75 if (mkdir((char*)buffer
.getStr()) == -1)
80 fprintf(stderr
, "%s: cannot create directory '%s'\n",
81 idlc()->getOptions()->getProgramName().getStr(), buffer
.getStr());
86 if ( !pCreatedDirectories
)
87 pCreatedDirectories
= new StringList();
88 pCreatedDirectories
->push_front(buffer
.getStr());
91 buffer
.append(SEPARATOR
);
92 } while( nIndex
!= -1 );
96 static sal_Bool
cleanPath()
98 if ( pCreatedDirectories
)
100 StringList::iterator iter
= pCreatedDirectories
->begin();
101 StringList::iterator end
= pCreatedDirectories
->end();
102 while ( iter
!= end
)
105 // if (rmdir((char*)(*iter).getStr(), 0777) == -1)
107 if (rmdir((char*)(*iter
).getStr()) == -1)
110 fprintf(stderr
, "%s: cannot remove directory '%s'\n",
111 idlc()->getOptions()->getProgramName().getStr(), (*iter
).getStr());
116 delete pCreatedDirectories
;
121 void removeIfExists(const OString
& pathname
)
123 osl::File::remove(OStringToOUString(pathname
, RTL_TEXTENCODING_UTF8
));
127 produceFile(const OString
& regFileName
, sPair_t
const*const pDepFile
)
129 Options
* pOptions
= idlc()->getOptions();
131 OString regTmpName
= regFileName
.replaceAt(regFileName
.getLength() -3, 3, "_idlc_");
133 if ( !checkOutputPath(regFileName
) )
135 fprintf(stderr
, "%s: could not create path of registry file '%s'.\n",
136 pOptions
->getProgramName().getStr(), regFileName
.getStr());
143 depTmpName
= pDepFile
->first
.replaceAt(
144 regFileName
.getLength() -3, 3, "_idlc_");
145 if ( !checkOutputPath(depTmpName
) )
147 fprintf(stderr
, "%s: could not create path of dep file '%s'.\n",
148 pOptions
->getProgramName().getStr(), pDepFile
->first
.getStr());
151 removeIfExists(depTmpName
);
154 removeIfExists(regTmpName
);
155 OString urlRegTmpName
= convertToFileUrl(regTmpName
);
158 if ( regFile
.create(OStringToOUString(urlRegTmpName
, RTL_TEXTENCODING_UTF8
)) != REG_NO_ERROR
)
160 fprintf(stderr
, "%s: could not create registry file '%s'\n",
161 pOptions
->getProgramName().getStr(), regTmpName
.getStr());
162 removeIfExists(regTmpName
);
163 removeIfExists(regFileName
);
169 if ( regFile
.openRootKey(rootKey
) != REG_NO_ERROR
)
171 fprintf(stderr
, "%s: could not open root of registry file '%s'\n",
172 pOptions
->getProgramName().getStr(), regFileName
.getStr());
173 removeIfExists(regTmpName
);
174 removeIfExists(regFileName
);
179 // produce registry file
180 if ( !idlc()->getRoot()->dump(rootKey
) )
182 rootKey
.releaseKey();
184 regFile
.destroy(OStringToOUString(regFileName
, RTL_TEXTENCODING_UTF8
));
185 removeIfExists(regFileName
);
190 rootKey
.releaseKey();
191 if ( regFile
.close() != REG_NO_ERROR
)
193 fprintf(stderr
, "%s: could not close registry file '%s'\n",
194 pOptions
->getProgramName().getStr(), regFileName
.getStr());
195 removeIfExists(regTmpName
);
196 removeIfExists(regFileName
);
201 if (pDepFile
&& !idlc()->dumpDeps(depTmpName
, pDepFile
->second
))
203 fprintf(stderr
, "%s: could not write dep file '%s'\n",
204 pOptions
->getProgramName().getStr(), pDepFile
->first
.getStr());
205 removeIfExists(depTmpName
);
206 removeIfExists(pDepFile
->first
);
207 removeIfExists(regTmpName
);
208 removeIfExists(regFileName
);
213 removeIfExists(regFileName
);
215 if ( File::move(OStringToOUString(regTmpName
, osl_getThreadTextEncoding()),
216 OStringToOUString(regFileName
, osl_getThreadTextEncoding())) != FileBase::E_None
) {
217 fprintf(stderr
, "%s: cannot rename temporary registry '%s' to '%s'\n",
218 idlc()->getOptions()->getProgramName().getStr(),
219 regTmpName
.getStr(), regFileName
.getStr());
220 removeIfExists(regTmpName
);
224 removeIfExists(regTmpName
);
228 removeIfExists(pDepFile
->first
);
229 if ( File::move(OStringToOUString(depTmpName
, osl_getThreadTextEncoding()),
230 OStringToOUString(pDepFile
->first
, osl_getThreadTextEncoding())) != FileBase::E_None
) {
231 fprintf(stderr
, "%s: cannot rename dep file '%s' to '%s'\n",
232 idlc()->getOptions()->getProgramName().getStr(),
233 depTmpName
.getStr(), pDepFile
->first
.getStr());
234 removeIfExists(depTmpName
);
235 removeIfExists(pDepFile
->first
);
236 removeIfExists(regFileName
);
240 removeIfExists(depTmpName
);
246 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */