Line data Source code
1 : /******************************************************************************
2 : *
3 : * Purpose: Implementation of the CLinkSegment class.
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 :
28 : #include "core/clinksegment.h"
29 : #include "segment/cpcidsksegment.h"
30 : #include "core/pcidsk_utils.h"
31 : #include "pcidsk_exception.h"
32 :
33 : #include <vector>
34 : #include <string>
35 : #include <cassert>
36 : #include <cstring>
37 :
38 : using namespace PCIDSK;
39 :
40 0 : CLinkSegment::CLinkSegment(PCIDSKFile *fileIn,
41 : int segmentIn,
42 0 : const char *segment_pointer) :
43 : CPCIDSKSegment(fileIn, segmentIn, segment_pointer),
44 0 : loaded_(false), modified_(false)
45 : {
46 0 : Load();
47 0 : }
48 :
49 :
50 0 : CLinkSegment::~CLinkSegment()
51 : {
52 0 : }
53 :
54 : // Load the contents of the segment
55 0 : void CLinkSegment::Load()
56 : {
57 : // Check if we've already loaded the segment into memory
58 0 : if (loaded_) {
59 0 : return;
60 : }
61 :
62 0 : seg_data.SetSize(data_size < 1024 ? -1 : (int) (data_size - 1024));
63 :
64 0 : ReadFromFile(seg_data.buffer, 0, data_size - 1024);
65 :
66 0 : if (seg_data.buffer_size < 8)
67 : {
68 0 : path.clear();
69 0 : return;
70 : }
71 :
72 0 : if (!STARTS_WITH(seg_data.buffer, "SysLinkF"))
73 : {
74 0 : seg_data.Put("SysLinkF",0,8);
75 0 : return;
76 : }
77 :
78 0 : const char * pszEnd = seg_data.buffer + seg_data.buffer_size;
79 0 : const char * pszPathStart = seg_data.buffer + 8;
80 0 : const char * pszPathEnd = pszPathStart;
81 :
82 : // Find the end of the path.
83 0 : while (pszPathEnd < pszEnd && *pszPathEnd)
84 0 : ++pszPathEnd;
85 :
86 : // Remove trailing spaces.
87 0 : while (pszPathEnd > pszPathStart && *pszPathEnd == ' ')
88 0 : --pszPathEnd;
89 :
90 0 : path = std::string(pszPathStart, pszPathEnd);
91 :
92 : // We've now loaded the structure up with data. Mark it as being loaded
93 : // properly.
94 0 : loaded_ = true;
95 :
96 : }
97 :
98 0 : void CLinkSegment::Write(void)
99 : {
100 : //We are not writing if nothing was loaded.
101 0 : if (!modified_) {
102 0 : return;
103 : }
104 :
105 0 : seg_data.Put("SysLinkF",0,8);
106 0 : seg_data.Put(path.c_str(), 8, static_cast<int>(path.size()), true);
107 :
108 0 : WriteToFile(seg_data.buffer, 0, data_size-1024);
109 0 : modified_ = false;
110 : }
111 :
112 0 : std::string CLinkSegment::GetPath(void) const
113 : {
114 0 : return path;
115 : }
116 :
117 0 : void CLinkSegment::SetPath(const std::string& oPath)
118 : {
119 0 : if(oPath.size() < 504)
120 : {
121 0 : path = oPath;
122 0 : modified_ = true;
123 : }
124 : else
125 : {
126 0 : return ThrowPCIDSKException("The size of the path cannot be"
127 0 : " bigger than 504 characters.");
128 : }
129 : }
130 :
131 0 : void CLinkSegment::Synchronize()
132 : {
133 0 : if(modified_)
134 : {
135 0 : this->Write();
136 : }
137 0 : }
138 :
|