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