-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathROMorg.c
More file actions
163 lines (131 loc) · 3.61 KB
/
ROMorg.c
File metadata and controls
163 lines (131 loc) · 3.61 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
/**
* ROMorg
* by Brandon, 2015
*/
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#define MAX_TITLE_LENGTH 128
#define TMP_DIR "romorg_tmp"
#define ROM_DIR "./"
int is_rom(char *filename)
{
char *ext = strrchr(filename, '.');
int is_rom = 0;
if (ext)
{
// lazy extension check
if (!strcasecmp(ext, ".3ds") || !strcasecmp(ext, ".3dz"))
is_rom = 1;
}
return is_rom;
}
int count_roms(struct dirent *ds, DIR *rom_dir)
{
int rom_count = 0;
while (ds = readdir(rom_dir))
{
if (!is_rom(ds->d_name))
continue;
rom_count++;
}
rewinddir(rom_dir);
return rom_count;
}
void scan_roms(char *roms_list_buffer, struct dirent *ds, DIR *rom_dir)
{
int i = 0;
while (ds = readdir(rom_dir))
{
if (!is_rom(ds->d_name))
continue;
strcpy(&roms_list_buffer[i], ds->d_name);
// fixme
// strncpy(&roms_list_buffer[i], ds->d_name, sizeof(ds->d_name));
i += MAX_TITLE_LENGTH;
}
}
void get_rom_order(int rom_count, int *rom_order_buffer)
{
int tmp_input; // move into get_user_order
// todo: instead of for(), use a do/while until sizeof(rom_order_buffer) = rom_count
for (int i = 0; i < rom_count; i++)
{
if (i >= 1)
printf("Next ID: ");
// check if valid int, and if valid rom id
// todo: check if input already exists in rom_order_buffer
if (scanf("%d", &tmp_input) && tmp_input <= rom_count && tmp_input > 0)
{
rom_order_buffer[i] = tmp_input;
}
else
{
// fixme: this will skip any remaining iterations and print invalid rom id for each, only if input != int
// if input is an invalid rom id but still an int, it continues as expected
puts("invalid rom id");
}
}
}
void main ()
{
puts("-----------------------------");
puts("--------- ROMorg --------");
puts("--------- by Brandon --------");
puts("-----------------------------\n");
struct dirent *ds;
DIR *dirp = opendir(ROM_DIR);
int rom_count = count_roms(ds, dirp);
int rom_order_buffer[rom_count];
char roms_list_buffer[(rom_count) * MAX_TITLE_LENGTH];
if (!rom_count)
{
puts("No ROMs found.");
return;
}
if (!(mkdir(TMP_DIR, 0777) == 0))
{
puts("Unable to create tmp directory.");
return;
}
scan_roms(roms_list_buffer, ds, dirp);
closedir(dirp);
printf("Found %d ROMs:\n", rom_count);
for (int i = 0; i < rom_count; i++)
{
printf("%d. %s\n", i+1, &roms_list_buffer[i * MAX_TITLE_LENGTH]);
}
printf("\nWhich ROM do you want to appear first?\nEnter ID: ");
get_rom_order(rom_count, rom_order_buffer);
printf("\nSorting...");
// move 'em in
char rom_in_tmp[(rom_count) * (MAX_TITLE_LENGTH + sizeof(TMP_DIR))];
int rom_in_tmp_index;
for (int i = 0; i < rom_count; i++)
{
rom_in_tmp_index = (i * (MAX_TITLE_LENGTH + sizeof(TMP_DIR)));
strcpy(&rom_in_tmp[rom_in_tmp_index], TMP_DIR "/");
strcat(&rom_in_tmp[rom_in_tmp_index], &roms_list_buffer[i * MAX_TITLE_LENGTH]);
// printf("\nmoving %s into %s...", &roms_list_buffer[i * MAX_TITLE_LENGTH], &rom_in_tmp[rom_in_tmp_index]);
rename(&roms_list_buffer[i * MAX_TITLE_LENGTH], &rom_in_tmp[rom_in_tmp_index]);
printf(".");
}
// take 'em out, in order
char rom_in_root;
int user_ordered_index;
for (int i = 0; i < rom_count; i++)
{
user_ordered_index = (rom_order_buffer[i] - 1);
rom_in_tmp_index = (user_ordered_index * (MAX_TITLE_LENGTH + sizeof(TMP_DIR)));
strcpy(&rom_in_root, ROM_DIR);
strcat(&rom_in_root, &roms_list_buffer[user_ordered_index * MAX_TITLE_LENGTH]);
// printf("\nmoving %s into %s...", &rom_in_tmp[rom_in_tmp_index], &rom_in_root);
rename(&rom_in_tmp[rom_in_tmp_index], &rom_in_root);
printf(".");
}
// bye bye
remove(TMP_DIR);
printf("\nDone!");
return;
}