少年修仙传客户端基础资源
hch
2024-04-01 d01413b00ef631ac20347716b23818b0b811f65f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <glib.h>
 
#include "Directory-c-api.h"
#include "Error-c-api.h"
 
struct _GDir {
    UnityPalFindHandle* handle;
    gchar* current;
    gchar* next;
    const gchar* path_for_rewind;
};
 
static gboolean
setup_dir_handle(GDir*dir, const gchar* path, GError **error)
{
    gchar* path_search;
    char* result_file_name = NULL;
    gint unused_attributes;
    UnityPalErrorCode result;
 
    dir->path_for_rewind = g_strdup (path);
    path_search = g_malloc ((strlen(path) + 3)*sizeof(gchar));
    strcpy (path_search, path);
#ifdef G_OS_WIN32
    strcat (path_search, "\\*");
#else
    strcat (path_search, "/*");
#endif
 
    dir->handle = UnityPalDirectoryFindHandleNew(path_search);
    result = UnityPalDirectoryFindFirstFile(dir->handle, path_search, &result_file_name, &unused_attributes);
    if (!UnityPalSuccess(result)) {
        if (error)
            *error = g_error_new (G_LOG_DOMAIN, g_file_error_from_errno (result), strerror (result));
        g_free (dir);
        return FALSE;
    }
 
    while ((strcmp (result_file_name, ".") == 0) || (strcmp (result_file_name, "..") == 0)) {
        result = UnityPalDirectoryFindNextFile(dir->handle, &result_file_name, &unused_attributes);
        if (!UnityPalSuccess(result)) {
            result_file_name = NULL;
            break;
        }
    }
 
    dir->current = NULL;
    dir->next = result_file_name;
    return TRUE;
}
 
static void close_dir_handle(GDir* dir)
{
    UnityPalDirectoryCloseOSHandle(dir->handle);
    UnityPalDirectoryFindHandleDelete(dir->handle);
    dir->handle = 0; 
}
 
GDir *
g_dir_open (const gchar *path, guint flags, GError **error)
{
    GDir *dir;
    gboolean success;
 
    g_return_val_if_fail (path != NULL, NULL);
    g_return_val_if_fail (error == NULL || *error == NULL, NULL);
 
    dir = g_new0 (GDir, 1);
 
    success = setup_dir_handle(dir, path, error);
    if (!success)
        return NULL;
    
    return dir;
}
 
const gchar *
g_dir_read_name (GDir *dir)
{
    char* result_file_name;
    gint unused_attributes;
    UnityPalErrorCode result;
 
    g_return_val_if_fail (dir != NULL && dir->handle != 0, NULL);
 
    if (dir->current)
        g_free (dir->current);
    dir->current = NULL;
 
    dir->current = dir->next;
 
    if (!dir->current)
        return NULL;
 
    dir->next = NULL;
 
    do {
        result = UnityPalDirectoryFindNextFile(dir->handle, &result_file_name, &unused_attributes);
        if (!UnityPalSuccess(result)) {
            dir->next = NULL;
            return dir->current;
        }
    } while ((strcmp (result_file_name, ".") == 0) || (strcmp (result_file_name, "..") == 0));
 
    dir->next = result_file_name;
    return dir->current;
}
 
void
g_dir_rewind (GDir *dir)
{
    g_return_if_fail (dir != NULL && dir->handle != NULL);
 
    close_dir_handle(dir);
    setup_dir_handle(dir, dir->path_for_rewind, NULL);
}
 
void
g_dir_close (GDir *dir)
{
    g_return_if_fail (dir != NULL && dir->handle != 0);
    
    if (dir->current)
        g_free (dir->current);
    dir->current = NULL;
    if (dir->next)
        g_free (dir->next);
    dir->next = NULL;
    if (dir->path_for_rewind)
        g_free(dir->path_for_rewind);
    dir->path_for_rewind = NULL;
    close_dir_handle(dir);
    g_free (dir);
}