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