Added note about 2.5 being Archos only
[kugel-rb.git] / apps / tagdb / main.c
blob61a0330c81e4b82650bbd9a801ea03585e49fe94
1 #include "config.h"
3 #include <stdio.h>
4 #include <string.h> // strcmp()
5 #include <dirent.h> // opendir() readdir() closedir()
6 #include <sys/stat.h> // IS_DIR
8 #include "malloc.h"
9 #include "db.h"
11 extern int out_of_memory;
13 // dir-is-album: all files in the dir ARE the same album, use the first name found.
14 // dir-is-album-name: if no tag found, use the dir's instead of "<no album tag>"
16 // files in different dirs are ALWAYS different albums
18 static char* strip_path = NULL;
19 static char* add_path = NULL;
21 static int iterate_dir(char* dir);
22 /* Iterates over each item in the given directory
23 * calls add_file() on each file
24 * calls iterate_directory() on each directory (recursively)
27 static int iterate_dir(char* dir) {
28 DIR *d;
29 struct dirent *e;
30 struct stat s;
31 int rc;
33 assert(dir != NULL);
35 if(!( d = opendir(dir) )) {
36 DEBUGF("iterate_dir: could not open directory \"%s\"\n", dir);
37 return ERR_FILE;
40 while(( e = readdir(d) )) {
41 char *path;
43 if( strcmp(e->d_name, ".") == 0 || strcmp(e->d_name, "..") == 0 )
44 continue; // we don't want to descend or loop around...
46 path = malloc(strlen(dir) + 1 + strlen(e->d_name) + 1); // "dir/d_name\0"
47 if( path == NULL ) {
48 DEBUGF("iterate_dir: could not malloc() directory-entry-name\n");
49 return ERR_MALLOC;
51 strcpy(path, dir);
52 strcat(path, "/");
53 strcat(path, e->d_name);
54 #if defined OS_LINUX
55 if( stat(path, &s) ) {
56 DEBUGF("iterate_dir: could not stat(\"%s\")\n", path);
57 return ERR_FILE;
60 if( S_ISDIR(s.st_mode) ) {
61 #elif defined OS_ROCKBOX
62 #error "Rockbox: not yet implemented: don't know how to list directory"
63 if( false ) {
64 #elif defined OS_WINDOWS
65 if( false ) {
66 #error "Windows: not yet implemented: don't know how to list directory"
67 #else
68 if( false ) {
69 #error "No OS specified: don't know how to list directory"
70 #endif
71 if(( rc = iterate_dir(path) )) {
72 closedir(d);
73 return rc;
75 } else {
76 if(( rc = db_add(path, strip_path, add_path) )) {
77 closedir(d);
78 return rc;
81 free(path);
84 if( closedir(d) ) {
85 DEBUGF("iterate_dir: could not close directory \"%s\", ignoring...\n", dir);
88 return ERR_NONE;
91 int main(int argc, char* argv[]) {
92 FILE *fd;
94 if( argc != 2 ) {
95 printf("usage: ./songdb dir\n");
96 return 1;
99 strip_path = "/home/niels/";
100 add_path = "TEST/";
102 db_construct();
104 iterate_dir(argv[1]);
106 fd = fopen("xxx.db", "w");
107 db_write(fd);
108 fclose(fd);
110 db_destruct();
112 malloc_stats();
114 return 0;