File: sartime.c

package info (click to toggle)
searchandrescue 0.8.2-10
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 4,656 kB
  • ctags: 6,111
  • sloc: ansic: 89,072; cpp: 7,691; sh: 90; makefile: 80
file content (507 lines) | stat: -rw-r--r-- 9,931 bytes parent folder | download | duplicates (6)
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <sys/types.h>
#ifdef __MSW__
# include <windows.h>
#else
# include <sys/time.h>
#endif

#include "../include/strexp.h"
#include "../include/string.h"

#include "sartime.h"

time_t SARGetCurMilliTime(void);
unsigned int SARRandom(unsigned int seed_offset);
float SARRandomCoeff(unsigned int seed_offset);

int SARParseTimeOfDay(
	const char *string,
	int *h, int *m, int *s
);
int SARParseLatitudeDMS(const char *string, float *dms);
int SARParseLongitudeDMS(const char *string, float *dms);


#define ATOI(s)         (((s) != NULL) ? atoi(s) : 0)
#define ATOL(s)         (((s) != NULL) ? atol(s) : 0)
#define ATOF(s)         (((s) != NULL) ? atof(s) : 0.0f)
#define STRDUP(s)       (((s) != NULL) ? strdup(s) : NULL)

#define MAX(a,b)        (((a) > (b)) ? (a) : (b))
#define MIN(a,b)        (((a) < (b)) ? (a) : (b))
#define CLIP(a,l,h)     (MIN(MAX((a),(l)),(h)))
#define IS_STRING_EMPTY(s)      (((s) != NULL) ? (*(s) == '\0') : TRUE)

#define RADTODEG(r)     ((r) * 180 / PI)
#define DEGTORAD(d)     ((d) * PI / 180)


/*
 *      Returns the current time since midnight in milliseconds from the
 *      system.
 */
time_t SARGetCurMilliTime(void)
{
#ifdef __MSW__
	SYSTEMTIME t;   /* Current time of day structure. */

	GetSystemTime(&t);
	return(
	    (time_t)(
		(((((t.wHour * 60.0) + t.wMinute) * 60) + t.wSecond) * 1000) +
		t.wMilliseconds
	    )
	);
#else
	struct timeval tv[1];

	if(gettimeofday(tv, NULL) < 0)
	    return(-1);

	return(((tv->tv_sec % 86400) * 1000) + (tv->tv_usec / 1000));
#endif
}

/*
 *      Returns a random number in the range of 0 to 0xffffffff.
 *
 *      If you call this function more than once per cycle then you
 *      should pass seed_offset as a unique value each time to better
 *      obtain a more random number.
 */
unsigned int SARRandom(unsigned int seed_offset)
{
#if defined(RAND_MAX)
/*	srand((unsigned int)cur_millitime + seed_offset); */
	return(rand());
#else
	static u_int32_t gSeed = 1234145;
#ifdef __MSW__
	long result = gSeed * (long)1103515245 + 12345;
#else
	int64_t result = gSeed * (int64_t)1103515245 + 12345;
#endif
	gSeed = (u_int32_t)result;

	return((u_int32_t)(result >> 16));
#endif
}

/*
 *      Returns a random coefficient from 0.0 to 1.0, setting the seed
 *      as the current time in milliseconds plus the given seed_offset.
 *
 *      If you call this function more than once per cycle then you
 *      should pass seed_offset as a unique value each time to better
 *      obtain a more random number.
 */
float SARRandomCoeff(unsigned int seed_offset)
{
#if defined(RAND_MAX)
/*	srand((unsigned int)cur_millitime + seed_offset); */
	return((float)rand() / (float)RAND_MAX);
#else
	return((float)SARRandom(seed_offset) / (float)0xffffffff);
#endif
}


/*
 *      Parses the given string which should be in the format
 *      "h:m:s". Two available formats 24 and 12 hour are supported,
 *      where the 12 hour is checked for by searching for an in
 *      string 'p' character (case insensitive).
 *
 *      String is terminated by a null or blank character.
 *
 *      Returns non-zero if parsing failed.
 */
