diff options
| author | grm <grm@eyesin.space> | 2025-10-16 00:47:21 +0300 | 
|---|---|---|
| committer | grm <grm@eyesin.space> | 2025-10-16 00:47:21 +0300 | 
| commit | f0a57d10dd93580ef0b553b8781185fdc09b2da0 (patch) | |
| tree | b96538bf209a55f13aa0fb98f2480d06ece7e58f /src/os.c | |
| parent | 4248bf57800447e1abef618db9da4e0f1291d0f3 (diff) | |
| download | synth-project-f0a57d10dd93580ef0b553b8781185fdc09b2da0.tar.gz synth-project-f0a57d10dd93580ef0b553b8781185fdc09b2da0.tar.bz2 synth-project-f0a57d10dd93580ef0b553b8781185fdc09b2da0.zip | |
Add spinner for sound picking
Diffstat (limited to 'src/os.c')
| -rw-r--r-- | src/os.c | 255 | 
1 files changed, 255 insertions, 0 deletions
| diff --git a/src/os.c b/src/os.c new file mode 100644 index 0000000..c9f4ee8 --- /dev/null +++ b/src/os.c @@ -0,0 +1,255 @@ +#include "os.h" + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <dirent.h> +#include <sys/stat.h> + +// 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; */ +/* } */ | 
