-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrust_interface.c
More file actions
108 lines (90 loc) · 2.73 KB
/
rust_interface.c
File metadata and controls
108 lines (90 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
// Define C equivalents of Rust structs
typedef struct {
const char *name;
const char *price;
const char *url;
const unsigned int a_n_o_hostel_club;
} Room;
typedef struct {
const char *name;
Room *room_options;
const char *link;
unsigned int number_of_rooms;
} Hostel;
typedef struct {
const char *name;
const char *ano_url;
Hostel *hostels;
unsigned int hostels_len;
} City;
typedef struct {
City *cities;
unsigned int len;
} ListCCity;
typedef struct {
const char **strings;
unsigned int length;
} ListCString;
void print_room(Room *room) {
printf("\t\tRoom %s for %s\n", room->name, room->price);
}
void print_hostel(Hostel *hostel) {
printf("\tHostel %s\n", hostel->name);
for (int i = 0; i < hostel->number_of_rooms; i++) {
print_room(&hostel->room_options[i]);
}
}
void print_city(City *city) {
printf("City %s\n", city->name);
for (int i = 0; i < city->hostels_len; i++) {
print_hostel(&city->hostels[i]);
}
}
void print_city_list(ListCCity *cities) {
if (!cities)
printf("city list nao existe\n");
for (int i = 0; i < cities->len; i++) {
print_city(&cities->cities[i]);
}
}
// Declare the Rust function
extern ListCCity *get_many_cities(ListCString cities_names_c,
const char *date_start, const char *date_end);
extern void free_city_list(ListCCity *city);
int main() {
const char *cities[] = {"Dresden", "Berlin"};
int num_cities = 2;
const char *date_start = "2025-07-01";
const char *date_end = "2025-07-05";
ListCString cities_names_c;
cities_names_c.length = num_cities;
cities_names_c.strings = (const char **)malloc(num_cities * sizeof(char *));
// Copy city names (ensure null-terminated strings)
for (int i = 0; i < num_cities; i++) {
cities_names_c.strings[i] = strdup(cities[i]);
if (!cities_names_c.strings[i]) {
fprintf(stderr, "String duplication failed\n");
// Cleanup
for (int j = 0; j < i; j++)
free((void *)cities_names_c.strings[j]);
free(cities_names_c.strings);
return 1;
}
}
// Call the Rust function
ListCCity *result = get_many_cities(cities_names_c, date_start, date_end);
printf("Concluded the rust part\n");
print_city_list(result);
// Cleanup input memory
for (int i = 0; i < num_cities; i++) {
free((void *)cities_names_c.strings[i]);
}
// Cleanup output memory (requires a Rust-provided free function)
// Example: extern void free_list_ccity(ListCCity);
// free_list_ccity(result);
return 0;
}