#include "os.h" #include #include #include #include #include // Function to check if a string ends with a specific suffix (case insensitive) int ends_with_wav(const char *str) { size_t len = strlen(str); if (len < 4) return 0; const char *suffix = str + len - 4; return (strcasecmp(suffix, ".wav") == 0); } // Function to extract the example name from folder format: "example name[2048-44.1khz-32bit]" char* extract_example_name(const char *folder_name) { char *result = malloc(strlen(folder_name) + 1); if (!result) return NULL; strcpy(result, folder_name); // Find the opening bracket and terminate the string there char *bracket = strchr(result, '['); if (bracket) { *bracket = '\0'; } // Remove trailing whitespace int len = strlen(result); while (len > 0 && (result[len-1] == ' ' || result[len-1] == '\t')) { result[--len] = '\0'; } return result; } char* scan_for_wav(const char *directory_path) { DIR *dir; struct dirent *entry; struct stat statbuf; char full_path[1024]; char *result = NULL; size_t result_size = 0; size_t result_capacity = 1024; int first_entry = 1; // Initialize result buffer result = malloc(result_capacity); if (!result) return NULL; result[0] = '\0'; // Open directory dir = opendir(directory_path); if (!dir) { free(result); return NULL; } // Iterate through directory entries while ((entry = readdir(dir)) != NULL) { // Skip . and .. entries if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) { continue; } // Create full path snprintf(full_path, sizeof(full_path), "%s/%s", directory_path, entry->d_name); // Check if it's a directory if (stat(full_path, &statbuf) == 0 && S_ISDIR(statbuf.st_mode)) { // This is a subdirectory, scan for WAV files in it DIR *subdir; struct dirent *subentry; subdir = opendir(full_path); if (!subdir) continue; // Extract example name from folder name char *example_name = extract_example_name(entry->d_name); if (!example_name) { closedir(subdir); continue; } // Look for WAV files in subdirectory while ((subentry = readdir(subdir)) != NULL) { if (ends_with_wav(subentry->d_name)) { // Calculate needed space for new entry (full path) size_t needed_space = strlen(full_path) + 1 + strlen(subentry->d_name) + 2; // +2 for ';' and '\0' if (!first_entry) needed_space += 1; // for semicolon separator // Resize buffer if needed while (result_size + needed_space >= result_capacity) { result_capacity *= 2; char *new_result = realloc(result, result_capacity); if (!new_result) { free(result); free(example_name); closedir(subdir); closedir(dir); return NULL; } result = new_result; } // Add separator if not first entry if (!first_entry) { strcat(result, ";"); result_size += 1; } // Add the full path strcat(result, full_path); strcat(result, "/"); strcat(result, subentry->d_name); result_size += strlen(full_path) + 1 + strlen(subentry->d_name); first_entry = 0; } } free(example_name); closedir(subdir); } } closedir(dir); // If no WAV files found, return empty string if (result_size == 0) { strcpy(result, ""); } return result; } // Main function to scan directory and return formatted string char* scan_for_wav_pretty(const char *directory_path) { DIR *dir; struct dirent *entry; struct stat statbuf; char full_path[1024]; char *result = NULL; size_t result_size = 0; size_t result_capacity = 1024; int first_entry = 1; // Initialize result buffer result = malloc(result_capacity); if (!result) return NULL; result[0] = '\0'; // Open directory dir = opendir(directory_path); if (!dir) { free(result); return NULL; } // Iterate through directory entries while ((entry = readdir(dir)) != NULL) { // Skip . and .. entries if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) { continue; } // Create full path snprintf(full_path, sizeof(full_path), "%s/%s", directory_path, entry->d_name); // Check if it's a directory if (stat(full_path, &statbuf) == 0 && S_ISDIR(statbuf.st_mode)) { // This is a subdirectory, scan for WAV files in it DIR *subdir; struct dirent *subentry; subdir = opendir(full_path); if (!subdir) continue; // Extract example name from folder name char *example_name = extract_example_name(entry->d_name); if (!example_name) { closedir(subdir); continue; } // Look for WAV files in subdirectory while ((subentry = readdir(subdir)) != NULL) { if (ends_with_wav(subentry->d_name)) { // Calculate needed space for new entry size_t needed_space = strlen(example_name) + 1 + strlen(subentry->d_name) + 2; // +2 for ';' and '\0' if (!first_entry) needed_space += 1; // for semicolon separator // Resize buffer if needed while (result_size + needed_space >= result_capacity) { result_capacity *= 2; char *new_result = realloc(result, result_capacity); if (!new_result) { free(result); free(example_name); closedir(subdir); closedir(dir); return NULL; } result = new_result; } // Add separator if not first entry if (!first_entry) { strcat(result, ";"); result_size += 1; } // Add the formatted entry strcat(result, example_name); strcat(result, "/"); strcat(result, subentry->d_name); result_size += strlen(example_name) + 1 + strlen(subentry->d_name); first_entry = 0; } } free(example_name); closedir(subdir); } } closedir(dir); // If no WAV files found, return empty string if (result_size == 0) { strcpy(result, ""); } return result; } /* // Example usage */ /* int main() { */ /* const char *test_dir = "/path/to/your/directory"; */ /* char *result = scan_wav_directory(test_dir); */ /* if (result) { */ /* printf("Found WAV files: %s\n", result); */ /* free(result); */ /* } else { */ /* printf("Error scanning directory or no WAV files found.\n"); */ /* } */ /* return 0; */ /* } */