1 #include <map>
2 #include "ogr_sosi.h"
3 
4 CPL_CVSID("$Id: ogrsosidatatypes.cpp 1a8cd1b07a75b672150242ef8a59b1821e82a137 2018-05-12 22:35:40 +0200 Even Rouault $")
5 
6 C2F* poTypes = nullptr;
7 
8 /*** class definitions ***/
9 
OGRSOSIDataType(int nSize)10 OGRSOSIDataType::OGRSOSIDataType(int nSize) {
11     poElements = new OGRSOSISimpleDataType[nSize];
12     nElementCount = nSize;
13 }
~OGRSOSIDataType()14 OGRSOSIDataType::~OGRSOSIDataType() {
15     delete[] poElements;
16 }
setElement(int nIndex,const char * name,OGRFieldType type)17 void OGRSOSIDataType::setElement(int nIndex, const char *name, OGRFieldType type) {
18     poElements[nIndex].setType(name, type);
19 }
20 
OGRSOSISimpleDataType()21 OGRSOSISimpleDataType::OGRSOSISimpleDataType ():
22     osName(),
23     nType(OFTString)
24 {}
25 
OGRSOSISimpleDataType(const char * name,OGRFieldType type)26 OGRSOSISimpleDataType::OGRSOSISimpleDataType (const char *name, OGRFieldType type) {
27     setType(name, type);
28 }
setType(const char * name,OGRFieldType type)29 void OGRSOSISimpleDataType::setType (const char *name, OGRFieldType type) {
30     osName  = name;
31     nType   = type;
32 }
~OGRSOSISimpleDataType()33 OGRSOSISimpleDataType::~OGRSOSISimpleDataType () {}
34 
35 /*** utility methods ***/
36 
addType(C2F * map,const char * key,OGRSOSIDataType * type)37 static void addType(C2F* map, const char *key, OGRSOSIDataType *type) {
38   map->insert(std::pair<CPLString, OGRSOSIDataType>(CPLString(key),*type));
39 }
addSimpleType(C2F * map,const char * key,const char * gmlKey,OGRFieldType type)40 static void addSimpleType(C2F* map, const char *key, const char *gmlKey, OGRFieldType type) {
41   OGRSOSIDataType *poType = new OGRSOSIDataType(1);
42   poType->setElement(0, gmlKey, type);
43   addType(map, key, poType);
44   delete poType;
45 }
46 
SOSIInitTypes()47 void SOSIInitTypes() {
48   CPLAssert(poTypes == nullptr);
49   poTypes = new C2F();
50 #include "ogrsosidatatypes.h"
51 
52   /* Actually not headers */
53   addSimpleType(poTypes, "PUNKT", "", OFTInteger); //ignore
54   addSimpleType(poTypes, "KURVE", "", OFTInteger); //ignore
55   addSimpleType(poTypes, "FLATE", "", OFTInteger); //ignore
56   addSimpleType(poTypes, "BUEP", "", OFTInteger);  //ignore
57   addSimpleType(poTypes, "TEKST", "", OFTInteger); //ignore
58   addSimpleType(poTypes, "REF", "", OFTString); //ignore this
59 }
60 
SOSICleanupTypes()61 void SOSICleanupTypes()
62 {
63     delete poTypes;
64     poTypes = nullptr;
65 }
66 
SOSITypeToInt(const char * value)67 int SOSITypeToInt(const char* value) {
68   return atoi(value);
69 }
SOSITypeToReal(const char * value)70 double SOSITypeToReal(const char* value) {
71   return CPLAtof(value);
72 }
73 
SOSITypeToDate(const char * value,int * date)74 void SOSITypeToDate(const char* value, int* date) {
75   char dato[9];
76   snprintf(dato, 9, "%s", value);
77   date[2] = atoi(dato+6);
78   dato[6]='\0';
79   date[1] = atoi(dato+4);
80   dato[4]='\0';
81   date[0] = atoi(dato);
82 }
83 
SOSITypeToDateTime(const char * value,int * date)84 void SOSITypeToDateTime(const char* value, int* date) {
85   char dato[15];
86   snprintf(dato, 15, "%s", value);
87   if (strlen(dato)==14) {
88     date[5] = atoi(dato+12);
89     dato[12]='\0';
90     date[4] = atoi(dato+10);
91     dato[10]='\0';
92     date[3] = atoi(dato+8);
93   } else {
94     date[3] = 0; date[4] = 0; date[5] = 0;
95   }
96   dato[8]='\0';
97   date[2] = atoi(dato+6);
98   dato[6]='\0';
99   date[1] = atoi(dato+4);
100   dato[4]='\0';
101   date[0] = atoi(dato);
102 }
103 
SOSIGetTypeFallback(const CPLString & name)104 static OGRSOSIDataType* SOSIGetTypeFallback(const CPLString& name) {
105   addSimpleType(poTypes, name.c_str(), name.c_str(), OFTString);
106   return SOSIGetType(name);
107 }
108 
SOSIGetType(const CPLString & name)109 OGRSOSIDataType* SOSIGetType(const CPLString& name) {
110   auto iTypes = poTypes->find(name);
111   if (iTypes != poTypes->end()) {
112     return &(iTypes->second);
113   } else {
114     return SOSIGetTypeFallback(name);
115   }
116 }
117