1
0

stationsmodule.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include <Python.h>
  2. #include "stations-code.h"
  3. static struct {
  4. char *callsign;
  5. char *locator;
  6. } station_list[] = {
  7. #include "stations.dat.h"
  8. };
  9. static int hash_f (const char *s, const int *T)
  10. {
  11. register int i, sum = 0;
  12. for (i = 0; s[i] != '\0'; i++) {
  13. sum += T[i] * s[i];
  14. sum %= NG;
  15. }
  16. return sum;
  17. }
  18. static int perf_hash (const char *k)
  19. {
  20. if (strlen (k) > NS)
  21. return 0;
  22. return (G[ hash_f(k, T1) ] + G[ hash_f(k, T2)] ) % NG;
  23. }
  24. static int getlocator (char *locator, const char *callsign)
  25. {
  26. int hashval = perf_hash (callsign);
  27. if (hashval < NK && strcmp(callsign, station_list[hashval].callsign) == 0) {
  28. strcpy (locator, station_list[hashval].locator);
  29. return 1;
  30. }
  31. return 0;
  32. }
  33. static PyObject *
  34. stations_locator(PyObject *self, PyObject *args)
  35. {
  36. const char *callsign;
  37. char locator[6];
  38. if (!PyArg_ParseTuple(args, "s", &callsign))
  39. return NULL;
  40. return Py_BuildValue("s", (getlocator (locator, callsign) == 1) ?
  41. locator : NULL);
  42. }
  43. static PyMethodDef StationsMethods[] = {
  44. {"locator", stations_locator, METH_VARARGS,
  45. "Get locator from callsign."},
  46. {NULL, NULL, 0, NULL} /* Sentinel */
  47. };
  48. PyMODINIT_FUNC
  49. initstations(void)
  50. {
  51. (void) Py_InitModule("stations", StationsMethods);
  52. }