diff --git a/trabalho-pratico/include/types/account_status.h b/trabalho-pratico/include/types/account_status.h new file mode 100644 index 0000000..3428c21 --- /dev/null +++ b/trabalho-pratico/include/types/account_status.h @@ -0,0 +1,42 @@ +/* + * Copyright 2023 Humberto Gomes, José Lopes, José Matos + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file account_status.h + * @brief Account status of a ::user_t. + */ + +#ifndef ACCOUNT_STATUS_H +#define ACCOUNT_STATUS_H + +/** @brief The account status of a ::user_t. */ +typedef enum { + ACCOUNT_STATUS_INACTIVE, /**< @brief Inactive account */ + ACCOUNT_STATUS_ACTIVE /**< @brief Active account */ +} account_status_t; + +/** + * @brief Parses a user's account status. + * + * @param output Where the parsed status will be placed (only on success). + * @param input Input (`"active"` or `"inactive"`, case-insensitive). + * + * @retval 0 Parsing success + * @retval 1 Parsing failure + */ +int account_status_from_string(account_status_t *output, const char *input); + +#endif diff --git a/trabalho-pratico/include/types/airport_code.h b/trabalho-pratico/include/types/airport_code.h new file mode 100644 index 0000000..362190e --- /dev/null +++ b/trabalho-pratico/include/types/airport_code.h @@ -0,0 +1,93 @@ +/* + * Copyright 2023 Humberto Gomes, José Lopes, José Matos + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file airport_code.h + * @brief Airport code in a ::flight_t. + * + * @anchor airport_examples + * ### Examples + * + * The following example shows how to validate airport codes and how to print them back to strings. + * It's also shown that all airport codes are stored in upper-case. + * + * ```c + * const char *string_codes[6] = { + * "", // Too short + * "O", // Too short + * "OP", // Too short + * "OPO", // Just right + * "oPo", // Everything should be converted to upper-case + * "OPOR", // Too long + * }; + * + * for (int i = 0; i < 6; ++i) { + * airport_code_t parsed; + * + * if (!airport_code_from_string(&parsed, string_codes[i])) { + * char back_to_string[AIRPORT_CODE_SPRINTF_MIN_BUFFER_SIZE]; + * airport_code_sprintf(back_to_string, parsed); + * printf("Parsed code: \"%s\"\n", back_to_string); + * + * } else { + * fprintf(stderr, "Failed to parse airport code \"%s\"!\n", string_codes[i]); + * } + * } + * ``` + */ + +#ifndef AIRPORT_CODE_H +#define AIRPORT_CODE_H + +#include + +/** + * @brief An airport code of a ::flight_t. + */ +typedef uint32_t airport_code_t; + +/** + * @brief Creates (and validates) a airport code from a string. + * + * @param output Where to place the parsed airport code (not modified on failure). + * @param input String to parse. + * + * @retval 0 Valid airport code + * @retval 1 Invalid airport code + */ +int airport_code_from_string(airport_code_t *output, const char *input); + +/** + * @brief The minimum number of characters (including null terminator) needed to write an airport + * code to a buffer using ::airport_code_sprintf. + * @details + * + * | O | P | O | \0 | + * | - | - | - | -- | + * | 1 | 2 | 3 | 4 | + */ +#define AIRPORT_CODE_SPRINTF_MIN_BUFFER_SIZE 4 + +/** + * @brief Prints a airport code using `sprintf`. + * + * @param output Where to print the airport code to. Must be at least + * ::AIRPORT_CODE_SPRINTF_MIN_BUFFER_SIZE long. + * @param airport Country code to be printed. + */ +void airport_code_sprintf(char *output, airport_code_t airport); + +#endif diff --git a/trabalho-pratico/include/types/country_code.h b/trabalho-pratico/include/types/country_code.h new file mode 100644 index 0000000..3d3a07f --- /dev/null +++ b/trabalho-pratico/include/types/country_code.h @@ -0,0 +1,97 @@ +/* + * Copyright 2023 Humberto Gomes, José Lopes, José Matos + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file country_code.h + * @brief Country code in a ::user_t. + * + * @anchor country_code_examples + * ### Examples + * + * The following example shows how to validate country codes and how to print them back to strings. + * + * ```c + * const char *string_codes[4] = { + * "", // Too short + * "P", // Too short + * "PT", // Just right + * "POR", // Too long + * }; + * + * for (int i = 0; i < 4; ++i) { + * country_code_t parsed; + * + * if (!country_code_from_string(&parsed, string_codes[i])) { + * char back_to_string[COUNTRY_CODE_SPRINTF_MIN_BUFFER_SIZE]; + * country_code_sprintf(back_to_string, parsed); + * printf("Parsed code: \"%s\"\n", back_to_string); + * + * } else { + * fprintf(stderr, "Failed to parse country code \"%s\"!\n", string_codes[i]); + * } + * } + * ``` + */ + +#ifndef COUNTRY_CODE_H +#define COUNTRY_CODE_H + +#include + +/** + * @brief A country code of a ::user_t. + */ +typedef uint16_t country_code_t; + +/** + * @brief Creates (and validates) a country code from a string. + * + * @param output Where to place the parsed country code (not modified on failure). + * @param input String to parse. + * + * @retval 0 Valid country code + * @retval 1 Invalid country code + * + * + * #### Examples + * See [the header file's documentation](@ref country_code_examples). + */ +int country_code_from_string(country_code_t *output, const char *input); + +/** + * @brief The minimum number of characters (including null terminator) needed to write a country + * code to a buffer using ::country_code_sprintf. + * @details + * + * | P | T | \0 | + * | - | - | -- | + * | 1 | 2 | 3 | + */ +#define COUNTRY_CODE_SPRINTF_MIN_BUFFER_SIZE 3 + +/** + * @brief Prints a country code using `sprintf`. + * + * @param output Where to print the country code to. Must be at least + * ::COUNTRY_CODE_SPRINTF_MIN_BUFFER_SIZE long. + * @param country Country code to be printed. + * + * #### Examples + * See [the header file's documentation](@ref country_code_examples). + */ +void country_code_sprintf(char *output, country_code_t country); + +#endif diff --git a/trabalho-pratico/include/types/flight.h b/trabalho-pratico/include/types/flight.h new file mode 100644 index 0000000..c9e5ced --- /dev/null +++ b/trabalho-pratico/include/types/flight.h @@ -0,0 +1,210 @@ +/* + * Copyright 2023 Humberto Gomes, José Lopes, José Matos + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file flight.h + * @brief Declaration of type ::flight_t. + * + * @details In this module you can find a declaration of the type `flight_t` as a struct flight, + * as well as getter and setter functions, which allow's the developer access to any + * previously created flight, or gives them the ability to create a new flight. + * + * You can see what fields define a flight (and thus available through getters and + * setters) in the [struct's documentation](@ref flight). + */ + +#ifndef FLIGHT_H +#define FLIGHT_H + +#include "types/airport_code.h" +#include "utils/date_and_time.h" + +/** + * @brief Type `flight_t` defined as a struct flight, that stores valuable information of a given + * flight. + * @details It's an opaque type. + */ +typedef struct flight flight_t; + +/** + * @brief Creates a new flight with uninitialized fields. + * @return A `malloc`-allocated flight (`NULL` on allocation failure). + */ +flight_t *flight_create(void); + +/** + * @brief Sets the flight's airline. + * @details @p airline will not get owned by @p flight, and you should free it later. + * + * @param flight Flight to have its airline set. + * @param airline Airline of the flight. + */ +void flight_set_airline(flight_t *flight, char *airline); + +/** + * @brief Sets the flight's plane model. + * @details @p plane_model will not get owned by @p flight, and you should free it later. + * + * @param flight Flight to have its plane model set. + * @param plane_model Plane model of the flight. + */ +void flight_set_plane_model(flight_t *flight, char *plane_model); + +/** + * @brief Sets the flight's origin. + * @details @p origin will not get owned by @p flight, and you should free it later. + * + * @param flight Flight to have its origin set. + * @param origin Origin of the flight. + */ +void flight_set_origin(flight_t *flight, airport_code_t origin); + +/** + * @brief Sets the flight's destination. + * @details @p destination will not get owned by @p flight, and you should free it later. + * + * @param flight Flight to have its destination set. + * @param destination Destination of the flight. + */ +void flight_set_destination(flight_t *flight, airport_code_t destination); + +/** + * @brief Sets the flight's identifier. + * @param flight Flight to have its id set. + * @param id Identifier of the flight. + */ +void flight_set_id(flight_t *flight, size_t id); + +/** + * @brief Sets the flight's scheduled departure date. + * @param flight Flight to have its scheduled departure date set. + * @param schedule_departure_date Scheduled departure date of the flight. + */ +void flight_set_schedule_departure_date(flight_t *flight, date_and_time_t schedule_departure_date); + +/** + * @brief Sets the flight's scheduled arrival date. + * @param flight Flight to have its scheduled arrival date set. + * @param schedule_arrival_date Scheduled arrival date of the flight. + */ +void flight_set_schedule_arrival_date(flight_t *flight, date_and_time_t schedule_arrival_date); + +/** + * @brief Increments the flight's number of passengers. + * @param flight Flight to have its number of passengers incremented. + * @param increment_factor Factor by which the number of passengers is incremented. + */ +void flight_increment_number_of_passengers(flight_t *flight, int increment_factor); + +/** + * @brief Sets the flight's number of passengers. + * @param flight Flight to have its number of passengers set. + * @param number_of_passengers Number of passengers of the flight. + */ +void flight_set_number_of_passengers(flight_t *flight, int number_of_passengers); + +/** + * @brief Sets the flight's real departure date. + * @param flight Flight to have its real departure date set. + * @param real_departure_date Real departure date of the flight. + */ +void flight_set_real_departure_date(flight_t *flight, date_and_time_t real_departure_date); + +/** + * @brief Sets the flight's number of total seats. + * @param flight Flight to have its number of total seats set. + * @param total_seats Number of total seats of the flight. + */ +void flight_set_total_seats(flight_t *flight, int total_seats); + +/** + * @brief Gets the flight's airline. + * @param flight Flight to get the airline from. + * @return The flight's airline, with modifications not allowed. + */ +const char *flight_get_const_airline(const flight_t *flight); + +/** + * @brief Gets the flight's plane model. + * @param flight Flight to get the plane model from. + * @return The flight's plane model, with modifications not allowed. + */ +const char *flight_get_const_plane_model(const flight_t *flight); + +/** + * @brief Gets the flight's origin. + * @param flight Flight to get the origin from. + * @return The flight's origin. + */ +airport_code_t flight_get_origin(const flight_t *flight); + +/** + * @brief Gets the flight's destination. + * @param flight Flight to get the destination from. + * @return The flight's destination. + */ +airport_code_t flight_get_destination(const flight_t *flight); + +/** + * @brief Gets the flight's identifier. + * @param flight Flight to get the id from. + * @return The flight's identifier. + */ +size_t flight_get_id(const flight_t *flight); + +/** + * @brief Gets the flight's scheduled departure date. + * @param flight Flight to get the scheduled departure date from. + * @return The flight's scheduled departure date. + */ +date_and_time_t flight_get_schedule_departure_date(const flight_t *flight); + +/** + * @brief Gets the flight's scheduled arrival date. + * @param flight Flight to get the scheduled arrival date from. + * @return The flight's scheduled arrival date. + */ +date_and_time_t flight_get_schedule_arrival_date(const flight_t *flight); + +/** + * @brief Gets the flight's number of passengers. + * @param flight Flight to get the number of passengers from. + * @return The flight's number of passengers. + */ +int flight_get_number_of_passengers(const flight_t *flight); + +/** + * @brief Gets the flight's real departure date. + * @param flight Flight to get the real departure date from. + * @return The flight's departure date. + */ +date_and_time_t flight_get_real_departure_date(const flight_t *flight); + +/** + * @brief Gets the flight's number of total seats. + * @param flight Flight to get the number of total seats from. + * @return The flight's number of total seats. + */ +int flight_get_total_seats(const flight_t *flight); + +/** + * @brief Frees the memory used for a given flight. + * @details All strings inside the flight won't be freed, as they're not owned by the flight. + * @param flight FLight to be deleted. + */ +void flight_free(flight_t *flight); + +#endif diff --git a/trabalho-pratico/include/types/includes_breakfast.h b/trabalho-pratico/include/types/includes_breakfast.h new file mode 100644 index 0000000..1064a40 --- /dev/null +++ b/trabalho-pratico/include/types/includes_breakfast.h @@ -0,0 +1,72 @@ +/* + * Copyright 2023 Humberto Gomes, José Lopes, José Matos + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file includes_breakfast.h + * @brief Includes breakfast of a ::reservation_t. + */ + +#ifndef INCLUDES_BREAKFAST_H +#define INCLUDES_BREAKFAST_H + +/** + * @brief Indicates if breakfast is included for a ::reservation_t. + * @details For the values `INCLUDES_BREAKFAST_0`, `INCLUDES_BREAKFAST_NO_INPUT`, + * `INCLUDES_BREAKFAST_F` and `INCLUDES_BREAKFAST_FALSE` a breakfast is not included, for + * every other value the reservation includes breakfast. + */ +typedef enum includes_breakfast { + INCLUDES_BREAKFAST_0, + INCLUDES_BREAKFAST_1, + INCLUDES_BREAKFAST_NO_INPUT, + INCLUDES_BREAKFAST_F, + INCLUDES_BREAKFAST_FALSE, + INCLUDES_BREAKFAST_T, + INCLUDES_BREAKFAST_TRUE +} includes_breakfast_t; + +/** + * @brief Parses a reservation's inclusion of breakfast. + * + * @param output Where the parsed value will be placed (only on success). + * @param input Input (`"0"`,`"1"`, NULL (no input), `"f"`, `"false"`, `"t"` or `"true"`). + * + * @retval 0 Parsing success + * @retval 1 Parsing failure + */ +int includes_breakfast_from_string(includes_breakfast_t *output, const char *input); + +/** + * @brief The minimum number of characters (including null terminator) needed to write an "includes + * breakfast" field to a buffer using ::includes_breakfast_sprintf. + * @details + * + * | F | A | L | S | E | \0 | + * | - | - | - | - | - | -- | + * | 1 | 2 | 3 | 4 | 5 | 6 | + */ +#define INCLUDES_BREAKFAST_SPRINTF_MIN_BUFFER_SIZE 4 + +/** + * @brief Prints an "includes breakfast" field using `sprintf`. + * + * @param output Where to print the airport code to. Must be at least + * ::INCLUDES_BREAKFAST_SPRINTF_MIN_BUFFER_SIZE long. + * @param breakfast "Includes breakfast" field. + */ +void includes_breakfast_sprintf(char *output, includes_breakfast_t breakfast); + +#endif diff --git a/trabalho-pratico/include/types/reservation.h b/trabalho-pratico/include/types/reservation.h new file mode 100644 index 0000000..b90c015 --- /dev/null +++ b/trabalho-pratico/include/types/reservation.h @@ -0,0 +1,216 @@ +/* + * Copyright 2023 Humberto Gomes, José Lopes, José Matos + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file reservation.h + * @brief Declaration of type ::reservation_t. + * + * @details In this module you can find a declaration of the type `reservation_t` as a struct + * reservation, as well as getter and setter functions, which allow's the developer + * access to any previously created reservation, or gives them the ability to create a + * new reservation. + * + * You can see what fields define a reservation (and thus available through getters and + * setters) in the [struct's documentation](@ref reservation). + */ +#ifndef RESERVATION_H +#define RESERVATION_H + +#include "types/includes_breakfast.h" +#include "utils/date.h" + +/** + * @brief Type `reservation_t` defined as a struct reservation, that stores valuable information of + * a given reservation. + * @details It's an opaque type. + */ +typedef struct reservation reservation_t; + +/** + * @brief Creates a new reservation with uninitialized fields. + * @return A `malloc`-allocated reservation (`NULL` on allocation failure). + */ +reservation_t *reservation_create(void); + +/** + * @brief Sets the reservation's user identifier. + * @details @p user_id will not get owned by @p reservation, and you should free it later. + * + * @param reservation Reservation to have its user identifier set. + * @param user_id User identifier of the reservation. + */ +void reservation_set_user_id(reservation_t *reservation, char *user_id); + +/** + * @brief Sets the reservation's hotel name. + * @details @p hotel_name will not get owned by @p reservation, and you should free it later. + * + * @param reservation Reservation to have its hotel name set. + * @param hotel_name Hotel name of the reservation. + */ +void reservation_set_hotel_name(reservation_t *reservation, char *hotel_name); + +/** + * @brief Sets the reservation's inclusion of breakfast. + * @param reservation Reservation to have its inclusion of breakfast set. + * @param includes_breakfast A flag that gives information on the inclusion of breakfast of + * a reservation. + */ +void reservation_set_includes_breakfast(reservation_t *reservation, + includes_breakfast_t includes_breakfast); + +/** + * @brief Sets the reservation's beginning date. + * @param reservation Reservation to have its beginning date set. + * @param begin_date Beginning date of the reservation. + */ +void reservation_set_begin_date(reservation_t *reservation, date_t begin_date); + +/** + * @brief Sets the reservation's end date. + * @param reservation Reservation to have its end date set. + * @param end_date End date of the reservation. + */ +void reservation_set_end_date(reservation_t *reservation, date_t end_date); + +/** + * @brief Sets the reservation's identifier. + * @param reservation Reservation to have its identifier set. + * @param id Identifier of the reservation. + */ +void reservation_set_id(reservation_t *reservation, size_t id); + +/** + * @brief Sets the reservation's rating. + * @param reservation Reservation to have its rating set. + * @param rating Rating of the reservation. + */ +void reservation_set_rating(reservation_t *reservation, int rating); + +/** + * @brief Sets the reservation's hotel identifier. + * @param reservation Reservation to have its hotel identifier set. + * @param hotel_id Hotel identifier of the reservation. + */ +void reservation_set_hotel_id(reservation_t *reservation, int hotel_id); + +/** + * @brief Sets the reservation's hotel stars. + * @param reservation Reservation to have its hotel stars set. + * @param hotel_stars Hotel stars of the reservation. + */ +void reservation_set_hotel_stars(reservation_t *reservation, int hotel_stars); + +/** + * @brief Sets the reservation's city tax. + * @param reservation Reservation to have its city tax set. + * @param city_tax City tax of the reservation. + */ +void reservation_set_city_tax(reservation_t *reservation, int city_tax); + +/** + * @brief Sets the reservation's price per night. + * @param reservation Reservation to have its price per night set. + * @param price_per_night Price per night of the reservation. + */ +void reservation_set_price_per_night(reservation_t *reservation, int price_per_night); + +/** + * @brief Gets the reservation's user identifier. + * @param reservation Reservation to get the user identifier from. + * @return The reservation's user identifier, with modifications not allowed. + */ +const char *reservation_get_const_user_id(const reservation_t *reservation); + +/** + * @brief Gets the reservation's hotel name. + * @param reservation Reservation to get the hotel name from. + * @return The reservation's hotel name, with modifications not allowed. + */ +const char *reservation_get_const_hotel_name(const reservation_t *reservation); + +/** + * @brief Gets information on the inclusion of breakfast of the reservation. + * @param reservation Reservation to get the `includes_breakfast` flag from. + * @return The reservation's flag `includes_breakfast`. + */ +includes_breakfast_t reservation_get_includes_breakfast(reservation_t *reservation); + +/** + * @brief Gets the reservation's beginning date. + * @param reservation Reservation to get the beginning date from. + * @return The reservation's beginning date. + */ +date_t reservation_get_begin_date(reservation_t *reservation); + +/** + * @brief Gets the reservation's end date. + * @param reservation Reservation to get the end date from. + * @return The reservation's end date. + */ +date_t reservation_get_end_date(reservation_t *reservation); + +/** + * @brief Gets the reservation's identifier. + * @param reservation Reservation to get the identifier from. + * @return The reservation's identifier. + */ +size_t reservation_get_id(reservation_t *reservation); + +/** + * @brief Gets the reservation's rating. + * @param reservation Reservation to get the rating from. + * @return The reservation's rating. + */ +int reservation_get_const_rating(reservation_t *reservation); + +/** + * @brief Gets the reservation's hotel identifier. + * @param reservation Reservation to get the hotel identifier from. + * @return The reservation's hotel identifier. + */ +int reservation_get_const_hotel_id(reservation_t *reservation); + +/** + * @brief Gets the reservation's hotel stars. + * @param reservation Reservation to get the hotel stars from. + * @return The reservation's hotel stars. + */ +int reservation_get_hotel_stars(reservation_t *reservation); + +/** + * @brief Gets the reservation's city tax. + * @param reservation Reservation to get the city tax from. + * @return The reservation's city tax. + */ +int reservation_get_city_tax(reservation_t *reservation); + +/** + * @brief Gets the reservation's price per night. + * @param reservation Reservation to get the price per night from. + * @return The reservation's price per night. + */ +int reservation_get_price_per_night(reservation_t *reservation); + +/** + * @brief Frees the memory used for a given reservation. + * @details All strings inside the reservation won't be freed, as they're not owned by the + * reservation. + * @param reservation Reservation to be deleted. + */ +void reservation_free(reservation_t *reservation); + +#endif diff --git a/trabalho-pratico/include/types/sex.h b/trabalho-pratico/include/types/sex.h new file mode 100644 index 0000000..a3d42a8 --- /dev/null +++ b/trabalho-pratico/include/types/sex.h @@ -0,0 +1,42 @@ +/* + * Copyright 2023 Humberto Gomes, José Lopes, José Matos + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file sex.h + * @brief Sex of a ::user_t. + */ + +#ifndef SEX_H +#define SEX_H + +/** @brief The sex of a ::user_t. */ +typedef enum { + SEX_F, /**< @brief Female */ + SEX_M /**< @brief Male */ +} sex_t; + +/** + * @brief Parses a user sex. + * + * @param output Where the parsed sex will be placed (only on success). + * @param input Input (`"M"` or `"F"`). + * + * @retval 0 Parsing success + * @retval 1 Parsing failure + */ +int sex_from_string(sex_t *output, const char *input); + +#endif diff --git a/trabalho-pratico/include/types/user.h b/trabalho-pratico/include/types/user.h new file mode 100644 index 0000000..c821429 --- /dev/null +++ b/trabalho-pratico/include/types/user.h @@ -0,0 +1,180 @@ +/* + * Copyright 2023 Humberto Gomes, José Lopes, José Matos + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file user.h + * @brief Declaration of type ::user_t. + * + * @details In this module you can find a declaration of the type `user_t`, as well as getter and + * setter functions, which allow the developer to access any previously created user, and + * functions to create new users. + * + * You can see what fields define a user (and thus available through getters and setters) + * in the [struct's documentation](@ref user). + */ + +#ifndef USER_H +#define USER_H + +#include "types/account_status.h" +#include "types/country_code.h" +#include "types/sex.h" +#include "utils/date.h" +#include "utils/date_and_time.h" + +/** + * @brief Type `user_t` defined as a struct user, that stores valuable information of a given + * person. + * @details It's an opaque type. + */ +typedef struct user user_t; + +/** + * @brief Creates a new user with uninitialized fields. + * @return A `malloc`-allocated user (`NULL` on allocation failure). + */ +user_t *user_create(void); + +/** + * @brief Sets the user's identifier. + * @details @p id will not get owned by @p user, and you should free it later. + * + * @param user User to have its identifier set. + * @param id Id of the user. + */ +void user_set_id(user_t *user, char *id); + +/** + * @brief Sets the user's name. + * @details @p name will not get owned by @p user, and you should free it later. + * + * @param user User to have its name set. + * @param name Name of the user. + */ +void user_set_name(user_t *user, char *name); + +/** + * @brief Sets the user's birth date. + * + * @param user User to have its birth date set. + * @param date Birth date of the user. + */ +void user_set_birth_date(user_t *user, date_t date); + +/** + * @brief Sets the user's passport. + * @details @p passport will not get owned by @p user, and you should free it later. + * + * @param user User to have its passport set. + * @param passport Passport number of the user. + */ +void user_set_passport(user_t *user, char *passport); + +/** + * @brief Sets the user's country code. + * + * @param user User to have its country code set. + * @param country_code Country code of the user. + */ +void user_set_country_code(user_t *user, country_code_t country_code); + +/** + * @brief Sets the user's sex. + * + * @param user User to have its sex set. + * @param sex Sex of the user. + */ +void user_set_sex(user_t *user, sex_t sex); + +/** + * @brief Sets the user's account status. + * + * @param user User to have its account status set. + * @param account_status Whether the user's account is active or inactive. + */ +void user_set_account_status(user_t *user, account_status_t account_status); + +/** + * @brief Sets the user's account creation date. + * + * @param user User to have its account status set. + * @param date Account creation date of the user. + */ +void user_set_account_creation_date(user_t *user, date_and_time_t date); + +/** + * @brief Gets the user's identifier. + * @param user User to get id from. + * @return The user's identifier, with modifications not allowed. + */ +const char *user_get_const_id(const user_t *user); + +/** + * @brief Gets the user name. + * @param user User to get name from. + * @return The user's name, with modifications not allowed. + */ +const char *user_get_const_name(const user_t *user); + +/** + * @brief Gets the user's birth date. + * @param user User to get birth date from. + * @return The user's birth date. + */ +date_t user_get_birth_date(const user_t *user); + +/** + * @brief Gets the user passport. + * @param user User to get passport number from. + * @return The user's passport number, with modifications not allowed. + */ +const char *user_get_const_passport(const user_t *user); + +/** + * @brief Gets the user's country code. + * @param user User to get country code from. + */ +country_code_t user_get_country_code(const user_t *user); + +/** + * @brief Gets the user's sex. + * @param user User to get sex from. + * @return The user's sex. + */ +sex_t user_get_sex(const user_t *user); + +/** + * @brief Gets the user's account_status. + * @param user User to get account status from. + * @return The user's account status. + */ +account_status_t user_get_account_status(const user_t *user); + +/** + * @brief Gets the user's account creation date. + * @param user User to get account create date from. + * @return The user's account creation date. + */ +date_and_time_t user_get_account_creation_date(const user_t *user); + +/** + * @brief Frees the memory used for a given user. + * @details All strings inside the user won't be freed, as they're not owned by the user. + * @param user User to be deleted. + */ +void user_free(user_t *user); + +#endif diff --git a/trabalho-pratico/src/test.c b/trabalho-pratico/src/test.c index 17e98a0..bbbb4e1 100644 --- a/trabalho-pratico/src/test.c +++ b/trabalho-pratico/src/test.c @@ -22,43 +22,36 @@ #include #include -#include "utils/string_pool.h" - -/** @brief Number of characters in a pool block. In practice, this should be way larger. */ -#define TEST_POOL_BLOCK_SIZE 32 - -/** @brief Number of pool items to be allocated */ -#define TEST_NUM_ITEMS 10000 +#include "types/airport_code.h" /** * @brief The entry point to the test program. - * @details Tests for string pools. + * @details Test for airport codes. * @retval 0 Success * @retval 1 Insuccess */ int main(void) { - string_pool_t *pool = string_pool_create(TEST_POOL_BLOCK_SIZE); - - const char *long_string = - "This string is longer than a single block, but the pool can still handle it!"; - const char *short_string = "Hello, world!"; - - char *allocated[TEST_NUM_ITEMS] = {0}; - for (size_t i = 0; i < TEST_NUM_ITEMS; ++i) { - int r = rand() % 2 == 1; - - allocated[i] = string_pool_put(pool, r ? short_string : long_string); - if (!allocated[i]) { - fputs("Allocation error!\n", stderr); - string_pool_free(pool); - return 1; + const char *string_codes[6] = { + "", /* Too short */ + "O", /* Too short */ + "OP", /* Too short */ + "OPO", /* Just right */ + "oPo", /* Everything should be converted to upper-case */ + "OPOR", /* Too long */ + }; + + for (int i = 0; i < 6; ++i) { + airport_code_t parsed; + + if (!airport_code_from_string(&parsed, string_codes[i])) { + char back_to_string[AIRPORT_CODE_SPRINTF_MIN_BUFFER_SIZE]; + airport_code_sprintf(back_to_string, parsed); + printf("Parsed code: \"%s\"\n", back_to_string); + + } else { + fprintf(stderr, "Failed to parse airport code \"%s\"!\n", string_codes[i]); } } - for (size_t i = 0; i < TEST_NUM_ITEMS; ++i) { - printf("%s\n", allocated[i]); - } - - string_pool_free(pool); return 0; } diff --git a/trabalho-pratico/src/types/account_status.c b/trabalho-pratico/src/types/account_status.c new file mode 100644 index 0000000..356a5f6 --- /dev/null +++ b/trabalho-pratico/src/types/account_status.c @@ -0,0 +1,49 @@ +/* + * Copyright 2023 Humberto Gomes, José Lopes, José Matos + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file account_status.c + * @brief Implementation of methods in include/types/account_status.h + */ + +#include +#include + +#include "types/account_status.h" + +/** @brief Value of `strlen("inactive")`. */ +#define ACCOUNT_STATUS_STRLEN_INACTIVE 8 + +int account_status_from_string(account_status_t *output, const char *input) { + size_t len = strlen(input); + if (len > ACCOUNT_STATUS_STRLEN_INACTIVE) + return 1; /* Too long for "inactive" */ + + char lower_case[ACCOUNT_STATUS_STRLEN_INACTIVE + 1]; + for (size_t i = 0; i < len; ++i) + lower_case[i] = tolower(input[i]); + lower_case[len] = '\0'; + + if (strcmp(lower_case, "inactive") == 0) { + *output = ACCOUNT_STATUS_INACTIVE; + return 0; + } else if (strcmp(lower_case, "active") == 0) { + *output = ACCOUNT_STATUS_ACTIVE; + return 0; + } else { + return 1; + } +} diff --git a/trabalho-pratico/src/types/airport_code.c b/trabalho-pratico/src/types/airport_code.c new file mode 100644 index 0000000..eed6e05 --- /dev/null +++ b/trabalho-pratico/src/types/airport_code.c @@ -0,0 +1,44 @@ +/* + * Copyright 2023 Humberto Gomes, José Lopes, José Matos + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file airport_code.c + * @brief Implementation of methods in include/types/airport_code.h + * + * ### Examples + * See [the header file's documentation](@ref airport_code_examples). + */ + +#include + +#include "types/airport_code.h" + +int airport_code_from_string(airport_code_t *output, const char *input) { + if (*input && *(input + 1) && *(input + 2) && !*(input + 3) && isalpha(*input) && + isalpha(*(input + 1)) && isalpha(*(input + 2))) { + + *((char *) output) = toupper(input[0]); + *(((char *) output) + 1) = toupper(input[1]); + *(((char *) output) + 2) = toupper(input[2]); + *(((char *) output) + 3) = '\0'; + return 0; + } + return 1; +} + +void airport_code_sprintf(char *output, airport_code_t airport) { + *((airport_code_t *) output) = airport; +} diff --git a/trabalho-pratico/src/types/country_code.c b/trabalho-pratico/src/types/country_code.c new file mode 100644 index 0000000..b93d186 --- /dev/null +++ b/trabalho-pratico/src/types/country_code.c @@ -0,0 +1,40 @@ +/* + * Copyright 2023 Humberto Gomes, José Lopes, José Matos + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file country_code.c + * @brief Implementation of methods in include/types/country_code.h + * + * ### Examples + * See [the header file's documentation](@ref country_code_examples). + */ + +#include + +#include "types/country_code.h" + +int country_code_from_string(country_code_t *output, const char *input) { + if (*input && *(input + 1) && !*(input + 2) && isalpha(*input) && isalpha(*(input + 1))) { + *output = *(country_code_t *) input; + return 0; + } + return 1; +} + +void country_code_sprintf(char *output, country_code_t country) { + *((country_code_t *) output) = country; + output[2] = '\0'; +} diff --git a/trabalho-pratico/src/types/flight.c b/trabalho-pratico/src/types/flight.c new file mode 100644 index 0000000..b272988 --- /dev/null +++ b/trabalho-pratico/src/types/flight.c @@ -0,0 +1,157 @@ +/* + * Copyright 2023 Humberto Gomes, José Lopes, José Matos + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file flight.c + * @brief Implementation of methods in include/types/flight.h + */ +#include + +#include "types/flight.h" + +/** + * @struct flight + * @brief Represents a flight in the datasets. + * @details NOTE: some fields in the project's requirements (such as real arrival date, pilot, + * copilot and notes) aren't put here, as they won't be required by any + * database query. + * + * @var flight::airline + * @brief Airline of a given flight. + * @var flight::plane_model + * @brief Plane model of a given flight. + * @var flight::origin + * @brief Airport of origin of the flight. + * @var flight::destination + * @brief Airport of origin of the flight. + * @var flight::id + * @brief Identifier of a given flight. + * @var flight::schedule_departure_date + * @brief Scheduled departure date of a given flight. + * @var flight::real_departure_date + * @brief Real departure date of a given flight. + * @var flight::schedule_arrival_date + * @brief Scheduled arrival date of a given flight. + * @var flight::number_of_passengers + * @brief Number of passengers of a given flight. + * @var flight::total_seats + * @brief Number of total seats of a given flight. + */ +struct flight { + char *airline; + char *plane_model; + airport_code_t origin; + airport_code_t destination; + size_t id; + date_and_time_t schedule_departure_date; + date_and_time_t real_departure_date; + date_and_time_t schedule_arrival_date; + int number_of_passengers; + int total_seats; +}; + +flight_t *flight_create(void) { + return malloc(sizeof(struct flight)); +} + +void flight_set_airline(flight_t *flight, char *airline) { + flight->airline = airline; +} + +void flight_set_plane_model(flight_t *flight, char *plane_model) { + flight->plane_model = plane_model; +} + +void flight_set_origin(flight_t *flight, airport_code_t origin) { + flight->origin = origin; +} + +void flight_set_destination(flight_t *flight, airport_code_t destination) { + flight->destination = destination; +} + +void flight_set_id(flight_t *flight, size_t id) { + flight->id = id; +} + +void flight_set_schedule_departure_date(flight_t *flight, + date_and_time_t scheduled_departure_date) { + flight->schedule_departure_date = scheduled_departure_date; +} + +void flight_set_schedule_arrival_date(flight_t *flight, date_and_time_t schedule_arrival_date) { + flight->schedule_arrival_date = schedule_arrival_date; +} + +void flight_increment_number_of_passengers(flight_t *flight, int increment_factor) { + flight->number_of_passengers += increment_factor; +} + +void flight_set_number_of_passengers(flight_t *flight, int number_of_passengers) { + flight->number_of_passengers = number_of_passengers; +} + +void flight_set_real_departure_date(flight_t *flight, date_and_time_t real_departure_date) { + flight->real_departure_date = real_departure_date; +} + +void flight_set_total_seats(flight_t *flight, int total_seats) { + flight->total_seats = total_seats; +} + +const char *flight_get_const_airline(const flight_t *flight) { + return flight->airline; +} + +const char *flight_get_const_plane_model(const flight_t *flight) { + return flight->plane_model; +} + +airport_code_t flight_get_origin(const flight_t *flight) { + return flight->origin; +} + +airport_code_t flight_get_destination(const flight_t *flight) { + return flight->destination; +} + +size_t flight_get_id(const flight_t *flight) { + return flight->id; +} + +date_and_time_t flight_get_schedule_departure_date(const flight_t *flight) { + return flight->schedule_departure_date; +} + +date_and_time_t flight_get_schedule_arrival_date(const flight_t *flight) { + return flight->schedule_arrival_date; +} + +int flight_get_number_of_passengers(const flight_t *flight) { + return flight->number_of_passengers; +} + +date_and_time_t flight_get_real_departure_date(const flight_t *flight) { + return flight->real_departure_date; +} + +int flight_get_total_seats(const flight_t *flight) { + return flight->total_seats; +} + +void flight_free(flight_t *flight) { + free(flight); +} diff --git a/trabalho-pratico/src/types/includes_breakfast.c b/trabalho-pratico/src/types/includes_breakfast.c new file mode 100644 index 0000000..2582b80 --- /dev/null +++ b/trabalho-pratico/src/types/includes_breakfast.c @@ -0,0 +1,79 @@ +/* + * Copyright 2023 Humberto Gomes, José Lopes, José Matos + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file includes_breakfast.c + * @brief Implementation of methods in include/types/includes_breakfast.h + */ +#include + +#include "types/includes_breakfast.h" + +int includes_breakfast_from_string(includes_breakfast_t *output, const char *input) { + if (*input == '\0') { + *output = INCLUDES_BREAKFAST_NO_INPUT; + return 0; + } else if (strcmp(input, "f") == 0) { + *output = INCLUDES_BREAKFAST_F; + return 0; + } else if (strcmp(input, "false") == 0) { + *output = INCLUDES_BREAKFAST_FALSE; + return 0; + } else if (strcmp(input, "t") == 0) { + *output = INCLUDES_BREAKFAST_T; + return 0; + } else if (strcmp(input, "true") == 0) { + *output = INCLUDES_BREAKFAST_TRUE; + return 0; + } else if (strcmp(input, "1") == 0) { + *output = INCLUDES_BREAKFAST_1; + return 0; + } else if (strcmp(input, "0") == 0) { + *output = INCLUDES_BREAKFAST_0; + return 0; + } + return 1; +} + +void includes_breakfast_sprintf(char *output, includes_breakfast_t breakfast) { + const char *source = ""; + + switch (breakfast) { + case INCLUDES_BREAKFAST_0: + source = "0"; + break; + case INCLUDES_BREAKFAST_1: + source = "1"; + break; + case INCLUDES_BREAKFAST_NO_INPUT: + source = ""; + break; + case INCLUDES_BREAKFAST_F: + source = "f"; + break; + case INCLUDES_BREAKFAST_FALSE: + source = "false"; + break; + case INCLUDES_BREAKFAST_T: + source = "t"; + break; + case INCLUDES_BREAKFAST_TRUE: + source = "true"; + break; + } + + strcpy(output, source); +} diff --git a/trabalho-pratico/src/types/reservation.c b/trabalho-pratico/src/types/reservation.c new file mode 100644 index 0000000..f273e5c --- /dev/null +++ b/trabalho-pratico/src/types/reservation.c @@ -0,0 +1,164 @@ +/* + * Copyright 2023 Humberto Gomes, José Lopes, José Matos + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * @file reservation.c + * @brief Implementation of methods in include/types/reservation.h + */ + +#include +#include + +#include "types/reservation.h" + +/** + * @struct reservation + * @brief Represents a reservation in the datasets. + * @details NOTE: some fields in the project's requirements (such as address, room details and + * comments) aren't put here, as they won't be required by any database query. + * + * @var reservation::user_id + * @brief User identifier of a given reservation. + * @var reservation::hotel_name + * @brief Hotel name of a given reservation. + * @var reservation::includes_breakfast + * @brief Flag identifying the inclusion of breakfast of a given reservation. + * @var reservation::begin_date + * @brief The beginning date of a given reservation. + * @var reservation::end_date + * @brief End date of a given reservation. + * @var reservation::id + * @brief Identifier of a given reservation. + * @var reservation::rating + * @brief Rating (equals `-1` if it wasn't given) of a given reservation. + * @var reservation::hotel_id + * @brief Hotel identifier of a given reservation. + * @var reservation::hotel_stars + * @brief Hotel stars of a given reservation. + * @var reservation::city_tax + * @brief City tax of a given reservation. + * @var reservation::price_per_night + * @brief Price per night of a given reservation. + */ +struct reservation { + char *user_id; + char *hotel_name; + enum includes_breakfast includes_breakfast; + date_t begin_date; + date_t end_date; + size_t id; + int rating; + int hotel_id; + int hotel_stars; + int city_tax; + int price_per_night; +}; + +reservation_t *reservation_create(void) { + return malloc(sizeof(struct reservation)); +} + +void reservation_set_user_id(reservation_t *reservation, char *user_id) { + reservation->user_id = user_id; +} + +void reservation_set_hotel_name(reservation_t *reservation, char *hotel_name) { + reservation->hotel_name = hotel_name; +} + +void reservation_set_includes_breakfast(reservation_t *reservation, + includes_breakfast_t includes_breakfast) { + reservation->includes_breakfast = includes_breakfast; +} + +void reservation_set_begin_date(reservation_t *reservation, date_t begin_date) { + reservation->begin_date = begin_date; +} + +void reservation_set_end_date(reservation_t *reservation, date_t end_date) { + reservation->end_date = end_date; +} + +void reservation_set_id(reservation_t *reservation, size_t id) { + reservation->id = id; +} + +void reservation_set_rating(reservation_t *reservation, int rating) { + reservation->rating = rating; +} + +void reservation_set_hotel_id(reservation_t *reservation, int hotel_id) { + reservation->hotel_id = hotel_id; +} + +void reservation_set_hotel_stars(reservation_t *reservation, int hotel_stars) { + reservation->hotel_stars = hotel_stars; +} + +void reservation_set_city_tax(reservation_t *reservation, int city_tax) { + reservation->city_tax = city_tax; +} + +void reservation_set_price_per_night(reservation_t *reservation, int price_per_night) { + reservation->price_per_night = price_per_night; +} + +const char *reservation_get_const_user_id(const reservation_t *reservation) { + return reservation->user_id; +} + +const char *reservation_get_const_hotel_name(const reservation_t *reservation) { + return reservation->hotel_name; +} + +includes_breakfast_t reservation_get_includes_breakfast(reservation_t *reservation) { + return reservation->includes_breakfast; +} + +date_t reservation_get_begin_date(reservation_t *reservation) { + return reservation->begin_date; +} + +date_t reservation_get_end_date(reservation_t *reservation) { + return reservation->end_date; +} + +size_t reservation_get_id(reservation_t *reservation) { + return reservation->id; +} + +int reservation_get_const_rating(reservation_t *reservation) { + return reservation->rating; +} + +int reservation_get_const_hotel_id(reservation_t *reservation) { + return reservation->hotel_id; +} + +int reservation_get_hotel_stars(reservation_t *reservation) { + return reservation->hotel_stars; +} + +int reservation_get_city_tax(reservation_t *reservation) { + return reservation->city_tax; +} + +int reservation_get_price_per_night(reservation_t *reservation) { + return reservation->price_per_night; +} + +void reservation_free(reservation_t *reservation) { + free(reservation); +} diff --git a/trabalho-pratico/src/types/sex.c b/trabalho-pratico/src/types/sex.c new file mode 100644 index 0000000..9f4ce40 --- /dev/null +++ b/trabalho-pratico/src/types/sex.c @@ -0,0 +1,36 @@ +/* + * Copyright 2023 Humberto Gomes, José Lopes, José Matos + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file sex.c + * @brief Implementation of methods in include/types/sex.h + */ + +#include + +#include "types/sex.h" + +int sex_from_string(sex_t *output, const char *input) { + if (strcmp(input, "M") == 0) { + *output = SEX_M; + return 0; + } else if (strcmp(input, "F") == 0) { + *output = SEX_F; + return 0; + } else { + return 1; + } +} diff --git a/trabalho-pratico/src/types/user.c b/trabalho-pratico/src/types/user.c new file mode 100644 index 0000000..3aa7a07 --- /dev/null +++ b/trabalho-pratico/src/types/user.c @@ -0,0 +1,131 @@ +/* + * Copyright 2023 Humberto Gomes, José Lopes, José Matos + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file user.c + * @brief Implementation of methods in include/types/user.h + */ + +#include + +#include "types/user.h" + +/** + * @struct user + * @brief Represents a user in the datasets. + * @details NOTE: some fields in the project's requirements (such as emails, phone numbers, + * addresses and payment methods) aren't put here, as they won't be required by any + * database query. + * + * @var user::id + * @brief Identifier of a given user. + * @var user::name + * @brief Full name of a given user. + * @var user::birth_date + * @brief Date of birth of the user. + * @var user::sex + * @brief Sex of the user. + * @var user::passport + * @brief Passport number of a given user. + * @var user::country_code + * @brief Code of the country of a given user. + * @var user::account_creation_date + * @brief Date of creation of the user's account. + * @var user::account_status + * @brief Whether a user's account is active or inactive. + */ +struct user { + char *id; + char *name; + date_t birth_date; + char *passport; + country_code_t country_code; + sex_t sex; + account_status_t account_status; + date_and_time_t account_creation_date; +}; + +user_t *user_create(void) { + return malloc(sizeof(struct user)); +} + +void user_set_id(user_t *user, char *id) { + user->id = id; +} + +void user_set_name(user_t *user, char *name) { + user->name = name; +} + +void user_set_birth_date(user_t *user, date_t date) { + user->birth_date = date; +} + +void user_set_passport(user_t *user, char *passport) { + user->passport = passport; +} + +void user_set_country_code(user_t *user, country_code_t country_code) { + user->country_code = country_code; +} + +void user_set_sex(user_t *user, sex_t sex) { + user->sex = sex; +} + +void user_set_account_status(user_t *user, account_status_t account_status) { + user->account_status = account_status; +} + +void user_set_account_creation_date(user_t *user, date_and_time_t date) { + user->account_creation_date = date; +} + +const char *user_get_const_id(const user_t *user) { + return user->id; +} + +const char *user_get_const_name(const user_t *user) { + return user->name; +} + +date_t user_get_birth_date(const user_t *user) { + return user->birth_date; +} + +const char *user_get_const_passport(const user_t *user) { + return user->passport; +} + +country_code_t user_get_country_code(const user_t *user) { + return user->country_code; +} + +sex_t user_get_sex(const user_t *user) { + return user->sex; +} + +account_status_t user_get_account_status(const user_t *user) { + return user->account_status; +} + +date_and_time_t user_get_account_creation_date(const user_t *user) { + return user->account_creation_date; +} + +void user_free(user_t *user) { + free(user); +}