Skip to content

Commit 8ef5a72

Browse files
committed
First commit
1 parent c906be1 commit 8ef5a72

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

mapcode/mapcode_swig.c

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include <wchar.h>
2+
#include "mapcode_swig.h"
3+
#include "mapcoder.c"
4+
5+
6+
char *version(void)
7+
{
8+
return mapcode_cversion;
9+
}
10+
11+
12+
int isvalid(char *mapcode, int includes_territory)
13+
{
14+
if (compareWithMapcodeFormat(mapcode, includes_territory ? 1 : 0) == 0) {
15+
return 1;
16+
} else {
17+
return 0;
18+
}
19+
}
20+
21+
22+
char encode_result[MAX_NR_OF_MAPCODE_RESULTS*2];
23+
char **encode(double latitude, double longitude, char *territory, int extra_digits)
24+
{
25+
int territorycode = 0;
26+
char **s = (char **) encode_result;
27+
28+
if (territory) {
29+
territorycode = convertTerritoryIsoNameToCode(territory, 0);
30+
/*
31+
printf("debug1: encode: territorystring: %s, code: %d\n", territory, territorycode);
32+
*/
33+
if (territorycode < 0) {
34+
/* terminate array pointer so caller can detect it's empty */
35+
s[0] = 0;
36+
return s;
37+
}
38+
}
39+
40+
/* printf("debug2: encode: territorystring: %s, code: %d\n", territory, territorycode);
41+
*/
42+
int n = encodeLatLonToMapcodes((char **) &encode_result, latitude, longitude, territorycode, extra_digits);
43+
if (n > 0) {
44+
/* for (int i = 0; i < n; ++i) {
45+
printf("debug: %s - %s\n", s[i*2], s[(i*2)+1]);
46+
}
47+
printf("debug: count = %d\n", i);
48+
*/
49+
/* terminate array pointer at the end */
50+
s[n * 2] = 0;
51+
52+
return s;
53+
} else {
54+
/* terminate array pointer at beginning */
55+
s[0] = 0;
56+
}
57+
return s;
58+
}
59+
60+
61+
char encode_single_result[MAX_NR_OF_MAPCODE_RESULTS];
62+
char *encode_single(double latitude, double longitude, char *territory, int extra_digits)
63+
{
64+
int territorycode = 0;
65+
66+
if (territory) {
67+
territorycode = convertTerritoryIsoNameToCode(territory, 0);
68+
if (territorycode < 0)
69+
return NULL;
70+
}
71+
72+
/*
73+
printf("debug: encode_single: territorystring: %s, code: %d\n", territory, territorycode);
74+
*/
75+
if (encodeLatLonToSingleMapcode(encode_single_result, latitude, longitude, territorycode, extra_digits) > 0) {
76+
return encode_single_result;
77+
} else
78+
return NULL;
79+
}
80+
81+
82+
int decode(double *latitude, double *longitude, const char *mapcode, char *territory)
83+
{
84+
int territorycode = 0;
85+
86+
if (territory) {
87+
territorycode = convertTerritoryIsoNameToCode(territory, 0);
88+
if (territorycode < 0) {
89+
*latitude = 0;
90+
*longitude = 0;
91+
return 1;
92+
}
93+
}
94+
return decodeMapcodeToLatLon(latitude, longitude, mapcode, territorycode);
95+
}

0 commit comments

Comments
 (0)