This repository was archived by the owner on Oct 26, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathhelpers.c
More file actions
252 lines (227 loc) · 6.38 KB
/
helpers.c
File metadata and controls
252 lines (227 loc) · 6.38 KB
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
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license in the file COPYING
* or http://www.opensource.org/licenses/CDDL-1.0.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file COPYING.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2015 Saso Kiselkov. All rights reserved.
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include "helpers.h"
/*
* How to turn to get from hdg1 to hdg2 with positive being right and negative
* being left. Always turns the shortest way around (<= 180 degrees).
*/
double
rel_hdg(double hdg1, double hdg2)
{
ASSERT(is_valid_hdg(hdg1) && is_valid_hdg(hdg2));
if (hdg1 > hdg2) {
if (hdg1 > hdg2 + 180)
return (360 - hdg1 + hdg2);
else
return (-(hdg1 - hdg2));
} else {
if (hdg2 > hdg1 + 180)
return (-(360 - hdg2 + hdg1));
else
return (hdg2 - hdg1);
}
}
bool_t
is_valid_vor_freq(double freq_mhz)
{
unsigned freq_khz = freq_mhz * 1000;
/* Check correct frequency band */
if (freq_khz < 108000 || freq_khz > 117950)
return (0);
/*
* Check the LOC band - freq must be multiple of 200 kHz or
* remainder must be 50 kHz.
*/
if (freq_khz >= 108000 && freq_khz <= 112000 &&
freq_khz % 200 != 0 && freq_khz % 200 != 50)
return (0);
/* Above 112 MHz, frequency must be multiple of 50 kHz */
if (freq_khz % 50 != 0)
return (0);
return (1);
}
bool_t
is_valid_loc_freq(double freq_mhz)
{
unsigned freq_khz = freq_mhz * 1000;
/* Check correct frequency band */
if (freq_khz < 108100 || freq_khz > 111950)
return (0);
/* Check 200 kHz spacing with 100 kHz or 150 kHz remainder. */
if (freq_khz % 200 != 100 && freq_khz % 200 != 150)
return (0);
return (1);
}
bool_t
is_valid_tacan_freq(double freq_mhz)
{
unsigned freq_khz = freq_mhz * 1000;
/* this is quite a guess! */
if (freq_khz < 133000 || freq_khz > 136000 ||
freq_khz % 100 != 0)
return (0);
return (1);
}
bool_t
is_valid_ndb_freq(double freq_khz)
{
unsigned freq_hz = freq_khz * 1000;
/* 177 kHz for an NDB is the lowest I've ever seen */
return (freq_hz >= 177000 && freq_hz <= 1750000);
}
/*
* Checks a runway ID for correct formatting. Runway IDs are
* 2-, 3- or 4-character strings conforming to the following format:
* *) Two-digit runway heading between 01 and 36. Headings less
* than 10 are always prefixed by a '0'. "00" is NOT valid.
* *) An optional parallel runway discriminator, one of 'L', 'R' or 'C'.
* *) An optional true magnetic heading indicator 'T'. Runways
* which use true headings must belong to an airport_t with
* true_hdg set (indicated by passing the appropriate argument).
*/
bool_t
is_valid_rwy_ID(const char *rwy_ID)
{
int hdg;
int len = strlen(rwy_ID);
if (len < 2 || len > 4 || !isdigit(rwy_ID[0]) || !isdigit(rwy_ID[1]))
return (B_FALSE);
hdg = atoi(rwy_ID);
if (hdg == 0 || hdg > 36)
return (B_FALSE);
if (len == 3) {
if (rwy_ID[2] != 'R' && rwy_ID[2] != 'L' &&
rwy_ID[2] != 'C' && rwy_ID[2] != 'T')
return (B_FALSE);
} else if (len == 4) {
if ((rwy_ID[2] != 'R' && rwy_ID[2] != 'L' &&
rwy_ID[2] != 'C') || rwy_ID[3] != 'T')
return (B_FALSE);
}
return (B_TRUE);
}
/*
* Grabs the next non-empty, non-comment line from a file, having stripped
* away all leading and trailing whitespace.
*
* @param fp File from which to retrieve the line.
* @param linep Line buffer which will hold the new line. If the buffer pointer
* is set to NULL, it will be allocated. If it is not long enough, it
* will be expanded.
* @param linecap The capacity of *linep. If set to zero a new buffer is
* allocated.
* @param linenum The current line number. Will be advanced by 1 for each
* new line read.
*
* @return The number of characters in the line (after stripping whitespace)
* without the terminating NUL.
*/
ssize_t
parser_get_next_line(FILE *fp, char **linep, size_t *linecap, size_t *linenum)
{
for (;;) {
ssize_t len = getline(linep, linecap, fp);
if (len == -1)
return (-1);
(*linenum)++;
strip_space(*linep);
if (**linep != 0 && **linep == '#')
continue;
return (strlen(*linep));
}
}
/*
* Breaks up a line into components delimited by a character.
*
* @param line The input line to break up. The buffer will be modified
* to insert NULs in between the components so that they can each
* be treated as a separate string.
* @param delim The delimiter character (e.g. ',').
* @param comps A list of pointers that will be set to point to the
* start of each substring.
* @param capacity The component capacity of `comps'. If more input
* components are encountered than is space in `comps', the array
* is not overflown.
*
* @return The number of components in the input string (at least 1).
* If the `comps' array was too small, returns the number of
* components that would have been needed to process the input
* string fully as a negative value.
*/
ssize_t
explode_line(char *line, char delim, char **comps, size_t capacity)
{
ssize_t i = 1;
bool_t toomany = B_FALSE;
ASSERT(capacity != 0);
comps[0] = line;
for (char *p = line; *p != 0; p++) {
if (*p == delim) {
*p = 0;
if (i < (ssize_t)capacity)
comps[i] = p + 1;
else
toomany = B_TRUE;
i++;
}
}
return (toomany ? -i : i);
}
/*
* Removes all leading & trailing whitespace from a line.
*/
void
strip_space(char *line)
{
char *p;
size_t len = strlen(line);
/* strip leading whitespace */
for (p = line; *p && isspace(*p); p++)
;
if (p != line)
memmove(line, p, (p + len) - p + 1);
for (p = line + len - 1; p >= line && isspace(*p); p--)
;
p[1] = 0;
}
void
append_format(char **str, size_t *sz, const char *format, ...)
{
va_list ap;
int needed;
va_start(ap, format);
needed = vsnprintf(NULL, 0, format, ap);
va_end(ap);
va_start(ap, format);
*str = realloc(*str, *sz + needed + 1);
(void) vsnprintf(*str + *sz, *sz + needed + 1, format, ap);
*sz += needed;
va_end(ap);
}