]> git.ipfire.org Git - location/libloc.git/blob - src/perl/Location/Location.xs
1b01c22c8b995d037098b4e9fba31f82d6ab0339
[location/libloc.git] / src / perl / Location / Location.xs
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5
6 #include <stdio.h>
7 #include <string.h>
8
9
10 #include <loc/libloc.h>
11 #include <loc/database.h>
12 #include <loc/network.h>
13
14
15 MODULE = Location PACKAGE = Location
16
17 struct loc_database *
18 init(file)
19 char* file = NULL;
20
21 CODE:
22 struct loc_ctx* ctx = NULL;
23
24 // Initialise location context
25 int err = loc_new(&ctx);
26 if (err < 0)
27 croak("Could not initialize libloc context: %d\n", err);
28
29 // Open the database file for reading
30 FILE* f = fopen(file, "r");
31 if (!f) {
32 loc_unref(ctx);
33
34 croak("Could not open file for reading: %s: %s\n",
35 file, strerror(errno));
36 }
37
38 // Parse the database
39 struct loc_database* db = NULL;
40 err = loc_database_new(ctx, &db, f);
41 if (err) {
42 loc_unref(ctx);
43
44 croak("Could not read database: %s\n", file);
45 }
46
47 // Cleanup
48 loc_unref(ctx);
49
50 RETVAL = db;
51 OUTPUT:
52 RETVAL
53
54 char*
55 get_country_code(db, address)
56 struct loc_database* db = NULL;
57 char* address = NULL;
58
59 CODE:
60 int err;
61 const char* country_code = NULL;
62
63 // Lookup network
64 struct loc_network *network;
65 err = loc_database_lookup_from_string(db, address, &network);
66 if (err) {
67 croak("Could not look up for %s\n", address);
68 }
69
70 country_code = loc_network_get_country_code(network);
71 loc_network_unref(network);
72
73 if (!country_code) {
74 croak("Could not get the country code for %s\n", address);
75 }
76
77 RETVAL = strdup(country_code);
78 OUTPUT:
79 RETVAL
80
81
82
83 char*
84 database_get_vendor(db)
85 struct loc_database* db = NULL;
86
87 CODE:
88 const char* vendor = NULL;
89
90 // Get vendor
91 vendor = loc_database_get_vendor(db);
92 if (!vendor) {
93 croak("Could not retrieve vendor\n");
94 }
95
96 RETVAL = strdup(vendor);
97 OUTPUT:
98 RETVAL
99
100 void
101 DESTROY(db)
102 struct loc_database* db = NULL;
103
104 CODE:
105 // Close database
106 loc_database_unref(db);