-
-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathmenu-list.c
More file actions
233 lines (190 loc) · 4.56 KB
/
menu-list.c
File metadata and controls
233 lines (190 loc) · 4.56 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
#include "common.h"
static menu_s s_menu[2];
static menu_s s_menuFileAssoc[2];
static bool s_curMenu, s_curMenuFileAssoc;
menu_s* menuGetCurrent(void)
{
return &s_menu[s_curMenu];
}
menu_s* menuFileAssocGetCurrent(void) {
return &s_menuFileAssoc[s_curMenuFileAssoc];
}
menuEntry_s* menuCreateEntry(MenuEntryType type)
{
menuEntry_s* me = (menuEntry_s*)malloc(sizeof(menuEntry_s));
menuEntryInit(me, type);
return me;
}
void menuDeleteEntry(menuEntry_s* me)
{
menuEntryFree(me);
free(me);
}
static void _menuAddEntry(menu_s* m, menuEntry_s* me)
{
me->menu = m;
if (m->lastEntry)
{
m->lastEntry->next = me;
m->lastEntry = me;
} else
{
m->firstEntry = me;
m->lastEntry = me;
}
m->nEntries++;
}
static void _menuClear(menu_s* menu)
{
menuEntry_s* cur, *next;
for (cur = menu->firstEntry; cur; cur = next) {
next = cur->next;
menuDeleteEntry(cur);
}
memset(menu, 0, sizeof(*menu));
}
static void menuClear(void)
{
_menuClear(&s_menu[!s_curMenu]);
}
void menuFileAssocClear(void)
{
_menuClear(&s_menuFileAssoc[!s_curMenuFileAssoc]);
}
void menuFileAssocAddEntry(menuEntry_s* me)
{
_menuAddEntry(&s_menuFileAssoc[!s_curMenuFileAssoc], me);
}
static void menuAddEntry(menuEntry_s* me)
{
_menuAddEntry(&s_menu[!s_curMenu], me);
}
static int menuEntryCmp(const void *p1, const void *p2)
{
const menuEntry_s* lhs = *(menuEntry_s**)p1;
const menuEntry_s* rhs = *(menuEntry_s**)p2;
if(lhs->isStarred != rhs->isStarred)
return lhs->isStarred ? -1 : 1;
if(lhs->type == rhs->type)
return strcasecmp(lhs->name, rhs->name);
if(lhs->type != ENTRY_TYPE_FOLDER)
return -1;
return 1;
}
static void menuSort(void)
{
int i;
menu_s* m = &s_menu[!s_curMenu];
int nEntries = m->nEntries;
if (nEntries==0) return;
menuEntry_s** list = (menuEntry_s**)calloc(nEntries, sizeof(menuEntry_s*));
if(list == NULL) return;
menuEntry_s* p = m->firstEntry;
for(i = 0; i < nEntries; ++i) {
list[i] = p;
p = p->next;
}
qsort(list, nEntries, sizeof(menuEntry_s*), menuEntryCmp);
menuEntry_s** pp = &m->firstEntry;
for(i = 0; i < nEntries; ++i) {
*pp = list[i];
pp = &(*pp)->next;
}
m->lastEntry = list[nEntries-1];
*pp = NULL;
free(list);
}
int menuFileAssocScan(const char* target)
{
menuFileAssocClear();
if (chdir(target) < 0)
return 1;
if (getcwd(s_menuFileAssoc[!s_curMenuFileAssoc].dirname, PATH_MAX + 1) == NULL)
return 1;
DIR* dir;
struct dirent* dp;
char temp[PATH_MAX + 1];
dir = opendir(s_menuFileAssoc[!s_curMenuFileAssoc].dirname);
if (!dir)
return 2;
while ((dp = readdir(dir))) {
if (dp->d_name[0] == '.')
continue;
memset(temp, 0, sizeof(temp));
snprintf(temp, sizeof(temp) - 1, "%s/%s", s_menuFileAssoc[!s_curMenuFileAssoc].dirname, dp->d_name);
const char* ext = getExtension(dp->d_name);
if (strcasecmp(ext, ".cfg") != 0)
continue;
menuEntryFileAssocLoad(temp);
}
closedir(dir);
s_curMenuFileAssoc = !s_curMenuFileAssoc;
menuFileAssocClear();
return 0;
}
int menuScan(const char* target)
{
if (chdir(target) < 0) return 1;
getcwd(s_menu[!s_curMenu].dirname, PATH_MAX+1);
DIR* dir;
struct dirent* dp;
dir = opendir(s_menu[!s_curMenu].dirname);
if (!dir) return 2;
while ((dp = readdir(dir)))
{
archive_dir_t* dirSt = (archive_dir_t*)dir->dirData->dirStruct;
FS_DirectoryEntry* entry = &dirSt->entry_data[dirSt->index];
menuEntry_s* me = NULL;
bool shortcut = false;
if (entry->attributes & FS_ATTRIBUTE_HIDDEN || dp->d_name[0] == '.')
continue;
if (entry->attributes & FS_ATTRIBUTE_DIRECTORY)
me = menuCreateEntry(ENTRY_TYPE_FOLDER);
else
{
const char* ext = getExtension(dp->d_name);
if (strcasecmp(ext, ".3dsx")==0 || (shortcut = strcasecmp(ext, ".xml")==0))
me = menuCreateEntry(ENTRY_TYPE_FILE);
if (!me)
me = menuCreateEntry(ENTRY_TYPE_FILE_OTHER);
}
if (!me)
continue;
snprintf(me->path, sizeof(me->path), "%s/%s", s_menu[!s_curMenu].dirname, dp->d_name);
if (menuEntryLoad(me, dp->d_name, shortcut))
menuAddEntry(me);
else
menuDeleteEntry(me);
}
closedir(dir);
menuSort();
// Swap the menu and clear the previous menu
s_curMenu = !s_curMenu;
menuClear();
return 0;
}
void menuToggleStar(menuEntry_s* me)
{
me->isStarred = !me->isStarred;
if (me->isStarred)
{
FILE* f = fopen(me->starpath, "w");
if (f) fclose(f);
} else
remove(me->starpath);
// Sort the menu again
s_curMenu = !s_curMenu;
menuSort();
s_curMenu = !s_curMenu;
menuClear();
menu_s* menu = menuGetCurrent();
int pos = -1;
for (menuEntry_s* cur = menu->firstEntry; cur; cur = cur->next)
{
++pos;
if (cur == me)
break;
}
menu->curEntry = pos;
menu->perturbed = true;
}