📄 dirhelper.c
字号:
/* * C Source file for dirhelper.c * Copyright (c) Sigma Designs 2002-2003. All Rights Reserved. * */#include <sys/stat.h>#include <sys/mount.h>#include <linux/unistd.h>#include <asm/errno.h>#include <ctype.h>#include <time.h>#include <dirent.h>#include "OsdWindow.h"#include "dirhelper.h"int comp(FileRec **fr1, FileRec **fr2){ FileRec *fl1 = *fr1, *fl2 = *fr2; //if (S_ISDIR(fl1->st_mode)) { // if (!S_ISDIR(fl2->st_mode)) // return -1; //} //else if (S_ISDIR(fl2->st_mode)) // return 1; //switch (sort_type) { //case SortByName: return strcmp(fl1->name, fl2->name); //case SortBySize: //return (int)(fl2->stats.st_size - fl1->stats.st_size); //case SortByMTime: //return (int)(fl2->stats.st_mtime - fl1->stats.st_mtime); //} //return 0;}void GetExt(char *filename, char *ext){ int len = strlen(filename); int i = 0; char *t; // assume ext has only 3 chars t = filename + len - 4; //while (*t != '.' && i < 3){ t--; i++; } if (*t == '.') { t++; for (i=0; i<3; i++, t++) { ext[i] = (char)toupper((int)*t); } ext[i] = '\0'; } else ext[0] = '\0'; return;}static void ultoa(unsigned long i, char *str){ char buf[FSIZE_MAX]; char *pos; unsigned long u; int c;// int negative = 0;// if (i < 0) {// negative = 1;// u = ((unsigned int)(-(1+i))) + 1;// } else { u = i;// } pos = buf + FSIZE_MAX - 1; *pos = 0; c = 0; if (u >= 1000) { *--pos = 'B'; *--pos = 'K'; *--pos = ' '; u /= 1000; } do { *--pos = '0' + (u % 10); u /= 10; c = (c + 1) % 3; if (c == 0 && u) *--pos = ','; } while (u); // if (negative) {// *--pos = '-';// } while (*pos) *str++ = *pos++; *str = '\0'; return;}static void freedlist(DirList d, int ndirs){ int i; for(i=0;i<ndirs;i++) { free(d[i]->name); free(d[i]); } free(d); return;}voidfreeflist(FileList f, int nfiles){ int i; for(i=0;i<nfiles;i++) { free(f[i]->name); free(f[i]->path); free(f[i]); } free(f); return;}DirList ReadDir(char *directory, int libType, int *pn_dirs){ static char path[PATH_MAX+1]; char ext[4] = "\0"; DIR *dir; struct dirent *entry; struct stat lst, statbuf; struct tm *loctime; int ndirs, ftype; DirList dl; if ((dir=opendir(directory)) == NULL) { printf("Cannot open dir: %s\n",directory); goto error2; } ndirs = 0; dl = NULL; while ((entry = readdir(dir))) { //printf("%s\n",entry->d_name); sprintf(path,"%s/%s",directory,entry->d_name); if (lstat(path, &lst) < 0) continue; if (stat(path, &statbuf) < 0) { printf("Cannot do stat %s\n",path); statbuf.st_mode = 0; //goto error1; } if ( (entry->d_name[0] == '.' && entry->d_name[1] == '\0') || (entry->d_name[0] == '.' && entry->d_name[1] == '.' && entry->d_name[2] == '\0') ) // not displaying . and .. continue; else if (S_ISDIR(statbuf.st_mode) ) { //else if ( (statbuf.st_mode & S_IFMT) == S_IFDIR ) { // Add to dirlist dl = (DirRec **) realloc(dl, (ndirs+1)*sizeof(DirRec *)); dl[ndirs] = (DirRec *) malloc(sizeof(DirRec)); dl[ndirs]->name = scopy(entry->d_name); ndirs++; } else { GetExt(entry->d_name, ext); //printf("ext=%s\n",ext); switch (libType) { case AUDIO_LIB: if ((ext[0] == 'M' && ext[1] == 'P' && ext[2] == '3') || (ext[0] == 'A' && ext[1] == 'C' && ext[2] == '3')) { ftype = AUDIO_LIB; goto AddtoFileList; } break; case VIDEO_LIB: if ((ext[0] == 'A' && ext[1] == 'V' && ext[2] == 'I') || (ext[0] == 'M' && ext[1] == 'P' && ext[2] == 'G') || (ext[0] == 'V' && ext[1] == 'O' && ext[2] == 'B')) { ftype = VIDEO_LIB; goto AddtoFileList; } break; case PHOTO_LIB: if (ext[0] == 'J' && ext[1] == 'P' && ext[2] == 'G') { ftype = PHOTO_LIB; goto AddtoFileList; } break; case PLAYLIST_LIB: if ((ext[0] == 'M' && ext[1] == 'P' && ext[2] == '3') || (ext[0] == 'A' && ext[1] == 'C' && ext[2] == '3')) { ftype = AUDIO_LIB; goto AddtoFileList; } else if (ext[0] == 'J' && ext[1] == 'P' && ext[2] == 'G') { ftype = PHOTO_LIB; goto AddtoFileList; } else if ((ext[0] == 'A' && ext[1] == 'V' && ext[2] == 'I') || (ext[0] == 'M' && ext[1] == 'P' && ext[2] == 'G') || (ext[0] == 'V' && ext[1] == 'O' && ext[2] == 'B')) { ftype = VIDEO_LIB; AddtoFileList: fl = (FileRec **) realloc(fl, (n_files+1)*sizeof(FileRec *)); fl[n_files] = (FileRec *) malloc(sizeof(FileRec)); fl[n_files]->name = scopy(entry->d_name); fl[n_files]->path = scopy(directory); fl[n_files]->selected = GR_FALSE; fl[n_files]->ftype = ftype; ultoa(statbuf.st_size, fl[n_files]->size); fl[n_files]->st_mode = statbuf.st_mode; loctime = localtime(&statbuf.st_mtime); strftime(fl[n_files]->ltime, sizeof(fl[n_files]->ltime), "%x %I:%M %p", loctime); n_files++; if (n_files >= FILES_MAX) { printf("Max file entries reached %d\n",FILES_MAX); goto error0; } } break; } } } error0: if (closedir(dir)) { printf("Cannot close dir\n"); goto error1; } *pn_dirs = ndirs; return dl; error1: perror("Error1"); freedlist(dl, ndirs); *pn_dirs = 0; return NULL; error2: perror("Error2"); *pn_dirs = 0; return NULL;}static void FindFiles(char *directory, int type){ int i, n_dirs; DirList sdl, dl; char path[PATH_MAX+1]; dirs[lev++] = directory; //printf("dirs=%s,lev=%d\n",directory,lev); sdl = dl = ReadDir(directory, type, &n_dirs);#ifdef _DEBUG_ printf("n_dirs=%d within directory %s\n",n_dirs,directory); for (i=0; i<n_dirs; i++) printf("%s\n",dl[i]->name); // printf("n_files=%d\n",n_files);// for (i=0; i<n_files; i++) {// printf("%s/%s, %s %s\n",fl[i]->path, fl[i]->name,(unsigned long)fl[i]->size, fl[i]->ltime); // }#endif if (!dl && n_dirs) { printf("Error opening dir!\n"); return; } for (i=0; i<n_dirs && n_files < FILES_MAX; i++) { sprintf(path,"%s/%s",directory,(*dl)->name); FindFiles(path, type); dl++; lev--; } freedlist(sdl, n_dirs); //qsort(fl, n_files, sizeof(FileRec *), (int (*)(const void *, const void *))comp); #ifdef _DEBUG_ printf("n_files=%d\n",n_files); for (i=0; i<n_files; i++) { printf("%s/%s, %s %s\n",fl[i]->path, fl[i]->name,(unsigned long)fl[i]->size, fl[i]->ltime); }#endif return;}FileList GetFiles(char *directory, int type, int *nfiles){ n_files = lev = 0; fl = NULL; FindFiles(directory, type); *nfiles = n_files; return fl; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -