Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / idlc / source / idlcproduce.cxx
blob93dc8ba7df865902fef4b7604a017718f929c1c4
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 ::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 )
51 return sal_True;
53 sal_Int32 nIndex = 0;
54 OString token(sysPathName.getToken(0, SEPARATOR, nIndex));
55 const sal_Char* p = token.getStr();
56 if (strcmp(p, "..") == 0
57 || *(p+1) == ':'
58 || strcmp(p, ".") == 0)
60 buffer.append(token);
61 buffer.append(SEPARATOR);
63 else
64 nIndex = 0;
68 buffer.append(sysPathName.getToken(0, SEPARATOR, nIndex));
70 if ( buffer.getLength() > 0 && nIndex != -1 )
72 #if defined(SAL_UNX)
73 if (mkdir((char*)buffer.getStr(), 0777) == -1)
74 #else
75 if (mkdir((char*)buffer.getStr()) == -1)
76 #endif
78 if (errno == ENOENT)
80 fprintf(stderr, "%s: cannot create directory '%s'\n",
81 idlc()->getOptions()->getProgramName().getStr(), buffer.getStr());
82 return sal_False;
84 } else
86 if ( !pCreatedDirectories )
87 pCreatedDirectories = new StringList();
88 pCreatedDirectories->push_front(buffer.getStr());
91 buffer.append(SEPARATOR);
92 } while( nIndex != -1 );
93 return sal_True;
96 static sal_Bool cleanPath()
98 if ( pCreatedDirectories )
100 StringList::iterator iter = pCreatedDirectories->begin();
101 StringList::iterator end = pCreatedDirectories->end();
102 while ( iter != end )
104 //#ifdef SAL_UNX
105 // if (rmdir((char*)(*iter).getStr(), 0777) == -1)
106 //#else
107 if (rmdir((char*)(*iter).getStr()) == -1)
108 //#endif
110 fprintf(stderr, "%s: cannot remove directory '%s'\n",
111 idlc()->getOptions()->getProgramName().getStr(), (*iter).getStr());
112 return sal_False;
114 ++iter;
116 delete pCreatedDirectories;
118 return sal_True;
121 void removeIfExists(const OString& pathname)
123 osl::File::remove(OStringToOUString(pathname, RTL_TEXTENCODING_UTF8));
126 sal_Int32 SAL_CALL
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());
137 return 1;
140 OString depTmpName;
141 if (pDepFile)
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());
149 return 1;
151 removeIfExists(depTmpName);
154 removeIfExists(regTmpName);
155 OString urlRegTmpName = convertToFileUrl(regTmpName);
157 Registry regFile;
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);
164 cleanPath();
165 return 1;
168 RegistryKey rootKey;
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);
175 cleanPath();
176 return 1;
179 // produce registry file
180 if ( !idlc()->getRoot()->dump(rootKey) )
182 rootKey.releaseKey();
183 regFile.close();
184 regFile.destroy(OStringToOUString(regFileName, RTL_TEXTENCODING_UTF8));
185 removeIfExists(regFileName);
186 cleanPath();
187 return 1;
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);
197 cleanPath();
198 return 1;
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);
209 cleanPath();
210 return 1;
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);
221 cleanPath();
222 return 1;
224 removeIfExists(regTmpName);
226 if (pDepFile)
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);
237 cleanPath();
238 return 1;
240 removeIfExists(depTmpName);
243 return 0;
246 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */