1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
22 #include <database.hxx>
23 #include <command.hxx>
24 #include <rtl/ustring.hxx>
25 #include <osl/file.hxx>
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() );
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
);
46 if( memcmp( pBuf1
.get(), pBuf2
.get(), nC1
) )
55 nC1
= aOutStm1
.ReadBytes(pBuf1
.get(), BR
);
56 nC2
= aOutStm2
.ReadBytes(pBuf2
.get(), BR
);
62 osl::FileBase::getFileURLFromSystemPath( rFile2
, fileURL2
);
64 {// something has changed
66 osl::FileBase::getFileURLFromSystemPath( rFile1
, fileURL1
);
68 if( osl::FileBase::E_None
!= osl::File::move( fileURL2
, fileURL1
) )
71 osl::File::remove( fileURL1
);
72 osl::File::remove( fileURL2
);
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
)
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
);
96 OString aStr
= "invalid filename: " +
97 OUStringToOString(fname
, RTL_TEXTENCODING_UTF8
);
98 fprintf(stderr
, "%s\n", aStr
.getStr());
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" );
114 std::unique_ptr
<SvIdlWorkingBase
> pDataBase( new SvIdlWorkingBase(aCommand
));
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
) )
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
)
148 fprintf( stderr
, "cannot write dependency file: %s\n",
149 OUStringToOString( aCommand
.m_DepFile
,
150 RTL_TEXTENCODING_UTF8
).getStr() );
160 bool bDoMove
= aCommand
.aTargetFile
.isEmpty();
161 OUString aErrFile
, aErrFile2
;
162 if (!aCommand
.aSlotMapFile
.isEmpty())
164 bErr
= !FileMove_Impl( aCommand
.aSlotMapFile
, aTmpSlotMapFile
, bDoMove
);
166 aErrFile
= aCommand
.aSlotMapFile
;
167 aErrFile2
= aTmpSlotMapFile
;
170 if (!bErr
&& !aCommand
.m_DepFile
.isEmpty())
172 bErr
|= !FileMove_Impl( aCommand
.m_DepFile
, aTmpDepFile
, bDoMove
);
174 aErrFile
= aCommand
.m_DepFile
;
175 aErrFile2
= aTmpDepFile
;
182 OString aStr
= "cannot move file from: " +
183 OUStringToOString(aErrFile2
, RTL_TEXTENCODING_UTF8
) +
185 OUStringToOString(aErrFile
, RTL_TEXTENCODING_UTF8
);
186 fprintf( stderr
, "%s\n", aStr
.getStr() );
190 if( !aCommand
.aTargetFile
.isEmpty() )
192 // stamp file, because idl passed through correctly
193 SvFileStream
aOutStm( aCommand
.aTargetFile
,
194 StreamMode::READWRITE
| StreamMode::TRUNC
);
201 if( !aCommand
.aSlotMapFile
.isEmpty() )
203 osl::FileBase::getSystemPathFromFileURL( aTmpSlotMapFile
, aTmpSlotMapFile
);
204 osl::File::remove( aTmpSlotMapFile
);
209 fprintf( stderr
, "svidl terminated with errors\n" );
213 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */