-
Notifications
You must be signed in to change notification settings - Fork 0
/
libex_file.h
executable file
·302 lines (223 loc) · 16.1 KB
/
libex_file.h
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
/* -------------------------------------------------------------------------- */
/* (c) ali@balarabe.com [libex_file.h] */
/* -------------------------------------------------------------------------- */
#if !defined INCLUDED_LIBEX_FILE_H
#define INCLUDED_LIBEX_FILE_H
#include "libex_config.h"
#if LX_PLATFORM_WIN32
#include <windows.h>
#endif
#include "libex.h"
#include "libex_string_class.h"
#if LX_PLATFORM_WIN32
#include "libex_win32.h"
#endif
#define LX_FILE_SIZE_MAX 9223372036854775807
/* -------------------------------------------------------------------------- */
LX_NAMESPACE(lx_c)
typedef struct lx_file_t {
uint8_t* ob; /* lx_file_t */
}
lx_file_t, lx_new_file_t;
#if LX_PLATFORM_LINUX
typedef long long lx_file_size_t;
#endif
#if LX_PLATFORM_WIN32
typedef ULONGLONG_win lx_file_size_t;
#endif
typedef enum _lx_File_TextModeEnum {
LX_FILE_TEXT_AUTO = 0, /* lx_File_TextModeEnum */
LX_FILE_TEXT_ANSI = 1, /* lx_File_TextModeEnum */
LX_FILE_TEXT_WIDE = 2 /* lx_File_TextModeEnum */
}
lx_File_TextModeEnum; /*enum*/
typedef enum _lx_File_EncryptModeEnum {
LX_FILE_NO_ENCRYPT = 0, /* lx_File_EncryptModeEnum */
LX_FILE_ENCRYPT = 1, /* lx_File_EncryptModeEnum */
LX_FILE_DECRYPT = -1 /* lx_File_EncryptModeEnum */
}
lx_File_EncryptModeEnum; /*enum*/
/* -------------------------------------------------------------------------- */
/* Constructors / Destructor: */
/* TODO: lx_File_init(). Use macro. */
LX_PUBLIC lx_new_file_t lx_File_init( lx_chars_t filename_ ); /*C*/
LX_PUBLIC lx_new_file_t lx_File_initInMode( /*C*/
lx_chars_t filename_, /*-*/
const lx_bool modify_, /*-*/
const lx_bool shared_ ); /*-*/
#define lx_File_initRead( CHARS__FILENAME_ ) /*M*/\
lx_File_initInMode( CHARS__FILENAME_, LX_C_FALSE, LX_C_TRUE) /*#*/
LX_PUBLIC void lx_File_free( lx_file_t* object_ ); /*D*/
/* -------------------------------------------------------------------------- */
/* Properties: Read-Only */
#if LX_PLATFORM_WIN32
LX_PUBLIC HANDLE_win lx_File_getHandle( lx_file_t object_ ); /*P*/
#endif
LX_PUBLIC lx_bool lx_File_isOpen( const lx_file_t object_ ); /*P*/
LX_PUBLIC lx_chars_t lx_File_getName( const lx_file_t object_ ); /*P*/
LX_PUBLIC size_t lx_File_getFileSize( const lx_file_t object_ ); /*P*/
LX_PUBLIC void lx_File_setFileSize( /*P*/
lx_file_t object_, /*-*/
const size_t value_ ); /*-*/
/* -------------------------------------------------------------------------- */
/* Methods: */
LX_PUBLIC void lx_File_open( /*M*/
lx_file_t object_, /*-*/
lx_chars_t filename_ ); /*-*/
LX_PUBLIC void lx_File_openInMode( /*M*/
lx_file_t object_, /*-*/
lx_chars_t filename_, /*-*/
const lx_bool modify_, /*-*/
const lx_bool shared_ ); /*-*/
LX_PUBLIC void lx_File_goTo( /*M*/
lx_file_t object_, /*-*/
const size_t new_position_ ); /*-*/
/* Move file pointer to the beginning. */
LX_PUBLIC void lx_File_goBegin( lx_file_t object_ ); /*M*/
/* Move file pointer to the end. */
LX_PUBLIC void lx_File_goEnd( lx_file_t object_ ); /*M*/
LX_PUBLIC void lx_File_write( /*M*/
lx_file_t object_, /*-*/
const void* bytes_, /*-*/
const size_t length_in_bytes_ ); /*-*/
LX_PUBLIC void lx_File_writeTextData( /*M*/
lx_file_t object_, /*-*/
lx_chars_t text_ ); /*-*/
LX_PUBLIC void lx_File_writeInt( /*M*/
lx_file_t object_, /*-*/
const int value_ ); /*-*/
LX_PUBLIC void lx_File_writeSize( /*M*/
lx_file_t object_, /*-*/
const size_t value_ ); /*-*/
LX_PUBLIC size_t lx_File_read( /*M*/
lx_file_t object_, /*-*/
void* bytes_, /*-*/
const size_t length_in_bytes_ ); /*-*/
/* Read a previously-stored text value from a file. */
LX_PUBLIC lx_new_str_t lx_File_readTextData( lx_file_t object_ ); /*M*/
LX_PUBLIC int lx_File_readInt( lx_file_t object_ ); /*M*/
LX_PUBLIC size_t lx_File_readSize( lx_file_t object_ ); /*M*/
/* Close the opened file. */
LX_PUBLIC void lx_File_close( lx_file_t object_ ); /*M*/
/* -------------------------------------------------------------------------- */
/* Functions: */
/* returns true if the specified file name exists. */
LX_PUBLIC lx_bool lx_file_exists( lx_chars_t filename_ ); /*F*/
/* delete the specified file, with optionally of confirming the deletion */
/* from the user, sending the file to the recycle bin (instead of just */
/* deleting it), or erasing its contents before deletion. */
LX_PUBLIC lx_bool lx_file_delete_4( /*F*/
lx_chars_t filename_, /*-*/
const lx_bool ask_user_, /*-*/
const lx_bool send_to_bin_, /*-*/
const lx_bool secure_delete_ ); /*-*/
#define lx_file_delete( CHARS__FILENAME_ ) /*F*/\
lx_file_delete_4( CHARS__FILENAME_, \
LX_C_FALSE, LX_C_FALSE, LX_C_FALSE) /*#*/
/* -------------------------------------------------------------------------- */
/* file information functions */
LX_PUBLIC lx_new_str_t lx_file_path( lx_chars_t filename_ ); /*F*/
/* If a path was not specified, adds the current directory to a file name. */
LX_PUBLIC lx_new_str_t lx_file_name_full( /*F*/
lx_chars_t filename_, /*-*/
lx_chars_t default_path_ ); /*-*/
LX_PUBLIC lx_new_str_t lx_file_locate( /*F*/
lx_chars_t filename_, /*-*/
lx_chars_t alternate_paths_ ); /*-*/
LX_PUBLIC lx_new_str_t lx_file_name( lx_chars_t filename_ ); /*F*/
LX_PUBLIC size_t lx_file_size( lx_chars_t filename_ ); /*F*/
LX_PUBLIC lx_new_str_t lx_file_size_description( /*F*/
lx_file_size_t size_, /*-*/
const lx_bool format_remainder_ ); /*-*/
LX_PUBLIC lx_bool lx_file_type_is_text( lx_chars_t filename_ ); /*F*/
/* -------------------------------------------------------------------------- */
/* file manipulation functions */
LX_PUBLIC lx_bool lx_file_move( /*F*/
lx_chars_t source_filename_, /*-*/
lx_chars_t target_filename_, /*-*/
const lx_bool overwrite_ ); /*-*/
LX_PUBLIC lx_bool lx_file_copy( /*F*/
lx_chars_t source_filename_, /*-*/
lx_chars_t target_filename_, /*-*/
const lx_bool overwrite_ ); /*-*/
/* Encrypts or decrypts from one file to another. */
LX_PUBLIC lx_bool lx_file_encrypt( /*F*/
lx_chars_t source_filename_, /*-*/
lx_chars_t target_filename_, /*-*/
const lx_bool overwrite_, /*-*/
const lx_File_EncryptModeEnum encrypt_mode_, /*-*/
lx_chars_t cipher_ ); /*-*/
LX_PUBLIC lx_bool lx_file_write_bytes( /*F*/
lx_chars_t filename_, /*-*/
const size_t start_byte_, /*-*/
const size_t length_in_bytes_, /*-*/
const void* data_ ); /*-*/
LX_PUBLIC lx_bool lx_file_append_bytes( /*F*/
lx_chars_t filename_, /*-*/
const void* bytes_, /*-*/
const size_t length_in_bytes_ ); /*-*/
LX_PUBLIC lx_bool lx_file_append_text( /*F*/
lx_chars_t filename_, /*-*/
lx_chars_t text_ ); /*-*/
LX_PUBLIC lx_bool lx_file_append_text_n( /*F*/
lx_chars_t filename_, /*-*/
lx_chars_t terminator_, /*-*/
... ); /*-*/
LX_PUBLIC lx_new_bytes_t lx_file_load_bytes( lx_chars_t filename_ ); /*F*/
/* -------------------------------------------------------------------------- */
LX_PUBLIC lx_new_str_t lx_file_read_text( /*F*/
lx_chars_t filename_, /*-*/
lx_chars_t alternate_paths_ ); /*-*/
#define lx_file_read_text1( CHARS__FILENAME_ ) /*F*/\
lx_file_read_text( CHARS__FILENAME_, LX_BLANK) /*#*/
/* -------------------------------------------------------------------------- */
/* path information functions */
/* return true if the specified folder or file exists. */
LX_PUBLIC lx_bool lx_path_exists( /*F*/
lx_chars_t path_, /*-*/
const lx_bool folder_, /*-*/
const lx_bool file_ ); /*-*/
/* return only the names of existing folders in the given path. */
LX_PUBLIC lx_new_str_t lx_path_existing( lx_chars_t path_or_file_ ); /*F*/
LX_PUBLIC lx_new_str_t lx_path_extract( /*F*/
lx_chars_t path_or_file_, /*-*/
const lx_bool root_only_ ); /*-*/
LX_PUBLIC lx_new_str_t lx_get_path( lx_chars_t path_or_file_ ); /*F*/
LX_PUBLIC lx_new_str_t lx_path_root( lx_chars_t path_or_file_ ); /*F*/
/* return the name of a folder path without a file name */
LX_PUBLIC lx_new_str_t lx_folder_name( lx_chars_t path_ ); /*F*/
/* return the free space on the specified path. */
LX_PUBLIC size_t lx_get_free_space( lx_chars_t path_ ); /*F*/
/* return the free space on the specified path (large). */
LX_PUBLIC lx_file_size_t lx_get_free_space_large( lx_chars_t path_ ); /*F*/
LX_PUBLIC lx_new_str_t lx_paths_format( lx_chars_t paths_ ); /*F*/
/* -------------------------------------------------------------------------- */
/* Function: lx_process_files_in_folder() */
/* Use the following declaration format for the callback function: */
/* lx_bool callback_fn( lx_chars_t filename_, const size_t file_size_ ) */
/* */
/* Use it as follows: */
/* lx_process_files_in_folder(_T("\\your_path"), true, &callback_fn); */
/* */
/* The callback function should return true to make */
/* lx_process_files_in_folder() continue reading file names */
/* or false to stop. */
/* */
/* (Substitute callback_fn with your function name.) */
#if 000 /* Callback function template: */
LX_PUBLIC lx_bool callback_fn( /*F*/
lx_chars_t filename_, /*-*/
const size_t file_size_ ) { /*-*/
LX_GO (LX_UID(FD5322));
const lx_bool ret = true; /* <- set the return value you want here */
LX_RETURN(ret);
} /* callback_fn */
#endif /* 000 */
LX_PUBLIC lx_bool lx_process_files_in_folder( /*F*/
lx_chars_t path_, /*-*/
const lx_bool scan_subfolders_, /*-*/
lx_bool (* callback_fn_)(lx_chars_t file_, /*-*/
const size_t file_size_ ) ); /*-*/
/* -------------------------------------------------------------------------- */
LX_END_NAMESPACE /*lx_c*/
#endif /*eof*/