-
Notifications
You must be signed in to change notification settings - Fork 7
/
genutils.c
610 lines (546 loc) · 15.8 KB
/
genutils.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
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
/***************************************************************************
*
* General utility functions.
*
* This file is part of the SeedLink Library.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright (C) 2024:
* @author Chad Trabant, EarthScope Data Services
***************************************************************************/
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "libslink.h"
/**********************************************************************/ /**
* @brief Return true if the byte order of the host is little endian
*
* Due to the lack of portable defines to determine host byte order this
* run-time test is provided.
*
* @returns 1 if the host is little endian, otherwise 0.
***************************************************************************/
uint8_t
sl_littleendianhost (void)
{
uint16_t host = 1;
return *((uint8_t *)(&host));
} /* End of sl_littleendianhost() */
/**********************************************************************/ /**
* @brief Compute the month and day-of-month from a year and day-of-year.
*
* @returns 0 on success and -1 on error.
***************************************************************************/
int
sl_doy2md (int year, int jday, int *month, int *mday)
{
int idx;
int leap;
int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
/* Sanity check for the supplied year */
if (year < 1900 || year > 2100)
{
sl_log_r (NULL, 2, 0, "%s(): year (%d) is out of range\n", __func__, year);
return -1;
}
/* Test for leap year */
leap = (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) ? 1 : 0;
/* Add a day to February if leap year */
if (leap)
days[1]++;
if (jday > 365 + leap || jday <= 0)
{
sl_log_r (NULL, 2, 0, "%s(): day-of-year (%d) is out of range\n", __func__, jday);
return -1;
}
for (idx = 0; idx < 12; idx++)
{
jday -= days[idx];
if (jday <= 0)
{
*month = idx + 1;
*mday = days[idx] + jday;
break;
}
}
return 0;
} /* End of sl_doy2md() */
/***********************************************************************/ /**
* @brief Return protocol details for a specified type
*
* @param protocol Protocol identifier
* @param major Pointer to value for major protocol version (optional)
* @param minor Pointer to value for minor protocol version (optional)
*
* @return Pointer to string description of protocol version.
***************************************************************************/
char *
sl_protocol_details (LIBPROTOCOL protocol, uint8_t *major, uint8_t *minor)
{
switch (protocol)
{
case SLPROTO3X:
if (major)
*major = 3;
if (minor)
*minor = 0;
return "3.X";
case SLPROTO40:
if (major)
*major = 4;
if (minor)
*minor = 0;
return "4.0";
default:
if (major)
*major = 0;
if (minor)
*minor = 0;
return "Unknown";
}
}
/**********************************************************************/ /**
* @brief Return human readable description for specified payload format
*
* @returns Descriptive string for payload format
***************************************************************************/
const char *
sl_formatstr (char format, char subformat)
{
switch (format)
{
case SLPAYLOAD_UNKNOWN:
return "Unknown";
break;
case SLPAYLOAD_MSEED2INFO:
return "INFO as XML in miniSEED 2";
break;
case SLPAYLOAD_MSEED2INFOTERM:
return "INFO (terminated) as XML in miniSEED 2";
break;
case SLPAYLOAD_MSEED2:
switch (subformat)
{
case 'E':
return "miniSEED 2 event detection";
break;
case 'C':
return "miniSEED 2 calibration";
break;
case 'T':
return "miniSEED 2 timing exception";
break;
case 'L':
return "miniSEED 2 log";
break;
case 'O':
return "miniSEED 2 opaque";
break;
default:
return "miniSEED 2";
}
break;
case SLPAYLOAD_MSEED3:
return "miniSEED 3";
break;
case SLPAYLOAD_JSON:
switch (subformat)
{
case SLPAYLOAD_JSON_INFO:
return "INFO in JSON";
break;
case SLPAYLOAD_JSON_ERROR:
return "ERROR in JSON";
break;
default:
return "JSON";
}
break;
default:
return "Unrecognized payload type";
}
} /* End of sl_typestr() */
/**********************************************************************/ /**
* @brief Return desriptive error message for system errno
*
* Return a description of the last system error, in the case of Win32
* this will be the last Windows Sockets error.
***************************************************************************/
const char *
sl_strerror (void)
{
#if defined(SLP_WIN)
static char errorstr[100];
snprintf (errorstr, sizeof (errorstr), "%d", WSAGetLastError ());
return (const char *)errorstr;
#else
return (const char *)strerror (errno);
#endif
} /* End of sl_strerror() */
/**********************************************************************/ /**
* @brief Get current time as nanosecond resolution Unix/POSIX time
*
* Actual resolution depends on system, nanosecond resolution should
* not be assumed.
*
* @returns Current time as nanoseconds since the Unix/POSIX epoch.
***************************************************************************/
int64_t
sl_nstime (void)
{
#if defined(SLP_WIN)
uint64_t rv;
FILETIME FileTime;
GetSystemTimeAsFileTime(&FileTime);
/* Full win32 epoch value, in 100ns */
rv = (((LONGLONG)FileTime.dwHighDateTime << 32) +
(LONGLONG)FileTime.dwLowDateTime);
rv -= 116444736000000000LL; /* Convert from FileTime to UNIX epoch time */
rv *= 100; /* Convert from 100ns to ns */
return rv;
#else
struct timeval tv;
gettimeofday (&tv, NULL);
return ((int64_t)tv.tv_sec * 1000000000 +
(int64_t)tv.tv_usec * 1000);
#endif
} /* End of sl_nstime() */
/**********************************************************************/ /**
* @brief Return ISO-compatible date-time formatted string
*
* Convert date-time string deliminters to match the following
* ISO-8601 date-time format if needed and possible:
*
* YYYY-MM-DDThh:mm:ss.sssssssssZ
*
* The output string will always be in UTC with a 'Z' designation if
* it contains a time portion.
*
* The output buffer pointer can be the same as the input pointer for
* an in-place conversion.
*
* This routine does very little validation, invalid input date-times
* will result in invalid conversions.
*
* The output buffer must be as large as the input date-time string
* plus a terminating 'Z' if not included in the original string.
*
* @param[out] isodatetime Buffer to write ISO-8601 time string
* @param[in] datetime Date-time string to convert
*
* @returns Date-time string converted ISO-8601 format
* @retval Pointer to date-time string on success
* @retval NULL on error
***************************************************************************/
char *
sl_isodatetime (char *isodatetime, const char *datetime)
{
char newchar;
int delims;
int idx;
if (datetime == NULL || isodatetime == NULL)
return NULL;
/* Create output string char-by-char from input string */
for (idx = 0, delims = 0; datetime[idx] != '\0'; idx++)
{
/* Pass through digits */
if (isdigit (datetime[idx]))
{
newchar = 0;
}
/* Pass through acceptable delimiters */
else if (datetime[idx] == '-' ||
datetime[idx] == 'T' ||
datetime[idx] == ':' ||
datetime[idx] == '.' ||
datetime[idx] == 'Z')
{
delims++;
newchar = 0;
}
/* Convert comma to appropriate delimiter */
else if (datetime[idx] == ',')
{
delims++;
switch (delims)
{
case 1:
case 2:
newchar = '-';
break;
case 3:
newchar = 'T';
break;
case 4:
case 5:
newchar = ':';
break;
case 6:
newchar = '.';
break;
default:
return NULL;
}
}
/* Unrecognized character in input date-time string */
else
{
return NULL;
}
/* Write new character if set */
if (newchar)
{
isodatetime[idx] = newchar;
}
/* Write input character if not the same buffer */
else if (datetime != isodatetime)
{
isodatetime[idx] = datetime[idx];
}
}
/* Add UTC 'Z' suffix if not present and time components included */
if (delims >= 3 && isodatetime[idx - 1] != 'Z')
{
isodatetime[idx++] = 'Z';
}
isodatetime[idx] = '\0';
return isodatetime;
} /* End of sl_isodatetime() */
/**********************************************************************/ /**
* @brief Return legacy SeedLink comma-deliminted date-time formatted string
*
* Convert date-time string deliminters to match the following
* comma-delimited format if needed and possible:
*
* YYYY,MM,DD,hh,mm,ss
*
* Truncate any fractional seconds, as they are not expected in the
* comma-delimited format.
*
* The output buffer pointer can be the same as the input pointer for
* an in-place conversion.
*
* This routine does very little validation, invalid input date-times
* will result in invalid conversions.
*
* @param[out] commadatetime Buffer to write comma-delimited time string
* @param[in] datetime Date-time string to convert
*
* @returns Date-time string converted comma-delimited format
* @retval Pointer to date-time string on success
* @retval NULL on error
***************************************************************************/
char *
sl_commadatetime (char *commadatetime, const char *datetime)
{
char newchar;
int delims;
int idx;
if (datetime == NULL || commadatetime == NULL)
return NULL;
/* Create output string char-by-char from input string */
for (idx = 0, delims = 0; datetime[idx] != '\0'; idx++)
{
/* Pass through digits */
if (isdigit (datetime[idx]))
{
newchar = 0;
}
/* Pass through acceptable delimiter */
else if (datetime[idx] == ',')
{
delims++;
newchar = 0;
}
/* Convert recognized delimiters to commas */
else if (datetime[idx] == '-' ||
datetime[idx] == 'T' ||
datetime[idx] == ':' ||
datetime[idx] == '.')
{
delims++;
newchar = ',';
}
/* Terminating 'Z' is skipped */
else if (datetime[idx] == 'Z' && datetime[idx+1] == '\0')
{
break;
}
/* Unrecognized character in input date-time string */
else
{
return NULL;
}
/* Truncate at separator for seconds and subseconds */
if (delims >= 6)
{
break;
}
/* Write new character if set */
if (newchar)
{
commadatetime[idx] = newchar;
}
/* Write input character if not the same buffer */
else if (datetime != commadatetime)
{
commadatetime[idx] = datetime[idx];
}
}
commadatetime[idx] = '\0';
return commadatetime;
} /* End of sl_commadatetime() */
/**********************************************************************/ /**
* @brief Convert SeedLink v3 to v4 selector string if possible
*
* SeedLink v3 seletcors are recognized in the following form:
*
* [LL]CCC[.T] (where LL and .T are optional)
*
* Example convesions:
* 00BHZ -> 00_B_H_Z
* BHZ -> *_B_H_Z
* EH?.D -> *_E_H_?.D
* --BHZ -> _B_H_Z
*
* This routine does very little validation, invalid input may result in
* invalid conversions.
*
* The output buffer must be large enough to accomodate the converted
* selector, a pragmatic maximum is 32 characters.
*
* @param[out] v4selector Buffer to write v4 selector string
* @param[in] v4selectorlength Length of v4selector buffer
* @param[in] selector Input selector
*
* @returns SeedLink v4 selector string
* @retval Pointer to v4 selector string on success
* @retval NULL when no conversion was possible
***************************************************************************/
char *
sl_v3to4selector (char *v4selector, int v4selectorlength, const char *selector)
{
size_t streamidlength;
char *type = NULL;
int emptylocation = 0;
int printed;
if (v4selector == NULL || selector == NULL)
return NULL;
/* Remove inital '-' characters indicating empty location code */
while (*selector == '-')
{
selector++;
emptylocation++;
}
/* Check if type is included */
if ((type = strchr (selector, '.')))
{
streamidlength = type - selector;
}
else
{
streamidlength = strlen (selector);
}
/* Check for valid v4 stream ID characters */
for (size_t idx = 0; idx < streamidlength; idx++)
{
if (isalnum ((int)selector[idx]) == 0 &&
selector[idx] != '?' &&
selector[idx] != '*')
{
return NULL;
}
}
/* CCC[.T] -> [*]_B_S_SS[.T] */
if (streamidlength == 3)
{
printed = snprintf (v4selector, v4selectorlength, "%s_%c_%c_%c%s",
(emptylocation) ? "" : "*",
selector[0], selector[1], selector[2],
(type) ? type : "");
if (printed >= v4selectorlength)
return NULL;
else
return v4selector;
}
/* LCCC[.T] -> L_B_S_SS[.T] */
else if (streamidlength == 4)
{
printed = snprintf (v4selector, v4selectorlength, "%c_%c_%c_%c%s",
selector[0], selector[1], selector[2], selector[3],
(type) ? type : "");
if (printed >= v4selectorlength)
return NULL;
else
return v4selector;
}
/* LLCCC[.T] -> LL_B_S_SS[.T] */
else if (streamidlength == 5)
{
printed = snprintf (v4selector, v4selectorlength, "%c%c_%c_%c_%c%s",
selector[0], selector[1], selector[2], selector[3], selector[4],
(type) ? type : "");
if (printed >= v4selectorlength)
return NULL;
else
return v4selector;
}
return NULL;
} /* End of sl_v3to4selector() */
/**********************************************************************/ /**
* @brief Sleep for a given number of microseconds
*
***************************************************************************/
void
sl_usleep (unsigned long int useconds)
{
#if defined(SLP_WIN)
SleepEx ((useconds / 1000), 1);
#else
struct timespec treq, trem;
treq.tv_sec = (time_t) (useconds / 1e6);
treq.tv_nsec = (long)((useconds * 1e3) - (treq.tv_sec * 1e9));
nanosleep (&treq, &trem);
#endif
} /* End of sl_usleep() */
/**********************************************************************/ /**
* @brief Copy 'source' string to 'dest' while removing spaces.
*
* Copy 'length' characters from 'source' to 'dest' while removing all
* spaces. The result is left justified and always null terminated.
* The source string must have at least 'length' characters and the
* destination string must have enough room needed for the non-space
* characters within 'length' and the null terminator.
*
* @return The number of characters (not including the null terminator) in
* the destination string.
***************************************************************************/
int
sl_strncpclean (char *dest, const char *source, int length)
{
int sidx, didx;
for (sidx = 0, didx = 0; sidx < length; sidx++)
{
if (*(source + sidx) != ' ')
{
*(dest + didx) = *(source + sidx);
didx++;
}
}
*(dest + didx) = '\0';
return didx;
} /* End of sl_strncpclean() */