update dev300-m58
[ooovba.git] / idlc / source / idlcproduce.cxx
blobf2738a9ba91ebed4a40b3da24584e820bf42d3c2
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: idlcproduce.cxx,v $
10 * $Revision: 1.17 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_idlc.hxx"
33 #include <idlc/idlc.hxx>
34 #include <idlc/astmodule.hxx>
35 #include <rtl/strbuf.hxx>
36 #include <osl/file.hxx>
37 #include <osl/thread.h>
39 #if defined(SAL_W32) || defined(SAL_OS2)
40 #include <io.h>
41 #include <direct.h>
42 #include <errno.h>
43 #endif
45 #ifdef SAL_UNX
46 #include <unistd.h>
47 #include <sys/stat.h>
48 #include <errno.h>
49 #endif
51 #include <string.h>
53 using namespace ::rtl;
54 using namespace ::osl;
56 StringList* pCreatedDirectories = NULL;
58 static sal_Bool checkOutputPath(const OString& completeName)
60 OString sysPathName = convertToAbsoluteSystemPath(completeName);
61 OStringBuffer buffer(sysPathName.getLength());
63 if ( sysPathName.indexOf( SEPARATOR ) == -1 )
64 return sal_True;
66 sal_Int32 nIndex = 0;
67 OString token(sysPathName.getToken(0, SEPARATOR, nIndex));
68 const sal_Char* p = token.getStr();
69 if (strcmp(p, "..") == 0
70 || *(p+1) == ':'
71 || strcmp(p, ".") == 0)
73 buffer.append(token);
74 buffer.append(SEPARATOR);
76 else
77 nIndex = 0;
81 buffer.append(sysPathName.getToken(0, SEPARATOR, nIndex));
83 if ( buffer.getLength() > 0 && nIndex != -1 )
85 #if defined(SAL_UNX) || defined(SAL_OS2)
86 if (mkdir((char*)buffer.getStr(), 0777) == -1)
87 #else
88 if (mkdir((char*)buffer.getStr()) == -1)
89 #endif
91 if (errno == ENOENT)
93 fprintf(stderr, "%s: cannot create directory '%s'\n",
94 idlc()->getOptions()->getProgramName().getStr(), buffer.getStr());
95 return sal_False;
97 } else
99 if ( !pCreatedDirectories )
100 pCreatedDirectories = new StringList();
101 pCreatedDirectories->push_front(buffer.getStr());
104 buffer.append(SEPARATOR);
105 } while( nIndex != -1 );
106 return sal_True;
109 static sal_Bool cleanPath()
111 if ( pCreatedDirectories )
113 StringList::iterator iter = pCreatedDirectories->begin();
114 StringList::iterator end = pCreatedDirectories->end();
115 while ( iter != end )
117 //#ifdef SAL_UNX
118 // if (rmdir((char*)(*iter).getStr(), 0777) == -1)
119 //#else
120 if (rmdir((char*)(*iter).getStr()) == -1)
121 //#endif
123 fprintf(stderr, "%s: cannot remove directory '%s'\n",
124 idlc()->getOptions()->getProgramName().getStr(), (*iter).getStr());
125 return sal_False;
127 ++iter;
129 delete pCreatedDirectories;
131 return sal_True;
134 void removeIfExists(const OString& pathname)
136 unlink(pathname.getStr());
139 sal_Int32 SAL_CALL produceFile(const OString& regFileName)
141 Options* pOptions = idlc()->getOptions();
143 OString regTmpName = regFileName.replaceAt(regFileName.getLength() -3, 3, "_idlc_");
145 if ( !checkOutputPath(regFileName) )
147 fprintf(stderr, "%s: could not create path of registry file '%s'.\n",
148 pOptions->getProgramName().getStr(), regFileName.getStr());
149 return 1;
152 Registry regFile;
154 removeIfExists(regTmpName);
155 OString urlRegTmpName = convertToFileUrl(regTmpName);
156 if ( regFile.create(OStringToOUString(urlRegTmpName, RTL_TEXTENCODING_UTF8)) )
158 fprintf(stderr, "%s: could not create registry file '%s'\n",
159 pOptions->getProgramName().getStr(), regTmpName.getStr());
160 removeIfExists(regTmpName);
161 removeIfExists(regFileName);
162 cleanPath();
163 return 1;
166 RegistryKey rootKey;
167 if ( regFile.openRootKey(rootKey) )
169 fprintf(stderr, "%s: could not open root of registry file '%s'\n",
170 pOptions->getProgramName().getStr(), regFileName.getStr());
171 removeIfExists(regTmpName);
172 removeIfExists(regFileName);
173 cleanPath();
174 return 1;
177 // produce registry file
178 if ( !idlc()->getRoot()->dump(rootKey) )
180 rootKey.closeKey();
181 regFile.close();
182 regFile.destroy(OStringToOUString(regFileName, RTL_TEXTENCODING_UTF8));
183 removeIfExists(regFileName);
184 cleanPath();
185 return 1;
188 if ( rootKey.closeKey() )
190 fprintf(stderr, "%s: could not close root of registry file '%s'\n",
191 pOptions->getProgramName().getStr(), regFileName.getStr());
192 removeIfExists(regTmpName);
193 removeIfExists(regFileName);
194 cleanPath();
195 return 1;
197 if ( regFile.close() )
199 fprintf(stderr, "%s: could not close registry file '%s'\n",
200 pOptions->getProgramName().getStr(), regFileName.getStr());
201 removeIfExists(regTmpName);
202 removeIfExists(regFileName);
203 cleanPath();
204 return 1;
207 removeIfExists(regFileName);
209 if ( File::move(OStringToOUString(regTmpName, osl_getThreadTextEncoding()),
210 OStringToOUString(regFileName, osl_getThreadTextEncoding())) != FileBase::E_None ) {
211 fprintf(stderr, "%s: cannot rename temporary registry '%s' to '%s'\n",
212 idlc()->getOptions()->getProgramName().getStr(),
213 regTmpName.getStr(), regFileName.getStr());
214 removeIfExists(regTmpName);
215 cleanPath();
216 return 1;
218 removeIfExists(regTmpName);
220 return 0;