]> git.ipfire.org Git - people/ms/libloc.git/blob - src/perl/Location/Location.xs
perl: Add perl bindings for libloc.
[people/ms/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 #include "ppport.h"
10
11 #include <loc/libloc.h>
12 #include <loc/database.h>
13 #include <loc/network.h>
14
15
16 MODULE = Location PACKAGE = Location
17
18 struct loc_database *
19 init(file)
20 char * file;
21
22 CODE:
23 int err;
24
25 struct loc_ctx *ctx;
26 err = loc_new(&ctx);
27 if (err < 0)
28 croak("Error");
29
30 FILE* f = fopen(file, "r");
31 if (!f) {
32 croak("Could not open file for reading: %s\n", file);
33 }
34
35 struct loc_database *db;
36 err = loc_database_new(ctx, &db, f);
37 if (err) {
38 croak("Could not open database: %s\n", file);
39 }
40
41 RETVAL = db;
42 OUTPUT:
43 RETVAL
44
45 char*
46 get_country_code(db, address)
47 struct loc_database *db;
48 char * address;
49
50 CODE:
51 int err;
52 const char * country_code;
53
54 struct loc_network *network;
55 err = loc_database_lookup_from_string(db, address, &network);
56 if (err) {
57 croak("Could not look up for %s\n", address);
58 }
59
60 country_code = loc_network_get_country_code(network);
61
62 loc_network_unref(network);
63
64 if (!country_code) {
65 croak("Could not get the country code\n");
66 }
67
68 RETVAL = strdup(country_code);
69 OUTPUT:
70 RETVAL
71
72
73
74 char*
75 database_get_vendor(db)
76 struct loc_database *db;
77
78 CODE:
79 const char * vendor;
80
81 vendor = loc_database_get_vendor(db);
82
83 if (!vendor) {
84 croak("Could not retrieve vendor\n");
85 }
86
87 RETVAL = strdup(vendor);
88 OUTPUT:
89 RETVAL
90
91 void
92 DESTROY(db)
93 struct loc_database *db ;
94 CODE:
95 loc_database_unref(db);