use insert function instead of for loop
[LibreOffice.git] / idl / source / prj / svidl.cxx
bloba7593c349377fb6c75a31fb7b676815fc33df41c
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 .
21 #include <stdio.h>
22 #include <database.hxx>
23 #include <command.hxx>
24 #include <rtl/ustring.hxx>
25 #include <osl/file.hxx>
26 #include <memory>
28 #define BR 0x8000
29 static bool FileMove_Impl( const OUString & rFile1, const OUString & rFile2, bool bMoveAlways )
31 //printf( "Move from %s to %s\n", rFile2.GetStr(), rFile1.GetStr() );
32 size_t nC1 = 0;
33 size_t nC2 = 1;
34 if( !bMoveAlways )
36 SvFileStream aOutStm1( rFile1, StreamMode::STD_READ );
37 SvFileStream aOutStm2( rFile2, StreamMode::STD_READ );
38 if( aOutStm1.GetError() == ERRCODE_NONE )
40 std::unique_ptr<sal_uInt8[]> pBuf1(new sal_uInt8[ BR ]);
41 std::unique_ptr<sal_uInt8[]> pBuf2(new sal_uInt8[ BR ]);
42 nC1 = aOutStm1.ReadBytes(pBuf1.get(), BR);
43 nC2 = aOutStm2.ReadBytes(pBuf2.get(), BR);
44 while( nC1 == nC2 )
46 if( memcmp( pBuf1.get(), pBuf2.get(), nC1 ) )
48 nC1++;
49 break;
51 else
53 if( 0x8000 != nC1 )
54 break;
55 nC1 = aOutStm1.ReadBytes(pBuf1.get(), BR);
56 nC2 = aOutStm2.ReadBytes(pBuf2.get(), BR);
61 OUString fileURL2;
62 osl::FileBase::getFileURLFromSystemPath( rFile2, fileURL2 );
63 if( nC1 != nC2 )
64 {// something has changed
65 OUString fileURL1;
66 osl::FileBase::getFileURLFromSystemPath( rFile1, fileURL1 );
67 // move file
68 if( osl::FileBase::E_None != osl::File::move( fileURL2, fileURL1 ) )
70 // delete both files
71 osl::File::remove( fileURL1 );
72 osl::File::remove( fileURL2 );
73 return false;
75 return true;
77 return osl::FileBase::E_None == osl::File::remove( fileURL2 );
80 //This function gets a system path to a file [fname], creates a temp file in
81 //the same folder as [fname] and returns the system path of the temp file.
82 static OUString tempFileHelper(std::u16string_view fname)
84 OUString aTmpFile;
86 size_t delimIndex = fname.rfind( '/' );
87 if( delimIndex > 0 && delimIndex != std::u16string_view::npos)
89 OUString aTmpDir( fname.substr( 0, delimIndex ) );
90 osl::FileBase::getFileURLFromSystemPath( aTmpDir, aTmpDir );
91 osl::FileBase::createTempFile( &aTmpDir, nullptr, &aTmpFile );
92 osl::FileBase::getSystemPathFromFileURL( aTmpFile, aTmpFile );
94 else
96 OString aStr = "invalid filename: " +
97 OUStringToOString(fname, RTL_TEXTENCODING_UTF8);
98 fprintf(stderr, "%s\n", aStr.getStr());
100 return aTmpFile;
103 int main ( int argc, char ** argv)
105 OUString aTmpSlotMapFile;
106 OUString aTmpDepFile;
108 SvCommand aCommand( argc, argv );
110 if( aCommand.nVerbosity != 0 )
111 printf( "StarView Interface Definition Language (IDL) Compiler 3.0\n" );
113 Init();
114 std::unique_ptr<SvIdlWorkingBase> pDataBase( new SvIdlWorkingBase(aCommand));
116 int nExit = 0;
117 if( !aCommand.aExportFile.isEmpty() )
119 osl::DirectoryItem aDI;
120 osl::FileStatus fileStatus( osl_FileStatus_Mask_FileName );
121 (void)osl::DirectoryItem::get( aCommand.aExportFile, aDI );
122 (void)aDI.getFileStatus(fileStatus);
123 pDataBase->SetExportFile( fileStatus.getFileName() );
126 if( ReadIdl( pDataBase.get(), aCommand ) )
128 if( nExit == 0 && !aCommand.aSlotMapFile.isEmpty() )
130 aTmpSlotMapFile = tempFileHelper(aCommand.aSlotMapFile);
131 SvFileStream aOutStm( aTmpSlotMapFile, StreamMode::READWRITE | StreamMode::TRUNC );
132 if( !pDataBase->WriteSfx( aOutStm ) )
134 nExit = -1;
135 OString aStr = "cannot write slotmap file: " +
136 OUStringToOString(aCommand.aSlotMapFile, RTL_TEXTENCODING_UTF8);
137 fprintf(stderr, "%s\n", aStr.getStr());
140 if (nExit == 0 && !aCommand.m_DepFile.isEmpty())
142 aTmpDepFile = tempFileHelper(aCommand.m_DepFile);
143 SvFileStream aOutStm( aTmpDepFile, StreamMode::READWRITE | StreamMode::TRUNC );
144 pDataBase->WriteDepFile(aOutStm, aCommand.aTargetFile);
145 if( aOutStm.GetError() != ERRCODE_NONE )
147 nExit = -1;
148 fprintf( stderr, "cannot write dependency file: %s\n",
149 OUStringToOString( aCommand.m_DepFile,
150 RTL_TEXTENCODING_UTF8 ).getStr() );
154 else
155 nExit = -1;
157 if( nExit == 0 )
159 bool bErr = false;
160 bool bDoMove = aCommand.aTargetFile.isEmpty();
161 OUString aErrFile, aErrFile2;
162 if (!aCommand.aSlotMapFile.isEmpty())
164 bErr = !FileMove_Impl( aCommand.aSlotMapFile, aTmpSlotMapFile, bDoMove );
165 if( bErr ) {
166 aErrFile = aCommand.aSlotMapFile;
167 aErrFile2 = aTmpSlotMapFile;
170 if (!bErr && !aCommand.m_DepFile.isEmpty())
172 bErr |= !FileMove_Impl( aCommand.m_DepFile, aTmpDepFile, bDoMove );
173 if (bErr) {
174 aErrFile = aCommand.m_DepFile;
175 aErrFile2 = aTmpDepFile;
179 if( bErr )
181 nExit = -1;
182 OString aStr = "cannot move file from: " +
183 OUStringToOString(aErrFile2, RTL_TEXTENCODING_UTF8) +
184 "\n to file: " +
185 OUStringToOString(aErrFile, RTL_TEXTENCODING_UTF8);
186 fprintf( stderr, "%s\n", aStr.getStr() );
188 else
190 if( !aCommand.aTargetFile.isEmpty() )
192 // stamp file, because idl passed through correctly
193 SvFileStream aOutStm( aCommand.aTargetFile,
194 StreamMode::READWRITE | StreamMode::TRUNC );
199 if( nExit != 0 )
201 if( !aCommand.aSlotMapFile.isEmpty() )
203 osl::FileBase::getSystemPathFromFileURL( aTmpSlotMapFile, aTmpSlotMapFile );
204 osl::File::remove( aTmpSlotMapFile );
208 if( nExit != 0 )
209 fprintf( stderr, "svidl terminated with errors\n" );
210 return nExit;
213 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */