]>
Commit | Line | Data |
---|---|---|
1ea740e9 SS |
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 | ||
1ea740e9 SS |
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: | |
da1039da | 22 | struct loc_ctx* ctx = NULL; |
1ea740e9 | 23 | |
da1039da | 24 | int err = loc_new(&ctx); |
1ea740e9 | 25 | if (err < 0) |
5242283a | 26 | croak("Could not initialize libloc context: %d\n", err); |
1ea740e9 SS |
27 | |
28 | FILE* f = fopen(file, "r"); | |
29 | if (!f) { | |
5242283a MT |
30 | croak("Could not open file for reading: %s: %s\n", |
31 | file, strerror(errno)); | |
1ea740e9 SS |
32 | } |
33 | ||
da1039da | 34 | struct loc_database *db = NULL; |
1ea740e9 SS |
35 | err = loc_database_new(ctx, &db, f); |
36 | if (err) { | |
5242283a | 37 | croak("Could not read database: %s\n", file); |
1ea740e9 SS |
38 | } |
39 | ||
40 | RETVAL = db; | |
41 | OUTPUT: | |
42 | RETVAL | |
43 | ||
44 | char* | |
45 | get_country_code(db, address) | |
da1039da MT |
46 | struct loc_database* db = NULL; |
47 | char* address = NULL; | |
1ea740e9 SS |
48 | |
49 | CODE: | |
50 | int err; | |
da1039da | 51 | const char* country_code = NULL; |
1ea740e9 SS |
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); | |
1ea740e9 SS |
60 | loc_network_unref(network); |
61 | ||
62 | if (!country_code) { | |
5242283a | 63 | croak("Could not get the country code for %s\n", address); |
1ea740e9 SS |
64 | } |
65 | ||
66 | RETVAL = strdup(country_code); | |
67 | OUTPUT: | |
68 | RETVAL | |
69 | ||
70 | ||
71 | ||
72 | char* | |
73 | database_get_vendor(db) | |
da1039da | 74 | struct loc_database* db = NULL; |
1ea740e9 SS |
75 | |
76 | CODE: | |
da1039da | 77 | const char* vendor = NULL; |
1ea740e9 SS |
78 | |
79 | vendor = loc_database_get_vendor(db); | |
1ea740e9 SS |
80 | if (!vendor) { |
81 | croak("Could not retrieve vendor\n"); | |
82 | } | |
83 | ||
84 | RETVAL = strdup(vendor); | |
85 | OUTPUT: | |
86 | RETVAL | |
87 | ||
88 | void | |
89 | DESTROY(db) | |
da1039da MT |
90 | struct loc_database* db = NULL; |
91 | ||
1ea740e9 SS |
92 | CODE: |
93 | loc_database_unref(db); |