From 308925f81dcac05471e818865a94061f3421f217 Mon Sep 17 00:00:00 2001 From: Clecio Jung Date: Thu, 23 Mar 2023 13:16:18 -0300 Subject: Simplifying memory management --- examples/ini_file_create.c | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) (limited to 'examples/ini_file_create.c') diff --git a/examples/ini_file_create.c b/examples/ini_file_create.c index 8910e9f..e1cfd21 100644 --- a/examples/ini_file_create.c +++ b/examples/ini_file_create.c @@ -6,6 +6,7 @@ #include #include #include +#include #include "../ini_file.h" @@ -14,22 +15,14 @@ /* This function reads a string of size MAX_STRING_SIZE from stdin. * It returns 0 if no valid string was retrieved. */ int get_string_from_stdin(const char *const prompt, char *string) { - int i, empty = 0; fputs(prompt, stdout); if (fgets(string, MAX_STRING_SIZE, stdin) == NULL) { return 0; } - /* Replace \n characters for \0 and check if string is empty (only composed of spaces) */ - for (i = 0; string[i] != '\0'; i++) { - if (string[i] == '\n') { - string[i] = '\0'; - break; - } - if (!isspace(string[i])) { - empty = 1; - } - } - return empty; + /* Replace space and new line characters by \0 */ + string[strcspn(string, " \t\r\n")] = '\0'; + /* Check if string is empty */ + return (string[0] != '\0'); } /*------------------------------------------------------------------------------ -- cgit v1.2.3