-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplaylist.c
More file actions
122 lines (102 loc) · 3.66 KB
/
playlist.c
File metadata and controls
122 lines (102 loc) · 3.66 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
#include "cMusix.h"
void add_song(const char* filepath) {
if (player.count >= MAX_SONGS) {
return;
}
strncpy(player.songs[player.count].path, filepath, MAX_PATH_LENGTH - 1);
player.songs[player.count].path[MAX_PATH_LENGTH - 1] = '\0';
const char* filename = strrchr(filepath, '/');
if (filename) {
filename++;
} else {
filename = filepath;
}
strncpy(player.songs[player.count].name, filename, MAX_FILENAME_LENGTH - 1);
player.songs[player.count].name[MAX_FILENAME_LENGTH - 1] = '\0';
player.count++;
}
void scan_directory(const char* dir_path) {
DIR* dir = opendir(dir_path);
if (!dir) {
printf("Warning: Could not open directory: %s\n", dir_path);
return;
}
struct dirent* entry;
while ((entry = readdir(dir)) != NULL) {
// Skip hidden files and current/parent directory entries
if (entry->d_name[0] == '.') continue;
char full_path[MAX_PATH_LENGTH];
int ret = snprintf(full_path, sizeof(full_path), "%s/%s", dir_path, entry->d_name);
// Check if path was truncated
if (ret >= (int)sizeof(full_path)) {
printf("Warning: Path too long, skipping: %s/%s\n", dir_path, entry->d_name);
continue;
}
struct stat file_stat;
if (stat(full_path, &file_stat) == 0) {
if (S_ISDIR(file_stat.st_mode)) {
// Recursively scan subdirectory
printf(" Scanning subdirectory: %s\n", entry->d_name);
scan_directory(full_path);
} else if (S_ISREG(file_stat.st_mode)) {
// Check if it's an audio file
printf(" Checking file: %s", entry->d_name);
if (audio_file(entry->d_name)) {
add_song(full_path);
printf(" -> ADDED\n");
} else {
printf(" -> skipped (not audio)\n");
}
}
} else {
printf("Warning: Could not stat file: %s\n", full_path);
}
}
closedir(dir);
}
void load_folder(const char* folder_path) {
player.count = 0;
player.current_index = 0;
player.list_offset = 0;
// Try to resolve relative path, but continue even if realpath fails
char resolved_path[MAX_PATH_LENGTH];
const char* path_to_use = folder_path;
if (realpath(folder_path, resolved_path) != NULL) {
path_to_use = resolved_path;
}
// Check if directory exists
struct stat dir_stat;
if (stat(path_to_use, &dir_stat) != 0) {
printf("Error: Directory does not exist: %s\n", path_to_use);
return;
}
if (!S_ISDIR(dir_stat.st_mode)) {
printf("Error: Path is not a directory: %s\n", path_to_use);
return;
}
printf("Scanning directory: %s\n", path_to_use);
// First, try to list directory contents to debug
DIR* test_dir = opendir(path_to_use);
if (!test_dir) {
printf("Error: Cannot open directory: %s\n", path_to_use);
return;
}
printf("Directory contents:\n");
struct dirent* entry;
while ((entry = readdir(test_dir)) != NULL) {
if (entry->d_name[0] != '.') {
printf(" Found: %s\n", entry->d_name);
}
}
closedir(test_dir);
// Now do the actual scan
scan_directory(path_to_use);
printf("Total found: %d audio files\n", player.count);
// Debug: List all found songs
if (player.count > 0) {
printf("\nFound audio files:\n");
for (int i = 0; i < player.count; i++) {
printf(" %d: %s\n", i + 1, player.songs[i].name);
}
}
}