summaryrefslogtreecommitdiff
path: root/idl/source/prj/svidl.cxx
blob: ee1bb85bb1629197ce47121be0045ee7d31add89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */


#include <stdlib.h>
#include <stdio.h>
#include <database.hxx>
#include <globals.hxx>
#include <command.hxx>
#include <rtl/ustring.hxx>
#include <osl/file.hxx>

#define BR 0x8000
bool FileMove_Impl( const OUString & rFile1, const OUString & rFile2, bool bImmerVerschieben )
{
    //printf( "Move from %s to %s\n", rFile2.GetStr(), rFile1.GetStr() );
    sal_uLong nC1 = 0;
    sal_uLong nC2 = 1;
    if( !bImmerVerschieben )
    {
        SvFileStream aOutStm1( rFile1, STREAM_STD_READ );
        SvFileStream aOutStm2( rFile2, STREAM_STD_READ );
        if( aOutStm1.GetError() == SVSTREAM_OK )
        {
            sal_uInt8 * pBuf1 = new sal_uInt8[ BR ];
            sal_uInt8 * pBuf2 = new sal_uInt8[ BR ];
            nC1 = aOutStm1.Read( pBuf1, BR );
            nC2 = aOutStm2.Read( pBuf2, BR );
            while( nC1 == nC2 )
            {
                if( memcmp( pBuf1, pBuf2, nC1 ) )
                {
                    nC1++;
                    break;
                }
                else
                {
                    if( 0x8000 != nC1 )
                        break;
                    nC1 = aOutStm1.Read( pBuf1, BR );
                    nC2 = aOutStm2.Read( pBuf2, BR );
                }
            }
            delete[] pBuf1;
            delete[] pBuf2;
        }
    }
    OUString fileURL2;
    osl::FileBase::getFileURLFromSystemPath( rFile2, fileURL2 );
    if( nC1 != nC2 )
    {// something has changed
        OUString fileURL1;
        osl::FileBase::getFileURLFromSystemPath( rFile1, fileURL1 );
        // move file
        if( osl::FileBase::E_None != osl::File::move( fileURL2, fileURL1 ) )
        {
            // delete both files
            osl::File::remove( fileURL1 );
            osl::File::remove( fileURL2 );
            return false;
        }
        return true;
    }
    return osl::FileBase::E_None == osl::File::remove( fileURL2 );
}

//This function gets a system path to a file [fname], creates a temp file in
//the same folder as [fname] and returns the system path of the temp file.
inline OUString tempFileHelper(OUString const & fname)
{
    OUString aTmpFile;

    sal_Int32 delimIndex = fname.lastIndexOf( '/' );
    if( delimIndex > 0 )
    {
        OUString aTmpDir( fname.copy( 0,  delimIndex ) );
        osl::FileBase::getFileURLFromSystemPath( aTmpDir, aTmpDir );
        osl::FileBase::createTempFile( &aTmpDir, 0, &aTmpFile );
        osl::FileBase::getSystemPathFromFileURL( aTmpFile, aTmpFile );
    }
    else
    {
        OStringBuffer aStr("invalid filename: ");
        aStr.append(OUStringToOString(fname, RTL_TEXTENCODING_UTF8));
        fprintf(stderr, "%s\n", aStr.getStr());
    }
    return aTmpFile;
}

int main ( int argc, char ** argv)
{
    OUString aTmpSlotMapFile;
    OUString aTmpDepFile;

    SvCommand aCommand( argc, argv );

    if( aCommand.nVerbosity != 0 )
        printf( "StarView Interface Definition Language (IDL) Compiler 3.0\n" );

    Init();
    SvIdlWorkingBase * pDataBase = new SvIdlWorkingBase(aCommand);

    int nExit = 0;
    if( !aCommand.aExportFile.isEmpty() )
    {
        osl::DirectoryItem aDI;
        osl::FileStatus fileStatus( osl_FileStatus_Mask_FileName );
        osl::DirectoryItem::get( aCommand.aExportFile, aDI );
        aDI.getFileStatus(fileStatus);
        pDataBase->SetExportFile( fileStatus.getFileName() );
    }

    if( ReadIdl( pDataBase, aCommand ) )
    {
        if( nExit == 0 && !aCommand.aSlotMapFile.isEmpty() )
        {
            aTmpSlotMapFile = tempFileHelper(aCommand.aSlotMapFile);
            SvFileStream aOutStm( aTmpSlotMapFile, STREAM_READWRITE | StreamMode::TRUNC );
            if( !pDataBase->WriteSfx( aOutStm ) )
            {
                nExit = -1;
                OStringBuffer aStr("cannot write slotmap file: ");
                aStr.append(OUStringToOString(aCommand.aSlotMapFile, RTL_TEXTENCODING_UTF8));
                fprintf(stderr, "%s\n", aStr.getStr());
            }
        }
        if (nExit == 0 && !aCommand.m_DepFile.isEmpty())
        {
            aTmpDepFile = tempFileHelper(aCommand.m_DepFile);
            SvFileStream aOutStm( aTmpDepFile, STREAM_READWRITE | StreamMode::TRUNC );
            pDataBase->WriteDepFile(aOutStm, aCommand.aTargetFile);
            if( aOutStm.GetError() != SVSTREAM_OK )
            {
                nExit = -1;
                fprintf( stderr, "cannot write dependency file: %s\n",
                        OUStringToOString( aCommand.m_DepFile,
                            RTL_TEXTENCODING_UTF8 ).getStr() );
            }
        }
    }
    else
        nExit = -1;

    if( nExit == 0 )
    {
        bool bErr = false;
        bool bDoMove = aCommand.aTargetFile.isEmpty();
        OUString aErrFile, aErrFile2;
        if( !bErr && !aCommand.aSlotMapFile.isEmpty() )
        {
            bErr |= !FileMove_Impl( aCommand.aSlotMapFile, aTmpSlotMapFile, bDoMove );
            if( bErr ) {
                aErrFile = aCommand.aSlotMapFile;
                aErrFile2 = aTmpSlotMapFile;
            }
        }
        if (!bErr && !aCommand.m_DepFile.isEmpty())
        {
            bErr |= !FileMove_Impl( aCommand.m_DepFile, aTmpDepFile, bDoMove );
            if (bErr) {
                aErrFile = aCommand.m_DepFile;
                aErrFile2 = aTmpDepFile;
            }
        }

        if( bErr )
        {
            nExit = -1;
            OStringBuffer aStr("cannot move file from: ");
            aStr.append(OUStringToOString(aErrFile2,
                RTL_TEXTENCODING_UTF8));
            aStr.append("\n              to file: ");
            aStr.append(OUStringToOString(aErrFile,
                RTL_TEXTENCODING_UTF8));
            fprintf( stderr, "%s\n", aStr.getStr() );
        }
        else
        {
            if( !aCommand.aTargetFile.isEmpty() )
            {
                // stamp file, because idl passed through correctly
                SvFileStream aOutStm( aCommand.aTargetFile,
                                STREAM_READWRITE | StreamMode::TRUNC );
            }
        }
    }

    if( nExit != 0 )
    {
        if( !aCommand.aSlotMapFile.isEmpty() )
        {
            osl::FileBase::getSystemPathFromFileURL( aTmpSlotMapFile, aTmpSlotMapFile );
            osl::File::remove( aTmpSlotMapFile );
        }
    }

    delete pDataBase;
    DeInit();
    if( nExit != 0 )
        fprintf( stderr, "svidl terminated with errors\n" );
    return nExit;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */