1 : /******************************************************************************
2 : *
3 : * Purpose: Implementation of the CPCIDSKBinarySegment class.
4 : *
5 : ******************************************************************************
6 : * Copyright (c) 2010
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/cpcidskbinarysegment.h"
29 : #include "segment/cpcidsksegment.h"
30 : #include "core/pcidsk_utils.h"
31 : #include "pcidsk_exception.h"
32 : #include "core/pcidsk_utils.h"
33 :
34 : #include <vector>
35 : #include <string>
36 : #include <cassert>
37 : #include <cstring>
38 :
39 : using namespace PCIDSK;
40 :
41 : /**
42 : * Binary Segment constructor
43 : * @param[in,out] file the PCIDSK file
44 : * @param[in] segment the segment index
45 : * @param[in] segment_pointer the segement pointer
46 : * @param[in] bLoad true to load the segment, else false (default true)
47 : */
48 0 : CPCIDSKBinarySegment::CPCIDSKBinarySegment(PCIDSKFile *file,
49 : int segment,
50 : const char *segment_pointer,
51 : bool bLoad) :
52 : CPCIDSKSegment(file, segment, segment_pointer),
53 0 : loaded_(false),mbModified(false)
54 : {
55 0 : if (true == bLoad)
56 : {
57 0 : Load();
58 : }
59 : return;
60 0 : }// Initializer constructor
61 :
62 :
63 0 : CPCIDSKBinarySegment::~CPCIDSKBinarySegment()
64 : {
65 0 : }
66 :
67 : /**
68 : * Load the contents of the segment
69 : */
70 0 : void CPCIDSKBinarySegment::Load()
71 : {
72 : // Check if we've already loaded the segment into memory
73 0 : if (loaded_) {
74 0 : return;
75 : }
76 :
77 0 : seg_data.SetSize((int)data_size - 1024);
78 :
79 0 : ReadFromFile(seg_data.buffer, 0, data_size - 1024);
80 :
81 : // Mark it as being loaded properly.
82 0 : loaded_ = true;
83 :
84 : }
85 :
86 : /**
87 : * Write the segment on disk
88 : */
89 0 : void CPCIDSKBinarySegment::Write(void)
90 : {
91 : //We are not writing if nothing was loaded.
92 0 : if (!loaded_) {
93 0 : return;
94 : }
95 :
96 0 : WriteToFile(seg_data.buffer, 0, seg_data.buffer_size);
97 :
98 0 : mbModified = false;
99 : }
100 :
101 : /**
102 : * Synchronize the segement, if it was modified then
103 : * write it into disk.
104 : */
105 0 : void CPCIDSKBinarySegment::Synchronize()
106 : {
107 0 : if(mbModified)
108 : {
109 0 : this->Write();
110 : }
111 0 : }
112 :
113 : void
114 0 : CPCIDSKBinarySegment::SetBuffer(const char* pabyBuf,
115 : unsigned int nBufSize)
116 : {
117 : // Round the buffer size up to the next multiple of 512.
118 0 : int nNumBlocks = nBufSize / 512 + ((0 == nBufSize % 512) ? 0 : 1);
119 0 : unsigned int nAllocBufSize = 512 * nNumBlocks;
120 :
121 0 : seg_data.SetSize((int)nAllocBufSize);
122 0 : data_size = nAllocBufSize + 1024; // Incl. header
123 :
124 0 : memcpy(seg_data.buffer, pabyBuf, nBufSize);
125 :
126 : // Fill unused data at end with zeroes.
127 0 : if (nBufSize < nAllocBufSize)
128 : {
129 : memset(seg_data.buffer + nBufSize, 0,
130 0 : nAllocBufSize - nBufSize);
131 : }
132 0 : mbModified = true;
133 :
134 : return;
135 : }// SetBuffer
|