1 : /******************************************************************************
2 : *
3 : * Purpose: Implementation of the PCIDSKBuffer class. This class is for
4 : * convenient parsing and formatting of PCIDSK ASCII headers.
5 : *
6 : ******************************************************************************
7 : * Copyright (c) 2009
8 : * PCI Geomatics, 50 West Wilmot Street, Richmond Hill, Ont, Canada
9 : *
10 : * Permission is hereby granted, free of charge, to any person obtaining a
11 : * copy of this software and associated documentation files (the "Software"),
12 : * to deal in the Software without restriction, including without limitation
13 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 : * and/or sell copies of the Software, and to permit persons to whom the
15 : * Software is furnished to do so, subject to the following conditions:
16 : *
17 : * The above copyright notice and this permission notice shall be included
18 : * in all copies or substantial portions of the Software.
19 : *
20 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 : * DEALINGS IN THE SOFTWARE.
27 : ****************************************************************************/
28 :
29 : #include "pcidsk_buffer.h"
30 : #include "pcidsk_exception.h"
31 : #include "core/pcidsk_utils.h"
32 :
33 : #include <cstdlib>
34 : #include <cstring>
35 : #include <cstdio>
36 :
37 : using namespace PCIDSK;
38 :
39 : #ifdef _MSC_VER
40 : #ifndef snprintf
41 : #define snprintf _snprintf
42 : #endif // !defined(snprintf)
43 : #endif
44 :
45 : /************************************************************************/
46 : /* PCIDSKBuffer() */
47 : /************************************************************************/
48 :
49 734 : PCIDSKBuffer::PCIDSKBuffer( int size )
50 :
51 : {
52 734 : buffer_size = 0;
53 734 : buffer = NULL;
54 :
55 734 : if( size > 0 )
56 489 : SetSize( size );
57 734 : }
58 :
59 : /************************************************************************/
60 : /* PCIDSKBuffer() */
61 : /************************************************************************/
62 :
63 85 : PCIDSKBuffer::PCIDSKBuffer( const char *src, int size )
64 :
65 : {
66 85 : buffer_size = 0;
67 85 : buffer = NULL;
68 :
69 85 : SetSize( size );
70 85 : memcpy( buffer, src, size );
71 85 : }
72 :
73 : /************************************************************************/
74 : /* ~PCIDSKBuffer() */
75 : /************************************************************************/
76 :
77 819 : PCIDSKBuffer::~PCIDSKBuffer()
78 :
79 : {
80 819 : free( buffer );
81 819 : }
82 :
83 : /************************************************************************/
84 : /* SetSize() */
85 : /************************************************************************/
86 :
87 974 : void PCIDSKBuffer::SetSize( int size )
88 :
89 : {
90 974 : buffer_size = size;
91 974 : if( buffer == NULL )
92 819 : buffer = (char *) malloc(size+1);
93 : else
94 155 : buffer = (char *) realloc(buffer,size+1);
95 :
96 974 : if( buffer == NULL )
97 : {
98 0 : buffer_size = 0;
99 : ThrowPCIDSKException( "Out of memory allocating %d byte PCIDSKBuffer.",
100 0 : size );
101 : }
102 :
103 974 : buffer[size] = '\0';
104 974 : }
105 :
106 : /************************************************************************/
107 : /* Get() */
108 : /************************************************************************/
109 :
110 1237 : const char *PCIDSKBuffer::Get( int offset, int size )
111 :
112 : {
113 1237 : Get( offset, size, work_field, 0 );
114 1237 : return work_field.c_str();
115 : }
116 :
117 : /************************************************************************/
118 : /* Get() */
119 : /************************************************************************/
120 :
121 1909 : void PCIDSKBuffer::Get( int offset, int size, std::string &target, int unpad )
122 :
123 : {
124 1909 : if( offset + size > buffer_size )
125 0 : ThrowPCIDSKException( "Get() past end of PCIDSKBuffer." );
126 :
127 1909 : if( unpad )
128 : {
129 19946 : while( size > 0 && buffer[offset+size-1] == ' ' )
130 18602 : size--;
131 : }
132 :
133 1909 : target.assign( buffer + offset, size );
134 1909 : }
135 :
136 : /************************************************************************/
137 : /* GetUInt64() */
138 : /************************************************************************/
139 :
140 154 : uint64 PCIDSKBuffer::GetUInt64( int offset, int size )
141 :
142 : {
143 154 : std::string value_str;
144 :
145 154 : if( offset + size > buffer_size )
146 0 : ThrowPCIDSKException( "GetUInt64() past end of PCIDSKBuffer." );
147 :
148 154 : value_str.assign( buffer + offset, size );
149 :
150 154 : return atouint64(value_str.c_str());
151 : }
152 :
153 : /************************************************************************/
154 : /* GetInt() */
155 : /************************************************************************/
156 :
157 1066 : int PCIDSKBuffer::GetInt( int offset, int size )
158 :
159 : {
160 1066 : std::string value_str;
161 :
162 1066 : if( offset + size > buffer_size )
163 0 : ThrowPCIDSKException( "GetInt() past end of PCIDSKBuffer." );
164 :
165 1066 : value_str.assign( buffer + offset, size );
166 :
167 1066 : return atoi(value_str.c_str());
168 : }
169 :
170 : /************************************************************************/
171 : /* GetDouble() */
172 : /************************************************************************/
173 :
174 1933 : double PCIDSKBuffer::GetDouble( int offset, int size )
175 :
176 : {
177 1933 : std::string value_str;
178 :
179 1933 : if( offset + size > buffer_size )
180 0 : ThrowPCIDSKException( "GetDouble() past end of PCIDSKBuffer." );
181 :
182 1933 : value_str.assign( buffer + offset, size );
183 :
184 : /* -------------------------------------------------------------------- */
185 : /* PCIDSK uses FORTRAN 'D' format for doubles - convert to 'E' */
186 : /* (C style) before calling atof. */
187 : /* -------------------------------------------------------------------- */
188 : int i;
189 :
190 52191 : for( i = 0; i < size; i++ )
191 : {
192 50258 : if( value_str[i] == 'D' )
193 1644 : value_str[i] = 'E';
194 : }
195 :
196 1933 : return atof(value_str.c_str());
197 : }
198 :
199 : /************************************************************************/
200 : /* Put() */
201 : /************************************************************************/
202 :
203 6260 : void PCIDSKBuffer::Put( const char *value, int offset, int size )
204 :
205 : {
206 6260 : if( offset + size > buffer_size )
207 0 : ThrowPCIDSKException( "Put() past end of PCIDSKBuffer." );
208 :
209 6260 : int v_size = strlen(value);
210 6260 : if( v_size > size )
211 0 : v_size = size;
212 :
213 6260 : if( v_size < size )
214 884 : memset( buffer + offset, ' ', size );
215 :
216 6260 : memcpy( buffer + offset, value, v_size );
217 6260 : }
218 :
219 : /************************************************************************/
220 : /* Put(uint64) */
221 : /************************************************************************/
222 :
223 1618 : void PCIDSKBuffer::Put( uint64 value, int offset, int size )
224 :
225 : {
226 : char fmt[64];
227 : char wrk[128];
228 :
229 1618 : sprintf( fmt, "%%%d%sd", size, PCIDSK_FRMT_64_WITHOUT_PREFIX );
230 1618 : sprintf( wrk, fmt, value );
231 :
232 1618 : Put( wrk, offset, size );
233 1618 : }
234 :
235 : /************************************************************************/
236 : /* Put(double) */
237 : /************************************************************************/
238 :
239 3348 : void PCIDSKBuffer::Put( double value, int offset, int size,
240 : const char *fmt )
241 :
242 : {
243 3348 : if( fmt == NULL )
244 0 : fmt = "%g";
245 :
246 : char wrk[128];
247 3348 : snprintf( wrk, 127, fmt, value );
248 :
249 3348 : char *exponent = strstr(wrk,"E");
250 3348 : if( exponent != NULL )
251 3076 : *exponent = 'D';
252 :
253 3348 : Put( wrk, offset, size );
254 3348 : }
255 :
256 : /************************************************************************/
257 : /* operator=() */
258 : /************************************************************************/
259 :
260 0 : PCIDSKBuffer &PCIDSKBuffer::operator=( const PCIDSKBuffer &src )
261 :
262 : {
263 0 : SetSize( src.buffer_size );
264 0 : memcpy( buffer, src.buffer, buffer_size );
265 :
266 0 : return *this;
267 : }
|