1 : /******************************************************************************
2 : *
3 : * Purpose: Implementation of the CPCIDSKADS40ModelSegment 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 "pcidsk_ads40.h"
29 : #include "segment/cpcidsksegment.h"
30 : #include "core/pcidsk_utils.h"
31 : #include "pcidsk_exception.h"
32 : #include "segment/cpcidskads40model.h"
33 :
34 : #include <vector>
35 : #include <string>
36 : #include <cassert>
37 : #include <cstring>
38 :
39 : using namespace PCIDSK;
40 :
41 : // Struct to store details of the RPC model
42 : struct CPCIDSKADS40ModelSegment::PCIDSKADS40Info
43 0 : {
44 : std::string path;
45 :
46 : // The raw segment data
47 : PCIDSKBuffer seg_data;
48 : };
49 :
50 0 : CPCIDSKADS40ModelSegment::CPCIDSKADS40ModelSegment(PCIDSKFile *file,
51 : int segment,
52 : const char *segment_pointer) :
53 : CPCIDSKSegment(file, segment, segment_pointer),
54 : pimpl_(new CPCIDSKADS40ModelSegment::PCIDSKADS40Info),
55 0 : loaded_(false),mbModified(false)
56 : {
57 0 : Load();
58 0 : }
59 :
60 :
61 0 : CPCIDSKADS40ModelSegment::~CPCIDSKADS40ModelSegment()
62 : {
63 0 : delete pimpl_;
64 0 : }
65 :
66 : // Load the contents of the segment
67 0 : void CPCIDSKADS40ModelSegment::Load()
68 : {
69 : // Check if we've already loaded the segment into memory
70 0 : if (loaded_) {
71 0 : return;
72 : }
73 :
74 0 : assert(data_size - 1024 == 1 * 512);
75 :
76 0 : pimpl_->seg_data.SetSize(data_size - 1024); // should be 1 * 512
77 :
78 0 : ReadFromFile(pimpl_->seg_data.buffer, 0, data_size - 1024);
79 :
80 : // The ADS40 Model Segment is defined as follows:
81 : // ADs40 Segment: 1 512-byte blocks
82 :
83 : // Block 1:
84 : // Bytes 0-7: 'ADS40 '
85 : // Byte 8-512: the path
86 :
87 0 : if (std::strncmp(pimpl_->seg_data.buffer, "ADS40 ", 8))
88 : {
89 0 : pimpl_->seg_data.Put("ADS40 ",0,8);
90 0 : return;
91 : // Something has gone terribly wrong!
92 : /*throw PCIDSKException("A segment that was previously identified as an RFMODEL "
93 : "segment does not contain the appropriate data. Found: [%s]",
94 : std::string(pimpl_->seg_data.buffer, 8).c_str());*/
95 : }
96 :
97 0 : pimpl_->path = std::string(&pimpl_->seg_data.buffer[8]);
98 :
99 : // We've now loaded the structure up with data. Mark it as being loaded
100 : // properly.
101 0 : loaded_ = true;
102 :
103 : }
104 :
105 0 : void CPCIDSKADS40ModelSegment::Write(void)
106 : {
107 : //We are not writing if nothing was loaded.
108 0 : if (!loaded_) {
109 0 : return;
110 : }
111 :
112 0 : pimpl_->seg_data.Put("ADS40 ",0,8);
113 0 : pimpl_->seg_data.Put(pimpl_->path.c_str(),8,pimpl_->path.size());
114 :
115 0 : WriteToFile(pimpl_->seg_data.buffer,0,data_size-1024);
116 0 : mbModified = false;
117 : }
118 :
119 : // Get sensor name
120 0 : std::string CPCIDSKADS40ModelSegment::GetPath(void) const
121 : {
122 0 : return pimpl_->path;
123 : }
124 :
125 : // Set sensor name
126 0 : void CPCIDSKADS40ModelSegment::SetPath(const std::string& oPath)
127 : {
128 0 : if(oPath.size() < 504)
129 : {
130 0 : pimpl_->path = oPath;
131 0 : mbModified = true;
132 : }
133 : else
134 : {
135 : throw PCIDSKException("The size of the path cannot be"
136 0 : " bigger than 504 characters.");
137 : }
138 0 : }
139 :
140 0 : void CPCIDSKADS40ModelSegment::Synchronize()
141 : {
142 0 : if(mbModified)
143 : {
144 0 : this->Write();
145 : }
146 0 : }
147 :
|