forked from ossimlabs/ossim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ossimDdfsubfielddefn.cpp
997 lines (854 loc) · 31.4 KB
/
ossimDdfsubfielddefn.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
/******************************************************************************
* Copied from "gdal" project. See licence below.
*
* Project: ISO 8211 Access
* Purpose: Implements the DDFSubfieldDefn class.
* Author: Frank Warmerdam, [email protected]
*
******************************************************************************
* Copyright (c) 1999, Frank Warmerdam
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
******************************************************************************
* $Id: ossimDdfsubfielddefn.cpp 23245 2015-04-08 20:53:04Z rashadkm $
*/
#include <cstring>
#include <ossim/imaging/ossimIso8211.h>
#include <ossim/base/ossimNotifyContext.h>
#include <ossim/base/ossimCommon.h>
/************************************************************************/
/* DDFSubfieldDefn() */
/************************************************************************/
ossimDDFSubfieldDefn::ossimDDFSubfieldDefn()
{
pszName = NULL;
bIsVariable = true;
nFormatWidth = 0;
chFormatDelimeter = OSSIM_DDF_UNIT_TERMINATOR;
eBinaryFormat = NotBinary;
eType = DDFString;
pszFormatString = strdup("");
nMaxBufChars = 0;
pachBuffer = NULL;
}
/************************************************************************/
/* ~DDFSubfieldDefn() */
/************************************************************************/
ossimDDFSubfieldDefn::~ossimDDFSubfieldDefn()
{
free( pszName );
free( pszFormatString );
free( pachBuffer );
}
/************************************************************************/
/* SetName() */
/************************************************************************/
void ossimDDFSubfieldDefn::SetName( const char * pszNewName )
{
int i;
free( pszName );
pszName = strdup( pszNewName );
for( i = (int)strlen(pszName)-1; i > 0 && pszName[i] == ' '; i-- )
pszName[i] = '\0';
}
/************************************************************************/
/* SetFormat() */
/* */
/* While interpreting the format string we don't support: */
/* */
/* o Passing an explicit terminator for variable length field. */
/* o 'X' for unused data ... this should really be filtered */
/* out by DDFFieldDefn::ApplyFormats(), but isn't. */
/* o 'B' bitstrings that aren't a multiple of eight. */
/************************************************************************/
int ossimDDFSubfieldDefn::SetFormat( const char * pszFormat )
{
free( pszFormatString );
pszFormatString = strdup( pszFormat );
/* -------------------------------------------------------------------- */
/* These values will likely be used. */
/* -------------------------------------------------------------------- */
if( pszFormatString[1] == '(' )
{
nFormatWidth = atoi(pszFormatString+2);
bIsVariable = nFormatWidth == 0;
}
else
bIsVariable = true;
/* -------------------------------------------------------------------- */
/* Interpret the format string. */
/* -------------------------------------------------------------------- */
switch( pszFormatString[0] )
{
case 'A':
case 'C': // It isn't clear to me how this is different than 'A'
eType = DDFString;
break;
case 'R':
eType = DDFFloat;
break;
case 'I':
case 'S':
eType = DDFInt;
break;
case 'B':
case 'b':
// Is the width expressed in bits? (is it a bitstring)
bIsVariable = false;
if( pszFormatString[1] == '(' )
{
// CPLAssert( atoi(pszFormatString+2) % 8 == 0 );
nFormatWidth = atoi(pszFormatString+2) / 8;
eBinaryFormat = SInt; // good default, works for SDTS.
if( nFormatWidth < 5 )
eType = DDFInt;
else
eType = DDFBinaryString;
}
// or do we have a binary type indicator? (is it binary)
else
{
eBinaryFormat = (DDFBinaryFormat) (pszFormatString[1] - '0');
nFormatWidth = atoi(pszFormatString+2);
if( eBinaryFormat == SInt || eBinaryFormat == UInt )
eType = DDFInt;
else
eType = DDFFloat;
}
break;
case 'X':
// 'X' is extra space, and shouldn't be directly assigned to a
// subfield ... I haven't encountered it in use yet though.
ossimNotify(ossimNotifyLevel_WARN)
<< "Format type of `%c' not supported.\n"
<< pszFormatString[0] << std::endl;
// CPLAssert( false );
return false;
default:
ossimNotify(ossimNotifyLevel_WARN)
<< "Format type of `%c' not recognised.\n"
<< pszFormatString[0] << std::endl;
// CPLAssert( false );
return false;
}
return true;
}
/************************************************************************/
/* Dump() */
/************************************************************************/
/**
* Write out subfield definition info to debugging file.
*
* A variety of information about this field definition is written to the
* give debugging file handle.
*
* @param fp The standard io file handle to write to. ie. stderr
*/
void ossimDDFSubfieldDefn::Dump( FILE * fp )
{
fprintf( fp, " DDFSubfieldDefn:\n" );
fprintf( fp, " Label = `%s'\n", pszName );
fprintf( fp, " FormatString = `%s'\n", pszFormatString );
}
/************************************************************************/
/* GetDataLength() */
/* */
/* This method will scan for the end of a variable field. */
/************************************************************************/
/**
* Scan for the end of variable length data. Given a pointer to the data
* for this subfield (from within a DDFRecord) this method will return the
* number of bytes which are data for this subfield. The number of bytes
* consumed as part of this field can also be fetched. This number may
* be one longer than the length if there is a terminator character
* used.<p>
*
* This method is mainly for internal use, or for applications which
* want the raw binary data to interpret themselves. Otherwise use one
* of ExtractStringData(), ExtractIntData() or ExtractFloatData().
*
* @param pachSourceData The pointer to the raw data for this field. This
* may have come from DDFRecord::GetData(), taking into account skip factors
* over previous subfields data.
* @param nMaxBytes The maximum number of bytes that are accessable after
* pachSourceData.
* @param pnConsumedBytes Pointer to an integer into which the number of
* bytes consumed by this field should be written. May be NULL to ignore.
*
* @return The number of bytes at pachSourceData which are actual data for
* this record (not including unit, or field terminator).
*/
int ossimDDFSubfieldDefn::GetDataLength( const char * pachSourceData,
int nMaxBytes, int * pnConsumedBytes )
{
if( !bIsVariable )
{
if( nFormatWidth > nMaxBytes )
{
ossimNotify(ossimNotifyLevel_WARN)
<< "Only %d bytes available for subfield %s with\n"
<< "format string %s ... returning shortened data."
<< nMaxBytes << pszName << pszFormatString << std::endl;
if( pnConsumedBytes != NULL )
*pnConsumedBytes = nMaxBytes;
return nMaxBytes;
}
else
{
if( pnConsumedBytes != NULL )
*pnConsumedBytes = nFormatWidth;
return nFormatWidth;
}
}
else
{
int nLength = 0;
int bCheckFieldTerminator = true;
/* We only check for the field terminator because of some buggy
* datasets with missing format terminators. However, we have found
* the field terminator is a legal character within the fields of
* some extended datasets (such as JP34NC94.000). So we don't check
* for the field terminator if the field appears to be multi-byte
* which we established by the first character being out of the
* ASCII printable range (32-127).
*/
if( pachSourceData[0] < 32 || pachSourceData[0] >= 127 )
bCheckFieldTerminator = false;
while( nLength < nMaxBytes
&& pachSourceData[nLength] != chFormatDelimeter )
{
if( bCheckFieldTerminator
&& pachSourceData[nLength] == OSSIM_DDF_FIELD_TERMINATOR )
break;
nLength++;
}
if( pnConsumedBytes != NULL )
{
if( nMaxBytes == 0 )
*pnConsumedBytes = nLength;
else
*pnConsumedBytes = nLength+1;
}
return nLength;
}
}
/************************************************************************/
/* ExtractStringData() */
/************************************************************************/
/**
* Extract a zero terminated string containing the data for this subfield.
* Given a pointer to the data
* for this subfield (from within a DDFRecord) this method will return the
* data for this subfield. The number of bytes
* consumed as part of this field can also be fetched. This number may
* be one longer than the string length if there is a terminator character
* used.<p>
*
* This function will return the raw binary data of a subfield for
* types other than DDFString, including data past zero chars. This is
* the standard way of extracting DDFBinaryString subfields for instance.<p>
*
* @param pachSourceData The pointer to the raw data for this field. This
* may have come from DDFRecord::GetData(), taking into account skip factors
* over previous subfields data.
* @param nMaxBytes The maximum number of bytes that are accessable after
* pachSourceData.
* @param pnConsumedBytes Pointer to an integer into which the number of
* bytes consumed by this field should be written. May be NULL to ignore.
* This is used as a skip factor to increment pachSourceData to point to the
* next subfields data.
*
* @return A pointer to a buffer containing the data for this field. The
* returned pointer is to an internal buffer which is invalidated on the
* next ExtractStringData() call on this DDFSubfieldDefn(). It should not
* be freed by the application.
*
* @see ExtractIntData(), ExtractFloatData()
*/
const char *
ossimDDFSubfieldDefn::ExtractStringData( const char * pachSourceData,
int nMaxBytes, int * pnConsumedBytes )
{
int nLength = GetDataLength( pachSourceData, nMaxBytes,
pnConsumedBytes );
/* -------------------------------------------------------------------- */
/* Do we need to grow the buffer. */
/* -------------------------------------------------------------------- */
if( nMaxBufChars < nLength+1 )
{
free( pachBuffer );
nMaxBufChars = nLength+1;
pachBuffer = (char *) malloc(nMaxBufChars);
}
/* -------------------------------------------------------------------- */
/* Copy the data to the buffer. We use memcpy() so that it */
/* will work for binary data. */
/* -------------------------------------------------------------------- */
memcpy( pachBuffer, pachSourceData, nLength );
pachBuffer[nLength] = '\0';
return pachBuffer;
}
/************************************************************************/
/* ExtractFloatData() */
/************************************************************************/
/**
* Extract a subfield value as a float. Given a pointer to the data
* for this subfield (from within a DDFRecord) this method will return the
* floating point data for this subfield. The number of bytes
* consumed as part of this field can also be fetched. This method may be
* called for any type of subfield, and will return zero if the subfield is
* not numeric.
*
* @param pachSourceData The pointer to the raw data for this field. This
* may have come from DDFRecord::GetData(), taking into account skip factors
* over previous subfields data.
* @param nMaxBytes The maximum number of bytes that are accessable after
* pachSourceData.
* @param pnConsumedBytes Pointer to an integer into which the number of
* bytes consumed by this field should be written. May be NULL to ignore.
* This is used as a skip factor to increment pachSourceData to point to the
* next subfields data.
*
* @return The subfield's numeric value (or zero if it isn't numeric).
*
* @see ExtractIntData(), ExtractStringData()
*/
double
ossimDDFSubfieldDefn::ExtractFloatData( const char * pachSourceData,
int nMaxBytes, int * pnConsumedBytes )
{
switch( pszFormatString[0] )
{
case 'A':
case 'I':
case 'R':
case 'S':
case 'C':
return atof(ExtractStringData(pachSourceData, nMaxBytes,
pnConsumedBytes));
case 'B':
case 'b':
{
unsigned char abyData[8];
// CPLAssert( nFormatWidth <= nMaxBytes );
if( pnConsumedBytes != NULL )
*pnConsumedBytes = nFormatWidth;
// Byte swap the data if it isn't in machine native format.
// In any event we copy it into our buffer to ensure it is
// word aligned.
#ifdef CPL_LSB
if( pszFormatString[0] == 'B' )
#else
if( pszFormatString[0] == 'b' )
#endif
{
for( int i = 0; i < nFormatWidth; i++ )
abyData[nFormatWidth-i-1] = pachSourceData[i];
}
else
{
memcpy( abyData, pachSourceData, nFormatWidth );
}
// Interpret the bytes of data.
switch( eBinaryFormat )
{
case UInt:
if( nFormatWidth == 1 )
{
return( abyData[0] );
}
else if( nFormatWidth == 2 )
{
ossim_uint16* ptr = (ossim_uint16*) abyData;
return *ptr;
}
else if( nFormatWidth == 4 )
{
ossim_uint32* ptr = (ossim_uint32*) abyData;
return *ptr;
}
else
{
// CPLAssert( false );
return 0.0;
}
case SInt:
if( nFormatWidth == 1 )
{
signed char* ptr = (signed char*) abyData;
return *ptr;
}
else if( nFormatWidth == 2 )
{
ossim_int16* ptr = (ossim_int16*) abyData;
return *ptr;
}
else if( nFormatWidth == 4 )
{
ossim_int32* ptr = (ossim_int32*) abyData;
return *ptr;
}
else
{
// CPLAssert( false );
return 0.0;
}
case FloatReal:
if( nFormatWidth == 4 )
{
float* ptr = (float*) abyData;
return *ptr;
}
else if( nFormatWidth == 8 )
{
double* ptr = (double*) abyData;
return *ptr;
}
else
{
// CPLAssert( false );
return 0.0;
}
case NotBinary:
case FPReal:
case FloatComplex:
// CPLAssert( false );
return 0.0;
}
break;
// end of 'b'/'B' case.
}
default:
// CPLAssert( false );
return 0.0;
}
// CPLAssert( false );
return 0.0;
}
/************************************************************************/
/* ExtractIntData() */
/************************************************************************/
/**
* Extract a subfield value as an integer. Given a pointer to the data
* for this subfield (from within a DDFRecord) this method will return the
* int data for this subfield. The number of bytes
* consumed as part of this field can also be fetched. This method may be
* called for any type of subfield, and will return zero if the subfield is
* not numeric.
*
* @param pachSourceData The pointer to the raw data for this field. This
* may have come from DDFRecord::GetData(), taking into account skip factors
* over previous subfields data.
* @param nMaxBytes The maximum number of bytes that are accessable after
* pachSourceData.
* @param pnConsumedBytes Pointer to an integer into which the number of
* bytes consumed by this field should be written. May be NULL to ignore.
* This is used as a skip factor to increment pachSourceData to point to the
* next subfields data.
*
* @return The subfield's numeric value (or zero if it isn't numeric).
*
* @see ExtractFloatData(), ExtractStringData()
*/
int
ossimDDFSubfieldDefn::ExtractIntData( const char * pachSourceData,
int nMaxBytes, int * pnConsumedBytes )
{
switch( pszFormatString[0] )
{
case 'A':
case 'I':
case 'R':
case 'S':
case 'C':
return atoi(ExtractStringData(pachSourceData, nMaxBytes,
pnConsumedBytes));
case 'B':
case 'b':
{
unsigned char abyData[8];
if( nFormatWidth > nMaxBytes )
{
ossimNotify(ossimNotifyLevel_WARN)
<< "Attempt to extract int subfield %s with format %s\n"
<< "failed as only %d bytes available. Using zero."
<< pszName << pszFormatString << nMaxBytes << std::endl;
return 0;
}
if( pnConsumedBytes != NULL )
*pnConsumedBytes = nFormatWidth;
// Byte swap the data if it isn't in machine native format.
// In any event we copy it into our buffer to ensure it is
// word aligned.
#ifdef CPL_LSB
if( pszFormatString[0] == 'B' )
#else
if( pszFormatString[0] == 'b' )
#endif
{
for( int i = 0; i < nFormatWidth; i++ )
abyData[nFormatWidth-i-1] = pachSourceData[i];
}
else
{
memcpy( abyData, pachSourceData, nFormatWidth );
}
// Interpret the bytes of data.
switch( eBinaryFormat )
{
case UInt:
if( nFormatWidth == 4 )
{
ossim_uint32* ptr = (ossim_uint32*) abyData;
return *ptr;
}
else if( nFormatWidth == 1 )
{
return( abyData[0] );
}
else if( nFormatWidth == 2 )
{
ossim_uint16* ptr = (ossim_uint16*)abyData;
return *ptr;
}
else
{
// CPLAssert( false );
return 0;
}
case SInt:
if( nFormatWidth == 4 )
{
ossim_int32* ptr = (ossim_int32 *) abyData;
return *ptr;
}
else if( nFormatWidth == 1 )
{
signed char* ptr = (signed char *) abyData;
return *ptr;
}
else if( nFormatWidth == 2 )
{
ossim_int16* ptr = (ossim_int16 *) abyData;
return *ptr;
}
else
{
// CPLAssert( false );
return 0;
}
case FloatReal:
if( nFormatWidth == 4 )
{
float* ptr = (float *) abyData;
return (int) *ptr;
}
else if( nFormatWidth == 8 )
{
double* ptr = (double *) abyData;
return (int) *ptr;
}
else
{
// CPLAssert( false );
return 0;
}
case NotBinary:
case FPReal:
case FloatComplex:
// CPLAssert( false );
return 0;
}
break;
// end of 'b'/'B' case.
}
default:
// CPLAssert( false );
return 0;
}
// CPLAssert( false );
return 0;
}
/************************************************************************/
/* DumpData() */
/* */
/* Dump the instance data for this subfield from a data */
/* record. This fits into the output dump stream of a DDFField. */
/************************************************************************/
/**
* Dump subfield value to debugging file.
*
* @param pachData Pointer to data for this subfield.
* @param nMaxBytes Maximum number of bytes available in pachData.
* @param fp File to write report to.
*/
void ossimDDFSubfieldDefn::DumpData( const char * pachData, int nMaxBytes,
FILE * fp )
{
if( eType == DDFFloat )
fprintf( fp, " Subfield `%s' = %f\n",
pszName,
ExtractFloatData( pachData, nMaxBytes, NULL ) );
else if( eType == DDFInt )
fprintf( fp, " Subfield `%s' = %d\n",
pszName,
ExtractIntData( pachData, nMaxBytes, NULL ) );
else if( eType == DDFBinaryString )
{
int nBytes, i;
ossim_uint8 *pabyBString = (ossim_uint8 *) ExtractStringData( pachData, nMaxBytes, &nBytes );
fprintf( fp, " Subfield `%s' = 0x", pszName );
for( i = 0; i < std::min(nBytes,24); i++ )
fprintf( fp, "%02X", pabyBString[i] );
if( nBytes > 24 )
fprintf( fp, "%s", "..." );
fprintf( fp, "\n" );
}
else
fprintf( fp, " Subfield `%s' = `%s'\n",
pszName,
ExtractStringData( pachData, nMaxBytes, NULL ) );
}
/************************************************************************/
/* GetDefaultValue() */
/************************************************************************/
/**
* Get default data.
*
* Returns the default subfield data contents for this subfield definition.
* For variable length numbers this will normally be "0<unit-terminator>".
* For variable length strings it will be "<unit-terminator>". For fixed
* length numbers it is zero filled. For fixed length strings it is space
* filled. For binary numbers it is binary zero filled.
*
* @param pachData the buffer into which the returned default will be placed.
* May be NULL if just querying default size.
* @param nBytesAvailable the size of pachData in bytes.
* @param pnBytesUsed will receive the size of the subfield default data in
* bytes.
*
* @return true on success or false on failure or if the passed buffer is too
* small to hold the default.
*/
int ossimDDFSubfieldDefn::GetDefaultValue( char *pachData, int nBytesAvailable,
int *pnBytesUsed )
{
int nDefaultSize;
if( !bIsVariable )
nDefaultSize = nFormatWidth;
else
nDefaultSize = 1;
if( pnBytesUsed != NULL )
*pnBytesUsed = nDefaultSize;
if( pachData == NULL )
return true;
if( nBytesAvailable < nDefaultSize )
return false;
if( bIsVariable )
{
pachData[0] = OSSIM_DDF_UNIT_TERMINATOR;
}
else
{
if( GetBinaryFormat() == NotBinary )
{
if( GetType() == DDFInt || GetType() == DDFFloat )
memset( pachData, 0, nDefaultSize );
else
memset( pachData, ' ', nDefaultSize );
}
else
memset( pachData, 0, nDefaultSize );
}
return true;
}
/************************************************************************/
/* FormatStringValue() */
/************************************************************************/
/**
* Format string subfield value.
*
* Returns a buffer with the passed in string value reformatted in a way
* suitable for storage in a DDFField for this subfield.
*/
int ossimDDFSubfieldDefn::FormatStringValue( char *pachData, int nBytesAvailable,
int *pnBytesUsed,
const char *pszValue,
int nValueLength )
{
int nSize;
if( nValueLength == -1 )
nValueLength = (int)strlen(pszValue);
if( bIsVariable )
{
nSize = nValueLength + 1;
}
else
{
nSize = nFormatWidth;
}
if( pnBytesUsed != NULL )
*pnBytesUsed = nSize;
if( pachData == NULL )
return true;
if( nBytesAvailable < nSize )
return false;
if( bIsVariable )
{
strncpy( pachData, pszValue, nSize-1 );
pachData[nSize-1] = OSSIM_DDF_UNIT_TERMINATOR;
}
else
{
if( GetBinaryFormat() == NotBinary )
{
memset( pachData, ' ', nSize );
memcpy( pachData, pszValue, std::min(nValueLength,nSize) );
}
else
{
memset( pachData, 0, nSize );
memcpy( pachData, pszValue, std::min(nValueLength,nSize) );
}
}
return true;
}
/************************************************************************/
/* FormatIntValue() */
/************************************************************************/
/**
* Format int subfield value.
*
* Returns a buffer with the passed in int value reformatted in a way
* suitable for storage in a DDFField for this subfield.
*/
int ossimDDFSubfieldDefn::FormatIntValue( char *pachData, int nBytesAvailable,
int *pnBytesUsed, int nNewValue )
{
int nSize;
char szWork[30];
sprintf( szWork, "%d", nNewValue );
if( bIsVariable )
{
nSize = (int)strlen(szWork) + 1;
}
else
{
nSize = nFormatWidth;
if( GetBinaryFormat() == NotBinary && (int) strlen(szWork) > nSize )
return false;
}
if( pnBytesUsed != NULL )
*pnBytesUsed = nSize;
if( pachData == NULL )
return true;
if( nBytesAvailable < nSize )
return false;
if( bIsVariable )
{
strncpy( pachData, szWork, nSize-1 );
pachData[nSize-1] = OSSIM_DDF_UNIT_TERMINATOR;
}
else
{
ossim_uint32 nMask = 0xff;
int i;
switch( GetBinaryFormat() )
{
case NotBinary:
memset( pachData, '0', nSize );
strncpy( pachData + nSize - strlen(szWork), szWork,
strlen(szWork) );
break;
case UInt:
case SInt:
for( i = 0; i < nFormatWidth; i++ )
{
int iOut;
// big endian required?
if( pszFormatString[0] == 'B' )
iOut = nFormatWidth - i - 1;
else
iOut = i;
pachData[iOut] = (nNewValue & nMask) >> (i*8);
nMask *= 256;
}
break;
case FloatReal:
// CPLAssert( false );
break;
default:
// CPLAssert( false );
break;
}
}
return true;
}
/************************************************************************/
/* FormatFloatValue() */
/************************************************************************/
/**
* Format float subfield value.
*
* Returns a buffer with the passed in float value reformatted in a way
* suitable for storage in a DDFField for this subfield.
*/
int ossimDDFSubfieldDefn::FormatFloatValue( char *pachData, int nBytesAvailable,
int *pnBytesUsed, double dfNewValue )
{
int nSize;
char szWork[120];
sprintf( szWork, "%.16g", dfNewValue );
if( bIsVariable )
{
nSize = (int)strlen(szWork) + 1;
}
else
{
nSize = nFormatWidth;
if( GetBinaryFormat() == NotBinary && (int) strlen(szWork) > nSize )
return false;
}
if( pnBytesUsed != NULL )
*pnBytesUsed = nSize;
if( pachData == NULL )
return true;
if( nBytesAvailable < nSize )
return false;
if( bIsVariable )
{
strncpy( pachData, szWork, nSize-1 );
pachData[nSize-1] = OSSIM_DDF_UNIT_TERMINATOR;
}
else
{
if( GetBinaryFormat() == NotBinary )
{
memset( pachData, '0', nSize );
strncpy( pachData + nSize - strlen(szWork), szWork,
strlen(szWork) );
}
else
{
// CPLAssert( false );
/* implement me */
}
}
return true;
}