2 libloc - A library to determine the location of someone on the Internet
4 Copyright (C) 2024 IPFire Development Team <info@ipfire.org>
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.
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.
24 #include <libloc/country.h>
29 typedef struct country
{
30 struct loc_country
* country
;
33 static Country
* luaL_checkcountry(lua_State
* L
, int i
) {
34 void* userdata
= luaL_checkudata(L
, i
, "location.Country");
36 // Throw an error if the argument doesn't match
37 luaL_argcheck(L
, userdata
, i
, "Country expected");
39 return (Country
*)userdata
;
42 int create_country(lua_State
* L
, struct loc_country
* country
) {
43 // Allocate a new object
44 Country
* self
= (Country
*)lua_newuserdata(L
, sizeof(*self
));
47 luaL_setmetatable(L
, "location.Country");
50 self
->country
= loc_country_ref(country
);
55 static int Country_new(lua_State
* L
) {
56 struct loc_country
* country
= NULL
;
57 const char* code
= NULL
;
61 code
= luaL_checkstring(L
, 1);
64 r
= loc_country_new(ctx
, &country
, code
);
66 return luaL_error(L
, "Could not create country %s: %s\n", code
, strerror(errno
));
69 r
= create_country(L
, country
);
70 loc_country_unref(country
);
75 static int Country_gc(lua_State
* L
) {
76 Country
* self
= luaL_checkcountry(L
, 1);
80 loc_country_unref(self
->country
);
87 static int Country_eq(lua_State
* L
) {
88 Country
* self
= luaL_checkcountry(L
, 1);
89 Country
* other
= luaL_checkcountry(L
, 2);
91 // Push comparison result
92 lua_pushboolean(L
, loc_country_cmp(self
->country
, other
->country
) == 0);
99 static int Country_get_name(lua_State
* L
) {
100 Country
* self
= luaL_checkcountry(L
, 1);
103 lua_pushstring(L
, loc_country_get_name(self
->country
));
110 static int Country_get_code(lua_State
* L
) {
111 Country
* self
= luaL_checkcountry(L
, 1);
114 lua_pushstring(L
, loc_country_get_code(self
->country
));
121 static int Country_get_continent_code(lua_State
* L
) {
122 Country
* self
= luaL_checkcountry(L
, 1);
125 lua_pushstring(L
, loc_country_get_continent_code(self
->country
));
130 static const struct luaL_Reg Country_functions
[] = {
131 { "new", Country_new
},
132 { "get_code", Country_get_code
},
133 { "get_continent_code", Country_get_continent_code
},
134 { "get_name", Country_get_name
},
135 { "__eq", Country_eq
},
136 { "__gc", Country_gc
},
137 { "__tostring", Country_get_code
},
141 int register_country(lua_State
* L
) {
142 return register_class(L
, "location.Country", Country_functions
);