]>
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) | |
e5a1a0bd | 19 | char* file; |
1ea740e9 SS |
20 | |
21 | CODE: | |
da1039da | 22 | struct loc_ctx* ctx = NULL; |
1ea740e9 | 23 | |
74ad091d | 24 | // Initialise location context |
da1039da | 25 | int err = loc_new(&ctx); |
1ea740e9 | 26 | if (err < 0) |
5242283a | 27 | croak("Could not initialize libloc context: %d\n", err); |
1ea740e9 | 28 | |
74ad091d | 29 | // Open the database file for reading |
1ea740e9 SS |
30 | FILE* f = fopen(file, "r"); |
31 | if (!f) { | |
c820094d MT |
32 | loc_unref(ctx); |
33 | ||
5242283a MT |
34 | croak("Could not open file for reading: %s: %s\n", |
35 | file, strerror(errno)); | |
1ea740e9 SS |
36 | } |
37 | ||
74ad091d | 38 | // Parse the database |
c820094d | 39 | struct loc_database* db = NULL; |
1ea740e9 | 40 | err = loc_database_new(ctx, &db, f); |
46d6c767 MT |
41 | |
42 | // We can close the database file straight away | |
43 | // because loc_database_new creates a copy of the file descriptor | |
44 | fclose(f); | |
45 | ||
1ea740e9 | 46 | if (err) { |
c820094d MT |
47 | loc_unref(ctx); |
48 | ||
5242283a | 49 | croak("Could not read database: %s\n", file); |
1ea740e9 SS |
50 | } |
51 | ||
c820094d MT |
52 | // Cleanup |
53 | loc_unref(ctx); | |
54 | ||
1ea740e9 SS |
55 | RETVAL = db; |
56 | OUTPUT: | |
57 | RETVAL | |
58 | ||
59 | char* | |
60 | get_country_code(db, address) | |
e5a1a0bd MT |
61 | struct loc_database* db; |
62 | char* address; | |
1ea740e9 SS |
63 | |
64 | CODE: | |
65 | int err; | |
da1039da | 66 | const char* country_code = NULL; |
1ea740e9 | 67 | |
74ad091d | 68 | // Lookup network |
1ea740e9 SS |
69 | struct loc_network *network; |
70 | err = loc_database_lookup_from_string(db, address, &network); | |
71 | if (err) { | |
72 | croak("Could not look up for %s\n", address); | |
73 | } | |
74 | ||
75 | country_code = loc_network_get_country_code(network); | |
1ea740e9 SS |
76 | loc_network_unref(network); |
77 | ||
78 | if (!country_code) { | |
5242283a | 79 | croak("Could not get the country code for %s\n", address); |
1ea740e9 SS |
80 | } |
81 | ||
82 | RETVAL = strdup(country_code); | |
83 | OUTPUT: | |
84 | RETVAL | |
85 | ||
86 | ||
87 | ||
88 | char* | |
89 | database_get_vendor(db) | |
e5a1a0bd | 90 | struct loc_database* db; |
1ea740e9 SS |
91 | |
92 | CODE: | |
da1039da | 93 | const char* vendor = NULL; |
1ea740e9 | 94 | |
74ad091d | 95 | // Get vendor |
1ea740e9 | 96 | vendor = loc_database_get_vendor(db); |
1ea740e9 SS |
97 | if (!vendor) { |
98 | croak("Could not retrieve vendor\n"); | |
99 | } | |
100 | ||
101 | RETVAL = strdup(vendor); | |
102 | OUTPUT: | |
103 | RETVAL | |
104 | ||
105 | void | |
106 | DESTROY(db) | |
da1039da MT |
107 | struct loc_database* db = NULL; |
108 | ||
1ea740e9 | 109 | CODE: |
74ad091d | 110 | // Close database |
1ea740e9 | 111 | loc_database_unref(db); |