Skip to content

Commit cc3eb2f

Browse files
Merge pull request #21 from mapcode-foundation/dev
2.1.3 useful routine distanceInMeters added to API and unit test
2 parents b3b0750 + 4d18622 commit cc3eb2f

File tree

2 files changed

+53
-45
lines changed

2 files changed

+53
-45
lines changed

mapcodelib/mapcoder.c

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ static const char *get_entity_iso3(char *entity_iso3_result, int ccode) {
169169

170170
static void makeupper(char *s)
171171
{
172-
for(;*s;*s++) { *s = toupper(*s); }
172+
while(*s) { *s = (char) toupper(*s); s++; }
173173
}
174174

175175
static int disambiguate_str(const char *s, const int len) // returns disambiguation 1-8, or negative if error
@@ -829,7 +829,7 @@ static int decodeNameless(decodeRec *dec, int m) {
829829
{
830830
const int p = 31 / A;
831831
const int r = 31 % A;
832-
int v;
832+
int v = 0;
833833
int SIDE;
834834
int swapletters = 0;
835835
int xSIDE;
@@ -1250,7 +1250,8 @@ static void encodeAutoHeader(char *result, const encodeRec *enc, const int m, co
12501250
firstindex--;
12511251
}
12521252

1253-
for (i = firstindex; i <= m; i++) {
1253+
i = firstindex;
1254+
for(;;) {
12541255
b = boundaries(i);
12551256
// determine how many cells
12561257
H = (b->maxy - b->miny + 89) / 90; // multiple of 10m
@@ -1265,37 +1266,36 @@ static void encodeAutoHeader(char *result, const encodeRec *enc, const int m, co
12651266
const int GOODROUNDER = codexm >= 23 ? (961 * 961 * 31) : (961 * 961);
12661267
product = ((STORAGE_START + product + GOODROUNDER - 1) / GOODROUNDER) * GOODROUNDER - STORAGE_START;
12671268
}
1268-
if (i < m) {
1269-
STORAGE_START += product;
1270-
}
1271-
}
1272-
1273-
{
1274-
// encode
1275-
const int dividerx = (b->maxx - b->minx + W - 1) / W;
1276-
const int vx = (enc->coord32.lon - b->minx) / dividerx;
1277-
const int extrax = (enc->coord32.lon - b->minx) % dividerx;
1269+
if (i == m) {
1270+
// encode
1271+
const int dividerx = (b->maxx - b->minx + W - 1) / W;
1272+
const int vx = (enc->coord32.lon - b->minx) / dividerx;
1273+
const int extrax = (enc->coord32.lon - b->minx) % dividerx;
12781274

1279-
const int dividery = (b->maxy - b->miny + H - 1) / H;
1280-
int vy = (b->maxy - enc->coord32.lat) / dividery;
1281-
int extray = (b->maxy - enc->coord32.lat) % dividery;
1275+
const int dividery = (b->maxy - b->miny + H - 1) / H;
1276+
int vy = (b->maxy - enc->coord32.lat) / dividery;
1277+
int extray = (b->maxy - enc->coord32.lat) % dividery;
12821278

1283-
const int codexlen = (codexm / 10) + (codexm % 10);
1284-
int value = (vx / 168) * (H / 176);
1279+
const int codexlen = (codexm / 10) + (codexm % 10);
1280+
int value = (vx / 168) * (H / 176);
12851281

1286-
if (extray == 0 && enc->fraclat > 0) {
1287-
vy--;
1288-
extray += dividery;
1289-
}
1282+
if (extray == 0 && enc->fraclat > 0) {
1283+
vy--;
1284+
extray += dividery;
1285+
}
12901286

1291-
value += (vy / 176);
1287+
value += (vy / 176);
12921288

1293-
// PIPELETTER ENCODE
1294-
encodeBase31(result, (STORAGE_START / (961 * 31)) + value, codexlen - 2);
1295-
result[codexlen - 2] = '.';
1296-
encode_triple(result + codexlen - 1, vx % 168, vy % 176);
1289+
// PIPELETTER ENCODE
1290+
encodeBase31(result, (STORAGE_START / (961 * 31)) + value, codexlen - 2);
1291+
result[codexlen - 2] = '.';
1292+
encode_triple(result + codexlen - 1, vx % 168, vy % 176);
12971293

1298-
encodeExtension(result, extrax << 2, extray, dividerx << 2, dividery, extraDigits, -1, enc); // autoheader
1294+
encodeExtension(result, extrax << 2, extray, dividerx << 2, dividery, extraDigits, -1, enc); // autoheader
1295+
return;
1296+
}
1297+
STORAGE_START += product;
1298+
i++;
12991299
}
13001300
}
13011301

@@ -1391,7 +1391,7 @@ static int decoderEngine(decodeRec *dec) {
13911391
int hasvowels = 0;
13921392
int hasletters = 0;
13931393
const char *dot = NULL;
1394-
int prelen;
1394+
int prelen = 0;
13951395
int len;
13961396
char *w;
13971397
// skip whitesace
@@ -2062,7 +2062,7 @@ int binfindmatch(int parentcode, const char *str) {
20622062
char tmp[5];
20632063
if (parentcode < 0) { return -1; }
20642064
if (parentcode > 0) {
2065-
tmp[0] = '0' + parentcode;
2065+
tmp[0] = (char) ('0' + parentcode);
20662066
memcpy(tmp + 1, str, 3);
20672067
} else {
20682068
memcpy(tmp, str, 4);

unitttest/unittest.c

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -193,22 +193,22 @@ static void testEncodeAndDecode(const char *str, double y, double x, int localso
193193
printf("*** ERROR *** encode(%0.8f , %0.8f,%d) does not deliver %d solutions\n", y, x, tc, localsolutions);
194194
printGeneratedMapcodes(&mapcodes);
195195
}
196-
}
197196

198-
// test that EXPECTED solution is there (if requested)
199-
if (str && localsolutions) {
200-
nrTests++;
201-
for (i = 0; i < nrresults; i++) {
202-
const char *m = mapcodes.mapcode[i];
203-
if (strstr(m, clean) == m) {
204-
found = 1;
205-
break;
197+
// test that EXPECTED solution is there (if requested)
198+
if (str) {
199+
nrTests++;
200+
for (i = 0; i < nrresults; i++) {
201+
const char *m = mapcodes.mapcode[i];
202+
if (strstr(m, clean) == m) {
203+
found = 1;
204+
break;
205+
}
206+
}
207+
if (!found) {
208+
nrErrors++;
209+
printf("*** ERROR *** encode(%0.8f , %0.8f) does not deliver \"%s\"\n", y, x, clean);
210+
printGeneratedMapcodes(&mapcodes);
206211
}
207-
}
208-
if (!found) {
209-
nrErrors++;
210-
printf("*** ERROR *** encode(%0.8f , %0.8f) does not deliver \"%s\"\n", y, x, clean);
211-
printGeneratedMapcodes(&mapcodes);
212212
}
213213
}
214214

@@ -398,6 +398,15 @@ void test_territory(const char *alphaCode, int tc, int isAlias, int needsParent,
398398
char alphacode[8];
399399
int tn;
400400
strcpy(alphacode, alphaCode);
401+
if (!needsParent && i==0) {
402+
tn = convertTerritoryIsoNameToCode(alphacode, 0);
403+
nrTests++;
404+
if (tn != tc) {
405+
nrErrors++;
406+
printf("*** ERROR *** convertTerritoryIsoNameToCode('%s')=%d but expected %d (%s)\n",
407+
alphacode, tn, tc, convertTerritoryCodeToIsoName(tc,0) );
408+
}
409+
}
401410
alphacode[i] = (char) tolower(alphacode[i]);
402411
tn = convertTerritoryIsoNameToCode(alphacode, tcParent);
403412
nrTests++;
@@ -443,7 +452,6 @@ static void re_encode_tests() {
443452
int nrrecords = lastrec(ccode_earth) + 1;
444453
printf("%d records\n", nrrecords);
445454
for (ccode = 0; ccode <= ccode_earth; ccode++) {
446-
int tc = (ccode + 1);
447455
for (m = firstrec(ccode); m <= lastrec(ccode); m++) {
448456
double y, x, midx, midy;
449457
const mminforec *b = boundaries(m);

0 commit comments

Comments
 (0)