Skip to content

Commit 2c526c4

Browse files
committed
Changed interface of full territory name
1 parent b697209 commit 2c526c4

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed

mapcodelib/mapcoder.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2951,7 +2951,8 @@ enum Alphabet recognizeAlphabetUtf8(const char *utf8) {
29512951
static int getFullTerritoryName_internal(
29522952
char *territoryName,
29532953
enum Territory territory,
2954-
int alternative, int alphabet,
2954+
int alternative,
2955+
int alphabet,
29552956
const char *namelist[]) {
29562957

29572958
if (!territoryName || !namelist || alternative < 0 ||
@@ -2964,7 +2965,7 @@ static int getFullTerritoryName_internal(
29642965
pipePtr = strstr(s, "|");
29652966

29662967
#ifndef NO_SUPPORT_ALPHABETS
2967-
if (alphabet >= 0) {
2968+
if ((int) _ALPHABET_MIN < alphabet && alphabet < (int) _ALPHABET_MAX) {
29682969
if (pipePtr) {
29692970
lengthCopy(territoryName, s, (int) (pipePtr - s), MAX_TERRITORY_FULLNAME_LEN);
29702971
} else {
@@ -3006,8 +3007,17 @@ int getFullTerritoryNameEnglish(char *territoryName, enum Territory territory, i
30063007

30073008
#ifndef NO_SUPPORT_ALPHABETS
30083009

3009-
int getFullTerritoryNameLocal(char *territoryName, enum Territory territory, int alternative, enum Alphabet alphabet) {
3010+
int getFullTerritoryNameLocalInAlphabet(char *territoryName, enum Territory territory, int alternative,
3011+
enum Alphabet alphabet) {
3012+
if ((alphabet <= _ALPHABET_MIN) || (alphabet >= _ALPHABET_MAX)) {
3013+
territoryName[0] = 0;
3014+
return 0;
3015+
}
30103016
return getFullTerritoryName_internal(territoryName, territory, alternative, (int) alphabet, localname_utf8);
30113017
}
30123018

3019+
int getFullTerritoryNameLocal(char *territoryName, enum Territory territory, int alternative) {
3020+
return getFullTerritoryName_internal(territoryName, territory, alternative, -1, localname_utf8);
3021+
}
3022+
30133023
#endif // NO_SUPPORT_ALPHABETS

mapcodelib/mapcoder.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ int multipleBordersNearby(
339339
* Return value:
340340
* non-0 if more alternatives are available (call again with alternative + 1).
341341
* 0 if no more alternatives are available.
342-
*/
342+
*/
343343

344344
int getFullTerritoryNameEnglish(char *territoryName, enum Territory territory, int alternative);
345345

@@ -357,19 +357,22 @@ int getFullTerritoryNameEnglish(char *territoryName, enum Territory territory, i
357357
#ifndef NO_SUPPORT_ALPHABETS
358358

359359
/**
360-
* Returns territory names in the local language.
360+
* Returns territory names in the local language. There are two variants of this call. One returns local
361+
* territory names in a specified alphabet only. The other simply returns the local names, regardless
362+
* of its alphabet.
361363
*
362364
* Arguments:
363365
* territoryName - Target string, allocated by caller to be at least MAX_TERRITORY_NAME_LENGTH + 1 bytes.
364366
* territory - Territory to get name for.
365367
* alternative - Which name to get, must be >= 0 (0 = default, 1 = first alternative, 2 = second etc.).
366-
* alphabet - Alphabet to use for territoryName.
368+
* alphabet - Alphabet to use for territoryName. Must be a valid alphabet value.
367369
*
368370
* Return value:
369371
* non-0 if more alternatives are available (call again with alternative + 1).
370372
* 0 if no more alternatives are available.
371-
*/
372-
int getFullTerritoryNameLocal(char *territoryName, enum Territory territory, int alternative, enum Alphabet alphabet);
373+
*/
374+
int getFullTerritoryNameLocalInAlphabet(char *territoryName, enum Territory territory, int alternative, enum Alphabet alphabet);
375+
int getFullTerritoryNameLocal(char *territoryName, enum Territory territory, int alternative);
373376

374377

375378
/**

unittest/unittest.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,15 +1492,31 @@ static int test_single_encodes(void) {
14921492
}
14931493

14941494

1495+
static int check_full_territory_name(const char* expectedName, const char* gotName, int expectedCode, int gotCode) {
1496+
int nrTests = 0;
1497+
++nrTests;
1498+
if (strcmp(expectedName, gotName)) {
1499+
found_error();
1500+
printf("*** ERROR *** getFullTerritoryName error, expected name '%s', but got '%s'\n", expectedName, gotName);
1501+
}
1502+
++nrTests;
1503+
if (expectedCode != gotCode) {
1504+
found_error();
1505+
printf("*** ERROR *** getFullTerritoryName error, expected return code %d, but got %d\n", expectedCode, gotCode);
1506+
}
1507+
return nrTests;
1508+
}
1509+
1510+
14951511
int territory_full_name_tests(void) {
14961512
int nrTests = 0;
14971513
enum Territory territory;
14981514
int minNames;
14991515
int nrNames = 0;
15001516
int maxLength = 0;
1517+
char territoryName[2048];
15011518
for (territory = _TERRITORY_MIN + 1; territory < _TERRITORY_MAX; ++territory) {
15021519
int alternative = 0;
1503-
char territoryName[2048];
15041520
nrTests++;
15051521
for (alternative = 0;; alternative++) {
15061522
int len;
@@ -1524,7 +1540,7 @@ int territory_full_name_tests(void) {
15241540
for (alternative = 0;; alternative++) {
15251541
int len;
15261542
++nrNames;
1527-
if (!getFullTerritoryNameLocal(territoryName, territory, alternative, _ALPHABET_MIN)) {
1543+
if (!getFullTerritoryNameLocal(territoryName, territory, alternative)) {
15281544
break;
15291545
}
15301546
len = (int) strlen(territoryName);
@@ -1551,6 +1567,10 @@ int territory_full_name_tests(void) {
15511567
found_error();
15521568
printf("*** ERROR *** Didn't find enough territory names, found %d, expected >= %d\n", nrNames, minNames);
15531569
}
1570+
1571+
++nrTests;
1572+
check_full_territory_name("Nederland", territoryName, getFullTerritoryNameLocal(territoryName, TERRITORY_NLD, 0), 0);
1573+
// TODO Add many more tests here.
15541574
return nrTests;
15551575
}
15561576

0 commit comments

Comments
 (0)