-
Notifications
You must be signed in to change notification settings - Fork 2
/
example.c
115 lines (105 loc) · 3.48 KB
/
example.c
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
#include "cpath.h"
#include <stdio.h>
/*
Output:
[D] .
[D] ..
[F] cpath.h
[D] a.out.dSYM
[D] .
[D] ..
[D] Contents
[D] .
[D] ..
[D] Resources
[D] .
[D] ..
[D] DWARF
[D] .
[D] ..
[F] a.out
[F] Info.plist
[F] example.c
[F] a.out
*/
void recursive_visit(cpath_dir *dir, int tab) {
cpath_file file;
while (cpathGetNextFile(dir, &file)) {
for (int i = 0; i < tab; i++) putchar('\t');
CPathByteRep flags = BYTE_REP_JEDEC | BYTE_REP_BYTE_WORD;
printf("[%c] %s %.1lf %s\n", (file.isDir ? 'D' : 'F'), file.name,
cpathGetFileSizeDec(&file, 1024), cpathGetFileSizeSuffix(&file, flags));
if (file.isDir && !cpathFileIsSpecialHardLink(&file)) {
cpath_dir tmp;
cpathFileToDir(&tmp, &file);
recursive_visit(&tmp, tab + 1);
cpathCloseDir(&tmp);
}
}
}
void emplace(cpath_dir *dir) {
/*
This method uses just one object and a few saved directories to iterate
This is safe from stackoverflow but will require dynamic allocation
*/
cpath_file file;
int tab = 0;
while (dir != NULL) {
while (cpathGetNextFile(dir, &file)) {
for (int i = 0; i < tab; i++) putchar('\t');
CPathByteRep flags = BYTE_REP_JEDEC | BYTE_REP_BYTE_WORD;
printf("[%c] %s %.1lf %s\n", (file.isDir ? 'D' : 'F'), file.name,
cpathGetFileSizeDec(&file, 1024), cpathGetFileSizeSuffix(&file, flags));
if (file.isDir && !cpathFileIsSpecialHardLink(&file)) {
cpathOpenSubFileEmplace(dir, &file, 1);
tab++;
}
}
cpathRevertEmplace(&dir);
tab--;
}
}
void for_file(cpath_file *file, cpath_dir *parent, int tab, void *_) {
for (int i = 0; i < tab; i++) putchar('\t');
CPathByteRep flags = BYTE_REP_JEDEC | BYTE_REP_BYTE_WORD;
printf("[%c] %s %.1lf %s\n", (file->isDir ? 'D' : 'F'), file->name,
cpathGetFileSizeDec(file, 1024), cpathGetFileSizeSuffix(file, flags));
}
void paths_example() {
// casts are automatic and should be crossplatform
// if you want to make it truly unicode independent and its a literal
// then you can use CPATH_STR() which will convert it to the wide version
// as required. This of course only applies to string literals
// and you can use cpathFromString(myStr, path) to write to paths
cpath path = cpathGetCwd();
CPATH_CONCAT_LIT(&path, "a.out");
// if you want crossplatform
if (cpathExists(&path)) {
printf("Exists\n");
// path exists and the real name is
// cpath real;
// // cpathCanonicalise(path, &real);
// // you can map it to a string
// // this is very platform dependent... and won't work with unicode
// printf("%s\n", (char*)real);
// // it is safer to do
// char *str = cpathToUtf8(real);
// // or more performant to do
// cpathPrint(real);
}
}
int main(void) {
cpath_dir dir;
cpath path = cpathFromUtf8("tests/A");
cpathOpenDir(&dir, &path);
// recursive_visit(&dir, 0);
paths_example();
// emplace(&dir);
cpath_traverse(&dir, 0, 1, NULL, for_file, NULL);
cpath_file file;
CPATH_CONCAT_LIT(&path, "a.out");
cpathOpenFile(&file, &path);
CPathByteRep flags = BYTE_REP_JEDEC | BYTE_REP_BYTE_WORD;
printf("[%c] %s %.1lf %s\n", (file.isDir ? 'D' : 'F'), file.name,
cpathGetFileSizeDec(&file, 1000), cpathGetFileSizeSuffix(&file, flags));
}