summaryrefslogtreecommitdiffstats
path: root/shell/source/win32/shlxthandler/ooofilt/stream_helper.cxx
blob: 76578ba70d25e1578c24133003aa499cb2620e0e (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
/* -*- 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 .
 */


#if !defined WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>

#include <stdio.h>
#include <objidl.h>
#include <stream_helper.hxx>

BufferStream::BufferStream(IStream *str) :
    stream(str)
{
    // These next few lines work around the "Seek pointer" bug found on Vista.
    char cBuf[20];
    unsigned long nCount;
    ULARGE_INTEGER nNewPosition;
    LARGE_INTEGER nMove;
    nMove.QuadPart = 0;
    stream->Seek( nMove, STREAM_SEEK_SET, &nNewPosition );
    stream->Read( cBuf, 20, &nCount );
}

BufferStream::~BufferStream()
{
}

unsigned long BufferStream::sread (unsigned char *buf, unsigned long size)
{
    unsigned long newsize;
    HRESULT hr;

    hr = stream->Read (buf, size, &newsize);
    if (hr == S_OK)
        return newsize;
    else
        return static_cast<unsigned long>(0);
}

long BufferStream::stell ()
{
    HRESULT hr;
    LARGE_INTEGER Move;
    ULARGE_INTEGER NewPosition;
    Move.QuadPart = 0;
    NewPosition.QuadPart = 0;

    hr = stream->Seek (Move, STREAM_SEEK_CUR, &NewPosition);
    if (hr == S_OK)
        return static_cast<long>(NewPosition.QuadPart);
    else
        return -1;
}

long BufferStream::sseek (long offset, int origin)
{
    HRESULT hr;
    LARGE_INTEGER Move;
    DWORD dwOrigin;
    Move.QuadPart = static_cast<__int64>(offset);

    switch (origin)
    {
    case SEEK_CUR:
        dwOrigin = STREAM_SEEK_CUR;
        break;
    case SEEK_END:
        dwOrigin = STREAM_SEEK_END;
        break;
    case SEEK_SET:
        dwOrigin = STREAM_SEEK_SET;
        break;
    default:
        return -1;
    }

    hr = stream->Seek (Move, dwOrigin, nullptr);
    if (hr == S_OK)
        return 0;
    else
        return -1;
}

FileStream::FileStream(const Filepath_char_t *filename) :
    file(nullptr)
{
    // fdo#67534: avoid locking to not interfere with soffice opening the file
    file = _wfsopen(filename, L"rb", _SH_DENYNO);
}

FileStream::~FileStream()
{
    if (file)
        fclose(file);
}

unsigned long FileStream::sread (unsigned char *buf, unsigned long size)
{
    if (file)
        return static_cast<unsigned long>(fread(buf, 1, size, file));
    return 0;
}

long FileStream::stell ()
{
    if (file)
        return ftell(file);
    return -1;
}

long FileStream::sseek (long offset, int origin)
{
    if (file)
        return fseek(file, offset, origin);
    return -1;
}

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