]> git.ipfire.org Git - people/ms/libloc.git/blame - src/loc/country.h
country: Add simple function to test if a country code is valid
[people/ms/libloc.git] / src / loc / country.h
CommitLineData
ec684c1a
MT
1/*
2 libloc - A library to determine the location of someone on the Internet
3
4 Copyright (C) 2019 IPFire Development Team <info@ipfire.org>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15*/
16
17#ifndef LIBLOC_COUNTRY_H
18#define LIBLOC_COUNTRY_H
19
20#include <loc/libloc.h>
21#include <loc/format.h>
22#include <loc/stringpool.h>
23
24struct loc_country;
25int loc_country_new(struct loc_ctx* ctx, struct loc_country** country, const char* country_code);
26struct loc_country* loc_country_ref(struct loc_country* country);
27struct loc_country* loc_country_unref(struct loc_country* country);
28
29const char* loc_country_get_code(struct loc_country* country);
30
31const char* loc_country_get_continent_code(struct loc_country* country);
32int loc_country_set_continent_code(struct loc_country* country, const char* continent_code);
33
34const char* loc_country_get_name(struct loc_country* country);
35int loc_country_set_name(struct loc_country* country, const char* name);
36
37int loc_country_cmp(struct loc_country* country1, struct loc_country* country2);
38
39#ifdef LIBLOC_PRIVATE
40
57146963
MT
41#include <string.h>
42
ec684c1a
MT
43int loc_country_new_from_database_v0(struct loc_ctx* ctx, struct loc_stringpool* pool,
44 struct loc_country** country, const struct loc_database_country_v0* dbobj);
45int loc_country_to_database_v0(struct loc_country* country,
46 struct loc_stringpool* pool, struct loc_database_country_v0* dbobj);
47
57146963
MT
48static inline int loc_country_code_is_valid(const char* cc) {
49 // It cannot be NULL
50 if (!cc || !*cc)
51 return 0;
52
53 // It must be 2 characters long
54 if (strlen(cc) != 2)
55 return 0;
56
57 // It must only contain A-Z
58 for (unsigned int i = 0; i < 2; i++) {
59 if (cc[i] < 'A' || cc[i] > 'Z')
60 return 0;
61 }
62
63 // Looks valid
64 return 1;
65}
66
ec684c1a
MT
67static inline void loc_country_copy_code(char* dst, const char* src) {
68 for (unsigned int i = 0; i < 2; i++) {
69 dst[i] = src[i];
70 }
71
72 // Terminate the string
6e197f37 73 dst[2] = '\0';
ec684c1a
MT
74}
75
76#endif
77
78#endif