1 : /******************************************************************************
2 : *
3 : * Purpose: Various private (undocumented) utility functions.
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 : #include "pcidsk_config.h"
28 : #include "pcidsk_types.h"
29 : #include "pcidsk_exception.h"
30 : #include "core/pcidsk_utils.h"
31 : #include <cstdlib>
32 : #include <cstring>
33 :
34 : using namespace PCIDSK;
35 :
36 : /************************************************************************/
37 : /* GetCurrentDateTime() */
38 : /************************************************************************/
39 :
40 : // format we want: "HH:MM DDMMMYYYY \0"
41 :
42 : #include <time.h>
43 : #include <sys/types.h>
44 :
45 86 : void PCIDSK::GetCurrentDateTime( char *out_time )
46 :
47 : {
48 : time_t clock;
49 : char ctime_out[25];
50 :
51 86 : time( &clock );
52 86 : strncpy( ctime_out, ctime(&clock), 24 ); // TODO: reentrance issue?
53 :
54 : // ctime() products: "Wed Jun 30 21:49:08 1993\n"
55 :
56 86 : ctime_out[24] = '\0';
57 :
58 86 : out_time[0] = ctime_out[11];
59 86 : out_time[1] = ctime_out[12];
60 86 : out_time[2] = ':';
61 86 : out_time[3] = ctime_out[14];
62 86 : out_time[4] = ctime_out[15];
63 86 : out_time[5] = ' ';
64 86 : out_time[6] = ctime_out[8];
65 86 : out_time[7] = ctime_out[9];
66 86 : out_time[8] = ctime_out[4];
67 86 : out_time[9] = ctime_out[5];
68 86 : out_time[10] = ctime_out[6];
69 86 : out_time[11] = ctime_out[20];
70 86 : out_time[12] = ctime_out[21];
71 86 : out_time[13] = ctime_out[22];
72 86 : out_time[14] = ctime_out[23];
73 86 : out_time[15] = ' ';
74 86 : out_time[16] = '\0';
75 86 : }
76 :
77 : /************************************************************************/
78 : /* UCaseStr() */
79 : /* */
80 : /* Force a string into upper case "in place". */
81 : /************************************************************************/
82 :
83 34 : std::string &PCIDSK::UCaseStr( std::string &target )
84 :
85 : {
86 170 : for( unsigned int i = 0; i < target.size(); i++ )
87 : {
88 136 : if( islower(target[i]) )
89 0 : target[i] = toupper(target[i]);
90 : }
91 :
92 34 : return target;
93 : }
94 :
95 : /************************************************************************/
96 : /* atouint64() */
97 : /************************************************************************/
98 :
99 558 : uint64 PCIDSK::atouint64( const char *str_value )
100 :
101 : {
102 : #if defined(__MSVCRT__) || defined(_MSC_VER)
103 : return (uint64) _atoi64( str_value );
104 : #else
105 558 : return (uint64) atoll( str_value );
106 : #endif
107 : }
108 :
109 : /************************************************************************/
110 : /* atoint64() */
111 : /************************************************************************/
112 :
113 0 : int64 PCIDSK::atoint64( const char *str_value )
114 :
115 : {
116 : #if defined(__MSVCRT__) || defined(_MSC_VER)
117 : return (int64) _atoi64( str_value );
118 : #else
119 0 : return (int64) atoll( str_value );
120 : #endif
121 : }
122 :
123 : /************************************************************************/
124 : /* SwapData() */
125 : /************************************************************************/
126 :
127 240 : void PCIDSK::SwapData( void *data, int size, int count )
128 :
129 : {
130 240 : uint8 *data8 = (uint8 *) data;
131 :
132 240 : if( size == 2 )
133 : {
134 : uint8 t;
135 :
136 4220 : for( ; count; count-- )
137 : {
138 4000 : t = data8[0];
139 4000 : data8[0] = data8[1];
140 4000 : data8[1] = t;
141 :
142 4000 : data8 += 2;
143 : }
144 : }
145 20 : else if( size == 1 )
146 : /* do nothing */;
147 20 : else if( size == 4 )
148 : {
149 : uint8 t;
150 :
151 220 : for( ; count; count-- )
152 : {
153 200 : t = data8[0];
154 200 : data8[0] = data8[3];
155 200 : data8[3] = t;
156 :
157 200 : t = data8[1];
158 200 : data8[1] = data8[2];
159 200 : data8[2] = t;
160 :
161 200 : data8 += 4;
162 : }
163 : }
164 0 : else if( size == 8 )
165 : {
166 : uint8 t;
167 :
168 0 : for( ; count; count-- )
169 : {
170 0 : t = data8[0];
171 0 : data8[0] = data8[7];
172 0 : data8[7] = t;
173 :
174 0 : t = data8[1];
175 0 : data8[1] = data8[6];
176 0 : data8[6] = t;
177 :
178 0 : t = data8[2];
179 0 : data8[2] = data8[5];
180 0 : data8[5] = t;
181 :
182 0 : t = data8[3];
183 0 : data8[3] = data8[4];
184 0 : data8[4] = t;
185 :
186 0 : data8 += 8;
187 : }
188 : }
189 : else
190 0 : ThrowPCIDSKException( "Unsupported data size in SwapData()" );
191 240 : }
192 :
193 : /************************************************************************/
194 : /* BigEndianSystem() */
195 : /************************************************************************/
196 :
197 0 : bool PCIDSK::BigEndianSystem()
198 :
199 : {
200 0 : unsigned short test_value = 1;
201 : char test_char_value[2];
202 :
203 0 : memcpy( test_char_value, &test_value, 2 );
204 :
205 0 : return test_char_value[0] == 0;
206 : }
207 :
208 :
209 : /************************************************************************/
210 : /* ParseTileFormat() */
211 : /* */
212 : /* Parse blocksize and compression out of a TILED interleaving */
213 : /* string as passed to the Create() function or stored in */
214 : /* _DBLayout metadata. */
215 : /************************************************************************/
216 :
217 0 : void PCIDSK::ParseTileFormat( std::string full_text,
218 : int &block_size, std::string &compression )
219 :
220 : {
221 0 : compression = "NONE";
222 0 : block_size = 127;
223 :
224 0 : UCaseStr( full_text );
225 :
226 : /* -------------------------------------------------------------------- */
227 : /* Only operate on tiled stuff. */
228 : /* -------------------------------------------------------------------- */
229 0 : if( strncmp(full_text.c_str(),"TILED",5) != 0 )
230 0 : return;
231 :
232 : /* -------------------------------------------------------------------- */
233 : /* Do we have a block size? */
234 : /* -------------------------------------------------------------------- */
235 0 : const char *next_text = full_text.c_str() + 5;
236 :
237 0 : if( isdigit(*next_text) )
238 : {
239 0 : block_size = atoi(next_text);
240 0 : while( isdigit(*next_text) )
241 0 : next_text++;
242 : }
243 :
244 0 : while( *next_text == ' ' )
245 0 : next_text++;
246 :
247 : /* -------------------------------------------------------------------- */
248 : /* Do we have a compression type? */
249 : /* -------------------------------------------------------------------- */
250 0 : if( *next_text != '\0' )
251 : {
252 0 : compression = next_text;
253 0 : if( compression != "RLE"
254 : && strncmp(compression.c_str(),"JPEG",4) != 0
255 : && compression != "NONE"
256 : && compression != "QUADTREE" )
257 : {
258 : ThrowPCIDSKException( "Unsupported tile compression scheme '%s' requested.",
259 0 : compression.c_str() );
260 : }
261 : }
262 : }
263 :
264 : /************************************************************************/
265 : /* pci_strcasecmp() */
266 : /************************************************************************/
267 :
268 0 : int PCIDSK::pci_strcasecmp( const char *string1, const char *string2 )
269 :
270 : {
271 : int i;
272 :
273 0 : for( i = 0; string1[i] != '\0' && string2[i] != '\0'; i++ )
274 : {
275 0 : char c1 = string1[i];
276 0 : char c2 = string2[i];
277 :
278 0 : if( islower(c1) )
279 0 : c1 = toupper(c1);
280 0 : if( islower(c2) )
281 0 : c2 = toupper(c2);
282 :
283 0 : if( c1 < c2 )
284 0 : return -1;
285 0 : else if( c1 > c2 )
286 0 : return 1;
287 : else
288 0 : return 0;
289 : }
290 :
291 0 : if( string1[i] == '\0' && string2[i] == '\0' )
292 0 : return 0;
293 0 : else if( string1[i] == '\0' )
294 0 : return 1;
295 : else
296 0 : return -1;
297 : }
298 :
299 : /************************************************************************/
300 : /* pci_strncasecmp() */
301 : /************************************************************************/
302 :
303 859 : int PCIDSK::pci_strncasecmp( const char *string1, const char *string2, int len )
304 :
305 : {
306 : int i;
307 :
308 1687 : for( i = 0; i < len; i++ )
309 : {
310 1456 : if( string1[i] == '\0' && string2[i] == '\0' )
311 0 : return 0;
312 1456 : else if( string1[i] == '\0' )
313 0 : return 1;
314 1456 : else if( string2[i] == '\0' )
315 0 : return -1;
316 :
317 1456 : char c1 = string1[i];
318 1456 : char c2 = string2[i];
319 :
320 1456 : if( islower(c1) )
321 0 : c1 = toupper(c1);
322 1456 : if( islower(c2) )
323 0 : c2 = toupper(c2);
324 :
325 1456 : if( c1 < c2 )
326 370 : return -1;
327 1086 : else if( c1 > c2 )
328 258 : return 1;
329 : }
330 :
331 231 : return 0;
332 : }
333 :
|