Skip to content

Commit

Permalink
feature: add new utilities functions
Browse files Browse the repository at this point in the history
  • Loading branch information
hozlucas28 committed Sep 11, 2024
1 parent 9f030b9 commit d88e3ac
Show file tree
Hide file tree
Showing 3 changed files with 246 additions and 32 deletions.
111 changes: 105 additions & 6 deletions libs/utilities.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,53 @@
#include "utilities.h"

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int getStrLength(char* str) {
int length = 0;
void fillDashboard(TGame* pGame, int with) {
int i;
int j;

while (*str != '\0') {
length++;
str++;
for (i = 0; i < pGame->rows; i++) {
for (j = 0; j < pGame->cols; j++) {
pGame->dashboard[i][j] = with;
}
}
}

char* getUserInputStr(char* message, int strLength,
int (*validator)(char* userInput)) {
char* userInput = malloc(strLength * sizeof(char));

printf("%s", message);
fflush(stdin);
fgets(userInput, strLength, stdin);
trimStr(userInput);

while (!(*validator)(userInput)) {
printf("Invalid input! Try again...\n");
printf("%s", message);
fflush(stdin);
fgets(userInput, strLength, stdin);
trimStr(userInput);
};

return length;
// TODO

return userInput;
}

int isStrIn(char* str, char* arr[], int size) {
int i;

for (i = 0; i < size; i++) {
if (strcmpi(str, *(arr + i)) == 0) {
return 1;
}
}

return 0;
}

void printDashboard(TGame* pGame) {
Expand All @@ -23,3 +60,65 @@ void printDashboard(TGame* pGame) {
printf("\n");
}
}

int strcmpi(const char* str01, const char* str02) {
int i;

int lengthStr01 = strlen(str01);
int lengthStr02 = strlen(str02);
int shortestLength = lengthStr01 < lengthStr02 ? lengthStr01 : lengthStr02;

char charStr01;
char charStr02;
int cmp = 1;

for (i = 0; i < shortestLength; i++) {
charStr01 = toupper(*(str01 + i));
charStr02 = toupper(*(str02 + i));
cmp = charStr01 - charStr02;

if (cmp != 0) {
return cmp;
};
};

return cmp;
}

void trimStr(char* str) {
trimLeftStr(str);
trimRightStr(str);
}

void trimLeftStr(char* str) {
int i;
int j;
int strLength = strlen(str);

int counter = 0;

for (i = 0; i < strLength; i++) {
if (!isspace(*(str + i))) break;
counter++;
};

for (j = 0; j < strLength - counter; j++) {
*(str + j) = *(str + j + counter);
};

*(str + strLength - counter) = '\0';
}

void trimRightStr(char* str) {
int i;
int strLength = strlen(str);

int counter = 0;

for (i = strLength - 1; i > 0; i--) {
if (!isspace(*(str + i))) break;
counter++;
}

*(str + strLength - counter) = '\0';
}
139 changes: 122 additions & 17 deletions libs/utilities.h
Original file line number Diff line number Diff line change
@@ -1,34 +1,139 @@
#ifndef UTILITIES_H_INCLUDED
#define UTILITIES_H_INCLUDED

#define ROWS 100
#define COLS 100
#include "macros.h"

/**
* @struct TGame
* @brief Represents a game structure.
*
* This structure represents a game in which the player's moves are
* recorded. It contains a dashboard, which is a 2D array representing the
* game board, as well as other properties such as the number of rows and
* columns in the dashboard, and the values to represent alive and dead cells.
*/
typedef struct {
int dashboard[ROWS][COLS];
int rows;
int cols;
int cellAlive;
int cellDead;

int (*dashboard)[COLS]; /** The game board represented as a 2D array. */
int rows; /** The number of rows in the game board. */
int cols; /** The number of columns in the game board. */
int cellsAlive; /** The value representing an alive cell. */
int cellsDead; /** The value representing a dead cell. */
} TGame;

/**
* @brief Calculates the length of a string.
* @brief Fills the dashboard of a game with a specified value.
*
* This function fills the dashboard of a game with a specified value. The
* dashboard represents the game board where the player's moves are recorded.
*
* @param pGame A pointer to the game structure.
* @param with The value to fill the dashboard with.
*
* @warning This function assumes that the game structure (`pGame`) has been
* properly initialized.
*/
void fillDashboard(TGame* pGame, int with);

/**
* @brief Gets user input as a string.
*
* This function takes a null-terminated string as input and returns the number
* of characters in the string, excluding the null character.
* This function prompts the user with a message and retrieves their input as a
* string. The user's input is validated using the provided validator function.
*
* @param str The null-terminated string for which the length needs to be
* calculated.
* @param message The message to display as a prompt to the user.
* @param strLength The maximum length of the string to be inputted by the user.
* @param validator A function pointer to a validator function that takes a
* string as input and returns an integer. The validator function should return
* 1 if the input is valid, and 0 otherwise.
*
* @return The length of the string.
* @return A pointer to the string entered by the user.
*
* @warning The input string must be null-terminated, otherwise the behavior is
* undefined.
* @warning The returned string may be longer than the specified strLength if
* the user enters more characters.
*/
int getStrLength(char* str);
char* getUserInputStr(char* message, int strLength,
int (*validator)(char* userInput));

/**
* @brief Checks if a string is present in an array of strings.
*
* This function checks if a given string is present in an array of strings.
*
* @param str The string to search for.
* @param arr The array of strings to search in.
* @param size The size of the array.
*
* @return 1 if the string is found in the array, 0 otherwise.
*/
int isStrIn(char* str, char* arr[], int size);

/**
* @brief Prints the dashboard of a game.
*
* This function prints the dashboard of a game, which represents the game board
* where the player's moves are recorded.
*
* @param pGame A pointer to the game structure.
*
* @warning This function assumes that the game structure (`pGame`) has been
* properly initialized and the dashboard has been filled with values.
*/
void printDashboard(TGame* pGame);

/**
* @brief Compares two strings case-insensitively.
*
* This function compares two strings case-insensitively and returns an integer
* indicating their relative order. The comparison is based on the ASCII values
* of the characters in the strings.
*
* @param str01 The first string to compare.
* @param str02 The second string to compare.
*
* @return An integer less than, equal to, or greater than zero if str01 is
* found, respectively, to be less than, to match, or be greater than str02.
*
* @warning This function assumes that the input strings are null-terminated.
*/
int strcmpi(const char* str01, const char* str02);

/**
* @brief Trims leading and trailing whitespace characters from a string.
*
* This function trims leading and trailing whitespace characters from a string
* by modifying the string in-place. The trimmed string will have no leading or
* trailing whitespace characters.
*
* @param str The string to trim.
*
* @warning This function assumes that the input string is null-terminated.
*/
void trimStr(char* str);

/**
* @brief Trims leading whitespace characters from a string.
*
* This function trims leading whitespace characters from a string by modifying
* the string in-place. The trimmed string will have no leading whitespace
* characters.
*
* @param str The string to trim.
*
* @warning This function assumes that the input string is null-terminated.
*/
void trimLeftStr(char* str);

/**
* @brief Trims trailing whitespace characters from a string.
*
* This function trims trailing whitespace characters from a string by modifying
* the string in-place. The trimmed string will have no trailing whitespace
* characters.
*
* @param str The string to trim.
*
* @warning This function assumes that the input string is null-terminated.
*/
void trimRightStr(char* str);

#endif // UTILITIES_H_INCLUDED
28 changes: 19 additions & 9 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
#include "../libs/main.h"

#include <stdio.h>
#include <stdlib.h>
#include "stdio.h"

int validatePattern(char* userInput) {
char* options[] = {"glider", "toad", "press", "glider cannon"};
return isStrIn(userInput, options, 2);
}

int main() {
TGame game;

int dashboard[ROWS][COLS];
int rows = ROWS;
int cols = COLS;
int cellAlive;
int cellDead;
int cellAlive = 0;
int cellDead = 0;

TGame game;
char* requestedPattern;

game.dashboard = dashboard;
game.rows = rows;
game.cols = cols;
game.cellAlive = cellAlive;
game.cellDead = cellDead;
game.cellsAlive = cellAlive;
game.cellsDead = cellDead;

fillDashboard(&game, 0);

printDashboard(&game);
requestedPattern = getUserInputStr("Which pattern do you want (XXX)? ", 100,
&validatePattern);
printf("\n'%s'", requestedPattern);
return 0;
}
}

0 comments on commit d88e3ac

Please sign in to comment.