1 #include <allegro5/allegro.h>
2 #include <stdio.h>
3
4 #include "common.c"
5
print_file(ALLEGRO_FS_ENTRY * entry)6 static void print_file(ALLEGRO_FS_ENTRY *entry)
7 {
8 int mode = al_get_fs_entry_mode(entry);
9 time_t now = time(NULL);
10 time_t atime = al_get_fs_entry_atime(entry);
11 time_t ctime = al_get_fs_entry_ctime(entry);
12 time_t mtime = al_get_fs_entry_mtime(entry);
13 off_t size = al_get_fs_entry_size(entry);
14 const char *name = al_get_fs_entry_name(entry);
15
16 log_printf("%-36s %s%s%s%s%s%s %8u %8u %8u %8u\n",
17 name,
18 mode & ALLEGRO_FILEMODE_READ ? "r" : ".",
19 mode & ALLEGRO_FILEMODE_WRITE ? "w" : ".",
20 mode & ALLEGRO_FILEMODE_EXECUTE ? "x" : ".",
21 mode & ALLEGRO_FILEMODE_HIDDEN ? "h" : ".",
22 mode & ALLEGRO_FILEMODE_ISFILE ? "f" : ".",
23 mode & ALLEGRO_FILEMODE_ISDIR ? "d" : ".",
24 (unsigned)(now - ctime),
25 (unsigned)(now - mtime),
26 (unsigned)(now - atime),
27 (unsigned)size);
28 }
29
print_entry(ALLEGRO_FS_ENTRY * entry)30 static void print_entry(ALLEGRO_FS_ENTRY *entry)
31 {
32 ALLEGRO_FS_ENTRY *next;
33
34 print_file(entry);
35
36 if (!(al_get_fs_entry_mode(entry) & ALLEGRO_FILEMODE_ISDIR))
37 return;
38
39 if (!al_open_directory(entry)) {
40 log_printf("Error opening directory: %s\n", al_get_fs_entry_name(entry));
41 return;
42 }
43
44 while (1) {
45 next = al_read_directory(entry);
46 if (!next)
47 break;
48
49 print_entry(next);
50 al_destroy_fs_entry(next);
51 }
52
53 al_close_directory(entry);
54 }
55
56
print_fs_entry_cb(ALLEGRO_FS_ENTRY * entry,void * extra)57 static int print_fs_entry_cb(ALLEGRO_FS_ENTRY * entry, void * extra) {
58 (void) extra;
59 print_file(entry);
60 return ALLEGRO_FOR_EACH_FS_ENTRY_OK;
61 }
62
print_fs_entry_cb_norecurse(ALLEGRO_FS_ENTRY * entry,void * extra)63 static int print_fs_entry_cb_norecurse(ALLEGRO_FS_ENTRY * entry, void * extra) {
64 (void) extra;
65 print_file(entry);
66 return ALLEGRO_FOR_EACH_FS_ENTRY_SKIP;
67 }
68
print_fs_entry(ALLEGRO_FS_ENTRY * dir)69 static void print_fs_entry(ALLEGRO_FS_ENTRY * dir)
70 {
71 log_printf("\n------------------------------------\nExample of al_for_each_fs_entry with recursion:\n\n");
72 al_for_each_fs_entry(dir, print_fs_entry_cb,
73 (void*) al_get_fs_entry_name(dir));
74 }
75
print_fs_entry_norecurse(ALLEGRO_FS_ENTRY * dir)76 static void print_fs_entry_norecurse(ALLEGRO_FS_ENTRY * dir)
77 {
78 log_printf("\n------------------------------------\nExample of al_for_each_fs_entry without recursion:\n\n");
79 al_for_each_fs_entry(dir, print_fs_entry_cb_norecurse,
80 (void*) al_get_fs_entry_name(dir));
81 }
82
main(int argc,char ** argv)83 int main(int argc, char **argv)
84 {
85 int i;
86
87 if (!al_init()) {
88 abort_example("Could not init Allegro.\n");
89 }
90 open_log_monospace();
91
92 log_printf("Example of filesystem entry functions:\n\n%-36s %-6s %8s %8s %8s %8s\n",
93 "name", "flags", "ctime", "mtime", "atime", "size");
94 log_printf(
95 "------------------------------------ "
96 "------ "
97 "-------- "
98 "-------- "
99 "-------- "
100 "--------\n");
101
102 #ifdef ALLEGRO_ANDROID
103 al_android_set_apk_fs_interface();
104 #endif
105
106 if (argc == 1) {
107 ALLEGRO_FS_ENTRY *entry = al_create_fs_entry("data");
108 print_entry(entry);
109 print_fs_entry(entry);
110 print_fs_entry_norecurse(entry);
111 al_destroy_fs_entry(entry);
112 }
113
114 for (i = 1; i < argc; i++) {
115 ALLEGRO_FS_ENTRY *entry = al_create_fs_entry(argv[i]);
116 print_entry(entry);
117 print_fs_entry(entry);
118 print_fs_entry_norecurse(entry);
119 al_destroy_fs_entry(entry);
120 }
121
122 close_log(true);
123 return 0;
124 }
125
126 /* vim: set sts=3 sw=3 et: */
127