summaryrefslogtreecommitdiffstats
path: root/src/os.c
blob: c9f4ee87c85b6e7a52065baf9940292df2c08f44 (plain) (blame)
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
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; */
/* } */