]>
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 | ||
46fbc975 SS |
59 | # |
60 | # Database functions | |
61 | # | |
777959b2 MT |
62 | const char* |
63 | get_vendor(db) | |
64 | struct loc_database* db; | |
65 | ||
66 | CODE: | |
67 | // Get vendor | |
68 | RETVAL = loc_database_get_vendor(db); | |
69 | OUTPUT: | |
70 | RETVAL | |
71 | ||
973c5f03 SS |
72 | const char* |
73 | get_description(db) | |
74 | struct loc_database* db; | |
75 | ||
76 | CODE: | |
77 | // Get database description | |
78 | RETVAL = loc_database_get_description(db); | |
79 | OUTPUT: | |
80 | RETVAL | |
81 | ||
733bd087 SS |
82 | const char* |
83 | get_license(db) | |
84 | struct loc_database* db; | |
85 | ||
86 | CODE: | |
87 | // Get database license | |
88 | RETVAL = loc_database_get_license(db); | |
89 | OUTPUT: | |
90 | RETVAL | |
91 | ||
46fbc975 SS |
92 | # |
93 | # Lookup functions | |
94 | # | |
6aa97cac | 95 | SV* |
70f45781 | 96 | lookup_country_code(db, address) |
e5a1a0bd MT |
97 | struct loc_database* db; |
98 | char* address; | |
1ea740e9 SS |
99 | |
100 | CODE: | |
6aa97cac SS |
101 | RETVAL = &PL_sv_undef; |
102 | ||
74ad091d | 103 | // Lookup network |
1ea740e9 | 104 | struct loc_network *network; |
d3daf87c | 105 | int err = loc_database_lookup_from_string(db, address, &network); |
6aa97cac SS |
106 | if (!err) { |
107 | // Extract the country code | |
108 | const char* country_code = loc_network_get_country_code(network); | |
109 | RETVAL = newSVpv(country_code, strlen(country_code)); | |
1ea740e9 | 110 | |
6aa97cac | 111 | loc_network_unref(network); |
1ea740e9 | 112 | } |
1ea740e9 SS |
113 | OUTPUT: |
114 | RETVAL | |
115 | ||
1ea740e9 SS |
116 | void |
117 | DESTROY(db) | |
34eed014 | 118 | struct loc_database* db; |
da1039da | 119 | |
1ea740e9 | 120 | CODE: |
74ad091d | 121 | // Close database |
1ea740e9 | 122 | loc_database_unref(db); |