int SARParseTimeOfDay(
	const char *string,
	int *h, int *m, int *s
)
{
	const char *sp;
	int in_len, is_12hourpm = 0;

	int lstring_len = 80;
	char *sp2, *sp3;
	char lstring[80];

	/* Reset inputs. */
	if(h != NULL)
	    (*h) = 0;
	if(m != NULL)
	    (*m) = 0;
	if(s != NULL)
	    (*s) = 0;

	if((string == NULL) ? 1 : ((*string) == '\0'))
	    return(-1);

	/* Check if this is 12 or 24 hour format. */
	sp = string;
	in_len = 0;
	while(((*sp) != '\0') && !ISBLANK(*sp))
	{
	    if(toupper(*sp) == 'P')
		is_12hourpm = 1;

	    sp++;
	    in_len++;
	}

	/* Copy input string to local string. */
	if(in_len > 0)
	    strncpy(
		lstring, string, MIN(in_len, lstring_len)
	    );
	else
	    (*lstring) = '\0';
	lstring[MIN(in_len, lstring_len - 1)] = '\0';

	/* Fetch hour. */
	sp2 = lstring;
	sp3 = strchr(sp2, ':');
	if(sp3 != NULL)
	{
	    (*sp3) = '\0';
	    if(h != NULL)
		*h = (ATOI(sp2) + ((is_12hourpm) ? + 12 : 0)) % 24;
	    sp2 = sp3 + 1;
	}
	else
	{
	    if(h != NULL)
		*h = (ATOI(sp2) + ((is_12hourpm) ? + 12 : 0)) % 24;
	    sp2 = NULL;
	}
	if(sp2 == NULL)
	    return(0);

	/* Fetch minute. */
	sp3 = strchr(sp2, ':');
	if(sp3 != NULL)
	{
	    *sp3 = '\0';
	    if(m != NULL)
		*m = ATOI(sp2) % 60;
	    sp2 = sp3 + 1;
	}
	else
	{
	    if(m != NULL)
		*m = ATOI(sp2) % 60;
	    sp2 = NULL;
	}
	if(sp2 == NULL)
	    return(0);

	/* Fetch seconds. */
	sp3 = strchr(sp2, ':');
	if(sp3 != NULL)
	{
	    *sp3 = '\0';
	    if(s != NULL)
		*s = ATOI(sp2) % 60;
	}
	else
	{
	    if(s != NULL)
		*s = ATOI(sp2) % 60;
	}

	return(0);
}

/*
 *      Parses the given string which should be in the format
 *      "d'm"s" or "d". Examples (respective) "34'10"59" or "34.1833".
 *
 *      For format "d" a decimal value from .000 to .999 can be used
 *      to represent decimal degrees.
 *
 *      Degrees is bounded by -90.0 and 90.0.
 *
 *      String is terminated by a null or blank character.
 *
 *      Returns non-zero if parsing failed.
 */
int SARParseLatitudeDMS(const char *string, float *dms)
{
	const char *sp;
	int in_len, is_traditional = 0, is_south = 0;

	int lstring_len = 80;
	char *sp2, *sp3;
	char lstring[80];

	/* Reset inputs. */
	if(dms != NULL)
	    *dms = 0.0f;

	if((string != NULL) ? (*string == '\0') : 1)
	    return(-1);

	/* Check if this is in decimal or traditional notation. */
	sp = string;
	in_len = 0;
	while((*sp != '\0') && !ISBLANK(*sp))
	{
	    if(toupper(*sp) == '\'')
		is_traditional = 1;

	    if(toupper(*sp) == 'S')
		is_south = 1;

	    sp++;
	    in_len++;
	}

	/* Copy input string to local string. */
	if(in_len > 0)
	    strncpy(
		lstring, string, MIN(in_len, lstring_len)
	    );
	else
	    *lstring = '\0';
	lstring[MIN(in_len, lstring_len - 1)] = '\0';

	/* Handle by type. */
	if(is_traditional)
	{
	    /* Traditional notation. */
	    int d = 0, m = 0, s = 0;

	    /* Fetch degrees. */
	    sp2 = lstring;
	    if(sp2 != NULL)
	    {
		sp3 = strchr(sp2, '\'');
		if(sp3 != NULL)
		{
		    *sp3 = '\0';
		    d = ATOI(sp2);
		    sp2 = sp3 + 1;
		}
		else
		{
		    d = ATOI(sp2);
		    sp2 = NULL;
		}
	    }

	    /* Fetch minutes. */
	    if(sp2 != NULL)
	    {
		sp3 = strchr(sp2, '"');
		if(sp3 != NULL)
		{
		    *sp3 = '\0';
		    m = ATOI(sp2);
		    sp2 = sp3 + 1;
		}
		else
		{
		    m = ATOI(sp2);
		    sp2 = NULL;
		}
	    }

	    /* Fetch seconds. */
	    if(sp2 != NULL)
	    {
		sp3 = strchr(sp2, '"');
		if(sp3 != NULL)
		{
		    *sp3 = '\0';
		    s = ATOI(sp2);
		    sp2 = sp3 + 1;
		}
		else
		{
		    s = ATOI(sp2);
		    sp2 = NULL;
		}
	    }

	    /* Convert and add to dms return. */
	    if(dms != NULL)
	    {
		/* Negative? */
		if(is_south && (d > 0.0f))
		    *dms = (float)-d -
			((float)m / 60.0f) - ((float)s / 60.0f / 100.0f);
		else if(d < 0)
		    *dms = (float)d -
			((float)m / 60.0f) - ((float)s / 60.0f / 100.0f);
		else
		    *dms = (float)d +
			((float)m / 60.0f) + ((float)s / 60.0f / 100.0f);
	    }
	}
	else
	{
	    /* Decimal notation (this is easy). */
	    sp2 = lstring;
	    if(dms != NULL)
	    {
		float d = (float)ATOF(sp2);
		if(is_south && (d > 0.0f))
		    *dms = -d;
		else
		    *dms = d;
	    }
	}

	return(0);
}

