OpenCPN Partial API docs
Loading...
Searching...
No Matches
s57RegistrarMgr.cpp
1/***************************************************************************
2 *
3 * Project: OpenCPN
4 *
5 ***************************************************************************
6 * Copyright (C) 2015 by David S. Register *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
22 ***************************************************************************
23 */
24
25#include <wx/log.h>
26#include <wx/tokenzr.h>
27#include <wx/textfile.h>
28#include <wx/filename.h>
29
30#include "config.h"
31
32#include "s57RegistrarMgr.h"
33#include "S57ClassRegistrar.h"
34
35extern S57ClassRegistrar* g_poRegistrar;
36
37static int s57_initialize(const wxString& csv_dir, FILE* flog) {
38 // Get one instance of the s57classregistrar,
39 // And be prepared to give it to any module that needs it
40
41 if (g_poRegistrar == NULL) {
42 g_poRegistrar = new S57ClassRegistrar();
43
44 if (!g_poRegistrar->LoadInfo(csv_dir.mb_str(), FALSE)) {
45 wxString msg(_T(" Error: Could not load S57 ClassInfo from "));
46 msg.Append(csv_dir);
47 wxLogMessage(msg);
48
49 delete g_poRegistrar;
50 g_poRegistrar = NULL;
51 }
52 }
53
54 return 0;
55}
56
57s57RegistrarMgr::s57RegistrarMgr(const wxString& csv_dir, FILE* flog) {
58 s57_initialize(csv_dir, flog);
59
60 // Create and initialize the S57 Attribute helpers
61 s57_attr_init(csv_dir);
62 // Create and initialize the S57 Feature code helpers
63 s57_feature_init(csv_dir);
64}
65
66s57RegistrarMgr::~s57RegistrarMgr() {
67 delete g_poRegistrar;
68 g_poRegistrar = NULL;
69}
70
71bool s57RegistrarMgr::s57_attr_init(const wxString& csv_dir) {
72 // Find, open, and read the file {csv_dir}/s57attributes.csv
73 wxString csv_t = csv_dir;
74 wxChar sep = wxFileName::GetPathSeparator();
75 if (csv_t.Last() != sep) csv_t.Append(sep);
76
77 wxTextFile tFile;
78 wxString targetFile = csv_t + _T("s57attributes.csv");
79
80 if (!tFile.Open(targetFile)) {
81 wxString msg(_T(" Error: Could not load S57 Attribute Info from "));
82 msg.Append(csv_dir);
83 wxLogMessage(msg);
84
85 return false;
86 }
87
88 // populate the member hashmaps
89
90 // First map: Key is char[] attribute acronym, value is standard ID
91 // Second map: Key is standard ID, value is char[] attribute acronym
92
93 wxString str;
94 for (str = tFile.GetFirstLine(); !tFile.Eof(); str = tFile.GetNextLine()) {
95 wxStringTokenizer tk(str, _T(","));
96
97 wxString ident = tk.GetNextToken();
98 long nID = -1;
99 if (ident.ToLong(&nID)) {
100 wxString description = tk.GetNextToken();
101 wxString acronym = tk.GetNextToken();
102
103 m_attrHash1[acronym] = nID;
104 m_attrHash2[nID] = acronym.c_str();
105 }
106 }
107
108 return true;
109}
110
111bool s57RegistrarMgr::s57_feature_init(const wxString& csv_dir) {
112 // Find, open, and read the file {csv_dir}/s57objectclasses.csv
113 wxString csv_t = csv_dir;
114 wxChar sep = wxFileName::GetPathSeparator();
115 if (csv_t.Last() != sep) csv_t.Append(sep);
116
117 wxTextFile tFile;
118 wxString targetFile = csv_t + _T("s57objectclasses.csv");
119
120 if (!tFile.Open(targetFile)) {
121 wxString msg(_T(" Error: Could not load S57 Feature Info from "));
122 msg.Append(csv_dir);
123 wxLogMessage(msg);
124
125 return false;
126 }
127
128 // populate the member hashmaps
129
130 // First map: Key is char[] feature acronym, value is standard ID
131 // Second map: Key is standard ID, value is char[] feature acronym
132
133 wxString str;
134 for (str = tFile.GetFirstLine(); !tFile.Eof(); str = tFile.GetNextLine()) {
135 wxStringTokenizer tk(str, _T(","));
136
137 wxString ident = tk.GetNextToken();
138 long nID = -1;
139 // if( ident.ToLong( &nID )){
140 // wxString description = tk.GetNextToken();
141 // wxString acronym = tk.GetNextToken();
142 //
143 // m_featureHash1[acronym] = nID;
144 // m_featureHash2[nID] = acronym.c_str();
145 //
146 // }
147 if (ident.ToLong(&nID)) {
148 wxString description = tk.GetNextToken();
149 // wxString d2;
150 while (!description.EndsWith("\"")) description += tk.GetNextToken();
151
152 wxString acronym = tk.GetNextToken();
153
154 m_featureHash1[acronym] = nID;
155 m_featureHash2[nID] = acronym.c_str();
156 }
157 }
158
159 return true;
160}
161
162int s57RegistrarMgr::getAttributeID(const char* pAttrName) {
163 wxString key(pAttrName);
164
165 if (m_attrHash1.find(key) == m_attrHash1.end())
166 return -1;
167 else
168 return m_attrHash1[key];
169}
170
171std::string s57RegistrarMgr::getAttributeAcronym(int nID) {
172 if (m_attrHash2.find(nID) == m_attrHash2.end())
173 return "";
174 else
175 return m_attrHash2[nID];
176}
177
178std::string s57RegistrarMgr::getFeatureAcronym(int nID) {
179 if (m_featureHash2.find(nID) == m_featureHash2.end())
180 return "";
181 else
182 return m_featureHash2[nID];
183}