Skip to content

Commit 02f39ca

Browse files
Extended power of getTerritoryCode
1 parent ef96f1a commit 02f39ca

File tree

2 files changed

+35
-25
lines changed

2 files changed

+35
-25
lines changed

mapcodelib/mapcoder.c

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,14 +1494,14 @@ static int decoderEngine(decodeRec *dec) {
14941494
if (s) {
14951495
*s++ = 0;
14961496
while (*s > 0 && *s <= 32) { s++; }
1497-
ccode = convertTerritoryIsoNameToCode(w, dec->context - 1) - 1;
1497+
ccode = getTerritoryCode(w, dec->context - 1) - 1;
14981498
}
14991499
else {
15001500
ccode = dec->context - 1;
15011501
s = w;
15021502
}
15031503
if (ccode == ccode_mex && len < 8) {
1504-
ccode = convertTerritoryIsoNameToCode("5MX", -1) - 1;
1504+
ccode = getTerritoryCode("5MX", -1) - 1;
15051505
} // special case for mexico country vs state
15061506
if ((*s == 'u') || (*s == 'U')) {
15071507
strcpy(s, s + 1);
@@ -2074,14 +2074,20 @@ static int cmp_alphacode(const void *e1, const void *e2) {
20742074
static int binfindmatch(const int parentcode, const char *str) {
20752075
// build a 4-letter uppercase search term
20762076
char tmp[5];
2077+
const char *r = str;
2078+
int len = 0;
2079+
20772080
if (parentcode < 0) { return -1; }
20782081
if (parentcode > 0) {
2079-
tmp[0] = (char) ('0' + parentcode);
2080-
memcpy(tmp + 1, str, 3);
2081-
} else {
2082-
memcpy(tmp, str, 4);
2082+
tmp[len++] = (char) ('0' + parentcode);
20832083
}
2084-
tmp[4] = 0;
2084+
while ((len < 4) && (*r > 32)) {
2085+
tmp[len++] = *r++;
2086+
}
2087+
if (*r > 32) {
2088+
return -1;
2089+
}
2090+
tmp[len] = 0;
20852091
makeupper(tmp);
20862092
{ // binary-search the result
20872093
const alphaRec *p;
@@ -2101,20 +2107,20 @@ static int binfindmatch(const int parentcode, const char *str) {
21012107

21022108
// PUBLIC - returns territoryCode of string (or negative if not found).
21032109
// optional_tc: context territoryCode to handle ambiguities (pass <=0 if unknown).
2104-
int convertTerritoryIsoNameToCode(const char *string, int optional_tc)
2110+
int getTerritoryCode(const char *string, int optional_tc)
21052111
{
2106-
const int ccode = optional_tc - 1;
21072112
if (string == NULL) { return -1; }
21082113
while (*string > 0 && *string <= 32) { string++; } // skip leading whitespace
21092114

21102115
if (string[0] && string[1]) {
2116+
const int ccode = optional_tc - 1;
21112117
if (string[2] == '-') {
21122118
return binfindmatch(getParentcode(string, 2), string + 3);
21132119
} else if (string[2] && string[3] == '-') {
21142120
return binfindmatch(getParentcode(string, 3), string + 4);
2115-
} else if (optional_tc > 0) {
2116-
int parentcode = parentnumber[ccode];
2117-
int b = binfindmatch(parentcode, string);
2121+
} else {
2122+
const int parentcode = ccode<0 ? 0 : ((parentnumber[ccode] > 0) ? parentnumber[ccode] : parentnumber[ParentTerritoryOf(ccode)]);
2123+
const int b = binfindmatch(parentcode, string);
21182124
if (b > 0) {
21192125
return b;
21202126
} //
@@ -2144,11 +2150,15 @@ int decodeMapcodeToLatLon(double *lat, double *lon, const char *input,
21442150
}
21452151
}
21462152

2147-
// PUBLIC - encode lat,lon for (optional) TerritoryCode tc to a mapcode with extraDigits accuracy
2153+
// PUBLIC - encode lat,lon for TerritoryCode tc to a mapcode with extraDigits accuracy
21482154
int encodeLatLonToSingleMapcode(char *result, double lat, double lon, int tc, int extraDigits) {
21492155
char *v[2];
21502156
Mapcodes rlocal;
2151-
const int ret = encodeLatLonToMapcodes_internal(v, &rlocal, lat, lon, tc, 1, debugStopAt, extraDigits);
2157+
int ret;
2158+
if (tc <= 0) {
2159+
return 0;
2160+
}
2161+
ret = encodeLatLonToMapcodes_internal(v, &rlocal, lat, lon, tc, 1, debugStopAt, extraDigits);
21522162
*result = 0;
21532163
if (ret <= 0) { // no solutions?
21542164
return -1;

mapcodelib/mapcoder.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
extern "C" {
1919
#endif
2020

21-
#define mapcode_cversion "2.2"
21+
#define mapcode_cversion "2.2.1"
2222

2323
#define UWORD unsigned short int // 2-byte unsigned integer.
2424

@@ -52,7 +52,7 @@ typedef struct {
5252
* mapcodes - a pointer to an Mapcodes, allocated by the caller.
5353
* lat - Latitude, in degrees. Range: -90..90.
5454
* lon - Longitude, in degrees. Range: -180..180.
55-
* territoryCode - Territory code (obtained from convertTerritoryIsoNameToCode), used as encoding context.
55+
* territoryCode - Territory code (obtained from getTerritoryCode), used as encoding context.
5656
* Pass 0 to get Mapcodes for all territories.
5757
* extraDigits - Number of extra "digits" to add to the generated mapcode. The preferred default is 0.
5858
* Other valid values are 1 and 2, which will add extra letters to the mapcodes to
@@ -84,7 +84,7 @@ int encodeLatLonToMapcodes(
8484
* by the next call to this method!
8585
* lat - Latitude, in degrees. Range: -90..90.
8686
* lon - Longitude, in degrees. Range: -180..180.
87-
* territoryCode - Territory code (obtained from convertTerritoryIsoNameToCode), used as encoding context.
87+
* territoryCode - Territory code (obtained from getTerritoryCode), used as encoding context.
8888
* Pass 0 to get Mapcodes for all territories.
8989
* extraDigits - Number of extra "digits" to add to the generated mapcode. The preferred default is 0.
9090
* Other valid values are 1 and 2, which will add extra letters to the mapcodes to
@@ -113,8 +113,7 @@ int encodeLatLonToMapcodes_Deprecated( // Warning: this method is deprecated
113113
* The caller should allocate at least MAX_MAPCODE_RESULT_LEN characters for the string.
114114
* lat - Latitude, in degrees. Range: -90..90.
115115
* lon - Longitude, in degrees. Range: -180..180.
116-
* territoryCode - Territory code (obtained from convertTerritoryIsoNameToCode), used as encoding context.
117-
* Pass 0 to get the shortest Mapcode for all territories.
116+
* territoryCode - Territory code (obtained from getTerritoryCode), used as encoding context.
118117
* extraDigits - Number of extra "digits" to add to the generated mapcode. The preferred default is 0.
119118
* Other valid values are 1 and 2, which will add extra letters to the mapcodes to
120119
* make them represent the coordinate more accurately.
@@ -136,7 +135,7 @@ int encodeLatLonToSingleMapcode(
136135
* lat - Decoded latitude, in degrees. Range: -90..90.
137136
* lon - Decoded longitude, in degrees. Range: -180..180.
138137
* mapcode - Mapcode to decode.
139-
* territoryCode - Territory code (obtained from convertTerritoryIsoNameToCode), used as decoding context.
138+
* territoryCode - Territory code (obtained from getTerritoryCode), used as decoding context.
140139
* Pass 0 if not available.
141140
*
142141
* Returns:
@@ -169,14 +168,14 @@ int compareWithMapcodeFormat(
169168
* Convert a territory name to a territory code.
170169
*
171170
* Arguments:
172-
* isoNam - Territory name to convert.
171+
* string - String starting with ISO code of territory (e.g. "USA" or "US-CA").
173172
* parentTerritoryCode - Parent territory code, or 0 if not available.
174173
*
175174
* Returns:
176175
* Territory code >0 if succeeded, or <0 if failed.
177176
*/
178-
int convertTerritoryIsoNameToCode(
179-
const char *isoName,
177+
int getTerritoryCode(
178+
const char *string,
180179
int parentTerritoryCode);
181180

182181
/**
@@ -250,7 +249,7 @@ double maxErrorInMeters(int extraDigits);
250249
* Arguments:
251250
* lat - Latitude, in degrees. Range: -90..90.
252251
* lon - Longitude, in degrees. Range: -180..180.
253-
* territoryCode - Territory code (obtained from convertTerritoryIsoNameToCode)
252+
* territoryCode - Territory code (obtained from getTerritoryCode)
254253
*
255254
* returns nonzero if coordinate is near more than one territory border
256255
*
@@ -332,11 +331,12 @@ const UWORD *encodeToAlphabet(const char *string, int alphabet);
332331
/**
333332
* List of #defines to support legacy systems.
334333
*/
334+
#define convertTerritoryIsoNameToCode getTerritoryCode
335335
#define coord2mc(results, lat, lon, territoryCode) encodeLatLonToMapcodes_Deprecated(results, lat, lon,territoryCode, 0)
336336
#define coord2mc1(results, lat, lon, territoryCode) encodeLatLonToSingleMapcode(results, lat, lon, territoryCode, 0)
337337
#define mc2coord decodeMapcodeToLatLon
338338
#define lookslikemapcode compareWithMapcodeFormat
339-
#define text2tc convertTerritoryIsoNameToCode
339+
#define text2tc getTerritoryCode
340340
#define tc2text convertTerritoryCodeToIsoName
341341
#define tccontext getCountryOrParentCountry
342342
#define tcparent getParentCountryOf

0 commit comments

Comments
 (0)