Search code examples
cgtkgtk3

How to tell if a Gtk EntryBox has only non-visible characters such as spaces? In C


I have a simple statement that gets the text of a GTK3 EntryBox. I need to check if the string is valid such that it does not contain just spaces. I could step through the string character by character looking for invalid ASCII character codes, however, I was wondering if there is a simpler way to do this using GTK.

GtkWidget* EntryBox;
char* db_name;

EntryBox = GTK_WIDGET ( gtk_builder_get_object (builder, "SomeRandomGtkEntryBoxId") );
db_name = strdup( gtk_entry_get_text ( GTK_ENTRY ( EntryBox ) ) );

For example gtk_entry_get_text_length ( GTK_ENTRY ( EntryBox ) ) will tell me how many characters are in an EntryBox, but it does not determine whether they are usable as filenames, etc (filenames that are only spaces keep crashing my application).


Solution

  • I wrote a function that invalidates any string with a newline, backslash, forward-slash, single quote, double quote, tilde, backtick, comma, parenthesis, square or curved brackets, or a double space.

    It invalidates strings that begin with the NULL character, a space, or an underscore.

    It also invalidates strings that end with a space, an underscore, or a period.

    {This could be expanded to include other characters.}

    #include<stdio.h>
    #include<string.h>
    #include<stdbool.h>
    
    bool check_valid_string(const char* string)
    {
        size_t len = strlen( string );
        
        /* The string cannot begin with these characters  */
        if ( strchr( " _\0" , (int) string[ 0 ] ) ) return false;
        
        /* The string cannot end with these characters  */
        if ( strchr( " _." , (int) string[ len - 1 ] ) ) return false;
        
        /* The string cannot contain these characters  */
        if ( strpbrk( string, "\n\"\'\\)(][}{/~`," ) ) return false;
        
        /* The string cannot contain a double space  */
        if ( strstr( string, "  " ) ) return false;
        
        return true;
    }
    
    void print_result(char* string)
    {
        if ( check_valid_string( string ) ){
            printf("The String Is Valid.\n");
        } else {
            printf("The String Is Invalid.\n");
        }
    }
    
    int main()
    /* All of these are invalid except the last three strings. */
    {
        print_result("     ");
        print_result("");
        print_result("\n");
        print_result("AString\n");
        print_result("AStrin/g");
        print_result("\"AString\"");
        print_result("\'AString\'");
        print_result("\\AString");
        print_result("~AString");
        print_result("`AString");
        print_result(" AString");
        print_result("AString ");
        print_result("_AString");
        print_result("AString_");
        print_result("A  String");
        print_result("AString.");
        print_result("AStrin,g");
        print_result("AStrin)g");
        print_result("AStrin(g");
        print_result("AStrin]g");
        print_result("AStrin[g");
        print_result("AStrin}g");
        print_result("AStrin{g");
        print_result("A String");
        print_result("A_String");
        print_result("AString");
        return 0;
    }