Bump version to 5.0-14
[LibreOffice.git] / shell / source / win32 / shlxthandler / ooofilt / stream_helper.cxx
blobf1fb3213cf94c07747d6e20d8b987058f2e926be
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
21 #if defined _MSC_VER
22 #pragma warning(push, 1)
23 #endif
24 #include <windows.h>
25 #if defined _MSC_VER
26 #pragma warning(pop)
27 #endif
29 #include <stdio.h>
30 #include <objidl.h>
31 #include "internal/stream_helper.hxx"
33 BufferStream::BufferStream(IStream *str) :
34 stream(str)
36 // These next few lines work around the "Seek pointer" bug found on Vista.
37 char cBuf[20];
38 unsigned long nCount;
39 ULARGE_INTEGER nNewPosition;
40 LARGE_INTEGER nMove;
41 nMove.QuadPart = 0;
42 stream->Seek( nMove, STREAM_SEEK_SET, &nNewPosition );
43 stream->Read( cBuf, 20, &nCount );
46 BufferStream::~BufferStream()
50 unsigned long BufferStream::sread (unsigned char *buf, unsigned long size)
52 unsigned long newsize;
53 HRESULT hr;
55 hr = stream->Read (buf, size, &newsize);
56 if (hr == S_OK)
57 return (unsigned long)newsize;
58 else
59 return (unsigned long)0;
62 long BufferStream::stell ()
64 HRESULT hr;
65 LARGE_INTEGER Move;
66 ULARGE_INTEGER NewPosition;
67 Move.QuadPart = 0;
68 NewPosition.QuadPart = 0;
70 hr = ((IStream *)stream)->Seek (Move, STREAM_SEEK_CUR, &NewPosition);
71 if (hr == S_OK)
72 return (long) NewPosition.QuadPart;
73 else
74 return -1;
77 long BufferStream::sseek (long offset, int origin)
79 HRESULT hr;
80 LARGE_INTEGER Move;
81 DWORD dwOrigin;
82 Move.QuadPart = (__int64)offset;
84 switch (origin)
86 case SEEK_CUR:
87 dwOrigin = STREAM_SEEK_CUR;
88 break;
89 case SEEK_END:
90 dwOrigin = STREAM_SEEK_END;
91 break;
92 case SEEK_SET:
93 dwOrigin = STREAM_SEEK_SET;
94 break;
95 default:
96 return -1;
99 hr = stream->Seek (Move, dwOrigin, NULL);
100 if (hr == S_OK)
101 return 0;
102 else
103 return -1;
106 FileStream::FileStream(const char *filename) :
107 file(0)
109 // fdo#67534: avoid locking to not interfere with soffice opening the file
110 file = _fsopen(filename, "rb", _SH_DENYNO);
113 FileStream::~FileStream()
115 if (file)
116 fclose(file);
119 unsigned long FileStream::sread (unsigned char *buf, unsigned long size)
121 if (file)
122 return static_cast<unsigned long>(fread(buf, 1, size, file));
123 return 0;
126 long FileStream::stell ()
128 if (file)
129 return ftell(file);
130 return -1L;
133 long FileStream::sseek (long offset, int origin)
135 if (file)
136 return fseek(file, offset, origin);
137 return -1L;
140 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */