Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Purpose: Implementation of the Open() function. 4 : * 5 : ****************************************************************************** 6 : * Copyright (c) 2009 7 : * PCI Geomatics, 90 Allstate Parkway, Markham, Ontario, 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 256 : PCIDSKFile *PCIDSK::Open( const std::string& filename, const std::string& access, 58 : const PCIDSKInterfaces *interfaces, 59 : int max_channel_count ) 60 : 61 : { 62 : /* -------------------------------------------------------------------- */ 63 : /* Use default interfaces if none are passed in. */ 64 : /* -------------------------------------------------------------------- */ 65 256 : PCIDSKInterfaces default_interfaces; 66 256 : if( interfaces == nullptr ) 67 0 : interfaces = &default_interfaces; 68 : 69 : /* -------------------------------------------------------------------- */ 70 : /* First open the file, and confirm that it is PCIDSK before */ 71 : /* going further. */ 72 : /* -------------------------------------------------------------------- */ 73 256 : void *io_handle = interfaces->io->Open( filename, access ); 74 : 75 256 : assert( io_handle != nullptr ); 76 : 77 : char header_check[6]; 78 : 79 256 : if( interfaces->io->Read( header_check, 1, 6, io_handle ) != 6 80 256 : || memcmp(header_check,"PCIDSK",6) != 0 ) 81 : { 82 1 : interfaces->io->Close( io_handle ); 83 1 : return (PCIDSKFile*)ThrowPCIDSKExceptionPtr( "File %s does not appear to be PCIDSK format.", 84 0 : filename.c_str() ); 85 : } 86 : 87 : /* -------------------------------------------------------------------- */ 88 : /* Create the PCIDSKFile object. */ 89 : /* -------------------------------------------------------------------- */ 90 : 91 255 : CPCIDSKFile *file = new CPCIDSKFile( filename ); 92 : 93 255 : file->interfaces = *interfaces; 94 255 : file->io_handle = io_handle; 95 255 : file->io_mutex = interfaces->CreateMutex(); 96 : 97 255 : if( strstr(access.c_str(),"+") != nullptr ) 98 124 : file->updatable = true; 99 : 100 : /* -------------------------------------------------------------------- */ 101 : /* Initialize it from the header. */ 102 : /* -------------------------------------------------------------------- */ 103 : try 104 : { 105 255 : file->InitializeFromHeader(max_channel_count); 106 : } 107 2 : catch(...) 108 : { 109 1 : delete file; 110 1 : throw; 111 : } 112 : 113 254 : return file; 114 : }