SAGA API Version 9.14
Loading...
Searching...
No Matches
api_translator.cpp
Go to the documentation of this file.
1/**********************************************************
2 * Version $Id$
3 *********************************************************/
4
6// //
7// SAGA //
8// //
9// System for Automated Geoscientific Analyses //
10// //
11// Application Programming Interface //
12// //
13// Library: SAGA_API //
14// //
15//-------------------------------------------------------//
16// //
17// api_translator.cpp //
18// //
19// Copyright (C) 2005 by Olaf Conrad //
20// //
21//-------------------------------------------------------//
22// //
23// This file is part of 'SAGA - System for Automated //
24// Geoscientific Analyses'. //
25// //
26// This library is free software; you can redistribute //
27// it and/or modify it under the terms of the GNU Lesser //
28// General Public License as published by the Free //
29// Software Foundation, either version 2.1 of the //
30// License, or (at your option) any later version. //
31// //
32// This library is distributed in the hope that it will //
33// be useful, but WITHOUT ANY WARRANTY; without even the //
34// implied warranty of MERCHANTABILITY or FITNESS FOR A //
35// PARTICULAR PURPOSE. See the GNU Lesser General Public //
36// License for more details. //
37// //
38// You should have received a copy of the GNU Lesser //
39// General Public License along with this program; if //
40// not, see <http://www.gnu.org/licenses/>. //
41// //
42//-------------------------------------------------------//
43// //
44// contact: Olaf Conrad //
45// Institute of Geography //
46// University of Goettingen //
47// Goldschmidtstr. 5 //
48// 37077 Goettingen //
49// Germany //
50// //
51// e-mail: oconrad@saga-gis.org //
52// //
54
55//---------------------------------------------------------
56
57
59// //
60// //
61// //
63
64//---------------------------------------------------------
65#include "api_core.h"
66
67#include "table.h"
68
69
71// //
72// //
73// //
75
76//---------------------------------------------------------
78
79//---------------------------------------------------------
81{
82 return( gSG_Translator );
83}
84
85
87// //
88// //
89// //
91
92//---------------------------------------------------------
93const SG_Char * SG_Translate(const CSG_String &Text)
94{
95 return( gSG_Translator.Get_Translation(Text) );
96}
97
98
100// //
101// //
102// //
104
105//---------------------------------------------------------
107{
108 m_nTranslations = 0;
109 m_Translations = NULL;
110}
111
112CSG_Translator::CSG_Translator(const CSG_String &FileName, bool bSetExtension, int fieldText, int fieldTranslation, bool bCmpNoCase)
113{
114 m_nTranslations = 0;
115 m_Translations = NULL;
116
117 Create(FileName, bSetExtension, fieldText, fieldTranslation, bCmpNoCase);
118}
119
120CSG_Translator::CSG_Translator(class CSG_Table *pTable, int fieldText, int fieldTranslation, bool bCmpNoCase)
121{
122 m_nTranslations = 0;
123 m_Translations = NULL;
124
125 Create(pTable, fieldText, fieldTranslation, bCmpNoCase);
126}
127
128//---------------------------------------------------------
133
134//---------------------------------------------------------
136{
137 if( m_Translations )
138 {
139 for(int i=0; i<m_nTranslations; i++)
140 {
141 delete(m_Translations[i]);
142 }
143
144 SG_Free(m_Translations);
145
146 m_nTranslations = 0;
147 m_Translations = NULL;
148 }
149}
150
151
153// //
155
156//---------------------------------------------------------
157bool CSG_Translator::Create(const CSG_String &_FileName, bool bSetExtension, int fieldText, int fieldTranslation, bool bCmpNoCase)
158{
159 Destroy();
160
161 CSG_Table Table; CSG_String FileName(bSetExtension ? SG_File_Make_Path("", _FileName, "lng") : _FileName);
162
163 SG_UI_Msg_Lock(true);
164
165 if( SG_File_Exists(FileName) && Table.Create(FileName, TABLE_FILETYPE_Text, SG_FILE_ENCODING_UTF8) )
166 {
167 Create(&Table, fieldText, fieldTranslation, bCmpNoCase);
168 }
169
170 SG_UI_Msg_Lock(false);
171
172 return( m_nTranslations > 0 );
173}
174
175//---------------------------------------------------------
176bool CSG_Translator::Create(CSG_Table *pTable, int fieldText, int fieldTranslation, bool bCmpNoCase)
177{
178 Destroy();
179
180 if( fieldText != fieldTranslation && pTable && pTable->Get_Field_Count() > fieldText && pTable->Get_Field_Count() > fieldTranslation && pTable->Get_Count() > 0 )
181 {
182 m_bCmpNoCase = bCmpNoCase;
183
184 if( m_bCmpNoCase )
185 {
186 for(sLong i=0; i<pTable->Get_Count(); i++)
187 {
188 CSG_Table_Record *pRecord = pTable->Get_Record(i);
189
190 if( !pRecord->is_NoData(fieldText) )
191 {
192 CSG_String s = pRecord->asString(fieldText);
193
194 pRecord->Set_Value(fieldText, s.Make_Lower().c_str());
195 }
196 }
197 }
198
199 SG_UI_Msg_Lock(true);
200 pTable->Set_Index(fieldText, TABLE_INDEX_Ascending);
201 SG_UI_Msg_Lock(false);
202
203 m_Translations = (CSG_Translation **)SG_Malloc(pTable->Get_Count() * sizeof(CSG_Translation *));
204
205 for(sLong i=0; i<pTable->Get_Count(); i++)
206 {
207 CSG_Table_Record *pRecord = pTable->Get_Record_byIndex(i);
208
209 if( !pRecord->is_NoData(fieldText) && !pRecord->is_NoData(fieldTranslation) )
210 {
211 m_Translations[m_nTranslations++] = new CSG_Translation(pRecord->asString(fieldText), pRecord->asString(fieldTranslation));
212 }
213 }
214
215 if( m_nTranslations < pTable->Get_Count() )
216 {
217 m_Translations = (CSG_Translation **)SG_Realloc(m_Translations, m_nTranslations * sizeof(CSG_Translation *));
218 }
219 }
220
221 return( m_nTranslations > 0 );
222}
223
224
226// //
228
229//---------------------------------------------------------
230#define COMPARE(Index, Text) (m_bCmpNoCase ? m_Translations[Index]->m_Text.CmpNoCase(Text) : m_Translations[Index]->m_Text.Cmp(Text))
231
232//---------------------------------------------------------
233int CSG_Translator::_Get_Index(const CSG_String &Text) const
234{
235 int a, b, i, c;
236
237 if( m_nTranslations == 1 )
238 {
239 c = COMPARE(0, Text);
240
241 return( c >= 0 ? 0 : 1 );
242 }
243
244 if( m_nTranslations > 1 )
245 {
246 for(a=0, b=m_nTranslations-1; b - a > 1; )
247 {
248 i = a + (b - a) / 2;
249 c = COMPARE(i, Text);
250
251 if( c > 0 )
252 {
253 b = i;
254 }
255 else if( c < 0 )
256 {
257 a = i;
258 }
259 else
260 {
261 return( i );
262 }
263 }
264
265 if( COMPARE(a, Text) < 0 )
266 {
267 if( COMPARE(b, Text) < 0 )
268 {
269 return( m_nTranslations );
270 }
271
272 return( b );
273 }
274
275 if( COMPARE(b, Text) > 0 )
276 {
277 return( a );
278 }
279 }
280
281 return( m_nTranslations );
282}
283
284
286// //
288
289//---------------------------------------------------------
290const SG_Char * CSG_Translator::Get_Translation(const SG_Char *Text, bool bReturnNullOnNotFound) const
291{
292 if( Text )
293 {
294 if( m_nTranslations > 0 )
295 {
296 CSG_String s(Text); if( *Text == '{' ) { s = s.AfterFirst('{').BeforeFirst('}'); }
297
298 int Index = _Get_Index(s);
299
300 if( Index < m_nTranslations && !COMPARE(Index, s) )
301 {
302 return(m_Translations[Index]->m_Translation);
303 }
304 }
305
306 if( bReturnNullOnNotFound )
307 {
308 return( NULL );
309 }
310
311 //-------------------------------------------------
312 if( *Text == '{' )
313 {
314 do { Text++; } while( *Text != '}' && *Text != '\0' );
315 do { Text++; } while( *Text == ' ' && *Text != '\0' );
316 }
317 }
318
319 return( Text );
320}
321
322//---------------------------------------------------------
323bool CSG_Translator::Get_Translation(const SG_Char *Text, CSG_String &Translation) const
324{
325 if( Text )
326 {
327 if( m_nTranslations > 0 )
328 {
329 CSG_String s(Text); if( *Text == '{' ) { s = s.AfterFirst('{').BeforeFirst('}'); }
330
331 int Index = _Get_Index(s);
332
333 if( Index < m_nTranslations && !COMPARE(Index, s) )
334 {
335 Translation = m_Translations[Index]->m_Translation;
336
337 return( true );
338 }
339 }
340
341 //-------------------------------------------------
342 if( *Text == '{' )
343 {
344 do { Text++; } while( *Text != '}' && *Text != '\0' );
345 do { Text++; } while( *Text == ' ' && *Text != '\0' );
346 }
347
348 Translation = Text;
349 }
350
351 return( false );
352}
353
354
356// //
357// //
358// //
360
361//---------------------------------------------------------
SAGA_API_DLL_EXPORT bool SG_File_Exists(const CSG_String &FileName)
SAGA_API_DLL_EXPORT void * SG_Malloc(size_t size)
signed long long sLong
Definition api_core.h:158
SAGA_API_DLL_EXPORT void SG_Free(void *memblock)
SAGA_API_DLL_EXPORT CSG_String SG_File_Make_Path(const CSG_String &Directory, const CSG_String &Name)
SAGA_API_DLL_EXPORT int SG_UI_Msg_Lock(bool bOn)
SAGA_API_DLL_EXPORT void * SG_Realloc(void *memblock, size_t size)
#define SG_Char
Definition api_core.h:536
@ SG_FILE_ENCODING_UTF8
Definition api_core.h:552
const SG_Char * SG_Translate(const CSG_String &Text)
CSG_Translator & SG_Get_Translator(void)
#define COMPARE(Index, Text)
CSG_Translator gSG_Translator
CSG_String AfterFirst(char Character) const
CSG_String BeforeFirst(char Character) const
const SG_Char * c_str(void) const
CSG_String & Make_Lower(void)
bool Set_Value(int Field, const CSG_String &Value)
bool is_NoData(int Field) const
const SG_Char * asString(int Field, int Decimals=-99) const
bool Create(void)
Definition table.cpp:153
sLong Get_Count(void) const
Definition table.h:410
bool Set_Index(CSG_Index &Index, int Field, bool bAscending=true) const
Definition table.cpp:1546
virtual CSG_Table_Record * Get_Record(sLong Index) const
Definition table.h:412
int Get_Field_Count(void) const
Definition table.h:371
CSG_Table_Record * Get_Record_byIndex(sLong Index) const
Definition table.h:417
const SG_Char * Get_Translation(int i) const
Definition api_core.h:1580
int Get_Count(void) const
Definition api_core.h:1578
bool Create(const CSG_String &FileName, bool bSetExtension=true, int iText=0, int iTranslation=1, bool bCmpNoCase=false)
virtual ~CSG_Translator(void)
@ TABLE_FILETYPE_Text
Definition table.h:95
@ TABLE_INDEX_Ascending
Definition table.h:105