]> git.tdb.fi Git - libs/core.git/blob - source/io/seekable.cpp
Add missing headers
[libs/core.git] / source / io / seekable.cpp
1 #ifndef WIN32
2 #define _LARGEFILE64_SOURCE
3 #include <unistd.h>
4 #else
5 #include <windows.h>
6 #endif
7 #include <stdexcept>
8 #include <msp/core/systemerror.h>
9 #include "handle.h"
10 #include "handle_private.h"
11 #include "seekable.h"
12
13 using namespace std;
14
15 namespace {
16
17 using namespace Msp::IO;
18
19 int sys_seek_type(SeekType st)
20 {
21 #ifdef WIN32
22         if(st==S_BEG)
23                 return FILE_BEGIN;
24         else if(st==S_CUR)
25                 return FILE_CURRENT;
26         else if(st==S_END)
27                 return FILE_END;
28 #else
29         if(st==S_BEG)
30                 return SEEK_SET;
31         else if(st==S_CUR)
32                 return SEEK_CUR;
33         else if(st==S_END)
34                 return SEEK_END;
35 #endif
36
37         throw invalid_argument("sys_seek_type");
38 }
39
40 } // namespace
41
42
43 namespace Msp {
44 namespace IO {
45
46 SeekOffset sys_seek(Handle &handle, SeekOffset offset, SeekType type)
47 {
48 #ifdef WIN32
49         LONG high = offset>>32;
50         DWORD ret = SetFilePointer(*handle, offset, &high, sys_seek_type(type));
51         if(ret==INVALID_SET_FILE_POINTER)
52         {
53                 DWORD err = GetLastError();
54                 if(err!=NO_ERROR)
55                         throw system_error("SetFilePointer");
56         }
57
58         return (SeekOffset(high)<<32) | ret;
59 #else
60         off64_t ret = lseek64(*handle, offset, sys_seek_type(type));
61         if(ret==(off64_t)-1)
62                 throw system_error("lseek64");
63
64         return ret;
65 #endif
66 }
67
68 } // namespace IO
69 } // namespace Msp