Skip to content

Commit 2e1b86e

Browse files
Minor improvements (get rid of compiler warnings)
1 parent 5808878 commit 2e1b86e

File tree

1 file changed

+58
-57
lines changed

1 file changed

+58
-57
lines changed

mapcodelib/mapcoder.c

Lines changed: 58 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
#include <string.h> // strlen strcpy strcat memcpy memmove strstr strchr memcmp strupr
17+
#include <string.h> // strlen strcpy strcat memcpy memmove strstr strchr memcmp
1818
#include <stdlib.h> // atof
1919
#include <ctype.h> // toupper
2020
#include <math.h> // floor
@@ -85,7 +85,7 @@ static int firstrec(int ccode) { return data_start[ccode]; }
8585
static int lastrec(int ccode) { return data_start[ccode + 1] - 1; }
8686

8787
#ifdef FAST_ALPHA
88-
#define ParentLetter(ccode) (parentletter[ccode])
88+
#define ParentLetter(ccode) ((int)parentletter[ccode])
8989
#else
9090

9191
static int ParentLetter(int ccode) // returns parent index (>0), or 0
@@ -167,6 +167,11 @@ static const char *get_entity_iso3(char *entity_iso3_result, int ccode) {
167167
return entity_iso3_result;
168168
}
169169

