libtest.cxx 3.9 KB
Newer Older
1 2 3 4 5 6 7 8 9
/* -*- 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/.
 */

10
#include <stdio.h>
11
#include <string.h>
12
#include <malloc.h>
13
#include <assert.h>
14
#include <math.h>
15

16
#include <LibreOfficeKit/LibreOfficeKitInit.h>
17
#include <LibreOfficeKit/LibreOfficeKit.hxx>
18

19 20 21

#ifdef _WIN32
//#include <Windows.h>   // come from LibreOfficeKitInit.h
22
    static long getTimeMS()
23 24 25 26
    {
        return GetTickCount();
    }

27
    static bool IsAbsolutePath(char const *pPath)
28 29 30 31 32 33 34 35 36 37 38 39 40
    {
        if (pPath[1] != ':')
        {
            fprintf( stderr, "Absolute path required to libreoffice install\n" );
            return false;
        }

        return true;
    }

#else
#include <sys/time.h>
#include <sal/types.h>
Stephan Bergmann's avatar
Stephan Bergmann committed
41
    static long getTimeMS()
42 43
    {
        struct timeval t;
44
        gettimeofday(&t, nullptr);
45 46 47
        return t.tv_sec*1000 + t.tv_usec/1000;
    }

Stephan Bergmann's avatar
Stephan Bergmann committed
48
    static bool IsAbsolutePath(char const *pPath)
49 50 51 52 53 54 55 56 57 58 59 60
    {
        if (pPath[0] != '/')
        {
            fprintf( stderr, "Absolute path required to libreoffice install\n" );
            return false;
        }

        return true;
    }
#endif


61 62
using namespace ::lok;

63

64 65 66 67 68 69
static int help()
{
    fprintf( stderr, "Usage: libtest <absolute-path-to-libreoffice-install> [path to load document] [path to save document].\n" );
    return 1;
}

70
int main (int argc, char **argv)
71
{
72 73 74 75
    long start, end;

    start = getTimeMS();

76
    if (argc < 2 || !strcmp(argv[1], "--help") || !strcmp(argv[1], "-h"))
77 78
        return help();

79 80

    if( !IsAbsolutePath(argv[1]) )
81 82
        return 1;

83
    // coverity[tainted_string] - build time test tool
84 85 86 87 88 89 90 91 92 93 94 95 96
    char *install_path = argv[1];

    if( argc > 4 )
    {
        fprintf( stderr, "testing preinit\n");
        char *imp_lib;
        void *dlhandle;
        dlhandle = lok_dlopen( install_path, &imp_lib );
        if( !dlhandle )
        {
            fprintf( stderr, "Failed to link '%s'\n", lok_dlerror() );
            return -1;
        }
Tor Lillqvist's avatar
Tor Lillqvist committed
97
        LokHookPreInit *preinit = reinterpret_cast<LokHookPreInit *>(lok_dlsym( dlhandle, "lok_preinit" ));
98 99 100 101 102
        if( !preinit )
        {
            fprintf( stderr, "Failed to find pre-init symbol: %s\n", lok_dlerror() );
            return -1;
        }
103
        preinit(install_path, nullptr);
104 105 106
    }

    Office *pOffice = lok_cpp_init( install_path );
107
    if( !pOffice )
108 109
    {
        fprintf( stderr, "Failed to initialize\n" );
110
        return -1;
111 112
    }

113 114 115 116
    end = getTimeMS();
    fprintf( stderr, "init time: %ld ms\n", (end-start) );
    start = end;

117
    fprintf( stderr, "start to load document '%s'\n", argv[2] );
118
    Document *pDocument = pOffice->documentLoad( argv[2] );
119 120
    if( !pDocument )
    {
121 122 123 124
        char *pError = pOffice->getError();
        fprintf( stderr, "failed to load document '%s': '%s'\n",
                 argv[2], pError );
        free (pError);
125 126
        return -1;
    }
127

128 129 130 131
    end = getTimeMS();
    fprintf( stderr, "load time: %ld ms\n", (end-start) );
    start = end;

132 133
    if( argc > 3 )
    {
134
        const char *pFilter = nullptr;
135 136 137 138
        if( argc > 4 )
            pFilter = argv[4];
        fprintf( stderr, "save document as '%s' (%s)\n", argv[3], pFilter ? pFilter : "<null>" );
        if( !pDocument->saveAs( argv[3], pFilter ) )
139 140 141 142 143 144
        {
            char *pError = pOffice->getError();
            fprintf( stderr, "failed to save document '%s'\n", pError);
            free (pError);
        }
        else
145
        {
146
            fprintf( stderr, "Save succeeded\n" );
147 148 149
            end = getTimeMS();
            fprintf( stderr, "save time: %ld ms\n", (end-start) );
        }
150
    }
151
    fprintf( stderr, "all tests passed.\n" );
152 153 154 155

    delete pDocument;
    delete pOffice;

156
    return 0;
157 158 159
}

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