📄 dirb.cpp
字号:
/* dirb.cpp Directory browser class This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details, www.gnu.org. Copyright (C) 2000 Simon Harrison (email smh@N_O_S_P_A_M@dr.com)*/#include "dirb.h"int CDirBrowser::ls_compare_func( const void *a, const void *b){ char* pa = (((CString *)a))->GetName(); char* pb = (((CString *)b))->GetName(); return strcmp( pa, pb ); }int CDirBrowser::isdirectory( char * path ){int i, dirlen;DIR *dirp;struct dirent *dp; if ((!path) || (!*path)) return 0; dirp = opendir(path); if (!dirp) { return 0; } else { closedir( dirp); return 1; }}int CDirBrowser::newlisting(int disp) { clean(); loadvalues(); if (disp) { update(); } return 1;}void CDirBrowser::left(){ int cs; strcpy( buffer2, cwdbuffer ); if (strcmp( cwdbuffer, "/" )!=0) { p = (char*)strrchr( cwdbuffer, '/' ); if (p==cwdbuffer) { strcpy( cwdbuffer, "/" ); } else { *p = '\0'; } p = (char*)strrchr( buffer2, '/' ); p++; newlisting(0); cs = findindex( p ); if (!cs) cs = 1; SetSelect(cs); update(); //sprintf( lastaction, "Changed dir to: %s",cwdbuffer ); } else { // at the top, do nothing }}void CDirBrowser::right(){ enter();}char* CDirBrowser::enter( )//// Returns a pointer to the file to use if it succeeds in finding one.//{ // form the file strcpy( buffer2, cwdbuffer ); if (buffer2[strlen(buffer2)-1]!='/') strcat( buffer2, "/" ); strcat( buffer2, GetListEntry(GetListSelected()-1) ); if (isdirectory(buffer2)) { strcpy( cwdbuffer, buffer2 ); SetSelect(1); newlisting(1); sprintf( lastaction, "Changed dir to: %s", cwdbuffer ); } else { // try to add buffer2 to the playlist if (strlen(buffer2)>3) { p = &buffer2[strlen(buffer2)-4]; } else { p = ""; } if (strcmp( ".mp3", p )==0) { // track seems to be mp3. return buffer2; } else { sprintf( lastaction, "can't add, not an mp3 file."); } } return NULL;}void CDirBrowser::loadvalues(){ int i, dirlen; DIR *dirp; CString* names; struct dirent *dp; if ((!cwdbuffer) || (!*cwdbuffer)) { return; } dirp = opendir(cwdbuffer); if (!dirp) { return; } // count # of entries in dir (worst case) for (dirlen = 0; (dp = readdir(dirp)) != NULL; dirlen++); if (!dirlen) { closedir(dirp); return; } if (dirlen>=2) dirlen -=2; // load up the entries, now that we know how many to make if (dirlen) names = new CString[dirlen]; //file_attribs = (char *)malloc(dirlen); if ((!names) && dirlen) { return; } rewinddir(dirp); for (i = 0; i < dirlen;) { dp = readdir(dirp); if (!dp) break; names[i].SetName(dp->d_name); if (!names) return; if (strcmp( dp->d_name, "." )==0) { } else if (strcmp( dp->d_name, ".." )==0) { } else { names[i].SetName(dp->d_name); i++; } } if (i < dirlen) dirlen = i; // dir got shorter... closedir(dirp); qsort(names, dirlen, sizeof(CString), ls_compare_func); SetList( names, dirlen );}int CDirBrowser::start_processing(char* cwd=NULL) // Call this before doing anything else. { if (cwd) { strncpy( cwdbuffer, cwd, 1024 ); } else { getcwd( cwdbuffer, 1024 ); } ok=newlisting(1); return 0;}/*CListBrowser cdb;main(){CString* cde;int n; printf( "Starting processing\n" ); cdb.start_processing(); printf( "done. processing\n" ); cde = cdb.GetPage(); n = cdb.GetPageEnts(); cdb.DebugDumpPage( cde, n ); printf( "Running\n" );}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -