From 8d24cdce756fc8a4b9ef0b273b841b18b0837f04 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 23 Apr 2015 18:31:49 +0100 Subject: meh --- librns510.c | 96 +++++++++++++ librns510.h | 3 + rns510.html | 463 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ rns510.pyx | 47 ++++++ setup.py | 16 +++ 5 files changed, 625 insertions(+) create mode 100644 librns510.c create mode 100644 librns510.h create mode 100644 rns510.html create mode 100644 rns510.pyx create mode 100755 setup.py diff --git a/librns510.c b/librns510.c new file mode 100644 index 0000000..060c766 --- /dev/null +++ b/librns510.c @@ -0,0 +1,96 @@ +#include +#include +#include +#include +#include +#include +#include + +#define POI_CA 0x80000034 /*0 */ +#define POI_CB 0xF1C71CD4 /*160 */ +// +#define ZERO (POI_CA) +#define SCALE (((double) (POI_CB-POI_CA))/160.) + +static void +fromz (int64_t a, double *lat, double *lon) +{ + uint64_t b, c; + uint32_t d, e, f; + + memcpy (&b, &a, sizeof (a)); + e = f = 0; + + for (c = 1, d = 1; c; c <<= 1, d <<= 1) + { + if (b & c) + e |= d; + c <<= 1; + if (b & c) + f |= d; + } + + *lon = e; + *lat = f; + + *lon -= ZERO; + *lat -= ZERO; + + *lon /= SCALE; + *lat /= SCALE; + +} + +static void +toz (double lat, double lon, int64_t * a) +{ + uint64_t b, c; + uint32_t d, e, f; + + lon *= SCALE; + lat *= SCALE; + lon += ZERO; + lat += ZERO; + + e = (uint32_t) (lon + .5); + f = (uint32_t) (lat + .5); + + //printf("%.1f %x\n",lat-ZERO, e,f); + + b = 0; + for (c = 1, d = 1; c; c <<= 1, d <<= 1) + { + if (e & d) + b |= c; + c <<= 1; + if (f & d) + b |= c; + } + + + memcpy (a, &b, sizeof (*a)); +} + +void +librns510_encode (const char *slat, const char *slon, char *out) +{ + double lat = atof (slat), lon = atof (slon); + int64_t z; + + toz (lat, lon, &z); + + sprintf (out, "%" PRId64, z); +} + +void +librns510_decode (const char *code, char *slat, char *slon) +{ + double lat, lon; + int64_t z = atoll (code); + + fromz (z, &lat, &lon); + + sprintf (slat, "%.10f", lat); + sprintf (slon, "%.10f", lon); + +} diff --git a/librns510.h b/librns510.h new file mode 100644 index 0000000..377ba61 --- /dev/null +++ b/librns510.h @@ -0,0 +1,3 @@ +/* librns510.c */ +void librns510_encode (const char *slat, const char *slon, char *out); +void librns510_decode (const char *code, char *slat, char *slon); diff --git a/rns510.html b/rns510.html new file mode 100644 index 0000000..4a4f67e --- /dev/null +++ b/rns510.html @@ -0,0 +1,463 @@ + + + + + + + + + +

Generated by Cython 0.21.1

+

Raw output: rns510.c

+
 01: import cython
+
 02: 
+
 03: from libc.stdlib cimport malloc
+
 04: from libc.stdlib cimport free
+
 05: 
+
 06: cdef extern from "librns510.h":
+
 07: 	void librns510_encode(const char *lat, const char *lon, char *code)
+
 08: cdef extern from "librns510.h":
+
 09: 	char *librns510_decode(const char *code, char *lat, char *lon)
+
 10: 
+
+11: cdef rns510_encode(char *lat, char *lon):
+
static PyObject *__pyx_f_6rns510_rns510_encode(char *__pyx_v_lat, char *__pyx_v_lon) {
+  PyObject *__pyx_v_py_string = 0;
+  char *__pyx_v_out;
+  PyObject *__pyx_r = NULL;
+  __Pyx_RefNannyDeclarations
+  __Pyx_RefNannySetupContext("rns510_encode", 0);
+/* … */
+  /* function exit code */
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_AddTraceback("rns510.rns510_encode", __pyx_clineno, __pyx_lineno, __pyx_filename);
+  __pyx_r = 0;
+  __pyx_L0:;
+  __Pyx_XDECREF(__pyx_v_py_string);
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_RefNannyFinishContext();
+  return __pyx_r;
+}
+
+12: 	assert lat is not NULL
+
  #ifndef CYTHON_WITHOUT_ASSERTIONS
+  if (unlikely(!Py_OptimizeFlag)) {
+    if (unlikely(!((__pyx_v_lat != NULL) != 0))) {
+      PyErr_SetNone(PyExc_AssertionError);
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 12; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    }
+  }
+  #endif
+
+13: 	assert lon is not NULL
+
  #ifndef CYTHON_WITHOUT_ASSERTIONS
+  if (unlikely(!Py_OptimizeFlag)) {
+    if (unlikely(!((__pyx_v_lon != NULL) != 0))) {
+      PyErr_SetNone(PyExc_AssertionError);
+      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 13; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    }
+  }
+  #endif
+
 14: 	cdef bytes py_string
+
 15: 	cdef char *out;
+
 16: 
+
+17: 	out=<char *> malloc(128)
+
  __pyx_v_out = ((char *)malloc(128));
+
+18: 	librns510_encode(lat,lon,out)
+
  librns510_encode(__pyx_v_lat, __pyx_v_lon, __pyx_v_out);
+
+19: 	try:
+
  /*try:*/ {
+
+20: 		py_string=out
+
    __pyx_t_1 = __Pyx_PyBytes_FromString(__pyx_v_out); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 20; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
+    __Pyx_GOTREF(__pyx_t_1);
+    __pyx_v_py_string = ((PyObject*)__pyx_t_1);
+    __pyx_t_1 = 0;
+  }
+
 21: 	finally:
+
+22: 		free(out)
+
  /*finally:*/ {
+    /*normal exit:*/{
+      free(__pyx_v_out);
+      goto __pyx_L5;
+    }
+    /*exception exit:*/{
+      __pyx_L4_error:;
+      __pyx_t_5 = 0; __pyx_t_6 = 0; __pyx_t_7 = 0; __pyx_t_8 = 0; __pyx_t_9 = 0; __pyx_t_10 = 0;
+      __Pyx_XDECREF(__pyx_t_1); __pyx_t_1 = 0;
+      if (PY_MAJOR_VERSION >= 3) __Pyx_ExceptionSwap(&__pyx_t_8, &__pyx_t_9, &__pyx_t_10);
+      if ((PY_MAJOR_VERSION < 3) || unlikely(__Pyx_GetException(&__pyx_t_5, &__pyx_t_6, &__pyx_t_7) < 0)) __Pyx_ErrFetch(&__pyx_t_5, &__pyx_t_6, &__pyx_t_7);
+      __Pyx_XGOTREF(__pyx_t_5);
+      __Pyx_XGOTREF(__pyx_t_6);
+      __Pyx_XGOTREF(__pyx_t_7);
+      __Pyx_XGOTREF(__pyx_t_8);
+      __Pyx_XGOTREF(__pyx_t_9);
+      __Pyx_XGOTREF(__pyx_t_10);
+      __pyx_t_2 = __pyx_lineno; __pyx_t_3 = __pyx_clineno; __pyx_t_4 = __pyx_filename;
+      {
+        free(__pyx_v_out);
+      }
+      if (PY_MAJOR_VERSION >= 3) {
+        __Pyx_XGIVEREF(__pyx_t_8);
+        __Pyx_XGIVEREF(__pyx_t_9);
+        __Pyx_XGIVEREF(__pyx_t_10);
+        __Pyx_ExceptionReset(__pyx_t_8, __pyx_t_9, __pyx_t_10);
+      }
+      __Pyx_XGIVEREF(__pyx_t_5);
+      __Pyx_XGIVEREF(__pyx_t_6);
+      __Pyx_XGIVEREF(__pyx_t_7);
+      __Pyx_ErrRestore(__pyx_t_5, __pyx_t_6, __pyx_t_7);
+      __pyx_t_5 = 0; __pyx_t_6 = 0; __pyx_t_7 = 0; __pyx_t_8 = 0; __pyx_t_9 = 0; __pyx_t_10 = 0;
+      __pyx_lineno = __pyx_t_2; __pyx_clineno = __pyx_t_3; __pyx_filename = __pyx_t_4;
+      goto __pyx_L1_error;
+    }
+    __pyx_L5:;
+  }
+
 23: 
+
+24: 	return py_string
+
  __Pyx_XDECREF(__pyx_r);
+  __Pyx_INCREF(__pyx_v_py_string);
+  __pyx_r = __pyx_v_py_string;
+  goto __pyx_L0;
+
 25: 
+
diff --git a/rns510.pyx b/rns510.pyx new file mode 100644 index 0000000..ca0f0ff --- /dev/null +++ b/rns510.pyx @@ -0,0 +1,47 @@ +import cython + +from libc.stdlib cimport malloc +from libc.stdlib cimport free + +cdef extern from "librns510.h": + void librns510_encode(const char *lat, const char *lon, char *code) +cdef extern from "librns510.h": + char *librns510_decode(const char *code, char *lat, char *lon) + +def encode(char *lat, char *lon): + assert lat is not NULL + assert lon is not NULL + cdef bytes py_string + cdef char *out + + out= malloc(128) + librns510_encode(lat,lon,out) + try: + py_string=out + finally: + free(out) + + return py_string + +def decode(char *code): + assert code is not NULL + cdef bytes py_lat + cdef bytes py_lon + cdef char *lat + cdef char *lon + + lat= malloc(128) + lon= malloc(128) + librns510_decode(code,lat,lon) + try: + py_lat=lat + finally: + free(lat) + + try: + py_lon=lon + finally: + free(lon) + + return py_lat,py_lon + diff --git a/setup.py b/setup.py new file mode 100755 index 0000000..7c75641 --- /dev/null +++ b/setup.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python + +from distutils.core import setup +from Cython.Build import cythonize +from distutils.extension import Extension + + +sourcefiles = ['rns510.pyx', 'librns510.c' ] + +extensions = [Extension("rns510", sourcefiles)] + +setup( + name = "rns510", + ext_modules = cythonize(extensions) +) + -- cgit v1.2.3