-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdh_file_util.c
More file actions
309 lines (280 loc) · 9.05 KB
/
dh_file_util.c
File metadata and controls
309 lines (280 loc) · 9.05 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/* file_util - file utilities
Copyright (C) 2022 Dream Helium
This file is part of litematica_reader_c.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
#include "dh_string_util.h"
#include "dh_file_util.h"
#include "dh_list_util.h"
#include "glib.h"
#include "glibconfig.h"
#include <stdlib.h>
#include <string.h>
#include <gio/gio.h>
static int internal_strcmp(gconstpointer a, gconstpointer b)
{
return strcmp(a, b);
}
static int minus_strcmp(gconstpointer a, gconstpointer b)
{
return -strcmp(a, b);
}
static int strstr_to_int(gconstpointer element, gconstpointer user_data)
{
char* ret = strstr(element, user_data);
if(ret) return 0;
else return -1;
}
GList* dh_file_list_create_full(const char* pos, gboolean with_dir)
{
GFile* dir = g_file_new_for_path(pos);
GError* err = NULL;
/* To use in Windows, we need attribute
* But why don't we need in Linux? */
GFileEnumerator* gfe = g_file_enumerate_children(dir, G_FILE_ATTRIBUTE_STANDARD_NAME, G_FILE_QUERY_INFO_NONE, NULL, &err);
if(gfe == NULL)
{
g_object_unref(dir);
return NULL;
}
GList* list = NULL;
GFileInfo* info = NULL;
while((info = g_file_enumerator_next_file(gfe, NULL, &err)) != NULL)
{
const char* filename = g_file_info_get_name(info);
if(!with_dir)
list = g_list_prepend(list, dh_strdup(g_file_info_get_name(info)));
else
{
char* new_filename = g_build_path(G_DIR_SEPARATOR_S, pos, filename, NULL);
list = g_list_prepend(list, dh_strdup(new_filename));
g_free(new_filename);
}
g_object_unref(info);
}
g_file_enumerator_close(gfe, NULL, &err);
g_object_unref(dir);
g_object_unref(gfe);
list = g_list_sort(list, internal_strcmp);
return list;
}
GList* dh_file_list_create(const char* pos)
{
return dh_file_list_create_full(pos, FALSE);
}
GList* dh_file_list_create_recursive(const char* pos)
{
GList* ret = dh_file_list_create_full(pos, TRUE);
GList* ret_d = ret;
for(; ret_d ; ret_d = ret_d->next)
{
char* filename = ret_d->data;
if(g_file_test(filename, G_FILE_TEST_IS_DIR))
{
GList* add_to_list = dh_file_list_create_recursive(filename);
GList* tmp_list = add_to_list;
for(; tmp_list ; tmp_list = tmp_list->next)
ret = g_list_prepend(ret, dh_strdup(tmp_list->data));
g_list_free_full(add_to_list, free);
}
}
ret = g_list_sort(ret, minus_strcmp);
return ret;
}
GList *dh_file_list_search_in_dir(const char *pos, const char *name)
{
GList* filelist = dh_file_list_create(pos);
if(filelist == NULL)
return NULL;
GList* result = dh_search_in_list(filelist, name);
g_list_free_full(filelist, free);
return result;
}
gboolean dh_file_create(const char* filepos, gboolean is_file)
{
GFile* file = g_file_new_for_path(filepos);
gboolean ret = dh_file_create_gfile(file, is_file);
g_object_unref(file);
return ret;
}
gboolean dh_file_create_gfile(GFile* file, gboolean is_file)
{
if(!file)
return FALSE;
GError* error = NULL;
if(file)
{
if(g_file_query_exists(file, NULL)) /* If file exists, delete file */
{
if(!g_file_delete(file, NULL, &error)) /* Couldn't delete file */
goto error_handle;
}
/* I'll use a dummy way to create file
* First, I'll mkdir */
if(!g_file_make_directory_with_parents(file, NULL, &error))
goto error_handle;
if(is_file)
{
if(!g_file_delete(file, NULL, &error))
goto error_handle;
GFileOutputStream* gfos = g_file_create(file, G_FILE_CREATE_NONE, NULL, &error);
if(error)
goto error_handle;
g_object_unref(gfos);
return TRUE;
}
else return TRUE; /* It's directory */
}
else return FALSE;
error_handle:
g_error_free(error);
return FALSE;
}
gboolean dh_file_exist(const char* filepos)
{
GFile* file = g_file_new_for_path(filepos);
if(file)
{
gboolean ret = g_file_query_exists(file, NULL);
g_object_unref(file);
return ret;
}
else return FALSE;
}
char* dh_read_file(const char* filepos, gsize* size)
{
GFile* file = g_file_new_for_path(filepos);
if(file)
{
if(g_file_query_exists(file, NULL))
{
char* contents = NULL;
if(g_file_load_contents(file, NULL, &contents, size, NULL, NULL))
{
g_object_unref(file);
return contents;
}
else
{
g_free(contents);
g_object_unref(file);
return NULL;
}
}
else
{
g_object_unref(file);
return NULL;
}
}
else return NULL;
}
gboolean dh_write_file(const char* filepos, char* content, gsize count)
{
GFile* file = g_file_new_for_path(filepos);
gboolean ret = dh_write_file_gfile(file, content, count);
g_object_unref(file);
return ret;
}
gboolean dh_write_file_gfile(GFile* file, char* content, gsize count)
{
if(!file)
return FALSE;
if(!dh_file_create_gfile(file, TRUE))
return FALSE;
GFileIOStream* fios = g_file_open_readwrite(file, NULL, NULL);
if(fios)
{
GOutputStream* os = g_io_stream_get_output_stream(G_IO_STREAM(fios));
int ret_d = g_output_stream_write(os, content, count, NULL, NULL);
gboolean ret = (ret_d == -1? FALSE : TRUE);
g_object_unref(fios);
return ret;
}
else
{
return FALSE;
}
}
gboolean dh_file_is_directory(const char *filepos)
{
GFile* file = g_file_new_for_path(filepos);
GFileType type = g_file_query_file_type(file, G_FILE_QUERY_INFO_NONE, NULL);
gboolean ret = FALSE;
if(type == G_FILE_TYPE_DIRECTORY)
ret = TRUE;
g_object_unref(file);
return ret;
}
gboolean dh_file_copy(const char *source, const char *dest, GFileCopyFlags flags)
{
return dh_file_copy_full_arg(source, dest, flags, NULL, NULL, NULL, NULL);
}
gboolean dh_file_copy_full_arg(const char* source, const char* dest, GFileCopyFlags flags,
GCancellable* cancellable, GFileProgressCallback callback,
gpointer data, GError** error)
{
GFile* file_source = g_file_new_for_path(source);
GFile* file_dest = g_file_new_for_path(dest);
if (!g_file_test(dest, G_FILE_TEST_IS_REGULAR))
dh_file_create(dest, TRUE);
gboolean ret = g_file_copy(file_source, file_dest, flags, cancellable, callback, data, error);
g_object_unref(file_source);
g_object_unref(file_dest);
return ret;
}
gboolean dh_file_copy_dir(const char* source, const char* dest, GFileCopyFlags flags)
{
return dh_file_copy_dir_full_arg(source, dest, flags, NULL, NULL, NULL, NULL);
}
gboolean dh_file_copy_dir_full_arg(const char* source, const char* dest, GFileCopyFlags flags,
GCancellable* cancellable, GFileProgressCallback callback,
gpointer data, GError** error)
{
if(!dh_file_exist(dest))
if(!dh_file_create(dest, FALSE))
return FALSE;
if(!dh_file_is_directory(source)) return FALSE;
GList* dir_files = dh_file_list_create(source);
gboolean ret = TRUE;
for(int i = 0 ; i < g_list_length(dir_files) ; i++)
{
if(!ret) break;
gchar* filename = g_list_nth_data(dir_files, i);
gchar* dir = g_build_path(G_DIR_SEPARATOR_S, source, filename, NULL);
gchar* dest_dir = g_build_path(G_DIR_SEPARATOR_S, dest, filename, NULL);
if(dh_file_is_directory(dir))
{
ret = dh_file_copy_dir_full_arg(dir, dest, flags, cancellable, callback, data, error);
g_free(dir);
g_free(dest_dir);
continue;
}
else ret = dh_file_copy_full_arg(dir, dest_dir, flags, cancellable, callback, data, error);
g_free(dir);
g_free(dest_dir);
}
g_list_free_full(dir_files, free);
return ret;
}
char* dh_file_get_current_program_dir(const char* arg_zero)
{
char* full_dir = g_find_program_in_path(arg_zero);
char* dir = g_path_get_dirname(full_dir);
g_free(full_dir);
return dir;
}
void dh_file_rm_file(const char* pos)
{
GFile* file = g_file_new_for_path(pos);
g_file_delete(file, NULL, NULL);
g_object_unref(file);
}