fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / idlc / source / idlcproduce.cxx
blobf40cb06ed0e438275005ac3ca26c78317d3779fa
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
26 #if defined(SAL_W32)
27 #include <io.h>
28 #include <direct.h>
29 #include <errno.h>
30 #endif
32 #ifdef SAL_UNX
33 #include <unistd.h>
34 #include <sys/stat.h>
35 #include <errno.h>
36 #endif
38 #include <string.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 )
50 return true;
52 sal_Int32 nIndex = 0;
53 OString token(sysPathName.getToken(0, SEPARATOR, nIndex));
54 const sal_Char* p = token.getStr();
55 if (strcmp(p, "..") == 0
56 || *(p+1) == ':'
57 || strcmp(p, ".") == 0)
59 buffer.append(token);
60 buffer.append(SEPARATOR);
62 else
63 nIndex = 0;
67 buffer.append(sysPathName.getToken(0, SEPARATOR, nIndex));
69 if ( !buffer.isEmpty() && nIndex != -1 )
71 #if defined(SAL_UNX)
72 if (mkdir(buffer.getStr(), 0777) == -1)
73 #else
74 if (mkdir((char*)buffer.getStr()) == -1)
75 #endif
77 if (errno == ENOENT)
79 fprintf(stderr, "%s: cannot create directory '%s'\n",
80 idlc()->getOptions()->getProgramName().getStr(), buffer.getStr());
81 return false;
83 } else
85 if ( !pCreatedDirectories )
86 pCreatedDirectories = new StringList();
87 pCreatedDirectories->push_front(buffer.getStr());
90 buffer.append(SEPARATOR);
91 } while( nIndex != -1 );
92 return true;
95 static bool cleanPath()
97 if ( pCreatedDirectories )
99 StringList::iterator iter = pCreatedDirectories->begin();
100 StringList::iterator end = pCreatedDirectories->end();
101 while ( iter != end )
103 //#ifdef SAL_UNX
104 // if (rmdir((char*)(*iter).getStr(), 0777) == -1)
105 //#else
106 if (rmdir((*iter).getStr()) == -1)
107 //#endif
109 fprintf(stderr, "%s: cannot remove directory '%s'\n",
110 idlc()->getOptions()->getProgramName().getStr(), (*iter).getStr());
111 return false;
113 ++iter;
115 delete pCreatedDirectories;
117 return true;
120 void removeIfExists(const OString& pathname)
122 osl::File::remove(OStringToOUString(pathname, RTL_TEXTENCODING_UTF8));
125 sal_Int32 SAL_CALL
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());
136 return 1;
139 OString depTmpName;
140 if (pDepFile)
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());
148 return 1;
150 removeIfExists(depTmpName);
153 removeIfExists(regTmpName);
154 OString urlRegTmpName = convertToFileUrl(regTmpName);
156 Registry regFile;
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);
163 cleanPath();
164 return 1;
167 RegistryKey rootKey;
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);
174 cleanPath();
175 return 1;
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);
189 cleanPath();
190 return 1;
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);
200 cleanPath();
201 return 1;
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);
212 cleanPath();
213 return 1;
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);
224 cleanPath();
225 return 1;
227 removeIfExists(regTmpName);
229 if (pDepFile)
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);
240 cleanPath();
241 return 1;
243 removeIfExists(depTmpName);
246 return 0;
249 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */