mirror of
https://github.com/komiko44240BB/BattleBay_tool_v1.0.git
synced 2026-01-18 16:27:29 +01:00
update and addition of delete functions
This commit is contained in:
28
objects.c
28
objects.c
@@ -167,3 +167,31 @@ void addPerk(struct Perk* p, struct Object* o) {
|
||||
}
|
||||
printf("Perk could not be added, maximum perks reached.\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Deletes all Objects in a linked list contained within a Slots structure.
|
||||
*
|
||||
* This function traverses the linked list of Objects pointed to by the head
|
||||
* of the provided Slots structure. It frees each Object in the list and
|
||||
* finally frees the Slots structure itself.
|
||||
*
|
||||
* @param slots A pointer to the Slots structure that contains the linked list
|
||||
* of Objects. If NULL, the function returns immediately.
|
||||
*/
|
||||
void deleteSlots(struct Slots* slots) {
|
||||
if (slots == NULL) {
|
||||
return; // Nothing to delete
|
||||
}
|
||||
|
||||
struct Object* current = slots->head; // Pointer to the current Object in the list
|
||||
struct Object* next; // Pointer to the next Object to be deleted
|
||||
|
||||
// Iterate through the linked list of Objects and free each one
|
||||
while (current != NULL) {
|
||||
next = current->next; // Store the next object to process
|
||||
deleteObject(current); // Free the current object from memory
|
||||
current = next; // Move to the next object in the list
|
||||
}
|
||||
|
||||
free(slots); // Free the Slots structure itself to avoid memory leaks
|
||||
}
|
||||
|
||||
@@ -48,5 +48,5 @@ void deleteObject(struct Object* o);
|
||||
void addPerk(struct Perk* p, struct Object* o);
|
||||
|
||||
struct Slots* createSlots();
|
||||
|
||||
void deleteSlots(struct Slots* slots);
|
||||
#endif
|
||||
|
||||
35
perks.c
35
perks.c
@@ -491,10 +491,33 @@ char* perk_to_string(struct Perk* p) {
|
||||
*
|
||||
* @param p Perk struct containing the details to be displayed.
|
||||
*/
|
||||
void displayPerk(struct Perk p) {
|
||||
printf("Is Event: %s\n", p.is_event ? "True" : "False");
|
||||
printf("First Boost Value: %.2f\n", p.first_boost_value);
|
||||
printf("First Boost Type: %s\n", p.first_boost_type);
|
||||
printf("Second Boost Value: %.2f\n", p.second_boost_value);
|
||||
printf("Second Boost Type: %s\n", p.second_boost_type);
|
||||
void displayPerk(struct Perk* p) {
|
||||
printf("Is Event: %s\n", p->is_event ? "True" : "False");
|
||||
printf("First Boost Value: %.2f\n", p->first_boost_value);
|
||||
printf("First Boost Type: %s\n", p->first_boost_type);
|
||||
printf("Second Boost Value: %.2f\n", p->second_boost_value);
|
||||
printf("Second Boost Type: %s\n", p->second_boost_type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Frees memory allocated for a Perk struct and its members.
|
||||
*
|
||||
* @param p Pointer to the Perk struct to be deleted. If p is NULL,
|
||||
* the function does nothing. Otherwise, it frees memory
|
||||
* for each allocated member before freeing the Perk itself.
|
||||
*/
|
||||
void deletePerk(struct Perk* p) {
|
||||
if (p == NULL) { // Check if the pointer is NULL to avoid unnecessary operations.
|
||||
return;
|
||||
}
|
||||
if (p->first_boost_type != NULL) { // Free first_boost_type if it has been allocated.
|
||||
free(p->first_boost_type);
|
||||
p->first_boost_type = NULL; // Set pointer to NULL to avoid dangling pointers.
|
||||
}
|
||||
if (p->second_boost_type != NULL) { // Free second_boost_type if it has been allocated.
|
||||
free(p->second_boost_type);
|
||||
p->second_boost_type = NULL; // Set pointer to NULL to avoid dangling pointers.
|
||||
}
|
||||
free(p); // Free the Perk struct itself.
|
||||
}
|
||||
|
||||
|
||||
3
perks.h
3
perks.h
@@ -20,5 +20,6 @@ struct Perk* displayAndChooseAvailablePerks(char** perk_list, bool is_event, str
|
||||
bool perk_already_fited(bool is_event, struct Object* o, char* line);
|
||||
char* perk_to_string(struct Perk* p);
|
||||
void updateEvent(struct Perk* p,bool is_event);
|
||||
void displayPerk(struct Perk p );
|
||||
void displayPerk(struct Perk* p );
|
||||
void deletePerk(struct Perk* p);
|
||||
#endif
|
||||
|
||||
@@ -151,4 +151,29 @@ struct Training* readTraining(char* object_name) {
|
||||
}
|
||||
fclose(file); // Close the file once training data is successfully read
|
||||
return t; // Return the populated Training struct
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Deletes a Training struct and frees associated memory.
|
||||
*
|
||||
* This function checks if the given Training struct pointer is not NULL,
|
||||
* then proceeds to free the memory allocated for its members and the struct itself.
|
||||
* It is important to ensure that each member is freed only if it was previously allocated.
|
||||
*
|
||||
* @param t A pointer to the Training struct to be deleted.
|
||||
*/
|
||||
void deleteTraining(struct Training* t) {
|
||||
// Check if the Training struct pointer is not NULL
|
||||
if (t != NULL) {
|
||||
// Free the memory allocated for the 'type' array, if it exists
|
||||
if (t->type != NULL) {
|
||||
free(t->type); // Free the type array
|
||||
}
|
||||
// Free the memory allocated for the 'value' array, if it exists
|
||||
if (t->value != NULL) {
|
||||
free(t->value); // Free the value array
|
||||
}
|
||||
// Free the memory allocated for the Training struct itself
|
||||
free(t); // Free the Training struct
|
||||
}
|
||||
}
|
||||
@@ -8,4 +8,5 @@ struct Training {
|
||||
};
|
||||
struct Training* createTraining(unsigned int training_lvl);
|
||||
struct Training* readTraining(char* object_name);
|
||||
void deleteTraining(struct Training* t);
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user