1 : /******************************************************************************
2 : * $Id: pcidskdataset.cpp 17097 2009-05-21 19:59:35Z warmerdam $
3 : *
4 : * Project: PCIDSK Database File
5 : * Purpose: PCIDSK SDK compatiable io interface built on VSI.
6 : * Author: Frank Warmerdam, warmerdam@pobox.com
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2009, Frank Warmerdam <warmerdam@pobox.com>
10 : *
11 : * Permission is hereby granted, free of charge, to any person obtaining a
12 : * copy of this software and associated documentation files (the "Software"),
13 : * to deal in the Software without restriction, including without limitation
14 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 : * and/or sell copies of the Software, and to permit persons to whom the
16 : * Software is furnished to do so, subject to the following conditions:
17 : *
18 : * The above copyright notice and this permission notice shall be included
19 : * in all copies or substantial portions of the Software.
20 : *
21 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 : * DEALINGS IN THE SOFTWARE.
28 : ****************************************************************************/
29 :
30 : #include "cpl_conv.h"
31 : #include "cpl_multiproc.h"
32 : #include "pcidsk.h"
33 :
34 : CPL_CVSID("$Id: pcidskdataset.cpp 17097 2009-05-21 19:59:35Z warmerdam $");
35 :
36 : using namespace PCIDSK;
37 :
38 : class VSI_IOInterface : public IOInterfaces
39 4 : {
40 : virtual void *Open( std::string filename, std::string access ) const;
41 : virtual uint64 Seek( void *io_handle, uint64 offset, int whence ) const;
42 : virtual uint64 Tell( void *io_handle ) const;
43 : virtual uint64 Read( void *buffer, uint64 size, uint64 nmemb, void *io_hanle ) const;
44 : virtual uint64 Write( const void *buffer, uint64 size, uint64 nmemb, void *io_handle ) const;
45 : virtual int Eof( void *io_handle ) const;
46 : virtual int Flush( void *io_handle ) const;
47 : virtual int Close( void *io_handle ) const;
48 :
49 : const char *LastError() const;
50 : };
51 :
52 : /************************************************************************/
53 : /* PCIDSK2GetIOInterfaces() */
54 : /************************************************************************/
55 :
56 78 : const PCIDSK::PCIDSKInterfaces *PCIDSK2GetInterfaces()
57 : {
58 78 : static VSI_IOInterface singleton_vsi_interface;
59 78 : static PCIDSKInterfaces singleton_pcidsk2_interfaces;
60 :
61 78 : singleton_pcidsk2_interfaces.io = &singleton_vsi_interface;
62 :
63 78 : return &singleton_pcidsk2_interfaces;
64 : }
65 :
66 : /************************************************************************/
67 : /* Open() */
68 : /************************************************************************/
69 :
70 : void *
71 112 : VSI_IOInterface::Open( std::string filename, std::string access ) const
72 :
73 : {
74 112 : FILE *fp = VSIFOpenL( filename.c_str(), access.c_str() );
75 :
76 112 : if( fp == NULL )
77 : ThrowPCIDSKException( "Failed to open %s: %s",
78 0 : filename.c_str(), LastError() );
79 :
80 112 : return fp;
81 : }
82 :
83 : /************************************************************************/
84 : /* Seek() */
85 : /************************************************************************/
86 :
87 : uint64
88 1381 : VSI_IOInterface::Seek( void *io_handle, uint64 offset, int whence ) const
89 :
90 : {
91 1381 : FILE *fp = (FILE *) io_handle;
92 :
93 1381 : uint64 result = VSIFSeekL( fp, offset, whence );
94 :
95 1381 : if( result == (uint64) -1 )
96 : ThrowPCIDSKException( "Seek(%d,%d): %s",
97 : (int) offset, whence,
98 0 : LastError() );
99 :
100 1381 : return result;
101 : }
102 :
103 : /************************************************************************/
104 : /* Tell() */
105 : /************************************************************************/
106 :
107 0 : uint64 VSI_IOInterface::Tell( void *io_handle ) const
108 :
109 : {
110 0 : FILE *fp = (FILE *) io_handle;
111 :
112 0 : return VSIFTellL( fp );
113 : }
114 :
115 : /************************************************************************/
116 : /* Read() */
117 : /************************************************************************/
118 :
119 678 : uint64 VSI_IOInterface::Read( void *buffer, uint64 size, uint64 nmemb,
120 : void *io_handle ) const
121 :
122 : {
123 678 : FILE *fp = (FILE *) io_handle;
124 :
125 678 : errno = 0;
126 :
127 678 : uint64 result = VSIFReadL( buffer, size, nmemb, fp );
128 :
129 678 : if( errno != 0 && result == 0 && nmemb != 0 )
130 : ThrowPCIDSKException( "Read(%d): %s",
131 : (int) size * nmemb,
132 0 : LastError() );
133 :
134 678 : return result;
135 : }
136 :
137 : /************************************************************************/
138 : /* Write() */
139 : /************************************************************************/
140 :
141 836 : uint64 VSI_IOInterface::Write( const void *buffer, uint64 size, uint64 nmemb,
142 : void *io_handle ) const
143 :
144 : {
145 836 : FILE *fp = (FILE *) io_handle;
146 :
147 836 : errno = 0;
148 :
149 836 : uint64 result = VSIFWriteL( buffer, size, nmemb, fp );
150 :
151 836 : if( errno != 0 && result == 0 && nmemb != 0 )
152 : ThrowPCIDSKException( "Write(%d): %s",
153 : (int) size * nmemb,
154 0 : LastError() );
155 :
156 836 : return result;
157 : }
158 :
159 : /************************************************************************/
160 : /* Eof() */
161 : /************************************************************************/
162 :
163 0 : int VSI_IOInterface::Eof( void *io_handle ) const
164 :
165 : {
166 0 : return VSIFEofL( (FILE *) io_handle );
167 : }
168 :
169 : /************************************************************************/
170 : /* Flush() */
171 : /************************************************************************/
172 :
173 0 : int VSI_IOInterface::Flush( void *io_handle ) const
174 :
175 : {
176 0 : return VSIFFlushL( (FILE *) io_handle );
177 : }
178 :
179 : /************************************************************************/
180 : /* Close() */
181 : /************************************************************************/
182 :
183 112 : int VSI_IOInterface::Close( void *io_handle ) const
184 :
185 : {
186 112 : return VSIFCloseL( (FILE *) io_handle );
187 : }
188 :
189 : /************************************************************************/
190 : /* LastError() */
191 : /* */
192 : /* Return a string representation of the last error. */
193 : /************************************************************************/
194 :
195 0 : const char *VSI_IOInterface::LastError() const
196 :
197 : {
198 0 : return strerror( errno );
199 : }
200 :
201 : /************************************************************************/
202 : /* If we are using the internal copy of the PCIDSK SDK we need */
203 : /* to provide stub implementations of GetDefaultIOInterfaces() */
204 : /* and GetDefaultMutex() */
205 : /************************************************************************/
206 :
207 : #ifdef PCIDSK_INTERNAL
208 :
209 191 : const IOInterfaces *PCIDSK::GetDefaultIOInterfaces()
210 : {
211 191 : static VSI_IOInterface singleton_vsi_interface;
212 :
213 191 : return &singleton_vsi_interface;
214 : }
215 :
216 : /************************************************************************/
217 : /* CPLThreadMutex */
218 : /************************************************************************/
219 :
220 : class CPLThreadMutex : public PCIDSK::Mutex
221 :
222 : {
223 : private:
224 : void *hMutex;
225 :
226 : public:
227 : CPLThreadMutex();
228 : ~CPLThreadMutex();
229 :
230 : int Acquire(void);
231 : int Release(void);
232 : };
233 :
234 : /************************************************************************/
235 : /* CPLThreadMutex() */
236 : /************************************************************************/
237 :
238 78 : CPLThreadMutex::CPLThreadMutex()
239 :
240 : {
241 78 : hMutex = CPLCreateMutex();
242 78 : }
243 :
244 : /************************************************************************/
245 : /* ~CPLThreadMutex() */
246 : /************************************************************************/
247 :
248 156 : CPLThreadMutex::~CPLThreadMutex()
249 :
250 : {
251 78 : CPLDestroyMutex( hMutex );
252 156 : }
253 :
254 : /************************************************************************/
255 : /* Release() */
256 : /************************************************************************/
257 :
258 1358 : int CPLThreadMutex::Release()
259 :
260 : {
261 1358 : CPLReleaseMutex( hMutex );
262 1358 : return 1;
263 : }
264 :
265 : /************************************************************************/
266 : /* Acquire() */
267 : /************************************************************************/
268 :
269 1358 : int CPLThreadMutex::Acquire()
270 :
271 : {
272 1358 : return CPLAcquireMutex( hMutex, 100.0 );
273 : }
274 :
275 : /************************************************************************/
276 : /* DefaultCreateMutex() */
277 : /************************************************************************/
278 :
279 78 : PCIDSK::Mutex *PCIDSK::DefaultCreateMutex(void)
280 :
281 : {
282 78 : return new CPLThreadMutex();
283 : }
284 :
285 : #endif /* def PCIDSK_INTERNAL */
|