-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles.h
788 lines (685 loc) · 25.7 KB
/
files.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
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
/*
* Released into the public domain by Tré Dudman - 2024
* For licensing and more info see https://github.com/Tremus/xhl
*
* stdio.h FILE* replacement for Windows & macOS
* Contains a few extra nice to have features not offered by libc
* All strings are assumed to be NULL terminated with UTF8 encoding
*
* WHY NOT JUST USE LIBC?
* Windows C/C++ runtime can break when using Cyrillic characters for example in your file paths.
* Presumably Microsofts implementation of libc interprets strings as ANSI rather than UTF8, which is a bummer for
* anyone using non-latin characters in their Windows username.
*
* This problem alone was enough of a reason to create this library. Under the hood, your UTF8 paths are converted to
* UTF16 on Windows which eliminates the above problem. Enjoy!
*
* LIMITATIONS:
* Windows support file paths with names up to 32,767 wide characters. This library lazily supports a maximum of
* MAX_PATH (260) wide characters using stack memory to avoid using malloc or similar. If you need longer paths, you
* will have to add this yourself!
* https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#maximum-path-length-limitation
*
* BUILDING:
* Simply #define XHL_FILES_IMPL in one of your build targets before including this header
* MacOS: Requires Objective-C/Objective-C++ for some functions. Supports both ARC and no ARC
*
* LINKING:
* Windows: Kernel32 Shell32 Shlwapi
* macOS: -framework AppKit
*
* CREDITS:
* Randy Gaul. 'xfiles_list' is a modified version of 'cf_scan' from the cute_files library
* https://github.com/RandyGaul/cute_headers_deprecated/blob/master/cute_files.h
*/
#ifndef XHL_FILES_H
#define XHL_FILES_H
#include <stdbool.h>
#include <stddef.h>
#ifdef _WIN32
#define XFILES_DIR_STR "\\"
#define XFILES_DIR_CHAR '\\'
#define XFILES_BROWSER_NAME "File Explorer"
#define XFILES_TRASH_NAME "Recycle Bin"
#else
#define XFILES_DIR_STR "/"
#define XFILES_DIR_CHAR '/'
#define XFILES_BROWSER_NAME "Finder"
#define XFILES_TRASH_NAME "Trash"
#endif
#define XFILES_ARRLEN(arr) (sizeof(arr) / sizeof(arr[0]))
#if !defined(XFILES_MALLOC) || !defined(XFILES_FREE)
#include <stdlib.h>
#define XFILES_MALLOC(size) malloc(size)
#define XFILES_FREE(ptr) free(ptr)
#endif
#ifndef XFILES_ASSERT
#ifdef NDEBUG
// clang-format off
#define XFILES_ASSERT(cond) do { (void)(cond); } while (0)
// clang-format on
#else
#ifdef _WIN32
#define XFILES_ASSERT(cond) (cond) ? (void)0 : __debugbreak()
#else // #if __APPLE__
#define XFILES_ASSERT(cond) (cond) ? (void)0 : __builtin_debugtrap()
#endif // _WIN32
#endif // NDEBUG
#endif // XFILES_ASSERT
#ifdef __cplusplus
extern "C" {
#endif
// On success, returns file or directory name, else returns NULL
const char* xfiles_get_name(const char* path);
// On success, returns file extension, else returns NULL
const char* xfiles_get_extension(const char* name);
// Returns true if directory or file exists
bool xfiles_exists(const char* path);
// Returns true if directory was created
bool xfiles_create_directory(const char* path);
// Creates directory and any required parent directories, then returns true if the exists or was craeted.
bool xfiles_create_directory_recursive(const char* path);
// Returns only true on success and sets 'out' and 'outlen' with file contents and size
// Must release 'out' with free()
bool xfiles_read(const char* path, void** out, size_t* outlen);
// Creates file if it doesn't exist with default access permissions.
// If file already exists, it overwrites all contents.
bool xfiles_write(const char* path, const void* in, size_t inlen);
// Creates file if it doesn't exist with default access permissions.
// Writes data starting from the end of a file, leaving old contents intact
bool xfiles_append(const char* path, const char* in, size_t inlen);
// Renames / moves file or folder
// You are advised to check for path collisions using xfiles_exists() beforehand
bool xfiles_move(const char* from, const char* to);
// Moves the file to:
// Win: Recycle Bin /
// OSX: Trash
bool xfiles_trash(const char* path);
// No bins. File is deleted and is likely unrecoverable
bool xfiles_delete(const char* path);
// Opens OS file or folder as if it was opened with
// Win: File Explorer /
// OSX: Finder
bool xfiles_open_file_explorer(const char* path);
// Opens OS file browsing app with path selected
// Win: File Explorer /
// OSX: Finder
bool xfiles_select_in_file_explorer(const char* path);
enum XFILES_USER_DIRECTORY
{
// Windows: {letter}:\\Users\\{username}
// macOS: /Users/{username}
XFILES_USER_DIRECTORY_HOME,
// Windows: [HOME]\\AppData\Roaming
// macOS: [HOME]/Library/Application\ Support
XFILES_USER_DIRECTORY_APPDATA,
XFILES_USER_DIRECTORY_DESKTOP,
XFILES_USER_DIRECTORY_DOCUMENTS,
XFILES_USER_DIRECTORY_DOWNLOADS,
XFILES_USER_DIRECTORY_MUSIC,
XFILES_USER_DIRECTORY_PICTURES,
XFILES_USER_DIRECTORY_VIDEOS,
XFILES_USER_DIRECTORY_COUNT,
};
bool xfiles_get_user_directory(char* out, size_t outlen, enum XFILES_USER_DIRECTORY loc);
typedef struct xfiles_list_item_t
{
char path[1024];
unsigned path_len;
unsigned name_idx;
unsigned ext_idx;
bool is_dir;
} xfiles_list_item_t;
typedef void(xfiles_list_callback_t)(void* data, const xfiles_list_item_t* item);
// Calls the above callback for each item in a directory
void xfiles_list(const char* path, void* data, xfiles_list_callback_t* cb);
#ifdef __cplusplus
}
#endif
#ifdef XHL_FILES_IMPL
#ifdef _WIN32
#include <Windows.h>
#include <Shlobj.h>
#include <Shlwapi.h>
#include <shellapi.h>
#include <stdio.h>
#pragma comment(lib, "Shlwapi.lib")
bool xfiles_exists(const char* path)
{
// https://learn.microsoft.com/en-us/windows/win32/api/shlwapi/nf-shlwapi-pathfileexistsw
// https://learn.microsoft.com/en-us/windows/win32/api/stringapiset/nf-stringapiset-multibytetowidechar
WCHAR pathunicode[MAX_PATH];
int num = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, path, -1, pathunicode, XFILES_ARRLEN(pathunicode));
XFILES_ASSERT(num);
if (num)
return PathFileExistsW(pathunicode);
return false;
}
bool xfiles_create_directory(const char* path)
{
// https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createdirectoryw
WCHAR DirPath[MAX_PATH];
int num = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, path, -1, DirPath, XFILES_ARRLEN(DirPath));
XFILES_ASSERT(num);
if (num)
{
BOOL ok = CreateDirectoryW(DirPath, 0);
XFILES_ASSERT(ok);
return ok;
}
return false;
}
bool xfiles_read(const char* path, void** out, size_t* outlen)
{
// https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilew
// https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfilesizeex
// https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-readfile
void* data = NULL;
HANDLE hFile = NULL;
LARGE_INTEGER FileSize = {0};
BOOL ok = FALSE;
WCHAR FileName[MAX_PATH];
int num = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, path, -1, FileName, XFILES_ARRLEN(FileName));
XFILES_ASSERT(num);
if (num)
{
hFile = CreateFileW(
FileName,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
NULL);
XFILES_ASSERT(hFile != INVALID_HANDLE_VALUE);
if (hFile != INVALID_HANDLE_VALUE)
{
ok = GetFileSizeEx(hFile, &FileSize);
XFILES_ASSERT(ok);
if (ok)
{
data = XFILES_MALLOC(FileSize.QuadPart);
ok = ReadFile(hFile, data, FileSize.QuadPart, NULL, NULL);
XFILES_ASSERT(ok);
if (ok)
{
*out = data;
*outlen = FileSize.QuadPart;
}
else
{
XFILES_FREE(data);
data = NULL;
FileSize.QuadPart = 0;
}
}
ok = ok && CloseHandle(hFile);
XFILES_ASSERT(ok);
}
}
return ok;
}
bool xfiles_write(const char* path, const void* in, size_t inlen)
{
// https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-writefile
WCHAR FilePath[MAX_PATH];
HANDLE hFile = NULL;
BOOL ok = FALSE;
DWORD nBytesWritten = 0;
int num = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, path, -1, FilePath, XFILES_ARRLEN(FilePath));
XFILES_ASSERT(num);
if (num)
{
hFile = CreateFileW(
FilePath,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
NULL);
XFILES_ASSERT(hFile != INVALID_HANDLE_VALUE);
if (hFile != INVALID_HANDLE_VALUE)
{
ok = WriteFile(hFile, in, inlen, &nBytesWritten, NULL);
XFILES_ASSERT(ok);
ok = ok && CloseHandle(hFile);
XFILES_ASSERT(ok);
}
}
return ok;
}
bool xfiles_append(const char* path, const char* in, size_t inlen)
{
WCHAR FilePath[MAX_PATH];
HANDLE hFile = NULL;
BOOL ok = FALSE;
DWORD nBytesWritten = 0;
int num = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, path, -1, FilePath, XFILES_ARRLEN(FilePath));
XFILES_ASSERT(num);
if (num)
{
hFile =
CreateFileW(FilePath, FILE_APPEND_DATA, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
XFILES_ASSERT(hFile != INVALID_HANDLE_VALUE);
if (hFile != INVALID_HANDLE_VALUE)
{
ok = WriteFile(hFile, in, inlen, &nBytesWritten, NULL);
XFILES_ASSERT(ok);
ok = ok && CloseHandle(hFile);
XFILES_ASSERT(ok);
}
}
return ok;
}
bool xfiles_move(const char* from, const char* to)
{
// https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-movefilew
WCHAR FromPath[MAX_PATH];
WCHAR ToPath[MAX_PATH];
int num1 = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, from, -1, FromPath, XFILES_ARRLEN(FromPath));
int num2 = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, to, -1, ToPath, XFILES_ARRLEN(ToPath));
if (num1 && num2)
return MoveFileW(FromPath, ToPath);
return false;
}
bool xfiles_trash(const char* path)
{
// https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shfileoperationw
// https://learn.microsoft.com/en-us/windows/win32/api/shellapi/ns-shellapi-shfileopstructw
WCHAR PathList[MAX_PATH + 8] = {0};
int num = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, path, -1, PathList, XFILES_ARRLEN(PathList));
XFILES_ASSERT(num);
if (num)
{
SHFILEOPSTRUCTW FileOp = {0};
FileOp.wFunc = FO_DELETE;
FileOp.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI | FOF_NORECURSION |
FOF_RENAMEONCOLLISION | FOF_SILENT;
FileOp.pFrom = PathList;
int ret = SHFileOperationW(&FileOp);
XFILES_ASSERT(ret == 0);
return ret == 0;
}
return false;
}
bool xfiles_delete(const char* path)
{
// https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-deletefilew
WCHAR FilePath[MAX_PATH];
int num = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, path, -1, FilePath, XFILES_ARRLEN(FilePath));
XFILES_ASSERT(num);
if (num)
{
BOOL ok = DeleteFileW(FilePath);
XFILES_ASSERT(ok);
return ok;
}
return false;
}
bool xfiles_open_file_explorer(const char* path)
{
// https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellexecutew
WCHAR FilePath[MAX_PATH];
int num = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, path, -1, FilePath, XFILES_ARRLEN(FilePath));
XFILES_ASSERT(num);
if (num)
{
INT_PTR ret = (INT_PTR)ShellExecuteW(NULL, L"open", FilePath, NULL, NULL, SW_SHOWDEFAULT);
XFILES_ASSERT(ret > 32);
return ret > 32;
// https://learn.microsoft.com/en-us/windows/win32/api/shlobj_core/nf-shlobj_core-shopenfolderandselectitems
LPITEMIDLIST pItemList = ILCreateFromPathW(FilePath);
if (pItemList)
{
SHOpenFolderAndSelectItems(pItemList, 0, 0, 0);
ILFree(pItemList);
}
}
return false;
}
bool xfiles_select_in_file_explorer(const char* path)
{
// https://learn.microsoft.com/en-us/windows/win32/api/shlobj_core/nf-shlobj_core-shopenfolderandselectitems
// https://learn.microsoft.com/en-us/windows/win32/api/shlobj_core/nf-shlobj_core-ilcreatefrompathw
WCHAR FilePath[MAX_PATH];
int num = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, path, -1, FilePath, XFILES_ARRLEN(FilePath));
XFILES_ASSERT(num);
if (num)
{
LPITEMIDLIST pItemList = ILCreateFromPathW(FilePath);
if (pItemList)
{
CoInitialize(NULL);
HRESULT hres = SHOpenFolderAndSelectItems(pItemList, 0, 0, 0);
XFILES_ASSERT(hres == S_OK);
ILFree(pItemList);
CoUninitialize();
return hres = S_OK;
}
}
return false;
}
bool xfiles_get_user_directory(char* out, size_t outlen, enum XFILES_USER_DIRECTORY loc)
{
// https://learn.microsoft.com/en-us/windows/win32/api/shlobj_core/nf-shlobj_core-shgetknownfolderpath
// https://learn.microsoft.com/en-us/windows/win32/shell/knownfolderid
static const KNOWNFOLDERID* FOLDER_IDS[] = {
&FOLDERID_Profile, // XFILES_USER_DIRECTORY_HOME
&FOLDERID_RoamingAppData, // XFILES_USER_DIRECTORY_APPDATA
&FOLDERID_Desktop, // XFILES_USER_DIRECTORY_DESKTOP
&FOLDERID_Documents, // XFILES_USER_DIRECTORY_DOCUMENTS
&FOLDERID_Downloads, // XFILES_USER_DIRECTORY_DOWNLOADS
&FOLDERID_Music, // XFILES_USER_DIRECTORY_MUSIC
&FOLDERID_Pictures, // XFILES_USER_DIRECTORY_PICTURES
&FOLDERID_Videos, // XFILES_USER_DIRECTORY_VIDEOS
};
_Static_assert(XFILES_ARRLEN(FOLDER_IDS) == XFILES_USER_DIRECTORY_COUNT, "");
#ifdef __cplusplus
#define XFILES_REF(ptr) *ptr
#else
#define XFILES_REF(ptr) ptr
#endif
PWSTR Path = NULL;
if (loc < 0)
loc = (enum XFILES_USER_DIRECTORY)0;
if (loc >= XFILES_USER_DIRECTORY_COUNT)
loc = (enum XFILES_USER_DIRECTORY)(XFILES_USER_DIRECTORY_COUNT - 1);
if (S_OK == SHGetKnownFolderPath(XFILES_REF(FOLDER_IDS[loc]), 0, NULL, &Path))
{
int num = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, Path, -1, out, outlen, NULL, NULL);
XFILES_ASSERT(num);
CoTaskMemFree(Path);
return num != 0;
}
return false;
}
void xfiles_list(const char* path, void* data, xfiles_list_callback_t* callback)
{
// https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-findfirstfilew
// https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-findnextfilew
// https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-findclose
HANDLE hFind = INVALID_HANDLE_VALUE;
WIN32_FIND_DATAW FindData;
xfiles_list_item_t Item;
bool HasNext = 0;
{
WCHAR PathUnicode[MAX_PATH] = {0};
int n = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, path, -1, PathUnicode, XFILES_ARRLEN(PathUnicode));
if (n)
{
PathUnicode[n] = 0;
wcsncat(PathUnicode, L"\\*", XFILES_ARRLEN(PathUnicode) - n - 1);
hFind = FindFirstFileW(PathUnicode, &FindData);
}
}
HasNext = hFind != INVALID_HANDLE_VALUE;
XFILES_ASSERT(hFind != INVALID_HANDLE_VALUE);
Item.name_idx = snprintf(Item.path, sizeof(Item.path), "%s\\", path);
while (HasNext)
{
int NameLen = WideCharToMultiByte(
CP_UTF8,
WC_ERR_INVALID_CHARS,
FindData.cFileName,
-1,
Item.path + Item.name_idx,
sizeof(Item.path) - Item.name_idx,
NULL,
NULL);
XFILES_ASSERT(NameLen);
if (NameLen == 0) // Failed conversion
goto iterate;
Item.path_len = Item.name_idx + NameLen - 1;
Item.ext_idx = Item.name_idx;
for (unsigned i = Item.name_idx; i < Item.path_len; i++)
if (Item.path[i] == '.')
Item.ext_idx = i;
if (Item.ext_idx == Item.name_idx) // Failed to find extension
Item.ext_idx = Item.path_len;
XFILES_ASSERT(strlen(Item.path) == Item.path_len);
Item.is_dir = FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
callback(data, &Item);
iterate:
HasNext = FindNextFileW(hFind, &FindData);
}
if (hFind != INVALID_HANDLE_VALUE)
FindClose(hFind);
}
#endif // _WIN32
#ifdef __APPLE__
#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
// https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/access.2.html
bool xfiles_exists(const char* path) { return access(path, F_OK) == 0; }
// https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/mkdir.2.html
bool xfiles_create_directory(const char* path) { return mkdir(path, 0777) == 0; }
bool xfiles_read(const char* path, void** out, size_t* outlen)
{
// https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/open.2.html
// https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/fstat.2.html
// https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/read.2.html
// https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/close.2.html
int fd = -1;
int ret = -1;
struct stat info = {0};
void* data = NULL;
ssize_t nread = -1;
fd = open(path, O_RDONLY);
XFILES_ASSERT(fd != -1);
if (fd != -1)
{
ret = fstat(fd, &info);
XFILES_ASSERT(ret != -1);
if (ret != -1)
{
data = XFILES_MALLOC(info.st_size);
nread = read(fd, data, info.st_size);
XFILES_ASSERT(nread != -1);
if (nread == -1)
{
XFILES_FREE(data);
data = NULL;
}
else
{
*out = data;
*outlen = info.st_size;
}
}
close(fd);
}
return nread != -1;
}
bool xfiles_write(const char* path, const void* in, size_t inlen)
{
// https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/write.2.html#//apple_ref/doc/man/2/write
int fd;
ssize_t nwritten = -1;
fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0777);
XFILES_ASSERT(fd != -1);
if (fd != -1)
{
nwritten = write(fd, in, inlen);
XFILES_ASSERT(nwritten != -1);
close(fd);
}
return nwritten != -1;
}
bool xfiles_append(const char* path, const char* in, size_t inlen)
{
int fd;
ssize_t nwritten = -1;
fd = open(path, O_WRONLY | O_CREAT | O_APPEND, 0777);
XFILES_ASSERT(fd != -1);
if (fd != -1)
{
nwritten = write(fd, in, inlen);
XFILES_ASSERT(nwritten != -1);
close(fd);
}
return nwritten != -1;
}
// https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/rename.2.html
bool xfiles_move(const char* from, const char* to) { return 0 == rename(from, to); }
// https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/unlink.2.html
bool xfiles_delete(const char* path) { return unlink(path) == 0; }
void xfiles_list(const char* path, void* data, xfiles_list_callback_t* callback)
{
// https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/opendir.3.html
// https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/readdir.3.html
// https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/closedir.3.html
DIR* dir = NULL;
struct dirent* entry = NULL;
xfiles_list_item_t item;
dir = opendir(path);
if (dir)
entry = readdir(dir);
item.name_idx = snprintf(item.path, sizeof(item.path), "%s/", path);
while (entry)
{
item.path_len = item.name_idx + entry->d_namlen;
XFILES_ASSERT(item.path_len < sizeof(item.path));
if (item.path_len >= sizeof(item.path)) // Guard overflow
goto iterate;
memcpy(&item.path[item.name_idx], entry->d_name, entry->d_namlen);
item.path[item.path_len] = 0;
item.ext_idx = item.name_idx;
for (uint32_t i = item.name_idx; i < item.path_len; i++)
if (item.path[i] == '.')
item.ext_idx = i;
if (item.ext_idx == item.name_idx) // Failed to find extension
item.ext_idx = item.path_len;
item.is_dir = DT_DIR & entry->d_type;
XFILES_ASSERT(strlen(item.path) == item.path_len);
callback(data, &item);
iterate:
// readdir returns NULL if no more files, breaking the loop
entry = readdir(dir);
}
if (dir)
closedir(dir);
}
#ifdef __OBJC__
#import <AppKit/NSWorkspace.h>
bool xfiles_trash(const char* path)
{
NSString* str = [[NSString alloc] initWithUTF8String:path];
NSURL* itemUrl = [NSURL fileURLWithPath:str isDirectory:FALSE];
const NSFileManager* fm = [NSFileManager defaultManager]; // strong
NSURL* resultingURL = NULL;
NSError* error = NULL;
BOOL ok = NO;
// Apparently a long standing bug in macOS is that this function will not give you the give 'Put Back' feature
// inside Trash that you would normally see if you deleted the file using the Finder app. Such a shame!
// https://openradar.appspot.com/radar?id=5063396789583872
ok = [fm trashItemAtURL:itemUrl resultingItemURL:&resultingURL error:&error];
#if !__has_feature(objc_arc)
[fm release];
[itemUrl release];
[str release];
#endif
return ok;
}
bool xfiles_open_file_explorer(const char* path)
{
BOOL ok = [[NSWorkspace sharedWorkspace] openFile:@(path) withApplication:@"Finder"];
XFILES_ASSERT(ok);
return ok;
}
bool xfiles_select_in_file_explorer(const char* path)
{
// https://developer.apple.com/documentation/appkit/nsworkspace/1524399-selectfile
[[NSWorkspace sharedWorkspace] selectFile:@(path) inFileViewerRootedAtPath:@("")];
return true;
}
bool xfiles_get_user_directory(char* out, size_t outlen, enum XFILES_USER_DIRECTORY loc)
{
static const char* PATHS[] = {
"", // XFILES_USER_DIRECTORY_HOME,
"/Library/Application Support", // XFILES_USER_DIRECTORY_APPDATA
"/Desktop", // XFILES_USER_DIRECTORY_DESKTOP
"/Documents", // XFILES_USER_DIRECTORY_DOCUMENTS
"/Downloads", // XFILES_USER_DIRECTORY_DOWNLOADS
"/Music", // XFILES_USER_DIRECTORY_MUSIC
"/Pictures", // XFILES_USER_DIRECTORY_PICTURES
"/Movies", // XFILES_USER_DIRECTORY_VIDEOS
};
_Static_assert(XFILES_ARRLEN(PATHS) == XFILES_USER_DIRECTORY_COUNT);
// I don't think calling this function touches the reference count, but then I haven't looked at the binary
// https://developer.apple.com/documentation/foundation/1413045-nshomedirectory
NSString* nsstr = NSHomeDirectory();
const char* homebuf = [nsstr UTF8String];
size_t homelen = strlen(homebuf);
const char* subdir;
size_t subdirlen;
if (loc < 0)
loc = 0;
if (loc >= XFILES_USER_DIRECTORY_COUNT)
loc = XFILES_USER_DIRECTORY_COUNT - 1;
subdir = PATHS[loc];
subdirlen = strlen(subdir);
XFILES_ASSERT((homelen + subdirlen + 1) <= outlen);
if ((homelen + subdirlen + 1) > outlen)
return false;
memcpy(out, homebuf, homelen);
memcpy(out + homelen, subdir, subdirlen);
out[homelen + subdirlen] = '\0';
return true;
}
#endif // __OBJC__
#endif // __APPLE__
const char* xfiles_get_name(const char* path)
{
const char* name = path;
for (const char* c = path; *c != '\0'; c++)
if (*c == XFILES_DIR_CHAR)
name = c + 1;
return name == path ? NULL : name;
}
const char* xfiles_get_extension(const char* name)
{
const char* extension = name;
for (const char* c = name; *c != '\0'; c++)
if (*c == '.')
extension = c;
return extension == name ? NULL : extension;
}
bool xfiles_create_directory_recursive(const char* path)
{
if (xfiles_exists(path))
return true;
char nextpath[1024];
int i;
nextpath[0] = path[0];
#ifdef _WIN32
// eg: "C:\\Users\\username", start at "U"
nextpath[1] = path[1];
nextpath[2] = path[2];
i = 3;
#else
// eg "/Users/username", start at "U"
i = 1;
#endif
for (; path[i] != '\0'; i++)
{
nextpath[i] = path[i];
if (nextpath[i] == XFILES_DIR_CHAR)
{
nextpath[i] = '\0';
if (!xfiles_exists(nextpath))
xfiles_create_directory(nextpath);
XFILES_ASSERT(xfiles_exists(nextpath));
nextpath[i] = XFILES_DIR_CHAR;
}
}
bool ok = xfiles_create_directory(path);
XFILES_ASSERT(ok);
return ok;
}
#endif // XHL_FILES_IMPL
#endif // XHL_FILES_H