-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcobra.c
More file actions
153 lines (124 loc) · 4.56 KB
/
cobra.c
File metadata and controls
153 lines (124 loc) · 4.56 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
/*
* Copyright (c) 2016, dashie <dashie@sigpipe.me>
*
* This file is part of libqth
*
* libqth is free software; you can redistribute it and/or modify it
* under the terms of the 3-clause BSD License as published by the
* Free Software Foundation: http://directory.fsf.org/wiki/License:BSD_3Clause
*
* libqth 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.
*
* You should have received a copy of the 3-clause BSD License along with GA;
* if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include <Python.h>
#include "qth.h"
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
static char module_docstring[] = "This module provides an interface to the libqth using C.";
static char qth_to_coords_docstring[] = "Converts a C string QTH locator to floating point coordinates\r\nqth_to_coords(qth, precision)";
static char coords_to_qth_docstring[] = "Converts floating point coordinates to a QTH locator.\r\ncoords_to_qth(lat, lon, precision)";
static char is_valid_qth_docstring[] = "Validates that a C string is a valid QTH\r\ncoords_to_qth(qth, precision)";
static PyObject *qth_to_coords_libqth(PyObject *self, PyObject *args);
static PyObject *coords_to_qth_libqth(PyObject *self, PyObject *args);
static PyObject *is_valid_qth_libqth(PyObject *self, PyObject *args);
static PyMethodDef module_methods[] = {
{"qth_to_coords", qth_to_coords_libqth, METH_VARARGS, qth_to_coords_docstring},
{"coords_to_qth", coords_to_qth_libqth, METH_VARARGS, coords_to_qth_docstring},
{"is_valid_qth", is_valid_qth_libqth, METH_VARARGS, is_valid_qth_docstring},
{NULL, NULL, 0, NULL}
};
#if PY_MAJOR_VERSION >= 3
PyMODINIT_FUNC PyInit_libqth(void) {
PyObject *module;
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"libqth",
module_docstring,
-1,
module_methods,
NULL,
NULL,
NULL,
NULL
};
module = PyModule_Create(&moduledef);
if (!module) return NULL;
return module;
}
#else
PyMODINIT_FUNC initlibqth(void)
{
PyObject *m = Py_InitModule3("libqth", module_methods, module_docstring);
if (m == NULL)
return;
}
#endif
// int qth_to_coords( const char *qth, size_t qthStringSize, double *latitude, double *longitude, size_t *precision);
static PyObject *qth_to_coords_libqth(PyObject *self, PyObject *args) {
char *qth;
double latitude = -300, longitude = -300;
size_t precision = 0;
PyObject *coordsDict;
/* Parse the input tuple */
if (!PyArg_ParseTuple(args, "sn", &qth, &precision))
return NULL;
size_t qthStringSize = strlen(qth);
/* Call the external C function to compute the coordinates. */
int retcode = qth_to_coords(qth, qthStringSize, &latitude, &longitude, &precision);
if (retcode != 0) {
Py_RETURN_FALSE;
}
/* Build the output dict */
coordsDict = Py_BuildValue("{s:s, s:I, s:d, s:d}",
"qth", qth,
"precision", precision,
"latitude", latitude,
"longitude", longitude);
return coordsDict;
}
// int coords_to_qth( double latitude, double longitude, size_t precision, char *qth);
static PyObject *coords_to_qth_libqth(PyObject *self, PyObject *args) {
double latitude = -300, longitude = -300;
size_t precision = 0;
char qth[13];
memset( qth, 0, 13);
PyObject *qthDict;
/* Parse the input tuple */
if (!PyArg_ParseTuple(args, "ddn", &latitude, &longitude, &precision))
return NULL;
/* Call the external C function to compute the QTH locator. */
int retcode = coords_to_qth(latitude, longitude, precision, qth);
if (retcode == EDOM) {
Py_RETURN_FALSE;
}
/* Build the output dict */
qthDict = Py_BuildValue("{s:s, s:I, s:d, s:d}",
"qth", qth,
"precision", precision,
"latitude", latitude,
"longitude", longitude);
return qthDict;
}
// int is_valid_qth( const char *qthToTest, size_t qthStringSize, size_t *precision);
static PyObject *is_valid_qth_libqth(PyObject *self, PyObject *args) {
char *qth;
size_t precision = 0;
/* Parse the input tuple */
if (!PyArg_ParseTuple(args, "sn", &qth, &precision))
return NULL;
size_t qthStringSize = strlen(qth);
/* Call the external C function to check the QTH locator validity. */
int retcode = is_valid_qth(qth, qthStringSize, &precision);
if (retcode == EDOM)
Py_RETURN_FALSE;
else
Py_RETURN_TRUE;
}