170+
static void makeupper(char *s)
171+
{
172+
for(;*s;*s++) { *s = toupper(*s); }
173+
}
174+
170175
static int disambiguate_str(const char *s, int len) // returns disambiguation 1-8, or negative if error
171176
{
172177
const char *p = (len == 2 ? parents2 : parents3);
@@ -176,7 +181,7 @@ static int disambiguate_str(const char *s, int len) // returns disambiguation 1-
176181
if (len != 2 && len != 3) { return -923; } // solve bad args
177182
memcpy(country, s, len);
178183
country[len] = 0;
179-
strupr(country);
184+
makeupper(country);
180185
f = strstr(p, country);
181186
if (f == NULL) {
182187
return -23; // unknown country
@@ -455,12 +460,12 @@ static int decodeBase31(const char *code) {
455460
static void encode_triple(char *result, int difx, int dify) {
456461
if (dify < 4 * 34) // first 4(x34) rows of 6(x28) wide
457462
{
458-
encodeBase31(result, ((difx / 28) + 6 * (dify / 34)), 1);
463+
*result = encode_chars[ ((difx / 28) + 6 * (dify / 34)) ];
459464
encodeBase31(result + 1, ((difx % 28) * 34 + (dify % 34)), 2);
460465
}
461466
else // bottom row
462467
{
463-
encodeBase31(result, (difx / 24) + 24, 1);
468+
*result = encode_chars[ (difx / 24) + 24 ];
464469
encodeBase31(result + 1, (difx % 24) * 40 + (dify - 136), 2);
465470
}
466471
} // encode_triple
@@ -550,11 +555,10 @@ static int decodeGrid(decodeRec *dec, int m, int hasHeaderLetter) {
550555
divy = yside[prelen];
551556
}
552557
else {
553-
int pw = nc[prelen];
554-
divx = (pw / divy);
558+
divx = (nc[prelen] / divy);
555559
}
556560

557-
if (prelen == 4 && divx == xside[4] && divy == yside[4]) {
561+
if (prelen == 4 && divx == 961 && divy == 961) {
558562
char t = result[1];
559563
result[1] = result[2];
560564
result[2] = t;
@@ -589,9 +593,9 @@ static int decodeGrid(decodeRec *dec, int m, int hasHeaderLetter) {
589593

590594
{
591595
int xp = xside[postlen];
592-
int dividerx = ((((xgridsize)) + xp - 1) / xp);
596+
int dividerx = ((xgridsize + xp - 1) / xp);
593597
int yp = yside[postlen];
594-
int dividery = ((((ygridsize)) + yp - 1) / yp);
598+
int dividery = ((ygridsize + yp - 1) / yp);
595599
// decoderelative
596600

597601
{
@@ -664,8 +668,12 @@ static void encodeGrid(char *result, const encodeRec *enc, int const m, int extr
664668

665669
int orgcodex = coDex(m);
666670
int codexm = orgcodex;
667-
if (codexm == 21) { codexm = 22; }
668-
if (codexm == 14) { codexm = 23; }
671+
if (codexm == 21) {
672+
codexm = 22;
673+
}
674+
else if (codexm == 14) {
675+
codexm = 23;
676+
}
669677

670678
*result = 0;
671679
if (headerLetter) { result++; }
@@ -681,8 +689,7 @@ static void encodeGrid(char *result, const encodeRec *enc, int const m, int extr
681689
divy = yside[prelen];
682690
}
683691
else {
684-
int pw = nc[prelen];
685-
divx = (pw / divy);
692+
divx = (nc[prelen] / divy);
686693
}
687694

688695
{ // grid
@@ -719,7 +726,7 @@ static void encodeGrid(char *result, const encodeRec *enc, int const m, int extr
719726
encodeBase31(result, v, prelen);
720727
} // prefix
721728

722-
if (prelen == 4 && divx == xside[4] && divy == yside[4]) {
729+
if (prelen == 4 && divx == 961 && divy == 961) {
723730
char t = result[1];
724731
result[1] = result[2];
725732
result[2] = t;
@@ -729,8 +736,8 @@ static void encodeGrid(char *result, const encodeRec *enc, int const m, int extr
729736
relx = b->minx + (relx * xgridsize);
730737

731738
{ // postfix
732-
int dividery = ((((ygridsize)) + yside[postlen] - 1) / yside[postlen]);
733-
int dividerx = ((((xgridsize)) + xside[postlen] - 1) / xside[postlen]);
739+
int dividery = ((ygridsize + yside[postlen] - 1) / yside[postlen]);
740+
int dividerx = ((xgridsize + xside[postlen] - 1) / xside[postlen]);
734741
int extrax, extray;
735742

736743
{
@@ -842,7 +849,7 @@ static int decodeNameless(decodeRec *dec, int m) {
842849
X = offset / (p + 1);
843850
}
844851
else {
845-
swapletters = (p == 1 && codexm == 22);
852+
swapletters = ((p == 1) && (codexm == 22));
846853
X = r + (offset - (r * (p + 1))) / p;
847854
}
848855
}
@@ -898,17 +905,16 @@ static int decodeNameless(decodeRec *dec, int m) {
898905
xSIDE = SIDE = smartDiv(m);
899906

900907
b = boundaries(m);
901-
if (isSpecialShape22(m)) {
902-
xSIDE *= SIDE;
903-
SIDE = 1 + ((b->maxy - b->miny) / 90); // side purely on y range
904-
xSIDE = xSIDE / SIDE;
905-
}
906908

907909
// decode
908910
{
909911
int dx, dy;
910912

911913
if (isSpecialShape22(m)) {
914+
xSIDE *= SIDE;
915+
SIDE = 1 + ((b->maxy - b->miny) / 90); // side purely on y range
916+
xSIDE = xSIDE / SIDE;
917+
912918
decodeSixWide(v, xSIDE, SIDE, &dx, &dy);
913919
dy = SIDE - 1 - dy;
914920
}
@@ -1070,7 +1076,7 @@ static void encodeNameless(char *result, const encodeRec *enc, int input_ctry, i
10701076
int xSIDE, orgSIDE;
10711077

10721078
if (codexm != 21 && A <= 31) {
1073-
storage_offset = (X * p + (X < r ? X : r)) * (961 * 961); // p=4,r=3: offset(X)={0,5,10,15,19,23,27}-31
1079+
storage_offset = (X * p + (X < r ? X : r)) * (961 * 961);
10741080
}
10751081
else if (codexm != 21 && A < 62) {
10761082
if (X < (62 - A)) {
@@ -1099,22 +1105,17 @@ static void encodeNameless(char *result, const encodeRec *enc, int input_ctry, i
10991105

11001106
b = boundaries(m);
11011107
orgSIDE = xSIDE = SIDE;
1102-
if (isSpecialShape22(m)) // - keep the existing rectangle!
1103-
{
1104-
SIDE = 1 + ((b->maxy - b->miny) / 90); // new side, based purely on y-distance
1105-
xSIDE = (orgSIDE * orgSIDE) / SIDE;
1106-
}
11071108

11081109
{
11091110
int v = storage_offset;
11101111

11111112
int dividerx4 = xDivider4(b->miny, b->maxy); // *** note: dividerx4 is 4 times too large!
11121113
int xFracture = (int)(enc->fraclon / MAX_PRECISION_FACTOR);
11131114
int dx = (4 * (enc->coord32.lon - b->minx) + xFracture) / dividerx4; // div with quarters
1114-
int extrax4 = (enc->coord32.lon - b->minx) * 4 - dx * dividerx4; // mod with quarters
1115+
int extrax4 = (enc->coord32.lon - b->minx) * 4 - (dx * dividerx4); // mod with quarters
11151116

11161117
int dividery = 90;
1117-
int dy = (b->maxy - enc->coord32.lat) / dividery; // between 0 and SIDE-1
1118+
int dy = (b->maxy - enc->coord32.lat) / dividery;
11181119
int extray = (b->maxy - enc->coord32.lat) % dividery;
11191120

11201121
if (extray == 0 && enc->fraclat > 0) {
@@ -1123,6 +1124,8 @@ static void encodeNameless(char *result, const encodeRec *enc, int input_ctry, i
11231124
}
11241125

11251126
if (isSpecialShape22(m)) {
1127+
SIDE = 1 + ((b->maxy - b->miny) / 90); // new side, based purely on y-distance
1128+
xSIDE = (orgSIDE * orgSIDE) / SIDE;
11261129
v += encodeSixWide(dx, SIDE - 1 - dy, xSIDE, SIDE);
11271130
}
11281131
else {
@@ -1186,7 +1189,7 @@ static int decodeAutoHeader(decodeRec *dec, int m) {
11861189
product = (W / 168) * (H / 176) * 961 * 31;
11871190

11881191
if (recType(m) == 2) {
1189-
int GOODROUNDER = coDex(m) >= 23 ? (961 * 961 * 31) : (961 * 961);
1192+
int GOODROUNDER = codexm >= 23 ? (961 * 961 * 31) : (961 * 961);
11901193
product = ((STORAGE_START + product + GOODROUNDER - 1) / GOODROUNDER) * GOODROUNDER - STORAGE_START;
11911194
}
11921195

@@ -1243,7 +1246,7 @@ static void encodeAutoHeader(char *result, const encodeRec *enc, int m, int extr
12431246

12441247
// search back to first of the group
12451248
int firstindex = m;
1246-
int codexm = coDex(m);
1249+
const int codexm = coDex(m);
12471250
while (recType(firstindex - 1) > 1 && coDex(firstindex - 1) == codexm) {
12481251
firstindex--;
12491252
}
@@ -1270,16 +1273,16 @@ static void encodeAutoHeader(char *result, const encodeRec *enc, int m, int extr
12701273

12711274
{
12721275
// encode
1273-
int dividerx = (b->maxx - b->minx + W - 1) / W;
1274-
int vx = (enc->coord32.lon - b->minx) / dividerx;
1275-
int extrax = (enc->coord32.lon - b->minx) % dividerx;
1276+
const int dividerx = (b->maxx - b->minx + W - 1) / W;
1277+
const int vx = (enc->coord32.lon - b->minx) / dividerx;
1278+
const int extrax = (enc->coord32.lon - b->minx) % dividerx;
12761279

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

1281-
int codexlen = (codexm / 10) + (codexm % 10);
1282-
int value = (vx / 168) * (H / 176);
1284+
const int codexlen = (codexm / 10) + (codexm % 10);
1285+
const int value = (vx / 168) * (H / 176);
12831286

12841287
if (extray == 0 && enc->fraclat > 0) {
12851288
vy--;
@@ -1397,7 +1400,7 @@ static int decoderEngine(decodeRec *dec) {
13971400
s = (char *) dec->orginput;
13981401
while (*s <= 32 && *s > 0) { s++; }
13991402
// remove trail and overhead
1400-
len = strlen(s);
1403+
len = (int) strlen(s);
14011404
if (len > MAX_MAPCODE_RESULT_LEN - 1) { len = MAX_MAPCODE_RESULT_LEN - 1; }
14021405
while (len > 0 && s[len - 1] <= 32 && s[len - 1] >= 0) { len--; }
14031406
// copy into dec->minput;
@@ -1466,7 +1469,7 @@ static int decoderEngine(decodeRec *dec) {
14661469
dec->extension = "";
14671470
}
14681471

1469-
codex = prelen * 10 + strlen(dot) - 1;
1472+
codex = prelen * 10 + (int) strlen(dot) - 1;
14701473

14711474
if (hasvowels) {
14721475
if (unpack_if_alldigits(s) <= 0) {
@@ -1497,20 +1500,19 @@ static int decoderEngine(decodeRec *dec) {
14971500

14981501
// try all ccode rectangles to decode s (pointing to first character of proper mapcode)
14991502
for (i = from; i <= upto; i++) {
1503+
int codexi = coDex(i);
15001504
int r = recType(i);
15011505
if (r == 0) {
15021506
if (isNameless(i)) {
1503-
int codexi = coDex(i);
1504-
if ((codexi == 21 && codex == 22)
1505-
|| (codexi == 22 && codex == 32)
1506-
|| (codexi == 13 && codex == 23)) {
1507+
if (((codexi == 21) && (codex == 22))
1508+
|| ((codexi == 22) && (codex == 32))
1509+
|| ((codexi == 13) && (codex == 23))) {
15071510
err = decodeNameless(dec, i);
15081511
break;
15091512
}
15101513
}
1511-
else {
1512-
int codexi = coDex(i);
1513-
if (codexi == codex || (codex == 22 && codexi == 21)) {
1514+
else {
1515+
if (codexi == codex || ((codex == 22) && (codexi == 21))) {
15141516
err = decodeGrid(dec, i, 0);
15151517

15161518
// *** make sure decode fits somewhere ***
@@ -1575,16 +1577,14 @@ static int decoderEngine(decodeRec *dec) {
15751577
}
15761578
}
15771579
}
1578-
else if (r == 1) {
1579-
int codexi = coDex(i);
1580+
else if (r == 1) {
15801581
if (codex == codexi + 10 && headerLetter(i) == *s) {
15811582
err = decodeGrid(dec, i, 1);
15821583
break;
15831584
}
15841585
}
1585-
else { //r>1
1586-
int codexi = coDex(i);
1587-
if (codex == 23 && codexi == 22 || codex == 33 && codexi == 23) {
1586+
else { //r>1
1587+
if (((codex == 23) && (codexi == 22)) || ((codex == 33) && (codexi == 23))) {
15881588
err = decodeAutoHeader(dec, i);
15891589
break;
15901590
}
@@ -1642,7 +1642,7 @@ static int decoderEngine(decodeRec *dec) {
16421642
dec->result.lon = (bmaxx - MICROMETER) / 1000000.0;
16431643
} // keep in encompassing territory
16441644

1645-
dec->coord32.lat = (int) floor(dec->result.lat * 1000000);
1645+
dec->coord32.lat = (int) floor(dec->result.lat * 1000000); // @@@ not needed
16461646
dec->coord32.lon = (int) floor(dec->result.lon * 1000000);
16471647
} // FORCE_RECODE
16481648
#endif
@@ -2040,10 +2040,11 @@ int binfindmatch(int parentcode, const char *str) {
20402040
memcpy(tmp, str, 4);
20412041
}
20422042
tmp[4] = 0;
2043+
makeupper(tmp);
20432044
{ // binary-search the result
20442045
const alphaRec *p;
20452046
alphaRec t;
2046-
t.alphaCode = strupr(tmp);
2047+
t.alphaCode = tmp;
20472048
t.ccode = parentcode;
20482049

20492050
p = (const alphaRec *) bsearch(&t, alphaSearch, NRTERREC, sizeof(alphaRec), cmp_alphacode);

0 commit comments

Comments
 (0)