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