forked from sduclos/S52
-
Notifications
You must be signed in to change notification settings - Fork 0
/
S52utils.c
323 lines (260 loc) · 7.09 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
// S52utils.c: utility
//
// Project: OpENCview
/*
This file is part of the OpENCview project, a viewer of ENC.
Copyright (C) 2000-2014 Sylvain Duclos sduclos@users.sourceforge.net
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>
#ifdef S52_USE_GLIB2
#include <glib/gprintf.h> // g_strrstr()
#include <glib/gstdio.h> // FILE
#else
#include <stdio.h> // FILE, fopen(), ...
#include <stdlib.h> // atof(), atoi()
#include <string.h> // strstr(), strlen()
#endif
#include <string.h> // strlen()
#include <stdio.h> // asctime
#include <time.h> // time
#include <unistd.h> // write
// configuration file
#ifdef S52_USE_ANDROID
#define CONF_NAME "/sdcard/s52droid/s52.cfg"
#else
#define CONF_NAME "s52.cfg"
#endif
#define NaN (1.0/0.0)
#ifdef S52_USE_LOG
static gint _log = 0;
#endif
typedef void (*GPrintFunc)(const gchar *string);
static GPrintFunc _oldPrintHandler = NULL;
static GTimeVal _now;
static S52_error_cb _err_cb = NULL;
void g_get_current_time(GTimeVal *result);
void _printf(const char *file, int line, const char *function, const char *frmt, ...)
{
const int MAX = 1024;
char buf[MAX];
snprintf(buf, MAX, "%s:%i in %s(): ", file, line, function);
int size = (int) strlen(buf);
if (size < MAX) {
va_list argptr;
va_start(argptr, frmt);
vsnprintf(&buf[size], MAX - size, frmt, argptr);
va_end(argptr);
}
g_print("%s", buf);
}
int S52_getConfig(const char *label, valueBuf *vbuf)
// return TRUE and string value in vbuf for label, FALSE if fail
{
FILE *fp;
int ret;
//char lbuf[PATH_MAX];
//char frmt[PATH_MAX];
char lbuf[MAXL];
char frmt[MAXL];
fp = fopen(CONF_NAME, "r");
if (NULL == fp) {
PRINTF("WARNING: conf not found: %s\n", CONF_NAME);
return FALSE;
}
// prevent buffer overflow
//sprintf(frmt, "%s%i%s", " %s %", MAXL-1, "[^\n]s");
SPRINTF(frmt, "%s%i%s", " %s %", MAXL-1, "[^\n]s");
//printf("frmt:%s\n", frmt);
ret = fscanf(fp, frmt, lbuf, vbuf);
while (ret > 0) {
//if (('#'!=lbuf[0]) && (0 == strncmp(lbuf, label, strlen(label)))) {
if (('#'!=lbuf[0]) && (0 == S52_strncmp(lbuf, label, S52_strlen(label)))) {
//sscanf(c, "%255[^\n]", *vbuf);
PRINTF("label:%s value:%s \n", lbuf, *vbuf);
fclose(fp);
return TRUE;
}
ret = fscanf(fp, frmt, lbuf, vbuf);
//ret = fscanf(fp, "%s%255[^\n]\n", lbuf, tmp);
//printf("label:%s \n", lbuf);
//printf("value:%s \n", tmp);
//printf("ret:%i\n", ret);
}
fclose(fp);
*vbuf[0] = '\0';
return FALSE;
}
int S52_atoi(const char *str)
// replacement of stdlib.h atoi
{
//return atoi(str);
// the to (int) might not be such a great idea!
return (int)S52_atof(str);
}
double S52_atof(const char *str)
// relacement of stdlib.h atof
{
if (NULL == str)
return (1.0/0.0); //nan
if (0==S52_strlen(str)) {
//PRINTF("WARNING: zero length string (inf)\n");
//g_assert(0);
return (1.0/0.0); //nan
}
#ifdef S52_USE_GLIB2
return g_strtod(str, NULL);
#else
return atof(str);
#endif
}
//int S52_strlen(const char *str)
size_t S52_strlen(const char *str)
{
return strlen(str);
//#ifdef S52_USE_GLIB2
// return g_utf8_strlen(str, -1);
//#else
// return strlen(str);
//#endif
}
char* S52_strstr(const char *haystack, const char *needle)
{
#ifdef S52_USE_GLIB2
return g_strrstr(haystack, needle);
#else
return (char *)strstr(haystack, needle);
#endif
}
gint S52_strncmp(const gchar *s1, const gchar *s2, gsize n)
{
#ifdef S52_USE_GLIB2
return g_ascii_strncasecmp(s1, s2, n);
//return g_strncasecmp(s1, s2, n);
//return strncmp(s1, s2, n);
#else
return strncmp(s1, s2, n);
#endif
}
FILE * S52_fopen (const gchar *filename, const gchar *mode)
{
#ifdef S52_USE_GLIB2
return g_fopen(filename, mode);
#else
return fopen(filename, mode);
#endif
}
int S52_fclose (FILE *fd)
{
// use same call - glib has no g_fclose()
return fclose(fd);
}
gboolean S52_string_equal(const GString *v, const GString *v2)
{
#ifdef S52_USE_GLIB2
// on android glid2 can't handle NULL string
if (NULL==v || NULL==v2) {
PRINTF("S52_string_equal():WARNING: string NULL\n");
return FALSE;
}
return g_string_equal(v, v2);
#else
if (v->len == v2->len) {
if (0 == memcmp(v->str, v2->str, v->len))
return TRUE;
}
return FALSE;
#endif
}
void S52_tree_replace(GTree *tree, gpointer key, gpointer value)
{
#ifdef S52_USE_GLIB2
g_tree_replace(tree, key, value);
#else
g_tree_insert(tree, key, value);
#endif
}
static void _S52_printf(const gchar *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 != _err_cb) {
_err_cb(str);
}
#ifdef S52_USE_LOG
// log to file
write(_log, str, strlen(str));
#endif
}
int S52_initLog(S52_error_cb err_cb)
// set print handler
// set tmp log file
{
_err_cb = err_cb;
#ifdef S52_USE_LOG
GError *error = NULL;
_log = g_file_open_tmp("XXXXXX", NULL, &error);
if (-1 == _log) {
PRINTF("g_file_open_tmp(): failed\n");
} else {
PRINTF("tmp dir:%s\n", g_get_tmp_dir());
}
_oldPrintHandler = g_set_print_handler(_S52_printf);
#else
PRINTF("NOTE: no LOG, compiler flags 'S52_USE_LOG' not set\n");
#endif
return TRUE;
}
int S52_doneLog()
{
g_set_print_handler(_oldPrintHandler);
_err_cb = NULL;
#ifdef S52_USE_LOG
if (0 != _log)
close(_log);
#endif
return TRUE;
}
//////////////////////
//
// 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;
}