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