/*
 *      Parses the given string which should be in the format
 *      "d'm"s" or "d". Examples (respective) "34'10"59" or "34.1833".
 *
 *      For format "d" a decimal value from .000 to .999 can be used
 *      to represent decimal degrees.
 *
 *      Degrees is bounded by -140.0 and 140.0.
 *
 *      String is terminated by a null or blank character.
 *
 *      Returns non-zero if parsing failed.
 */
int SARParseLongitudeDMS(const char *string, float *dms)
{
	const char *sp;
	int in_len, is_traditional = 0, is_west = 0;

	int lstring_len = 80;
	char *sp2, *sp3;
	char lstring[80];

	/* Reset inputs. */
	if(dms != NULL)
	    *dms = 0.0f;

	if((string != NULL) ? (*string == '\0') : 1)
	    return(-1);

	/* Check if this is in decimal or traditional notation. */
	sp = string;
	in_len = 0;
	while((*sp != '\0') && !ISBLANK(*sp))
	{
	    if(toupper(*sp) == '\'')
		is_traditional = 1;

	    if(toupper(*sp) == 'W')
		is_west = 1;

	    sp++;
	    in_len++;
	}

	/* Copy input string to local string. */
	if(in_len > 0)
	    strncpy(
		lstring, string, MIN(in_len, lstring_len)
	    );
	else
	    *lstring = '\0';
	lstring[MIN(in_len, lstring_len - 1)] = '\0';

	/* Handle by type. */
	if(is_traditional)
	{
	    /* Traditional notation. */
	    int d = 0, m = 0, s = 0;

	    /* Fetch degrees. */
	    sp2 = lstring;
	    if(sp2 != NULL)
	    {
		sp3 = strchr(sp2, '\'');
		if(sp3 != NULL)
		{
		    *sp3 = '\0';
		    d = ATOI(sp2);
		    sp2 = sp3 + 1;
		}
		else
		{
		    d = ATOI(sp2);
		    sp2 = NULL;
		}
	    }

	    /* Fetch minutes. */
	    if(sp2 != NULL)
	    {
		sp3 = strchr(sp2, '"');
		if(sp3 != NULL)
		{
		    *sp3 = '\0';
		    m = ATOI(sp2);
		    sp2 = sp3 + 1;
		}
		else
		{
		    m = ATOI(sp2);
		    sp2 = NULL;
		}
	    }

	    /* Fetch seconds. */
	    if(sp2 != NULL)
	    {
		sp3 = strchr(sp2, '"');
		if(sp3 != NULL)
		{
		    *sp3 = '\0';
		    s = ATOI(sp2);
		    sp2 = sp3 + 1;
		}
		else
		{
		    s = ATOI(sp2);
		    sp2 = NULL;
		}
	    }

	    /* Convert and add to dms return. */
	    if(dms != NULL)
	    {
		/* Negative? */
		if(is_west && (d > 0))
		    *dms = (float)-d -
			((float)m / 60.0f) - ((float)s / 60.0f / 100.0f);
		else if(d < 0)
		    *dms = (float)d -
			((float)m / 60.0f) - ((float)s / 60.0f / 100.0f);
		else
		    *dms = (float)d +
			((float)m / 60.0f) + ((float)s / 60.0f / 100.0f);
	    }
	}
	else
	{
	    /* Decimal notation (this is easy). */
	    sp2 = lstring;
	    if(dms != NULL)
	    {
		float d = (float)ATOF(sp2);
		if(is_west && (d > 0.0f))
		    *dms = -d;
		else
		    *dms = d;
	    }
	}

	return(0);
}