// Recursively visit files and directories FXbool FXDirVisitor::traverse(const FXString& path){ FXStat data; if(FXStat::statLink(path,data)){ if(data.isDirectory()){ if(enter(path)){ FXDir directory(path); FXString name; while(directory.next(name)){ if(name[0]!='.' || (name[1]!=0 && (name[1]!='.' || name[2]!=0))){ if(!traverse(path+(ISPATHSEP(path.tail())?"":PATHSEPSTRING)+name)) break; } } return leave(path); } } else{ return visit(path); // Its a file } } return false; }
// List child items void FXDirList::listChildItems(FXDirItem *par,FXbool force,FXbool notify){ FXDirItem *oldlist=par->list; FXDirItem *newlist=NULL; FXDirItem **po=&oldlist; FXDirItem **pn=&newlist; FXDirItem *olditem=NULL; FXDirItem *newitem=NULL; FXDirItem *link; FXString pathname; FXString directory; FXString name; FXStat info; FXDir dir; FXuint mode; //FXTRACE((1,"FXDirList::listChildItems(%p = \"%s\",%s)\n",par,par->getText().text(),force?"true":"false")); // Path to parent node directory=getItemPathname(par); // Managed to open directory if(dir.open(directory)){ // Process directory entries while(dir.next(name)){ // Never show "." and ".." in the directory list widget if(name[0]=='.' && (name[1]==0 || (name[1]=='.' && name[2]==0))) continue; // Normally don't show hiden files or directories if(name[0]=='.' && !(options&DIRLIST_SHOWHIDDEN)) continue; // Build full pathname of entry pathname=directory; if(!ISPATHSEP(pathname.tail())) pathname+=PATHSEPSTRING; pathname+=name; #ifdef WIN32 // Get file/link info if(!FXStat::statFile(pathname,info)) continue; mode=info.mode(); // Normally don't show hiden files or directories if((mode&FXIO::Hidden) && !(options&DIRLIST_SHOWHIDDEN)) continue; #else // Get file/link info if(!FXStat::statLink(pathname,info)) continue; mode=info.mode(); // If its a link, get file mode from target if(info.isLink()){ mode=FXStat::mode(pathname) | FXIO::SymLink; } #endif // Skip item if it is a file and we want only directories, or if it fails to match wildcard pattern if(!(mode&FXIO::Directory) && !((options&DIRLIST_SHOWFILES) && FXPath::match(name,pattern,matchmode))) continue; // Find it, and take it out from the old list if found for(FXDirItem** pp=po; (olditem=*pp)!=NULL; pp=&olditem->link){ if(compare(olditem->label,name)==0){ *pp=olditem->link; olditem->link=NULL; break; } } // Use a new item if forced, if there was no old item, or if the item information was changed if(force || !olditem || olditem->getDate()!=info.modified() || olditem->getSize()!=info.size() || olditem->getMode()!=mode){ // Make new item newitem=(FXDirItem*)createItem(name,NULL,NULL,NULL); // Update item information newitem->setDraggable(draggable); newitem->setAssoc(NULL); newitem->setSize(info.size()); newitem->setDate(info.modified()); newitem->setMode(mode); // Determine icons and type if(newitem->isDirectory()){ newitem->setHasItems(true); newitem->setOpenIcon(opendiricon); newitem->setClosedIcon(closeddiricon); if(associations) newitem->setAssoc(associations->findDirBinding(pathname)); } else if(newitem->isExecutable()){ newitem->setHasItems(false); newitem->setOpenIcon(applicationicon); newitem->setClosedIcon(applicationicon); if(associations) newitem->setAssoc(associations->findExecBinding(pathname)); } else{ newitem->setHasItems(false); newitem->setOpenIcon(documenticon); newitem->setClosedIcon(documenticon); if(associations) newitem->setAssoc(associations->findFileBinding(pathname)); } // If association is found, use it if(newitem->getAssoc()){ if(newitem->getAssoc()->miniicon) newitem->setClosedIcon(newitem->getAssoc()->miniicon); if(newitem->getAssoc()->miniiconopen) newitem->setOpenIcon(newitem->getAssoc()->miniiconopen); } // Create item if(id()) newitem->create(); // Replace existing item or add new one if(olditem){ newitem->list=olditem->list; // Old items list of children setItem(olditem,newitem,notify); } else{ appendItem(par,newitem,notify); } *pn=newitem; pn=&newitem->link; // Hang newitem in list } // No change else{ *pn=olditem; pn=&olditem->link; // Hang olditem in list } } dir.close(); } // Wipe items remaining in list:- they have disappeared!! for(olditem=oldlist; olditem; olditem=link){ link=olditem->link; removeItem(olditem,notify); } // Now we know for sure whether we really have subitems or not par->setHasItems(!!par->first); // Remember new list par->list=newlist; // Update sort order sortChildItems(par); // Need to layout recalc(); }
// List all the files in directory FXint FXDir::listFiles(FXString*& filelist,const FXString& path,const FXString& pattern,FXuint flags){ FXDir dir(path); // Initialize to empty filelist=NULL; // Get directory stream pointer if(dir.isOpen()){ FXuint mode=(flags&CaseFold)?(FILEMATCH_FILE_NAME|FILEMATCH_NOESCAPE|FILEMATCH_CASEFOLD):(FILEMATCH_FILE_NAME|FILEMATCH_NOESCAPE); FXString *newlist; FXint size=0; FXint count=0; FXString pathname; FXString name; FXStat data; // Loop over directory entries while(dir.next(name)){ // Build full pathname pathname=path; if(!ISPATHSEP(pathname.tail())) pathname+=PATHSEPSTRING; pathname+=name; // Get info on file if(!FXStat::statFile(pathname,data)) continue; #ifdef WIN32 // Filter out files; a bit tricky... if(!data.isDirectory() && ((flags&NoFiles) || (data.isHidden() && !(flags&HiddenFiles)) || (!(flags&AllFiles) && !FXPath::match(pattern,name,mode)))) continue; // Filter out directories; even more tricky! if(data.isDirectory() && ((flags&NoDirs) || (data.isHidden() && !(flags&HiddenDirs)) || ((name[0]=='.' && (name[1]==0 || (name[1]=='.' && name[2]==0))) && (flags&NoParent)) || (!(flags&AllDirs) && !FXPath::match(pattern,name,mode)))) continue; #else // Filter out files; a bit tricky... if(!data.isDirectory() && ((flags&NoFiles) || (name[0]=='.' && !(flags&HiddenFiles)) || (!(flags&AllFiles) && !FXPath::match(pattern,name,mode)))) continue; // Filter out directories; even more tricky! if(data.isDirectory() && ((flags&NoDirs) || (name[0]=='.' && !(flags&HiddenDirs)) || ((name[0]=='.' && (name[1]==0 || (name[1]=='.' && name[2]==0))) && (flags&NoParent)) || (!(flags&AllDirs) && !FXPath::match(pattern,name,mode)))) continue; #endif // Grow list if(count+1>=size){ size=size?(size<<1):256; newlist=new FXString [size]; for(int i=0; i<count; i++){ newlist[i].adopt(filelist[i]); } delete [] filelist; filelist=newlist; } // Add to list filelist[count++].adopt(name); } return count; } return 0; }