1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: idlcproduce.cxx,v $
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)
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 )
67 OString
token(sysPathName
.getToken(0, SEPARATOR
, nIndex
));
68 const sal_Char
* p
= token
.getStr();
69 if (strcmp(p
, "..") == 0
71 || strcmp(p
, ".") == 0)
74 buffer
.append(SEPARATOR
);
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)
88 if (mkdir((char*)buffer
.getStr()) == -1)
93 fprintf(stderr
, "%s: cannot create directory '%s'\n",
94 idlc()->getOptions()->getProgramName().getStr(), buffer
.getStr());
99 if ( !pCreatedDirectories
)
100 pCreatedDirectories
= new StringList();
101 pCreatedDirectories
->push_front(buffer
.getStr());
104 buffer
.append(SEPARATOR
);
105 } while( nIndex
!= -1 );
109 static sal_Bool
cleanPath()
111 if ( pCreatedDirectories
)
113 StringList::iterator iter
= pCreatedDirectories
->begin();
114 StringList::iterator end
= pCreatedDirectories
->end();
115 while ( iter
!= end
)
118 // if (rmdir((char*)(*iter).getStr(), 0777) == -1)
120 if (rmdir((char*)(*iter
).getStr()) == -1)
123 fprintf(stderr
, "%s: cannot remove directory '%s'\n",
124 idlc()->getOptions()->getProgramName().getStr(), (*iter
).getStr());
129 delete pCreatedDirectories
;
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());
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
);
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
);
177 // produce registry file
178 if ( !idlc()->getRoot()->dump(rootKey
) )
182 regFile
.destroy(OStringToOUString(regFileName
, RTL_TEXTENCODING_UTF8
));
183 removeIfExists(regFileName
);
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
);
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
);
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
);
218 removeIfExists(regTmpName
);