1 /******************************************************************************
2  * $Id: ogrfielddefn.cpp 14372 2008-04-30 04:53:13Z warmerdam $
3  *
4  * Project:  OpenGIS Simple Features Reference Implementation
5  * Purpose:  The OGRFieldDefn class implementation.
6  * Author:   Frank Warmerdam, warmerda@home.com
7  *
8  ******************************************************************************
9  * Copyright (c) 1999,  Les Technologies SoftMap Inc.
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a
12  * copy of this software and associated documentation files (the "Software"),
13  * to deal in the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  * and/or sell copies of the Software, and to permit persons to whom the
16  * Software is furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included
19  * in all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  ****************************************************************************/
29 
30 #include "ogr_feature.h"
31 #include "ogr_api.h"
32 #include "ogr_p.h"
33 
34 CPL_CVSID("$Id: ogrfielddefn.cpp 14372 2008-04-30 04:53:13Z warmerdam $");
35 
36 /************************************************************************/
37 /*                            OGRFieldDefn()                            */
38 /************************************************************************/
39 
40 /**
41  * Constructor.
42  *
43  * @param pszNameIn the name of the new field.
44  * @param eTypeIn the type of the new field.
45  */
46 
OGRFieldDefn(const char * pszNameIn,OGRFieldType eTypeIn)47 OGRFieldDefn::OGRFieldDefn( const char * pszNameIn, OGRFieldType eTypeIn )
48 
49 {
50     Initialize( pszNameIn, eTypeIn );
51 }
52 
53 /************************************************************************/
54 /*                            OGRFieldDefn()                            */
55 /************************************************************************/
56 
57 /**
58  * Constructor.
59  *
60  * Create by cloning an existing field definition.
61  *
62  * @param poPrototype the field definition to clone.
63  */
64 
OGRFieldDefn(OGRFieldDefn * poPrototype)65 OGRFieldDefn::OGRFieldDefn( OGRFieldDefn *poPrototype )
66 
67 {
68     Initialize( poPrototype->GetNameRef(), poPrototype->GetType() );
69 
70     SetJustify( poPrototype->GetJustify() );
71     SetWidth( poPrototype->GetWidth() );
72     SetPrecision( poPrototype->GetPrecision() );
73 //    SetDefault( poPrototype->GetDefaultRef() );
74 }
75 
76 /************************************************************************/
77 /*                           OGR_Fld_Create()                           */
78 /************************************************************************/
79 /**
80  * Create a new field definition.
81  *
82  * This function is the same as the CPP method OGRFieldDefn::OGRFieldDefn().
83  *
84  * @param pszName the name of the new field definition.
85  * @param eType the type of the new field definition.
86  * @return handle to the new field definition.
87  */
88 
OGR_Fld_Create(const char * pszName,OGRFieldType eType)89 OGRFieldDefnH OGR_Fld_Create( const char *pszName, OGRFieldType eType )
90 
91 {
92     return (OGRFieldDefnH) (new OGRFieldDefn(pszName,eType));
93 }
94 
95 /************************************************************************/
96 /*                             Initialize()                             */
97 /************************************************************************/
98 
Initialize(const char * pszNameIn,OGRFieldType eTypeIn)99 void OGRFieldDefn::Initialize( const char * pszNameIn, OGRFieldType eTypeIn )
100 
101 {
102     pszName = CPLStrdup( pszNameIn );
103     eType = eTypeIn;
104     eJustify = OJUndefined;
105 
106     nWidth = 0;         // should these be defined in some particular way
107     nPrecision = 0;     // for numbers?
108 
109     memset( &uDefault, 0, sizeof(OGRField) );
110 }
111 
112 /************************************************************************/
113 /*                           ~OGRFieldDefn()                            */
114 /************************************************************************/
115 
~OGRFieldDefn()116 OGRFieldDefn::~OGRFieldDefn()
117 
118 {
119     CPLFree( pszName );
120 }
121 
122 /************************************************************************/
123 /*                          OGR_Fld_Destroy()                           */
124 /************************************************************************/
125 /**
126  * Destroy a field definition.
127  *
128  * @param hDefn handle to the field definition to destroy.
129  */
130 
OGR_Fld_Destroy(OGRFieldDefnH hDefn)131 void OGR_Fld_Destroy( OGRFieldDefnH hDefn )
132 
133 {
134     delete (OGRFieldDefn *) hDefn;
135 }
136 
137 /************************************************************************/
138 /*                              SetName()                               */
139 /************************************************************************/
140 
141 /**
142  * Reset the name of this field.
143  *
144  * This method is the same as the C function OGR_Fld_SetName().
145  *
146  * @param pszNameIn the new name to apply.
147  */
148 
SetName(const char * pszNameIn)149 void OGRFieldDefn::SetName( const char * pszNameIn )
150 
151 {
152     CPLFree( pszName );
153     pszName = CPLStrdup( pszNameIn );
154 }
155 
156 /************************************************************************/
157 /*                          OGR_Fld_SetName()                           */
158 /************************************************************************/
159 /**
160  * Reset the name of this field.
161  *
162  * This function is the same as the CPP method OGRFieldDefn::SetName().
163  *
164  * @param hDefn handle to the field definition to apply the new name to.
165  * @param pszName the new name to apply.
166  */
167 
OGR_Fld_SetName(OGRFieldDefnH hDefn,const char * pszName)168 void OGR_Fld_SetName( OGRFieldDefnH hDefn, const char *pszName )
169 
170 {
171     ((OGRFieldDefn *) hDefn)->SetName( pszName );
172 }
173 
174 /************************************************************************/
175 /*                             GetNameRef()                             */
176 /************************************************************************/
177 
178 /**
179  * \fn const char *OGRFieldDefn::GetNameRef();
180  *
181  * Fetch name of this field.
182  *
183  * This method is the same as the C function OGR_Fld_GetNameRef().
184  *
185  * @return pointer to an internal name string that should not be freed or
186  * modified.
187  */
188 
189 /************************************************************************/
190 /*                         OGR_Fld_GetNameRef()                         */
191 /************************************************************************/
192 /**
193  * Fetch name of this field.
194  *
195  * This function is the same as the CPP method OGRFieldDefn::GetNameRef().
196  *
197  * @param hDefn handle to the field definition.
198  * @return the name of the field definition.
199  *
200  */
201 
OGR_Fld_GetNameRef(OGRFieldDefnH hDefn)202 const char *OGR_Fld_GetNameRef( OGRFieldDefnH hDefn )
203 
204 {
205     return ((OGRFieldDefn *) hDefn)->GetNameRef();
206 }
207 
208 /************************************************************************/
209 /*                              GetType()                               */
210 /************************************************************************/
211 
212 /**
213  * \fn OGRFieldType OGRFieldDefn::GetType();
214  *
215  * Fetch type of this field.
216  *
217  * This method is the same as the C function OGR_Fld_GetType().
218  *
219  * @return field type.
220  */
221 
222 /************************************************************************/
223 /*                          OGR_Fld_GetType()                           */
224 /************************************************************************/
225 /**
226  * Fetch type of this field.
227  *
228  * This function is the same as the CPP method OGRFieldDefn::GetType().
229  *
230  * @param hDefn handle to the field definition to get type from.
231  * @return field type.
232  */
233 
OGR_Fld_GetType(OGRFieldDefnH hDefn)234 OGRFieldType OGR_Fld_GetType( OGRFieldDefnH hDefn )
235 
236 {
237     return ((OGRFieldDefn *) hDefn)->GetType();
238 }
239 
240 /************************************************************************/
241 /*                              SetType()                               */
242 /************************************************************************/
243 
244 /**
245  * \fn void OGRFieldDefn::SetType( OGRFieldType eType );
246  *
247  * Set the type of this field.  This should never be done to an OGRFieldDefn
248  * that is already part of an OGRFeatureDefn.
249  *
250  * This method is the same as the C function OGR_Fld_SetType().
251  *
252  * @param eType the new field type.
253  */
254 
255 /************************************************************************/
256 /*                          OGR_Fld_SetType()                           */
257 /************************************************************************/
258 /**
259  * Set the type of this field.  This should never be done to an OGRFieldDefn
260  * that is already part of an OGRFeatureDefn.
261  *
262  * This function is the same as the CPP method OGRFieldDefn::SetType().
263  *
264  * @param hDefn handle to the field definition to set type to.
265  * @param eType the new field type.
266  */
267 
OGR_Fld_SetType(OGRFieldDefnH hDefn,OGRFieldType eType)268 void OGR_Fld_SetType( OGRFieldDefnH hDefn, OGRFieldType eType )
269 
270 {
271     ((OGRFieldDefn *) hDefn)->SetType( eType );
272 }
273 
274 /************************************************************************/
275 /*                             SetDefault()                             */
276 /************************************************************************/
277 
278 /**
279  * Set default field value.
280  *
281  * Currently use of OGRFieldDefn "defaults" is discouraged.  This feature
282  * may be fleshed out in the future.
283  *
284  */
285 
SetDefault(const OGRField * puDefaultIn)286 void OGRFieldDefn::SetDefault( const OGRField * puDefaultIn )
287 
288 {
289     switch( eType )
290     {
291       case OFTInteger:
292       case OFTReal:
293         uDefault = *puDefaultIn;
294         break;
295 
296       case OFTString:
297 //        CPLFree( uDefault.String );
298 //        uDefault.String = CPLStrdup( puDefaultIn->String );
299         break;
300 
301       default:
302         // add handling for other complex types.
303         CPLAssert( FALSE );
304         break;
305     }
306 }
307 
308 /************************************************************************/
309 /*                          GetFieldTypeName()                          */
310 /************************************************************************/
311 
312 /**
313  * Fetch human readable name for a field type.
314  *
315  * This static method is the same as the C function OGR_GetFieldTypeName().
316  *
317  * @param eType the field type to get name for.
318  *
319  * @return pointer to an internal static name string. It should not be
320  * modified or freed.
321  */
322 
GetFieldTypeName(OGRFieldType eType)323 const char * OGRFieldDefn::GetFieldTypeName( OGRFieldType eType )
324 
325 {
326     switch( eType )
327     {
328       case OFTInteger:
329         return "Integer";
330 
331       case OFTReal:
332         return "Real";
333 
334       case OFTString:
335         return "String";
336 
337       case OFTIntegerList:
338         return "IntegerList";
339 
340       case OFTRealList:
341         return "RealList";
342 
343       case OFTStringList:
344         return "StringList";
345 
346       case OFTBinary:
347         return "Binary";
348 
349       case OFTDate:
350         return "Date";
351 
352       case OFTTime:
353         return "Time";
354 
355       case OFTDateTime:
356         return "DateTime";
357 
358       default:
359         CPLAssert( FALSE );
360         return "(unknown)";
361     }
362 }
363 
364 /************************************************************************/
365 /*                        OGR_GetFieldTypeName()                        */
366 /************************************************************************/
367 /**
368  * Fetch human readable name for a field type.
369  *
370  * This function is the same as the CPP method
371  * OGRFieldDefn::GetFieldTypeName().
372  *
373  * @param eType the field type to get name for.
374  * @return the name.
375  */
376 
OGR_GetFieldTypeName(OGRFieldType eType)377 const char *OGR_GetFieldTypeName( OGRFieldType eType )
378 
379 {
380     return OGRFieldDefn::GetFieldTypeName( eType );
381 }
382 
383 /************************************************************************/
384 /*                             GetJustify()                             */
385 /************************************************************************/
386 
387 /**
388  * \fn OGRJustification OGRFieldDefn::GetJustify();
389  *
390  * Get the justification for this field.
391  *
392  * This method is the same as the C function OGR_Fld_GetJustify().
393  *
394  * @return the justification.
395  */
396 
397 /************************************************************************/
398 /*                         OGR_Fld_GetJustify()                         */
399 /************************************************************************/
400 /**
401  * Get the justification for this field.
402  *
403  * This function is the same as the CPP method OGRFieldDefn::GetJustify().
404  *
405  * @param hDefn handle to the field definition to get justification from.
406  * @return the justification.
407  */
408 
OGR_Fld_GetJustify(OGRFieldDefnH hDefn)409 OGRJustification OGR_Fld_GetJustify( OGRFieldDefnH hDefn )
410 
411 {
412     return ((OGRFieldDefn *) hDefn)->GetJustify();
413 }
414 
415 /************************************************************************/
416 /*                             SetJustify()                             */
417 /************************************************************************/
418 
419 /**
420  * \fn void OGRFieldDefn::SetJustify( OGRJustification eJustify );
421  *
422  * Set the justification for this field.
423  *
424  * This method is the same as the C function OGR_Fld_SetJustify().
425  *
426  * @param eJustify the new justification.
427  */
428 
429 /************************************************************************/
430 /*                         OGR_Fld_SetJustify()                         */
431 /************************************************************************/
432 /**
433  * Set the justification for this field.
434  *
435  * This function is the same as the CPP method OGRFieldDefn::SetJustify().
436  *
437  * @param hDefn handle to the field definition to set justification to.
438  * @param eJustify the new justification.
439  */
440 
OGR_Fld_SetJustify(OGRFieldDefnH hDefn,OGRJustification eJustify)441 void OGR_Fld_SetJustify( OGRFieldDefnH hDefn, OGRJustification eJustify )
442 
443 {
444     ((OGRFieldDefn *) hDefn)->SetJustify( eJustify );
445 }
446 
447 /************************************************************************/
448 /*                              GetWidth()                              */
449 /************************************************************************/
450 
451 /**
452  * \fn int OGRFieldDefn::GetWidth();
453  *
454  * Get the formatting width for this field.
455  *
456  * This method is the same as the C function OGR_Fld_GetWidth().
457  *
458  * @return the width, zero means no specified width.
459  */
460 
461 /************************************************************************/
462 /*                          OGR_Fld_GetWidth()                          */
463 /************************************************************************/
464 /**
465  * Get the formatting width for this field.
466  *
467  * This function is the same as the CPP method OGRFieldDefn::GetWidth().
468  *
469  * @param hDefn handle to the field definition to get width from.
470  * @return the width, zero means no specified width.
471  */
472 
OGR_Fld_GetWidth(OGRFieldDefnH hDefn)473 int OGR_Fld_GetWidth( OGRFieldDefnH hDefn )
474 
475 {
476     return ((OGRFieldDefn *) hDefn)->GetWidth();
477 }
478 
479 /************************************************************************/
480 /*                              SetWidth()                              */
481 /************************************************************************/
482 
483 /**
484  * \fn void OGRFieldDefn::SetWidth( int nWidth );
485  *
486  * Set the formatting width for this field in characters.
487  *
488  * This method is the same as the C function OGR_Fld_SetWidth().
489  *
490  * @param nWidth the new width.
491  */
492 
493 /************************************************************************/
494 /*                          OGR_Fld_SetWidth()                          */
495 /************************************************************************/
496 /**
497  * Set the formatting width for this field in characters.
498  *
499  * This function is the same as the CPP method OGRFieldDefn::SetWidth().
500  *
501  * @param hDefn handle to the field definition to set width to.
502  * @param nNewWidth the new width.
503  */
504 
OGR_Fld_SetWidth(OGRFieldDefnH hDefn,int nNewWidth)505 void OGR_Fld_SetWidth( OGRFieldDefnH hDefn, int nNewWidth )
506 
507 {
508     ((OGRFieldDefn *) hDefn)->SetWidth( nNewWidth );
509 }
510 
511 /************************************************************************/
512 /*                            GetPrecision()                            */
513 /************************************************************************/
514 
515 /**
516  * \fn int OGRFieldDefn::GetPrecision();
517  *
518  * Get the formatting precision for this field.  This should normally be
519  * zero for fields of types other than OFTReal.
520  *
521  * This method is the same as the C function OGR_Fld_GetPrecision().
522  *
523  * @return the precision.
524  */
525 
526 /************************************************************************/
527 /*                        OGR_Fld_GetPrecision()                        */
528 /************************************************************************/
529 /**
530  * Get the formatting precision for this field.  This should normally be
531  * zero for fields of types other than OFTReal.
532  *
533  * This function is the same as the CPP method OGRFieldDefn::GetPrecision().
534  *
535  * @param hDefn handle to the field definition to get precision from.
536  * @return the precision.
537  */
538 
OGR_Fld_GetPrecision(OGRFieldDefnH hDefn)539 int OGR_Fld_GetPrecision( OGRFieldDefnH hDefn )
540 
541 {
542     return ((OGRFieldDefn *) hDefn)->GetPrecision();
543 }
544 
545 /************************************************************************/
546 /*                            SetPrecision()                            */
547 /************************************************************************/
548 
549 /**
550  * \fn void OGRFieldDefn::SetPrecision( int nPrecision );
551  *
552  * Set the formatting precision for this field in characters.
553  *
554  * This should normally be zero for fields of types other than OFTReal.
555  *
556  * This method is the same as the C function OGR_Fld_SetPrecision().
557  *
558  * @param nPrecision the new precision.
559  */
560 
561 /************************************************************************/
562 /*                        OGR_Fld_SetPrecision()                        */
563 /************************************************************************/
564 /**
565  * Set the formatting precision for this field in characters.
566  *
567  * This should normally be zero for fields of types other than OFTReal.
568  *
569  * This function is the same as the CPP method OGRFieldDefn::SetPrecision().
570  *
571  * @param hDefn handle to the field definition to set precision to.
572  * @param nPrecision the new precision.
573  */
574 
OGR_Fld_SetPrecision(OGRFieldDefnH hDefn,int nPrecision)575 void OGR_Fld_SetPrecision( OGRFieldDefnH hDefn, int nPrecision )
576 
577 {
578     ((OGRFieldDefn *) hDefn)->SetPrecision( nPrecision );
579 }
580 
581 /************************************************************************/
582 /*                                Set()                                 */
583 /************************************************************************/
584 
585 /**
586  * Set defining parameters for a field in one call.
587  *
588  * This method is the same as the C function OGR_Fld_Set().
589  *
590  * @param pszNameIn the new name to assign.
591  * @param eTypeIn the new type (one of the OFT values like OFTInteger).
592  * @param nWidthIn the preferred formatting width.  Defaults to zero indicating
593  * undefined.
594  * @param nPrecisionIn number of decimals places for formatting, defaults to
595  * zero indicating undefined.
596  * @param eJustifyIn the formatting justification (OJLeft or OJRight), defaults
597  * to OJUndefined.
598  */
599 
Set(const char * pszNameIn,OGRFieldType eTypeIn,int nWidthIn,int nPrecisionIn,OGRJustification eJustifyIn)600 void OGRFieldDefn::Set( const char *pszNameIn,
601                         OGRFieldType eTypeIn,
602                         int nWidthIn, int nPrecisionIn,
603                         OGRJustification eJustifyIn )
604 {
605     SetName( pszNameIn );
606     SetType( eTypeIn );
607     SetWidth( nWidthIn );
608     SetPrecision( nPrecisionIn );
609     SetJustify( eJustifyIn );
610 }
611 
612 /************************************************************************/
613 /*                            OGR_Fld_Set()                             */
614 /************************************************************************/
615 /**
616  * Set defining parameters for a field in one call.
617  *
618  * This function is the same as the CPP method OGRFieldDefn::Set().
619  *
620  * @param hDefn handle to the field definition to set to.
621  * @param pszNameIn the new name to assign.
622  * @param eTypeIn the new type (one of the OFT values like OFTInteger).
623  * @param nWidthIn the preferred formatting width.  Defaults to zero indicating
624  * undefined.
625  * @param nPrecisionIn number of decimals places for formatting, defaults to
626  * zero indicating undefined.
627  * @param eJustifyIn the formatting justification (OJLeft or OJRight), defaults
628  * to OJUndefined.
629  */
630 
OGR_Fld_Set(OGRFieldDefnH hDefn,const char * pszNameIn,OGRFieldType eTypeIn,int nWidthIn,int nPrecisionIn,OGRJustification eJustifyIn)631 void OGR_Fld_Set( OGRFieldDefnH hDefn, const char *pszNameIn,
632                         OGRFieldType eTypeIn,
633                         int nWidthIn, int nPrecisionIn,
634                         OGRJustification eJustifyIn )
635 
636 {
637     ((OGRFieldDefn *) hDefn)->Set( pszNameIn, eTypeIn, nWidthIn,
638                                    nPrecisionIn, eJustifyIn );
639 }
640