forked from sduclos/S52
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathS52utils.c
394 lines (333 loc) · 8.64 KB
/
S52utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// S52utils.c: utility
//
// Project: OpENCview
/*
This file is part of the OpENCview project, a viewer of ENC.
Copyright (C) 2000-2016 Sylvain Duclos [email protected]
OpENCview is free software: you can redistribute it and/or modify
it under the terms of the Lesser GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpENCview is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Lesser GNU General Public License for more details.
You should have received a copy of the Lesser GNU General Public License
along with OpENCview. If not, see <http://www.gnu.org/licenses/>.
*/
#include "S52utils.h"
#include <glib.h> // g_get_current_time()
#include <glib/gprintf.h> // g_strrstr()
#include <glib/gstdio.h> // FILE
#include <stdlib.h> // atoi(), atof()
#include <string.h> // strlen()
#include <unistd.h> // write()
// debug - configuration file
#ifdef S52_USE_ANDROID
#define CFG_NAME "/sdcard/s52droid/s52.cfg"
#else
#define CFG_NAME "s52.cfg"
#endif
#ifdef S52_USE_LOGFILE
static gint _logFile = 0;
static GTimeVal _now;
typedef void (*GPrintFunc)(const gchar *string);
static GPrintFunc _oldPrintHandler = NULL;
#endif
static S52_log_cb _log_cb = NULL;
// internal libS52.so version + build def's
static const char _version[] = S52_VERSION
#ifdef _MINGW
",_MINGW"
#endif
#ifdef S52_USE_GV
",S52_USE_GV"
#endif
#ifdef GV_USE_DOUBLE_PRECISION_COORD
",GV_USE_DOUBLE_PRECISION_COORD"
#endif
#ifdef S52_USE_OGR_FILECOLLECTOR
",S52_USE_OGR_FILECOLLECTOR"
#endif
#ifdef S52_USE_PROJ
",S52_USE_PROJ"
#endif
#ifdef S52_USE_SUPP_LINE_OVERLAP
",S52_USE_SUPP_LINE_OVERLAP"
#endif
#ifdef S52_DEBUG
",S52_DEBUG"
#endif
#ifdef S52_USE_LOGFILE
",S52_USE_LOGFILE"
#endif
#ifdef S52_USE_DBUS
",S52_USE_DBUS"
#endif
#ifdef S52_USE_SOCK
",S52_USE_SOCK"
#endif
#ifdef S52_USE_GOBJECT
",S52_USE_GOBJECT"
#endif
#ifdef S52_USE_BACKTRACE
",S52_USE_BACKTRACE"
#endif
#ifdef S52_USE_EGL
",S52_USE_EGL"
#endif
#ifdef S52_USE_GL1
",S52_USE_GL1"
#endif
#ifdef S52_USE_OPENGL_VBO
",S52_USE_OPENGL_VBO"
#endif
#ifdef S52_USE_GLSC1
",S52_USE_GLSC1"
#endif
#ifdef S52_USE_GL2
",S52_USE_GL2"
#endif
#ifdef S52_USE_GLES2
",S52_USE_GLES2"
#endif
#ifdef S52_USE_ANDROID
",S52_USE_ANDROID"
#endif
#ifdef S52_USE_TEGRA2
",S52_USE_TEGRA2"
#endif
#ifdef S52_USE_ADRENO
",S52_USE_ADRENO"
#endif
#ifdef S52_USE_COGL
",S52_USE_COGL"
#endif
#ifdef S52_USE_FREETYPE_GL
",S52_USE_FREETYPE_GL"
#endif
#ifdef S52_USE_SYM_AISSEL01
",S52_USE_SYM_AISSEL01"
#endif
#ifdef S52_USE_WORLD
",S52_USE_WORLD"
#endif
#ifdef S52_USE_SYM_VESSEL_DNGHL
",S52_USE_SYM_VESSEL_DNGHL"
#endif
#ifdef S52_USE_TXT_SHADOW
",S52_USE_TXT_SHADOW"
#endif
#ifdef S52_USE_RADAR
",S52_USE_RADAR"
#endif
#ifdef S52_USE_RASTER
",S52_USE_RASTER"
#endif
#ifdef S52_USE_MESA3D
",S52_USE_MESA3D"
#endif
#ifdef S52_USE_C_AGGR_C_ASSO
",S52_USE_C_AGGR_C_ASSO"
#endif
"\n";
cchar *S52_utils_version(void)
{
return _version;
}
int S52_utils_getConfig(cchar *label, char *vbuf)
// return TRUE and string value in vbuf for label, FALSE if fail
{
FILE *fp;
int ret;
int nline = 1;
//char lbuf[PATH_MAX];
//char frmt[PATH_MAX];
char lbuf[MAXL];
char frmt[MAXL];
char str [MAXL];
char *pstr = NULL;
fp = g_fopen(CFG_NAME, "r");
if (NULL == fp) {
PRINTF("WARNING: .cfg not found: %s\n", CFG_NAME);
return FALSE;
}
// prevent buffer overflow
SNPRINTF(frmt, MAXL, "%s%i%s", " %s %", MAXL-1, "[^\n]s");
//printf("frmt:%s\n", frmt);
pstr = fgets(str, MAXL, fp);
while (NULL != pstr) {
// debug
//printf("%i - label:%s value:%s\n", nline, lbuf, vbuf);
if ('#' != str[0]) {
ret = sscanf(str, frmt, lbuf, vbuf);
if (0 == g_strcmp0(lbuf, label)) {
PRINTF("--->>> label:%s value:%s \n", lbuf, vbuf);
fclose(fp);
return TRUE;
}
}
++nline;
pstr = fgets(str, MAXL, fp);
}
fclose(fp);
vbuf[0] = '\0';
return FALSE;
}
void _printf(const char *file, int line, const char *function, const char *frmt, ...)
{
int MAX = 128;
char buf[MAX];
char frmtstr[] = "%s:%i in %s(): ";
int size = snprintf(buf, MAX, frmtstr, file, line, function);
if (size < MAX) {
va_list argptr;
va_start(argptr, frmt);
int n = vsnprintf(&buf[size], (MAX-size), frmt, argptr);
va_end(argptr);
char bufFinal[size + n + 1];
memcpy(bufFinal, buf, size);
va_start(argptr, frmt);
//int nn = vsnprintf(&buf[size], (MAX-size), frmt, argptr);
int nn = vsnprintf(&bufFinal[size], (n+1), frmt, argptr);
va_end(argptr);
if (nn > (n+1)) {
//if (n > (MAX-size)) {
//g_print("WARNING: _printf(): string buffer FULL, str len:%i, buf len:%i\n", n, (MAX-size));
g_print("WARNING: _printf(): string buffer FULL, str len:%i, buf len:%i\n", nn, (n+1));
g_assert(0);
}
} else {
g_print("WARNING: _printf(): buf FULL, str size:%i, buf len:%i\n", size, MAX);
g_assert(0);
}
g_print("%s", buf);
#if !defined(S52_USE_LOGFILE)
// if user set a callback .. call it,
// unless logging to file witch will call the cb
if (NULL != _log_cb) {
_log_cb(buf);
}
#endif
}
#ifdef S52_USE_LOGFILE
static void _S52_printf(cchar *string)
{
char str[MAXL];
g_get_current_time(&_now);
snprintf(str, MAXL-1, "%s %s", g_time_val_to_iso8601(&_now), string);
// if user set a callback .. call it
if (NULL != _log_cb) {
_log_cb(str);
}
// log to file
if (NULL != _logFile) {
write(_logFile, str, strlen(str));
}
// STDOUT
g_printf("%s", str);
}
#endif
int S52_utils_initLog(S52_log_cb log_cb)
// set print handler
// set tmp log file
{
if (NULL != log_cb) {
log_cb("S52_utils_initLog(): init logging\n");
_log_cb = log_cb;
}
#ifdef S52_USE_LOGFILE
GError *error = NULL;
_logFile = g_file_open_tmp("XXXXXX", NULL, &error);
if (-1 == _logFile) {
PRINTF("WARNING: g_file_open_tmp(): failed\n");
} else {
PRINTF("DEBUG: logfile tmp dir:%s\n", g_get_tmp_dir());
}
if (NULL != error) {
g_printf("WARNING: g_file_open_tmp() failed (%s)\n", error->message);
g_error_free(error);
}
_oldPrintHandler = g_set_print_handler(_S52_printf);
#else
PRINTF("DEBUG: no LOGFILE, compiler flags 'S52_USE_LOGFILE' not set\n");
#endif
return TRUE;
}
int S52_utils_doneLog()
{
_log_cb = NULL;
#ifdef S52_USE_LOGFILE
g_set_print_handler(_oldPrintHandler);
_oldPrintHandler = NULL;
if (0 != _logFile)
close(_logFile);
#endif
return TRUE;
}
int S52_atoi(cchar *str)
// safe atoi()
// use for parsing the PLib and S57 attribute
{
if (NULL == str) {
PRINTF("WARNING: NULL string\n");
g_assert(0);
return 0;
}
if (0 == strlen(str)) {
PRINTF("WARNING: zero length string\n");
g_assert(0);
return 0;
}
// the to (int) might not be such a great idea! (no rounding)
//return (int)S52_atof(str);
//return (int)g_strtod(str, NULL);
return atoi(str);
}
double S52_atof(cchar *str)
// safe atof()
{
if (NULL == str) {
PRINTF("WARNING: NULL string\n");
g_assert(0);
return 0;
}
if (0 == strlen(str)) {
PRINTF("WARNING: zero length string\n");
g_assert(0);
return 0;
}
//return g_strtod(str, NULL);
return atof(str);
}
//////////////////////
//
// from GIMP xyz2rgb.c
//
static double m2[3][3] =
{
{ 3.240479, -1.537150, -0.498535 },
{ -0.969256, 1.875992, 0.041556 },
{ 0.055648, -0.204043, 1.057311 }
};
int S52_xyL2rgb(double *xr, double *yg, double *Lb)
{
double R, G, B;
double Y = *Lb;
double X = (*xr / *yg) * Y;
double Z = ((1 - *xr - *yg) / *yg) * Y;
X = X / 100.0;
Y = Y / 100.0;
Z = Z / 100.0;
R = m2[0][0] * X + m2[0][1] * Y + m2[0][2] * Z;
G = m2[1][0] * X + m2[1][1] * Y + m2[1][2] * Z;
B = m2[2][0] * X + m2[2][1] * Y + m2[2][2] * Z;
//printf ("RGB = (%f %f %f) \n", R, G, B);
R = (R<0.0)? 0.0 : (R>1.0)? 1.0 : R;
G = (G<0.0)? 0.0 : (G>1.0)? 1.0 : G;
B = (B<0.0)? 0.0 : (B>1.0)? 1.0 : B;
*xr = R*255.0;
*yg = G*255.0;;
*Lb = B*255.0;;
return 1;
}