如何判断 Gtk EntryBox 是否只有空格等不可见字符?在 C 中

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

我有一个获取 GTK3 EntryBox 文本的简单语句。 我需要检查字符串是否有效,以便它不只包含空格。 我可以逐个字符地遍历字符串以查找无效的 ASCII 字符代码,但是,我想知道是否有使用 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 ) ) );

例如 gtk_entry_get_text_length ( GTK_ENTRY ( EntryBox ) ) 会告诉我 EntryBox 中有多少个字符,但它不能确定它们是否可用作文件名等(只有空格的文件名会使我的应用程序崩溃)。

我写了一个函数,可以使任何带有换行符、反斜杠、正斜杠、单引号、双引号、波浪符、反引号、逗号、圆括号、方括号或弯括号或双括号的字符串无效 space .

它使以 NULL 字符、space 或下划线开头的字符串无效。

它还会使以 space、下划线或句点结尾的字符串无效。

{这可以扩展到包括其他字符。}

#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( " _[=10=]" , (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;
}