|
| 1 | +/* Taken from https://opensource.com/article/19/5/how-write-good-c-main-function */ |
| 2 | +#include <stdio.h> |
| 3 | +#include <stdlib.h> |
| 4 | +#include <unistd.h> |
| 5 | +#include <libgen.h> |
| 6 | +#include <errno.h> |
| 7 | +#include <string.h> |
| 8 | +#include <getopt.h> |
| 9 | + |
| 10 | +#define OPTSTR "va:l:h" |
| 11 | +#define USAGE_FMT "%s [-v] [-a latitude] [-l longitude] [-h]" |
| 12 | +#define ERR_LAT "lat" |
| 13 | +#define ERR_LON "lon" |
| 14 | +#define ERR_DO_GET_GRID "do_get_grid blew up" |
| 15 | +#define DEFAULT_PROGNAME "get_grid" |
| 16 | + |
| 17 | +extern int errno; |
| 18 | +extern char *optarg; |
| 19 | +extern int opterr, optind; |
| 20 | + |
| 21 | +typedef struct { |
| 22 | + int verbose; |
| 23 | + char *dst; |
| 24 | + double lon; |
| 25 | + double lat; |
| 26 | +} options_t; |
| 27 | + |
| 28 | +int dumb_global_variable = -11; |
| 29 | +void usage(char *progname, int opt); |
| 30 | +int do_get_grid(options_t *options); |
| 31 | +void calcLocator(char *dst, double lat, double lon); |
| 32 | + |
| 33 | +int main(int argc, char *argv[]) { |
| 34 | + int opt; |
| 35 | + options_t options = { 0, 0, 0.0, 0.0 }; |
| 36 | + |
| 37 | + opterr = 0; |
| 38 | + |
| 39 | + while ((opt = getopt(argc, argv, OPTSTR)) != EOF) |
| 40 | + switch(opt) { |
| 41 | + case 'a': |
| 42 | + if (!(options.lat = strtod(optarg, &optarg) )) { |
| 43 | + perror(ERR_LAT); |
| 44 | + exit(EXIT_FAILURE); |
| 45 | + /* NOTREACHED */ |
| 46 | + } |
| 47 | + break; |
| 48 | + |
| 49 | + case 'l': |
| 50 | + if (!(options.lon = strtod(optarg, &optarg) )) { |
| 51 | + perror(ERR_LON); |
| 52 | + exit(EXIT_FAILURE); |
| 53 | + /* NOTREACHED */ |
| 54 | + } |
| 55 | + break; |
| 56 | + |
| 57 | + case 'v': |
| 58 | + options.verbose += 1; |
| 59 | + break; |
| 60 | + |
| 61 | + case 'h': |
| 62 | + default: |
| 63 | + usage(basename(argv[0]), opt); |
| 64 | + /* NOTREACHED */ |
| 65 | + break; |
| 66 | + } |
| 67 | + |
| 68 | + if (do_get_grid(&options) != EXIT_SUCCESS) { |
| 69 | + perror(ERR_DO_GET_GRID); |
| 70 | + exit(EXIT_FAILURE); |
| 71 | + /* NOTREACHED */ |
| 72 | + } |
| 73 | + fprintf(stdout, "%s", options.dst); |
| 74 | + free(options.dst); |
| 75 | + return EXIT_SUCCESS; |
| 76 | +} |
| 77 | + |
| 78 | +void usage(char *progname, int opt) { |
| 79 | + fprintf(stderr, USAGE_FMT, progname ? progname:DEFAULT_PROGNAME); |
| 80 | + exit(EXIT_FAILURE); |
| 81 | + /* NOTREACHED */ |
| 82 | +} |
| 83 | + |
| 84 | +int do_get_grid(options_t *options) { |
| 85 | + if (!options) { |
| 86 | + errno = EINVAL; |
| 87 | + return EXIT_FAILURE; |
| 88 | + } |
| 89 | + |
| 90 | + if (!options->lon || !options->lat) { |
| 91 | + errno = ENOENT; |
| 92 | + return EXIT_FAILURE; |
| 93 | + } |
| 94 | + |
| 95 | + options->dst = malloc((sizeof(char) * 3) + 1); /* + 1 allows for null string terminator. */ |
| 96 | + |
| 97 | + calcLocator(options->dst, options->lat, options->lon); |
| 98 | + return EXIT_SUCCESS; |
| 99 | +} |
| 100 | + |
| 101 | +/* https://ham.stackexchange.com/a/5599 */ |
| 102 | +void calcLocator(char *dst, double lat, double lon) { |
| 103 | + int o1, o2, o3; |
| 104 | + int a1, a2, a3; |
| 105 | + double remainder; |
| 106 | + // longitude |
| 107 | + remainder = lon + 180.0; |
| 108 | + o1 = (int)(remainder / 20.0); |
| 109 | + remainder = remainder - (double)o1 * 20.0; |
| 110 | + o2 = (int)(remainder / 2.0); |
| 111 | + remainder = remainder - 2.0 * (double)o2; |
| 112 | + o3 = (int)(12.0 * remainder); |
| 113 | + |
| 114 | + // latitude |
| 115 | + remainder = lat + 90.0; |
| 116 | + a1 = (int)(remainder / 10.0); |
| 117 | + remainder = remainder - (double)a1 * 10.0; |
| 118 | + a2 = (int)(remainder); |
| 119 | + remainder = remainder - (double)a2; |
| 120 | + a3 = (int)(24.0 * remainder); |
| 121 | + dst[0] = (char)o1 + 65; |
| 122 | + dst[1] = (char)a1 + 65; |
| 123 | + dst[2] = (char)o2 + 48; |
| 124 | + dst[3] = (char)a2 + 48; |
| 125 | + dst[4] = (char)o3 + 97; |
| 126 | + dst[5] = (char)a3 + 97; |
| 127 | + /* dst[0] = (char)o1 + 'A'; |
| 128 | + dst[1] = (char)a1 + 'A'; |
| 129 | + dst[2] = (char)o2 + '0'; |
| 130 | + dst[3] = (char)a2 + '0'; |
| 131 | + dst[4] = (char)o3 + 'A'; |
| 132 | + dst[5] = (char)a3 + 'A';*/ |
| 133 | + dst[6] = (char)0; |
| 134 | +} |
0 commit comments