1 : /******************************************************************************
2 : *
3 : * Purpose: Implementation of the Open() function.
4 : *
5 : ******************************************************************************
6 : * Copyright (c) 2009
7 : * PCI Geomatics, 50 West Wilmot Street, Richmond Hill, Ont, Canada
8 : *
9 : * Permission is hereby granted, free of charge, to any person obtaining a
10 : * copy of this software and associated documentation files (the "Software"),
11 : * to deal in the Software without restriction, including without limitation
12 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 : * and/or sell copies of the Software, and to permit persons to whom the
14 : * Software is furnished to do so, subject to the following conditions:
15 : *
16 : * The above copyright notice and this permission notice shall be included
17 : * in all copies or substantial portions of the Software.
18 : *
19 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 : * DEALINGS IN THE SOFTWARE.
26 : ****************************************************************************/
27 : #include "pcidsk.h"
28 : #include "pcidsk_config.h"
29 : #include "pcidsk_types.h"
30 : #include "pcidsk_file.h"
31 : #include "pcidsk_interfaces.h"
32 : #include "core/cpcidskfile.h"
33 : #include <string>
34 : #include <cstring>
35 : #include <cassert>
36 :
37 : using namespace PCIDSK;
38 :
39 : /************************************************************************/
40 : /* Open() */
41 : /************************************************************************/
42 :
43 : /**
44 : * Open a PCIDSK (.pix) file.
45 : *
46 : * This function attempts to open the named file, with the indicated
47 : * access and the provided set of system interface methods.
48 : *
49 : * @param filename the name of the PCIDSK file to access.
50 : * @param access either "r" for read-only, or "r+" for read-write access.
51 : * @param interfaces Either NULL to use default interfaces, or a pointer
52 : * to a populated interfaces object.
53 : *
54 : * @return a pointer to a file object for accessing the PCIDSK file.
55 : */
56 :
57 110 : PCIDSKFile *PCIDSK::Open( std::string filename, std::string access,
58 : const PCIDSKInterfaces *interfaces )
59 :
60 : {
61 : /* -------------------------------------------------------------------- */
62 : /* Use default interfaces if none are passed in. */
63 : /* -------------------------------------------------------------------- */
64 110 : PCIDSKInterfaces default_interfaces;
65 110 : if( interfaces == NULL )
66 0 : interfaces = &default_interfaces;
67 :
68 : /* -------------------------------------------------------------------- */
69 : /* First open the file, and confirm that it is PCIDSK before */
70 : /* going further. */
71 : /* -------------------------------------------------------------------- */
72 110 : void *io_handle = interfaces->io->Open( filename, access );
73 :
74 110 : assert( io_handle != NULL );
75 :
76 : char header_check[6];
77 :
78 110 : if( interfaces->io->Read( header_check, 1, 6, io_handle ) != 6
79 : || memcmp(header_check,"PCIDSK",6) != 0 )
80 : {
81 0 : interfaces->io->Close( io_handle );
82 : ThrowPCIDSKException( "File %s does not appear to be PCIDSK format.",
83 0 : filename.c_str() );
84 : }
85 :
86 : /* -------------------------------------------------------------------- */
87 : /* Create the PCIDSKFile object. */
88 : /* -------------------------------------------------------------------- */
89 :
90 110 : CPCIDSKFile *file = new CPCIDSKFile( filename );
91 :
92 110 : file->interfaces = *interfaces;
93 110 : file->io_handle = io_handle;
94 110 : file->io_mutex = interfaces->CreateMutex();
95 :
96 110 : if( strstr(access.c_str(),"+") != NULL )
97 59 : file->updatable = true;
98 :
99 : /* -------------------------------------------------------------------- */
100 : /* Initialize it from the header. */
101 : /* -------------------------------------------------------------------- */
102 : try
103 : {
104 110 : file->InitializeFromHeader();
105 : }
106 0 : catch(...)
107 : {
108 0 : delete file;
109 0 : throw;
110 : }
111 :
112 110 : return file;
113 : }
|