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 : * SPDX-License-Identifier: MIT 10 : ****************************************************************************/ 11 : #include "pcidsk.h" 12 : #include "pcidsk_config.h" 13 : #include "pcidsk_types.h" 14 : #include "pcidsk_file.h" 15 : #include "pcidsk_interfaces.h" 16 : #include "core/cpcidskfile.h" 17 : #include <string> 18 : #include <cstring> 19 : #include <cassert> 20 : 21 : using namespace PCIDSK; 22 : 23 : /************************************************************************/ 24 : /* Open() */ 25 : /************************************************************************/ 26 : 27 : /** 28 : * Open a PCIDSK (.pix) file. 29 : * 30 : * This function attempts to open the named file, with the indicated 31 : * access and the provided set of system interface methods. 32 : * 33 : * @param filename the name of the PCIDSK file to access. 34 : * @param access either "r" for read-only, or "r+" for read-write access. 35 : * @param interfaces Either NULL to use default interfaces, or a pointer 36 : * to a populated interfaces object. 37 : * 38 : * @return a pointer to a file object for accessing the PCIDSK file. 39 : */ 40 : 41 256 : PCIDSKFile *PCIDSK::Open( const std::string& filename, const std::string& access, 42 : const PCIDSKInterfaces *interfaces, 43 : int max_channel_count ) 44 : 45 : { 46 : /* -------------------------------------------------------------------- */ 47 : /* Use default interfaces if none are passed in. */ 48 : /* -------------------------------------------------------------------- */ 49 256 : PCIDSKInterfaces default_interfaces; 50 256 : if( interfaces == nullptr ) 51 0 : interfaces = &default_interfaces; 52 : 53 : /* -------------------------------------------------------------------- */ 54 : /* First open the file, and confirm that it is PCIDSK before */ 55 : /* going further. */ 56 : /* -------------------------------------------------------------------- */ 57 256 : void *io_handle = interfaces->io->Open( filename, access ); 58 : 59 256 : assert( io_handle != nullptr ); 60 : 61 : char header_check[6]; 62 : 63 256 : if( interfaces->io->Read( header_check, 1, 6, io_handle ) != 6 64 256 : || memcmp(header_check,"PCIDSK",6) != 0 ) 65 : { 66 1 : interfaces->io->Close( io_handle ); 67 1 : return (PCIDSKFile*)ThrowPCIDSKExceptionPtr( "File %s does not appear to be PCIDSK format.", 68 0 : filename.c_str() ); 69 : } 70 : 71 : /* -------------------------------------------------------------------- */ 72 : /* Create the PCIDSKFile object. */ 73 : /* -------------------------------------------------------------------- */ 74 : 75 255 : CPCIDSKFile *file = new CPCIDSKFile( filename ); 76 : 77 255 : file->interfaces = *interfaces; 78 255 : file->io_handle = io_handle; 79 255 : file->io_mutex = interfaces->CreateMutex(); 80 : 81 255 : if( strstr(access.c_str(),"+") != nullptr ) 82 124 : file->updatable = true; 83 : 84 : /* -------------------------------------------------------------------- */ 85 : /* Initialize it from the header. */ 86 : /* -------------------------------------------------------------------- */ 87 : try 88 : { 89 255 : file->InitializeFromHeader(max_channel_count); 90 : } 91 2 : catch(...) 92 : { 93 1 : delete file; 94 1 : throw; 95 : } 96 : 97 254 : return file; 98 : }