Skip to content

Commit bcfb846

Browse files
committed
Cleaned mapcoder.c
1 parent 5bb8d96 commit bcfb846

File tree

1 file changed

+30
-27
lines changed

1 file changed

+30
-27
lines changed

mapcodelib/mapcoder.c

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ void _TestAssert(int iCondition, const char *cstrFile, int iLine) {
6868
#define REC_TYPE(m) ((TERRITORY_BOUNDARIES[m].flags >> 7) & 3)
6969
#define SMART_DIV(m) (TERRITORY_BOUNDARIES[m].flags >> 16)
7070
#define HEADER_LETTER(m) (ENCODE_CHARS[(TERRITORY_BOUNDARIES[m].flags >> 11) & 31])
71-
#define BOUNDARY_PTR(m) (&TERRITORY_BOUNDARIES[m])
7271

7372
#define TOKENSEP 0
7473
#define TOKENDOT 1
@@ -90,6 +89,12 @@ void _TestAssert(int iCondition, const char *cstrFile, int iLine) {
9089
#define EARTH_CIRCUMFERENCE_X (EARTH_RADIUS_X_METERS * 2 * MATH_PI)
9190
#define EARTH_CIRCUMFERENCE_Y (EARTH_RADIUS_Y_METERS * 2 * MATH_PI)
9291

92+
#define MICROLAT_TO_FRACTIONS_FACTOR ((double) MAX_PRECISION_FACTOR)
93+
#define MICROLON_TO_FRACTIONS_FACTOR (4.0 * MAX_PRECISION_FACTOR)
94+
95+
#define FLAG_UTF8_STRING 0 // interpret pointer a utf8 characters
96+
#define FLAG_UTF16_STRING 1 // interpret pointer a UWORD* to utf16 characters
97+
9398
// Meters per degree latitude is fixed. For longitude: use factor * cos(midpoint of two degree latitudes).
9499
static const double METERS_PER_DEGREE_LAT = EARTH_CIRCUMFERENCE_Y / 360.0;
95100
static const double METERS_PER_DEGREE_LON = EARTH_CIRCUMFERENCE_X / 360.0;
@@ -330,8 +335,7 @@ static TerritoryBoundary *getExtendedBoundaries(TerritoryBoundary *target, const
330335
//
331336
///////////////////////////////////////////////////////////////////////////////////////////////
332337

333-
#define MICROLAT_TO_FRACTIONS_FACTOR ((double)MAX_PRECISION_FACTOR)
334-
#define MICROLON_TO_FRACTIONS_FACTOR (4.0 * MAX_PRECISION_FACTOR)
338+
335339
typedef struct {
336340
// latitudes in "810 billionths", range [-729 E11 .. +720 E11), is well within (-2^47 ... +2^47)
337341
double fminy;
@@ -822,7 +826,7 @@ static const int NC[6] = {1, 31, 961, 29791, 923521, 28629151};
822826
// returns *result==0 in case of error
823827
static void encodeGrid(char *result, const EncodeRec *enc, const int m, const int extraDigits,
824828
const char headerLetter) {
825-
const TerritoryBoundary *b = BOUNDARY_PTR(m);
829+
const TerritoryBoundary *b = TERRITORY_BOUNDARY(m);
826830
const int orgcodex = coDex(m);
827831
int codexm;
828832
ASSERT(result);
@@ -999,7 +1003,7 @@ static void encodeNameless(char *result, const EncodeRec *enc, const enum Territ
9991003

10001004
SIDE = SMART_DIV(m);
10011005

1002-
b = BOUNDARY_PTR(m);
1006+
b = TERRITORY_BOUNDARY(m);
10031007
orgSIDE = SIDE;
10041008

10051009
{
@@ -1075,7 +1079,7 @@ static void encodeAutoHeader(char *result, const EncodeRec *enc, const int m, co
10751079

10761080
i = firstindex;
10771081
for (;;) {
1078-
b = BOUNDARY_PTR(i);
1082+
b = TERRITORY_BOUNDARY(i);
10791083
// determine how many cells
10801084
H = (b->maxy - b->miny + 89) / 90; // multiple of 10m
10811085
xdiv = xDivider4(b->miny, b->maxy);
@@ -1137,7 +1141,7 @@ static void encoderEngine(const enum Territory ccode, const EncodeRec *enc, cons
11371141
from = firstRec(ccode);
11381142
upto = lastRec(ccode);
11391143

1140-
if (!fitsInsideBoundaries(&enc->coord32, BOUNDARY_PTR(upto))) {
1144+
if (!fitsInsideBoundaries(&enc->coord32, TERRITORY_BOUNDARY(upto))) {
11411145
return;
11421146
}
11431147

@@ -1151,7 +1155,7 @@ static void encoderEngine(const enum Territory ccode, const EncodeRec *enc, cons
11511155

11521156
*result = 0;
11531157
for (i = from; i <= upto; i++) {
1154-
if (fitsInsideBoundaries(&enc->coord32, BOUNDARY_PTR(i))) {
1158+
if (fitsInsideBoundaries(&enc->coord32, TERRITORY_BOUNDARY(i))) {
11551159
if (IS_NAMELESS(i)) {
11561160
encodeNameless(result, enc, ccode, extraDigits, i);
11571161
} else if (REC_TYPE(i) > 1) {
@@ -1478,7 +1482,7 @@ static enum MapcodeError decodeGrid(DecodeRec *dec, const int m, const int hasHe
14781482

14791483

14801484
{
1481-
const TerritoryBoundary *b = BOUNDARY_PTR(m);
1485+
const TerritoryBoundary *b = TERRITORY_BOUNDARY(m);
14821486
const int ygridsize = (b->maxy - b->miny + divy - 1) / divy; // microdegrees per cell
14831487
const int xgridsize = (b->maxx - b->minx + divx - 1) / divx; // microdegrees per cell
14841488

@@ -1521,7 +1525,7 @@ static enum MapcodeError decodeGrid(DecodeRec *dec, const int m, const int hasHe
15211525

15221526
dec->coord32.lonMicroDeg = relx + (difx * dividerx);
15231527
dec->coord32.latMicroDeg = rely + (dify * dividery);
1524-
if (!fitsInsideBoundaries(&dec->coord32, BOUNDARY_PTR(m))) {
1528+
if (!fitsInsideBoundaries(&dec->coord32, TERRITORY_BOUNDARY(m))) {
15251529
return ERR_MAPCODE_UNDECODABLE; // type 2 "NLD Q000.000"
15261530
}
15271531

@@ -1637,7 +1641,7 @@ static enum MapcodeError decodeNameless(DecodeRec *dec, int m) {
16371641

16381642
xSIDE = SIDE = SMART_DIV(m);
16391643

1640-
b = BOUNDARY_PTR(m);
1644+
b = TERRITORY_BOUNDARY(m);
16411645

16421646
// decode
16431647
{
@@ -1692,7 +1696,7 @@ static enum MapcodeError decodeAutoHeader(DecodeRec *dec, int m) {
16921696
value *= (961 * 31);
16931697

16941698
for (; coDex(m) == codexm && REC_TYPE(m) > 1; m++) {
1695-
const TerritoryBoundary *b = BOUNDARY_PTR(m);
1699+
const TerritoryBoundary *b = TERRITORY_BOUNDARY(m);
16961700
// determine how many cells
16971701
int H = (b->maxy - b->miny + 89) / 90; // multiple of 10m
16981702
const int xdiv = xDivider4(b->miny, b->maxy);
@@ -2020,12 +2024,8 @@ static const int STATE_MACHINE[27][6] = {
20202024

20212025

20222026
// Returns 0 if ok, negative in case of error (where -999 represents "may BECOME a valid mapcode if more characters are added)
2023-
#define FLAG_UTF8_STRING 0 // interpret pointer a utf8 characters
2024-
#define FLAG_UTF16_STRING 1 // interpret pointer a UWORD* to utf16 characters
2025-
2026-
20272027
static enum MapcodeError parseMapcodeString(MapcodeElements *mapcodeElements, const char *string, int interpretAsUtf16,
2028-
enum Territory territory) {
2028+
enum Territory territory) {
20292029
const UWORD *utf16 = (const UWORD *) string;
20302030
int isAbjad = 0;
20312031
const unsigned char *utf8 = (unsigned char *) string;
@@ -2168,7 +2168,8 @@ static enum MapcodeError parseMapcodeString(MapcodeElements *mapcodeElements, co
21682168
}
21692169
if (isAbjad) {
21702170
convertFromAbjad(mapcodeElements->properMapcode);
2171-
mapcodeElements->indexOfDot = (int) (strchr(mapcodeElements->properMapcode, '.') - mapcodeElements->properMapcode);
2171+
mapcodeElements->indexOfDot = (int) (strchr(mapcodeElements->properMapcode, '.') -
2172+
mapcodeElements->properMapcode);
21722173
}
21732174
if (*mapcodeElements->territoryISO) {
21742175
mapcodeElements->territoryCode = getTerritoryCode(mapcodeElements->territoryISO, territory);
@@ -2270,7 +2271,7 @@ static enum MapcodeError decoderEngine(DecodeRec *dec, int parseFlags) {
22702271
err = decodeGrid(dec, i, 0);
22712272

22722273
// first of all, make sure the zone fits the country
2273-
restrictZoneTo(&dec->zone, &dec->zone, BOUNDARY_PTR(upto));
2274+
restrictZoneTo(&dec->zone, &dec->zone, TERRITORY_BOUNDARY(upto));
22742275

22752276
if ((err == ERR_OK) && IS_RESTRICTED(i)) {
22762277
int nrZoneOverlaps = 0;
@@ -2281,7 +2282,7 @@ static enum MapcodeError decoderEngine(DecodeRec *dec, int parseFlags) {
22812282
dec->coord32 = convertFractionsToCoord32(&dec->result);
22822283
for (j = i - 1; j >= from; j--) { // look in previous rects
22832284
if (!IS_RESTRICTED(j)) {
2284-
if (fitsInsideBoundaries(&dec->coord32, BOUNDARY_PTR(j))) {
2285+
if (fitsInsideBoundaries(&dec->coord32, TERRITORY_BOUNDARY(j))) {
22852286
nrZoneOverlaps = 1;
22862287
break;
22872288
}
@@ -2294,13 +2295,13 @@ static enum MapcodeError decoderEngine(DecodeRec *dec, int parseFlags) {
22942295
for (j = from; j < i; j++) { // try all smaller rectangles j
22952296
if (!IS_RESTRICTED(j)) {
22962297
MapcodeZone z;
2297-
if (restrictZoneTo(&z, &dec->zone, BOUNDARY_PTR(j))) {
2298+
if (restrictZoneTo(&z, &dec->zone, TERRITORY_BOUNDARY(j))) {
22982299
nrZoneOverlaps++;
22992300
if (nrZoneOverlaps == 1) {
23002301
// first fit! remember...
23012302
zoneCopyFrom(&zfound, &z);
23022303
ASSERT(j <= MAPCODE_BOUNDARY_MAX);
2303-
memcpy(&prevu, BOUNDARY_PTR(j), sizeof(TerritoryBoundary));
2304+
memcpy(&prevu, TERRITORY_BOUNDARY(j), sizeof(TerritoryBoundary));
23042305
} else { // nrZoneOverlaps >= 2
23052306
// more than one hit
23062307
break; // give up
@@ -2338,7 +2339,7 @@ static enum MapcodeError decoderEngine(DecodeRec *dec, int parseFlags) {
23382339
} // for
23392340

23402341
if (!err) {
2341-
restrictZoneTo(&dec->zone, &dec->zone, BOUNDARY_PTR(lastRec(ccode)));
2342+
restrictZoneTo(&dec->zone, &dec->zone, TERRITORY_BOUNDARY(lastRec(ccode)));
23422343

23432344
if (isEmpty(&dec->zone)) {
23442345
err = ERR_MAPCODE_UNDECODABLE; // type 0 "BRA xx.xx"
@@ -2794,7 +2795,7 @@ int multipleBordersNearby(double latDeg, double lonDeg, enum Territory territory
27942795
convertCoordsToMicrosAndFractions(&coord32, NULL, NULL, latDeg, lonDeg);
27952796
for (m = upto; m >= from; m--) {
27962797
if (!IS_RESTRICTED(m)) {
2797-
if (isNearBorderOf(&coord32, BOUNDARY_PTR(m))) {
2798+
if (isNearBorderOf(&coord32, TERRITORY_BOUNDARY(m))) {
27982799
nrFound++;
27992800
if (nrFound > 1) {
28002801
return 1;
@@ -2924,8 +2925,9 @@ decodeMapcodeToLatLonUtf8(double *latDeg, double *lonDeg, const char *mapcode, e
29242925

29252926

29262927
// PUBLIC - decode string into lat,lon; returns negative in case of error
2927-
enum MapcodeError decodeMapcodeToLatLonUtf16(double *latDeg, double *lonDeg, const UWORD *mapcode, enum Territory territory,
2928-
MapcodeElements *mapcodeElements) {
2928+
enum MapcodeError
2929+
decodeMapcodeToLatLonUtf16(double *latDeg, double *lonDeg, const UWORD *mapcode, enum Territory territory,
2930+
MapcodeElements *mapcodeElements) {
29292931
if ((latDeg == NULL) || (lonDeg == NULL) || (mapcode == NULL)) {
29302932
return ERR_BAD_ARGUMENTS;
29312933
} else {
@@ -3093,7 +3095,8 @@ int getFullTerritoryNameLocalInAlphabet(char *territoryName, enum Territory terr
30933095
*territoryName = 0;
30943096
return 0;
30953097
}
3096-
return getFullTerritoryName_internal(territoryName, territory, alternative, (int) alphabet, TERRITORY_LOCAL_NAME_UTF8);
3098+
return getFullTerritoryName_internal(territoryName, territory, alternative, (int) alphabet,
3099+
TERRITORY_LOCAL_NAME_UTF8);
30973100
}
30983101

30993102

0 commit comments

Comments
 (0)