-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibex_file.c
More file actions
executable file
·2343 lines (1890 loc) · 79.7 KB
/
libex_file.c
File metadata and controls
executable file
·2343 lines (1890 loc) · 79.7 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -------------------------------------------------------------------------- */
/* (c) ali@balarabe.com [libex_file.c] */
/* -------------------------------------------------------------------------- */
#include "libex_c_precompiled.h"
#include "libex_file_.h"
#if defined INCLUDED_LIBEX_FILE_H
#include "libex_config_.h"
#include <limits.h>
#include <stdarg.h> /* for va_arg va_end va_list va_start */
#include <stdio.h> /* for fpos_t fclose() fread() fseek() SEEK_SET */
/* file_size _fsopen() SEEK_END fgetpos() */
#include <string.h> /* for memset() */
#if PLATFORM_LINUX
#define _XOPEN_SOURCE 500
#include <fcntl.h>
#include <ftw.h>
#endif
#if PLATFORM_WIN32
#include <share.h> /* for _SH_DENYNO */
#include <tchar.h> /* on Windows */
#endif
#include "libex_.h"
#include "libex_call_.h"
#include "libex_const_.h"
#include "libex_debug_.h"
#include "libex_dtime_.h"
#include "libex_error_.h"
#include "libex_logging_.h" /* important to give descriptive errors */
#include "libex_macro_.h"
#include "libex_string_class_.h"
#include "libex_stringc_.h"
#include "libex_system_.h"
#include "libex_type_.h"
#include "libex_variant_functions_.h"
#if defined __cplusplus_cli
#pragma unmanaged
#endif
#if PLATFORM_WIN32 && defined _MSC_VER
#pragma comment (lib, "shell32.lib")
/* SHFileOperation() */
#endif
#define _CHECK_HAS_NAME( NAME_, EXEC_ON_ERROR_, SRC_UID_ ) \
if (STRLEN_T(NAME_) == 0) \
{ \
ERR(_T("file or folder not specified"), SRC_UID_); \
EXEC_ON_ERROR_ \
} /*#*/
/* -------------------------------------------------------------------------- */
NAMESPACE(c_)
typedef struct _file_t {
new_chars_t full_name; /* _file_t */
new_chars_t path; /* _file_t */
new_chars_t name; /* _file_t */
size_t size; /* _file_t */
size_t pos; /* _file_t */
dtime_t created_on; /* _file_t */
dtime_t modified_on; /* _file_t */
bool is_opened; /* _file_t */
bool modify; /* _file_t */
#if PLATFORM_LINUX
int file; /* _file_t */
#endif
#if PLATFORM_WIN32
HANDLE_win file; /* _file_t */
#endif
}
_file_t;
/* -------------------------------------------------------------------------- */
/* Functions: Private */
#define _BASE_OBJ_WRAP_INC
#define _OBJ_HANDLE_TYPE file_t
#define _OBJ_INNER_TYPE _file_t
#define _OBJ_ALLOC_FN_UID UID(FBE11A)
#define _OBJ_FREE_FN_UID UID(F48E41)
#define _OBJ_CONST_FN_UID UID(F7CE8B)
#define _OBJ_FN_UID UID(FCB40F)
#define _OBJ_WRAP_FN_UID UID(F9F2C3)
#include "libex_object_wrapper.inc.c"
/* -------------------------------------------------------------------------- */
/* Constructors / Destructor: */
PUBLIC new_file_t File_init( chars_t filename_ ) { /*C*/
GO (UID(FB6218));
const new_file_t ret = File_initInMode(filename_, true, true);
RETURN(ret);
} /* File_init */
PUBLIC new_file_t File_initInMode( /*C*/
chars_t filename_, /*-*/
const bool modify_, /*-*/
const bool shared_ ) { /*-*/
GO (UID(F8479C));
_file_t* ob = _object_alloc();
new_file_t ret = _object_wrap(ob);
ob->modify = modify_;
ob->name = T_chars(filename_);
File_openInMode(ret, filename_, modify_, shared_);
RETURN(ret);
} /* File_initInMode */
/* -------------------------------------------------------------------------- */
/* Destructor: */
PUBLIC void File_free( file_t* object_ ) { /*D*/
GO (UID(F44183));
_file_t* const me = _object(object_, UID(E5C333));
File_close(*object_);
me->modify = true;
me->is_opened = false;
me->pos = 0;
me->size = 0;
#if PLATFORM_WIN32
me->file = 0;
#endif
freeT(&me->full_name);
freeT(&me->name);
freeT(&me->path);
CLEAR(me->created_on);
CLEAR(me->modified_on);
_object_free(object_);
RETURN(NIL);
} /* File_free */
/* -------------------------------------------------------------------------- */
/* Properties: Read-Only */
#if PLATFORM_WIN32
PUBLIC HANDLE_win File_getHandle( file_t object_ ) { /*P*/
GO (UID(F26CE6));
const _file_t* const me = _object(&object_, UID(E627F6));
const HANDLE_win ret = CAST(HANDLE_win, me->file);
RETURN(ret);
} /* File_getHandle */
#endif /* PLATFORM_WIN32 */
PUBLIC bool File_isOpen( const file_t object_ ) { /*P*/
GO (UID(F07363));
const _file_t* const me = _object_const(&object_, UID(E56C97));
const bool ret = me->is_opened;
RETURN(ret);
} /* File_isOpen */
PUBLIC chars_t File_getName( const file_t object_ ) { /*P*/
GO (UID(FA5295));
const _file_t* const me = _object_const(&object_, UID(E9B9DD));
chars_t ret = me->full_name;
RETURN(ret);
} /* File_getName */
PUBLIC size_t File_getFileSize( const file_t object_ ) { /*P*/
GO (UID(F46E16));
const _file_t* const me = _object_const(&object_, UID(E8877B));
const size_t ret = me->is_opened ? me->size : 0;
RETURN(ret);
} /* File_getFileSize */
PUBLIC void File_setFileSize( /*P*/
file_t object_, /*-*/
const size_t value_ ) { /*-*/
GO (UID(F791F9));
_file_t* const me = _object(&object_, UID(E98220));
me->size = value_;
RETURN(NIL);
} /* File_setFileSize */
/* -------------------------------------------------------------------------- */
/* Methods: */
/* TODO: File_open(). Use macro instead. */
PUBLIC void File_open( /*M*/
file_t object_, /*-*/
chars_t filename_ ) { /*-*/
GO (UID(F1810B));
File_openInMode(object_, filename_, true, true);
RETURN(NIL);
} /* File_open */
PUBLIC void File_openInMode( /*M*/
file_t object_, /*-*/
chars_t filename_, /*-*/
const bool modify_, /*-*/
const bool shared_ ) { /*-*/
GO (UID(F68F1F));
_file_t* const me = _object(&object_, UID(ED7258));
size_t fsize = 0;
if (me->is_opened) {
File_close(object_);
}
me->modify = modify_;
#if PLATFORM_LINUX
{
int access = (modify_ ? (O_CREAT | O_RDWR) : O_RDONLY);
if (!shared_) {
access |= O_EXCL;
}
{
const int file = open(filename_, access);
if (file == -1) {
me->is_opened = false;
OS_WARN(_T("open()"), UID(E825DD));
RETURN(NIL);
}
{
const off_t pos = lseek(file, 0, SEEK_END);
if (pos == CAST(off_t, -1)) {
OS_WARN(_T("lseek()"), UID(E6BCC2));
RETURN(NIL);
}
fsize = CAST(size_t, pos);
me->pos = fsize - 1;
me->file = file;
}
}
}
#endif /* PLATFORM_LINUX */
#if PLATFORM_WIN32
{
const DWORD_win desired_access = (modify_ ? GENERIC_WRITE_win : 0) |
GENERIC_READ_win;
const DWORD_win share_mode =
shared_ ? (FILE_SHARE_READ_win | FILE_SHARE_WRITE_win) : 0;
const HANDLE_win file =
CreateFile_win(
filename_, /* fine name */
desired_access, /* desired access */
share_mode, /* share mode */
NULL, /* security attributes */
OPEN_ALWAYS_win, /* creation disposition */
FILE_ATTRIBUTE_NORMAL_win, /* flags and attributes */
NULL); /* template file */
if (file == INVALID_HANDLE_VALUE_win) {
me->is_opened = false;
OS_WARN(_T("CreateFile_win()"), UID(EF94F6));
RETURN(NIL);
}
fsize = GetFileSize_win(file, NULL);
if (fsize == INVALID_FILE_SIZE_win) {
OS_WARN(_T("GetFileSize_win()"), UID(ECC78A));
RETURN(NIL);
}
me->file = file;
}
#endif /* PLATFORM_WIN32 */
me->size = fsize;
me->is_opened = true;
File_goBegin(object_);
RETURN(NIL);
} /* File_openInMode */
/* move file pointer to the specified byte position */
PUBLIC void File_goTo( /*M*/
file_t object_, /*-*/
const size_t new_position_ ) { /*-*/
GO (UID(FE5D09));
_file_t* const me = _object(&object_, UID(EBCB50));
if (new_position_ > (me->size - 1)) {
WARN(_T("new_position_ out of range."), UID(E4CCEB));
RETURN(NIL);
}
#if PLATFORM_LINUX
if (me->pos != new_position_) {
const off_t pos = lseek(me->file, new_position_, SEEK_SET);
if (pos == CAST(off_t, -1)) {
OS_WARN(_T("lseek()"), UID(E57C4B));
RETURN(NIL);
}
}
#endif /* PLATFORM_LINUX */
#if PLATFORM_WIN32
if (me->pos != new_position_) {
const DWORD_win result
= SetFilePointer_win(
me->file, /* file */
0, /* lDistanceToMove */
0, /* lpDistanceToMoveHigh */
FILE_BEGIN_win); /* dwMoveMethod */
if (result == INVALID_SET_FILE_POINTER_win) {
OS_WARN(_T("SetFilePointer_win()")
_T(" == INVALID_SET_FILE_POINTER_win"), UID(EA92ED));
RETURN(NIL);
}
}
#endif /* PLATFORM_WIN32 */
RETURN(NIL);
} /* File_goTo */
PUBLIC void File_goBegin( file_t object_ ) { /*M*/
GO (UID(F1BCCA));
File_goTo(object_, 0);
RETURN(NIL);
} /* File_goBegin */
PUBLIC void File_goEnd( file_t object_ ) { /*M*/
GO (UID(F12C23));
const _file_t* const me = _object_const(&object_, UID(E0ECB8));
File_goTo(object_, me->size);
RETURN(NIL);
} /* File_goEnd */
PUBLIC void File_write( /*M*/
file_t object_, /*-*/
const void* bytes_, /*-*/
const size_t length_in_bytes_ ) { /*-*/
GO (UID(F77F50));
#if PLATFORM_LINUX
UNUSED(object_);
UNUSED(bytes_);
UNUSED(length_in_bytes_);
IMPLEMENT_LINUX(); /* TODO: implement File_write() on Linux */
#endif /* PLATFORM_LINUX */
#if PLATFORM_WIN32
_file_t* const me = _object(&object_, UID(E3619F));
const DWORD_win begun = GetTickCount_win();
const DWORD_win offset_lo = CAST(DWORD_win, me->size);
const DWORD_win offset_hi = 0;
const DWORD_win bytes_lo = offset_lo + CAST(DWORD_win, length_in_bytes_);
const DWORD_win bytes_hi = 0;
BOOL_win locked = FALSE_win;
/* return if the output length is zero-length */
if (length_in_bytes_ < 1) {
RETURN(NIL);
}
/* lock the part of the file being written to */
/* (attempt to acquire lock in 2 seconds) */
while (!locked && GetTickCount_win() < begun + 2000) {
locked = LockFile_win(
me->file, offset_lo, offset_hi, bytes_lo, bytes_hi);
if (!locked) {
Sleep_win(250);
}
}
if (!locked) {
OS_WARN(_T("LockFile_win()"), UID(E22D30));
RETURN(NIL);
}
/* write to the file */
if (locked) {
DWORD_win bytes_writ = 0;
const BOOL_win wfile = WriteFile_win(
me->file, /* file handle */
bytes_, /* lpBuffer */
CAST(DWORD_win, length_in_bytes_),
/* nNumberOfBytesToWrite */
&bytes_writ, /* lpNumberOfBytesWritten */
NULL); /* lpOverlapped */
VERIFY(wfile, UID(E07439));
if (wfile) {
me->pos += bytes_writ;
me->size += bytes_writ;
}
/* unlock the file */
VERIFY(UnlockFile_win(
me->file, offset_lo, offset_hi, bytes_lo, bytes_hi),
UID(ED920A));
}
#endif /* PLATFORM_WIN32 */
RETURN(NIL);
} /* File_write */
PUBLIC void File_writeTextData( /*M*/
file_t object_, /*-*/
chars_t text_ ) { /*-*/
GO (UID(FBC57A));
const size_t len = STRLEN_T(text_);
const size_t bytes_size = len * sizeof(char_t);
File_write(object_, &len, sizeof(len));
File_write(object_, text_, bytes_size);
RETURN(NIL);
} /* File_writeTextData */
PUBLIC void File_writeInt( /*M*/
file_t object_, /*-*/
const int value_ ) { /*-*/
GO (UID(F8D7B4));
File_write(object_, &value_, sizeof(value_));
RETURN(NIL);
} /* File_writeInt */
PUBLIC void File_writeSize( /*M*/
file_t object_, /*-*/
const size_t value_ ) { /*-*/
GO (UID(FB792C));
File_write(object_, &value_, sizeof(value_));
RETURN(NIL);
} /* File_writeSize */
PUBLIC size_t File_read( /*M*/
file_t object_, /*-*/
void* bytes_, /*-*/
const size_t length_in_bytes_ ) { /*-*/
GO (UID(FD075A));
_file_t* const me = _object(&object_, UID(EB7D61));
#if PLATFORM_LINUX
const size_t ret = read(me->file, bytes_, length_in_bytes_);
if (ret == CAST(size_t, -1)) {
OS_WARN(_T("read()"), UID(E88C73));
} else {
me->pos += ret;
}
#endif
#if PLATFORM_WIN32
DWORD_win ret = 0;
if (0 != ReadFile_win(
me->file, bytes_, CAST(DWORD_win, length_in_bytes_), &ret, NULL)) {
me->pos += ret;
} else {
OS_WARN(_T("ReadFile_win()"), UID(E4A738));
}
#endif
RETURN(CAST(size_t, ret)); /* return number of bytes read */
} /* File_read */
PUBLIC new_str_t File_readTextData( file_t object_ ) { /*M*/
GO (UID(FBB1EB));
/* read length of text */
size_t len = 0;
File_read(object_, &len, sizeof(len));
{
/* read text from the file: */
new_str_t ret;
new_chars_t buf = mallocT(len + 1);
File_read(object_, buf, len * sizeof(char_t));
*(buf + len) = '\0';
ret = S_newT(buf);
RETURN(ret);
}
} /* File_readTextData */
PUBLIC int File_readInt( file_t object_ ) { /*M*/
GO (UID(F30D63));
int ret = 0;
File_read(object_, &ret, sizeof(ret));
RETURN(ret);
} /* File_readInt */
PUBLIC size_t File_readSize( file_t object_ ) { /*M*/
GO (UID(F039B0));
size_t ret = 0;
File_read(object_, &ret, sizeof(ret));
RETURN(ret);
} /* File_readSize */
/* close the opened file */
PUBLIC void File_close( file_t object_ ) { /*M*/
GO (UID(FC36B5));
_file_t* const me = _object(&object_, UID(EDD6AB));
#if PLATFORM_WIN32
if (me->is_opened) {
if (me->modify) {
VERIFY(FlushFileBuffers_win(me->file), UID(E906A2));
}
VERIFY(CloseHandle_win(me->file), UID(E20B6B));
me->file = 0;
me->is_opened = false;
}
#endif /* PLATFORM_WIN32 */
RETURN(NIL);
} /* File_close */
/* -------------------------------------------------------------------------- */
/* Functions: File Information */
PUBLIC new_str_t file_path( chars_t filename_ ) { /*F*/
GO (UID(F0F9BA));
const new_str_t ret = path_extract(filename_, false);
RETURN(ret);
} /* file_path */
/* if a path was not specified, adds the current directory to a file name */
PUBLIC new_str_t file_name_full( /*F*/
chars_t filename_, /*-*/
chars_t default_path_ ) { /*-*/
GO (UID(FA7F0E));
new_str_t ret = { NULL };
#if PLATFORM_LINUX
UNUSED(filename_);
UNUSED(default_path_);
IMPLEMENT_LINUX(); /* TODO: implement file_name_full() on Linux */
#endif /* PLATFORM_LINUX */
#if PLATFORM_WIN32
char_t buf[MAX_PATH_win];
new_str_t default_path = { NULL };
new_str_t path = { NULL };
/* if trimmed filename_ is blank, return a blank */
if (is_whitespaceT(filename_)) {
ret = S_blank();
RETURN(ret);
}
path = file_path(filename_);
if (!emptyS(path)) {
MEMSET(buf, 0x00, sizeof(buf));
if (0 != GetFullPathName_win(path.cs, MAX_PATH_win, buf, NULL)) {
freeS(&path);
path = S_chars(buf);
}
if (!path_exists(path.cs, true, false)) {
freeS(&path);
}
}
default_path = file_path(default_path_);
if (!emptyS(default_path)) {
MEMSET(buf, 0x00, sizeof(buf));
if (0 != GetFullPathName_win(
default_path.cs, MAX_PATH_win, buf, NULL)) {
freeS(&default_path);
default_path = S_chars(buf);
}
if (!path_exists(default_path.cs, true, false)) {
freeS(&default_path);
}
}
/* if both the file name's path and default path are unspecified */
/* or don't exist, then use the current directory as the path */
if (emptyS(path) && emptyS(default_path)) {
DWORD_win result = 0;
MEMSET(buf, 0x00, sizeof(buf));
result = GetCurrentDirectory_win(MAX_PATH_win, buf);
if (!result) {
OS_ERR(_T("GetCurrentDirectory_win"), UID(E4A74D));
} else {
freeS(&path);
path = S_chars(buf);
result = GetFullPathName_win(path.cs, MAX_PATH_win, buf, NULL);
if (!result) {
OS_ERR(_T("GetFullPathName_win"), UID(E76BDD));
} else {
freeS(&path);
path = S_chars(buf);
}
}
}
if (emptyS(path) && !emptyS(default_path)) {
freeS(&path);
path = S_copy(default_path);
}
{
new_str_t filename = file_name(filename_);
const size_t path_len = lengthS(path);
const size_t len = path_len + 1 + lengthS(filename);
ret = S_reserve(len);
addTS(&ret, path.cs);
if (path.cs[path_len - 1] != '\\') {
addTS(&ret, _T("\\"));
}
addTS(&ret, filename.cs);
freeS(&default_path);
freeS(&filename);
}
#endif /* PLATFORM_WIN32 */
RETURN(ret);
} /* file_name_full */
PUBLIC new_str_t file_locate( /*F*/
chars_t filename_, /*-*/
chars_t alternate_paths_ ) { /*-*/
GO (UID(FEFF20));
new_str_t ret = S_chars(filename_);
bool exists = file_exists(ret.cs);
if (!exists) {
new_str_t paths = S_chars(alternate_paths_);
trimIn1S(&paths);
if (!emptyS(paths)) {
new_str_t name = file_name(ret.cs);
const int max = tokenCountS(paths, _T(";"));
int i = 0;
for (i = 0; i < max; i++) {
new_str_t path = S_getToken(paths, i, _T(";"));
trimIn1S(&path);
trimInS(&path, _T("\\"), TRIM_END);
/* TODO: use simpler, one-line syntax here, when available */
setInS(&ret, path.cs);
addTS(&ret, _T("\\"));
addTS(&ret, name.cs);
freeS(&path);
if (file_exists(ret.cs)) {
exists = true;
break;
}
}
freeS(&name);
}
freeS(&paths);
}
if (!exists) {
ret = S_blank();
}
RETURN(ret);
} /* file_locate */
PUBLIC new_str_t file_name( chars_t filename_ ) { /*F*/
GO (UID(FFBF51));
new_str_t ret = S_blank();
/* find position of the rightmost slash or backslash */
size_t loc = find_last_T(filename_, _T("\\"));
{
const size_t loc_slash = find_last_T(filename_, _T("/"));
if (loc_slash > loc) {
loc = loc_slash;
}
}
/* if a (back)slash is found, return the text after the (back)slash */
/* (otherwise return the same file name) */
if (loc == NONE) {
ret = S_chars(filename_);
} else {
ret = S_chars(filename_ + loc + 1);
}
RETURN(ret);
} /* file_name */
PUBLIC size_t file_size( chars_t filename_ ) { /*F*/
GO (UID(FF4ED5));
size_t ret = 0;
#if PLATFORM_LINUX
UNUSED(filename_);
IMPLEMENT_LINUX(); /* TODO: implement file_size() on Linux */
#endif /* PLATFORM_LINUX */
#if PLATFORM_WIN32
/* prepare data structure */
WIN32_FIND_DATA_win fdata;
CLEAR(fdata);
_CHECK_HAS_NAME(filename_, RETURN(0); , UID(E9ED5A));
{
/* get file information: */
const HANDLE_win handle = FindFirstFile_win(filename_, &fdata);
if (handle == INVALID_HANDLE_VALUE_win) {
new_chars_t msg = formatT(_T("FindFirstFile_win('%s')")
_T(" == INVALID_HANDLE_VALUE_win"),
filename_);
OS_WARN(msg, UID(E1483D));
freeT(&msg);
RETURN(0);
}
if (!FindClose_win(handle)) {
OS_WARN(_T("FindClose_win()"), UID(E84A96));
RETURN(0);
}
}
if (fdata.nFileSizeHigh > 0) {
RETURN(SIZE_MAX);
}
ret = fdata.nFileSizeLow;
#endif /* PLATFORM_WIN32 */
RETURN(ret);
} /* file_size */
PUBLIC new_str_t file_size_description( /*F*/
file_size_t size_, /*-*/
const bool format_remainder_ ) { /*-*/
GO (UID(FA7113));
file_size_t i = 0;
file_size_t factor = 0;
file_size_t remainder = 0;
chars_t suffix = NULL;
new_str_t ret = S_reserve(64);
if (size_ >= GB(1)) {
factor = GB(1);
suffix = _T("gb");
} else if (size_ >= MB(1)) {
factor = MB(1);
suffix = _T("mb");
} else if (size_ >= KB(1)) {
factor = KB(1);
suffix = _T("kb");
} else {
factor = 1;
if (size_ == 1) {
suffix = _T(" byte");
} else {
suffix = _T(" bytes");
}
}
if (factor < KB(1)) {
i = size_;
remainder = 0;
} else {
i = size_ / factor;
remainder = size_ % factor;
}
{
new_chars_t fsize = T_int(CAST(int, i));
addTS(&ret, fsize);
addTS(&ret, suffix);
if (format_remainder_ && remainder > 0) {
new_str_t str = file_size_description(remainder, false);
addTS(&ret, _T(" + "));
addTS(&ret, str.cs);
freeS(&str);
}
RETURN(ret);
}
} /* file_size_description */
PUBLIC bool file_type_is_text( chars_t filename_ ) { /*F*/
GO (UID(F55136));
chars_t _TEXT_FILE_TYPES[] = {
/* Miscellaneous Files */
_T(".bat"),
_T(".reg"),
_T(".sql"),
_T(".txt"),
/* Web Files */
_T(".asp"),
_T(".aspx"),
_T(".css"),
_T(".ctp"),
_T(".hta"),
_T(".htm"),
_T(".html"),
_T(".java"),
_T(".js"),
_T(".php"),
_T(".shtml"),
_T(".xhtml"),
_T(".ctp"),
/* Android Files */
_T(".classpath"),
_T(".java"),
_T(".prefs"),
_T(".project"),
_T(".properties"),
/* Markup Files */
_T(".svg"),
_T(".xaml"),
_T(".xml"),
/* C++ */
_T(".c"),
_T(".cbp"),
_T(".clw"),
_T(".cpp"),
_T(".def"),
_T(".dsp"),
_T(".dsw"),
_T(".fd"),
_T(".h"),
_T(".hc"),
_T(".hpp"),
_T(".layout"),
_T(".manifest"),
_T(".odl"),
_T(".plg"),
_T(".rc"),
_T(".rc2"),
_T(".sln"),
_T(".vcproj"),
_T(".vcxproj"),
_T(".workspace"),
/* C# */
_T(".cs"),
_T(".csproj"),
_T(".resx"),
_T(".sln"),
_T(".user"),
_T(".xsd"),
_T(".xsx"),
/* Visual Basic */
_T(".bas"),
_T(".cls"),
_T(".ctl"),
_T(".dep"),
_T(".frm"),
_T(".log"),
_T(".pdm"),
_T(".vbp"),
_T(".vbw"),
NULL
};
{
uint32_t type = 0;
while (_TEXT_FILE_TYPES[type++] != NULL) {
chars_t str = _TEXT_FILE_TYPES[type];
if (ends3T(filename_, str, IGNORE_CASE)) {
RETURN(true);
}
}
}
RETURN(false);
} /* file_type_is_text */
/* -------------------------------------------------------------------------- */
/* Functions: File Manipulation */
PUBLIC bool file_move( /*F*/
chars_t source_filename_, /*-*/
chars_t target_filename_, /*-*/
const bool overwrite_ ) { /*-*/
GO (UID(F7B3F2));
bool ret = false;
if (file_exists(target_filename_)) {
if (overwrite_) {
file_delete(target_filename_);
} else {
RETURN(false);
}
}
#if PLATFORM_LINUX
UNUSED(source_filename_);
UNUSED(target_filename_);
UNUSED(overwrite_);
IMPLEMENT_LINUX(); /* TODO: implement file_move() on Linux */
#endif /* PLATFORM_LINUX */
#if PLATFORM_WIN32
if (0 != MoveFile_win(source_filename_, target_filename_)) {
ret = true;
} else {
new_chars_t msg = formatT(_T("MoveFile_win('%s', '%s')"),
source_filename_, target_filename_);
OS_WARN(msg, UID(E80308));
freeT(&msg);
ret = false;
}
#endif /* PLATFORM_WIN32 */
RETURN(ret);
} /* file_move */