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 .
23 #include <database.hxx>
24 #include <globals.hxx>
25 #include <command.hxx>
26 #include <rtl/ustring.hxx>
27 #include <osl/file.hxx>
31 bool FileMove_Impl( const OUString
& rFile1
, const OUString
& rFile2
, bool bMoveAlways
)
33 //printf( "Move from %s to %s\n", rFile2.GetStr(), rFile1.GetStr() );
38 SvFileStream
aOutStm1( rFile1
, StreamMode::STD_READ
);
39 SvFileStream
aOutStm2( rFile2
, StreamMode::STD_READ
);
40 if( aOutStm1
.GetError() == ERRCODE_NONE
)
42 std::unique_ptr
<sal_uInt8
[]> pBuf1(new sal_uInt8
[ BR
]);
43 std::unique_ptr
<sal_uInt8
[]> pBuf2(new sal_uInt8
[ BR
]);
44 nC1
= aOutStm1
.ReadBytes(pBuf1
.get(), BR
);
45 nC2
= aOutStm2
.ReadBytes(pBuf2
.get(), BR
);
48 if( memcmp( pBuf1
.get(), pBuf2
.get(), nC1
) )
57 nC1
= aOutStm1
.ReadBytes(pBuf1
.get(), BR
);
58 nC2
= aOutStm2
.ReadBytes(pBuf2
.get(), BR
);
64 osl::FileBase::getFileURLFromSystemPath( rFile2
, fileURL2
);
66 {// something has changed
68 osl::FileBase::getFileURLFromSystemPath( rFile1
, fileURL1
);
70 if( osl::FileBase::E_None
!= osl::File::move( fileURL2
, fileURL1
) )
73 osl::File::remove( fileURL1
);
74 osl::File::remove( fileURL2
);
79 return osl::FileBase::E_None
== osl::File::remove( fileURL2
);
82 //This function gets a system path to a file [fname], creates a temp file in
83 //the same folder as [fname] and returns the system path of the temp file.
84 inline OUString
tempFileHelper(OUString
const & fname
)
88 sal_Int32 delimIndex
= fname
.lastIndexOf( '/' );
91 OUString
aTmpDir( fname
.copy( 0, delimIndex
) );
92 osl::FileBase::getFileURLFromSystemPath( aTmpDir
, aTmpDir
);
93 osl::FileBase::createTempFile( &aTmpDir
, nullptr, &aTmpFile
);
94 osl::FileBase::getSystemPathFromFileURL( aTmpFile
, aTmpFile
);
98 OStringBuffer
aStr("invalid filename: ");
99 aStr
.append(OUStringToOString(fname
, RTL_TEXTENCODING_UTF8
));
100 fprintf(stderr
, "%s\n", aStr
.getStr());
105 int main ( int argc
, char ** argv
)
107 OUString aTmpSlotMapFile
;
108 OUString aTmpDepFile
;
110 SvCommand
aCommand( argc
, argv
);
112 if( aCommand
.nVerbosity
!= 0 )
113 printf( "StarView Interface Definition Language (IDL) Compiler 3.0\n" );
116 std::unique_ptr
<SvIdlWorkingBase
> pDataBase( new SvIdlWorkingBase(aCommand
));
119 if( !aCommand
.aExportFile
.isEmpty() )
121 osl::DirectoryItem aDI
;
122 osl::FileStatus
fileStatus( osl_FileStatus_Mask_FileName
);
123 osl::DirectoryItem::get( aCommand
.aExportFile
, aDI
);
124 aDI
.getFileStatus(fileStatus
);
125 pDataBase
->SetExportFile( fileStatus
.getFileName() );
128 if( ReadIdl( pDataBase
.get(), aCommand
) )
130 if( nExit
== 0 && !aCommand
.aSlotMapFile
.isEmpty() )
132 aTmpSlotMapFile
= tempFileHelper(aCommand
.aSlotMapFile
);
133 SvFileStream
aOutStm( aTmpSlotMapFile
, StreamMode::READWRITE
| StreamMode::TRUNC
);
134 if( !pDataBase
->WriteSfx( aOutStm
) )
137 OStringBuffer
aStr("cannot write slotmap file: ");
138 aStr
.append(OUStringToOString(aCommand
.aSlotMapFile
, RTL_TEXTENCODING_UTF8
));
139 fprintf(stderr
, "%s\n", aStr
.getStr());
142 if (nExit
== 0 && !aCommand
.m_DepFile
.isEmpty())
144 aTmpDepFile
= tempFileHelper(aCommand
.m_DepFile
);
145 SvFileStream
aOutStm( aTmpDepFile
, StreamMode::READWRITE
| StreamMode::TRUNC
);
146 pDataBase
->WriteDepFile(aOutStm
, aCommand
.aTargetFile
);
147 if( aOutStm
.GetError() != ERRCODE_NONE
)
150 fprintf( stderr
, "cannot write dependency file: %s\n",
151 OUStringToOString( aCommand
.m_DepFile
,
152 RTL_TEXTENCODING_UTF8
).getStr() );
162 bool bDoMove
= aCommand
.aTargetFile
.isEmpty();
163 OUString aErrFile
, aErrFile2
;
164 if( !bErr
&& !aCommand
.aSlotMapFile
.isEmpty() )
166 bErr
|= !FileMove_Impl( aCommand
.aSlotMapFile
, aTmpSlotMapFile
, bDoMove
);
168 aErrFile
= aCommand
.aSlotMapFile
;
169 aErrFile2
= aTmpSlotMapFile
;
172 if (!bErr
&& !aCommand
.m_DepFile
.isEmpty())
174 bErr
|= !FileMove_Impl( aCommand
.m_DepFile
, aTmpDepFile
, bDoMove
);
176 aErrFile
= aCommand
.m_DepFile
;
177 aErrFile2
= aTmpDepFile
;
184 OStringBuffer
aStr("cannot move file from: ");
185 aStr
.append(OUStringToOString(aErrFile2
,
186 RTL_TEXTENCODING_UTF8
));
187 aStr
.append("\n to file: ");
188 aStr
.append(OUStringToOString(aErrFile
,
189 RTL_TEXTENCODING_UTF8
));
190 fprintf( stderr
, "%s\n", aStr
.getStr() );
194 if( !aCommand
.aTargetFile
.isEmpty() )
196 // stamp file, because idl passed through correctly
197 SvFileStream
aOutStm( aCommand
.aTargetFile
,
198 StreamMode::READWRITE
| StreamMode::TRUNC
);
205 if( !aCommand
.aSlotMapFile
.isEmpty() )
207 osl::FileBase::getSystemPathFromFileURL( aTmpSlotMapFile
, aTmpSlotMapFile
);
208 osl::File::remove( aTmpSlotMapFile
);
213 fprintf( stderr
, "svidl terminated with errors\n" );
217 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */