1 : /******************************************************************************
2 : *
3 : * Purpose: Implementation of the CPCIDSKSegment class.
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 :
28 : #include "segment/cpcidsksegment.h"
29 : #include "core/metadataset.h"
30 : #include "core/cpcidskfile.h"
31 : #include "core/pcidsk_utils.h"
32 : #include "pcidsk_buffer.h"
33 : #include "pcidsk_exception.h"
34 : #include <cassert>
35 : #include <cstdlib>
36 :
37 : using namespace PCIDSK;
38 :
39 : /************************************************************************/
40 : /* PCIDSKSegment() */
41 : /************************************************************************/
42 :
43 85 : CPCIDSKSegment::CPCIDSKSegment( PCIDSKFile *file, int segment,
44 85 : const char *segment_pointer )
45 :
46 : {
47 85 : this->file = file;
48 85 : this->segment = segment;
49 :
50 85 : LoadSegmentPointer( segment_pointer );
51 85 : LoadSegmentHeader(); // eventually we might want to defer this.
52 :
53 : /* -------------------------------------------------------------------- */
54 : /* Initialize the metadata object, but do not try to load till */
55 : /* needed. */
56 : /* -------------------------------------------------------------------- */
57 85 : metadata = new MetadataSet;
58 170 : metadata->Initialize( file, SegmentTypeName(segment_type), segment );
59 85 : }
60 :
61 : /************************************************************************/
62 : /* ~PCIDSKSegment() */
63 : /************************************************************************/
64 :
65 88 : CPCIDSKSegment::~CPCIDSKSegment()
66 :
67 : {
68 85 : delete metadata;
69 88 : }
70 :
71 0 : std::string CPCIDSKSegment::GetMetadataValue( std::string key )
72 : {
73 0 : return metadata->GetMetadataValue(key);
74 : }
75 :
76 0 : void CPCIDSKSegment::SetMetadataValue( std::string key, std::string value )
77 : {
78 0 : metadata->SetMetadataValue(key,value);
79 0 : }
80 :
81 1 : std::vector<std::string> CPCIDSKSegment::GetMetadataKeys()
82 : {
83 1 : return metadata->GetMetadataKeys();
84 : }
85 :
86 : /************************************************************************/
87 : /* LoadSegmentPointer() */
88 : /************************************************************************/
89 :
90 85 : void CPCIDSKSegment::LoadSegmentPointer( const char *segment_pointer )
91 :
92 : {
93 85 : PCIDSKBuffer segptr( segment_pointer, 32 );
94 :
95 85 : segment_flag = segptr.buffer[0];
96 85 : segment_type = (eSegType) (atoi(segptr.Get(1,3)));
97 85 : data_offset = (atouint64(segptr.Get(12,11))-1) * 512;
98 85 : data_size = atouint64(segptr.Get(23,9)) * 512;
99 :
100 85 : segptr.Get(4,8,segment_name);
101 85 : }
102 :
103 : /************************************************************************/
104 : /* LoadSegmentHeader() */
105 : /************************************************************************/
106 :
107 85 : void CPCIDSKSegment::LoadSegmentHeader()
108 :
109 : {
110 85 : header.SetSize(1024);
111 :
112 85 : file->ReadFromFile( header.buffer, data_offset, 1024 );
113 :
114 : // parse out history, etc
115 85 : }
116 :
117 : /************************************************************************/
118 : /* ReadFromFile() */
119 : /************************************************************************/
120 :
121 154 : void CPCIDSKSegment::ReadFromFile( void *buffer, uint64 offset, uint64 size )
122 :
123 : {
124 154 : if( offset+size+1024 > data_size )
125 : ThrowPCIDSKException(
126 : "Attempt to read past end of segment %d (%d bytes at offset %d)",
127 0 : segment, (int) offset, (int) size );
128 154 : file->ReadFromFile( buffer, offset + data_offset + 1024, size );
129 154 : }
130 :
131 : /************************************************************************/
132 : /* WriteToFile() */
133 : /************************************************************************/
134 :
135 103 : void CPCIDSKSegment::WriteToFile( const void *buffer, uint64 offset, uint64 size )
136 : {
137 103 : if( offset+size > data_size-1024 )
138 : {
139 17 : CPCIDSKFile *poFile = dynamic_cast<CPCIDSKFile *>(file);
140 :
141 17 : if (poFile == NULL) {
142 : ThrowPCIDSKException("Attempt to dynamic_cast the file interface "
143 : "to a CPCIDSKFile failed. This is a programmer error, and should "
144 0 : "be reported to your software provider.");
145 : }
146 :
147 17 : if( !IsAtEOF() )
148 0 : poFile->MoveSegmentToEOF( segment );
149 :
150 : uint64 blocks_to_add =
151 17 : ((offset+size+511) - (data_size - 1024)) / 512;
152 :
153 : // prezero if we aren't directly writing all the new blocks.
154 : poFile->ExtendSegment( segment, blocks_to_add,
155 : !(offset == data_size - 1024
156 17 : && size == blocks_to_add * 512) );
157 17 : data_size += blocks_to_add * 512;
158 : }
159 :
160 103 : file->WriteToFile( buffer, offset + data_offset + 1024, size );
161 103 : }
162 :
163 : /************************************************************************/
164 : /* GetDescription() */
165 : /************************************************************************/
166 :
167 0 : std::string CPCIDSKSegment::GetDescription()
168 : {
169 0 : return "";
170 : }
171 :
172 : /************************************************************************/
173 : /* IsAtEOF() */
174 : /************************************************************************/
175 :
176 17 : bool CPCIDSKSegment::IsAtEOF()
177 : {
178 17 : if( 512 * file->GetFileSize() == data_offset + data_size )
179 17 : return true;
180 : else
181 0 : return false;
182 : }
|