// returns -1 if not found otherwise the Index __int64 GStringList::Find(const char *pzFindWhat, int bMatchCase/* = 1*/) { GString *p = (GString *)First(); __int64 nIndex = -1; while(p) { nIndex++; if (bMatchCase) { if (p->Compare(pzFindWhat) == 0) { return nIndex; } } else { if (p->CompareNoCase(pzFindWhat) == 0) { return nIndex; } } p = (GString *)Next(); } return -1; // not found }
// removes the first occurance of pzMatch from the list // or every occurance if bRemoveAllOccurances = 1 // if bMatchCase = 1 only ExAcT Case Matches are removed // returns number of items removed. __int64 GStringList::Remove(const char *pzMatch, int bMatchCase, int bRemoveAllOccurances) { GString *p = (GString *)First(); __int64 nItemsRemoved = 0; while(p) { if (bMatchCase) { if (p->Compare(pzMatch) == 0) { nItemsRemoved++; RemoveCurrent(); delete p; if (!bRemoveAllOccurances) break; } } else { if (p->CompareNoCase(pzMatch) == 0) { nItemsRemoved++; RemoveCurrent(); delete p; if (!bRemoveAllOccurances) break; } } p = (GString *)Next(); } return nItemsRemoved; }
void CDirectoryListing::Init(const char *szDirPath, int nMode) { static GString dotdot(".."); static GString dot("."); bool bIncludeSubDirs = (nMode == 2 || nMode == 3) ? 1 : 0; bool bIncludeFiles = (nMode == 1 || nMode == 3) ? 1 : 0; GString strPathWithTrailingSlash(szDirPath); GString strPathWithNoTrailingSlash(szDirPath); // if szDirPath ends with a slash if ( strPathWithNoTrailingSlash.Right(1) == "/" || strPathWithNoTrailingSlash.Right(1) == "\\" ) { // if the path is "/" leave it alone if (strPathWithNoTrailingSlash.Length() > 1) strPathWithNoTrailingSlash = strPathWithNoTrailingSlash.Left(strPathWithNoTrailingSlash.Length() - 1); } else { strPathWithTrailingSlash += "\\"; } if( _access( (const char *)strPathWithNoTrailingSlash, 0 ) ) { throw GenericException("GDirectoryListing",0,(const char *)strPathWithNoTrailingSlash, errno); } // FindFirstFile & FindNextFile HANDLE hFindFile; WIN32_FIND_DATA find; BOOL fRet = TRUE; GString strSearch( strPathWithTrailingSlash ); strSearch += "*.*"; hFindFile = FindFirstFile((const char *)strSearch, &find); while (hFindFile != (HANDLE)-1 && fRet == TRUE) { GString strTemp( strPathWithTrailingSlash ); strTemp += find.cFileName; struct stat sstruct; int result = stat(strTemp, &sstruct); if (result == 0) { if ( !(sstruct.st_mode & _S_IFDIR) ) { // Add the file if (bIncludeFiles) { AddLast((const char *)find.cFileName); } } else if (bIncludeSubDirs) { GString strFileName( LastLeaf( (char *)(const char *)strTemp,'\\') ); if ( ( dotdot.Compare(strFileName) != 0 ) && ( dot.Compare(strFileName) != 0 )) { GString strFormattedDir; strFormattedDir.Format("[dir] %s", LastLeaf( (char *)(const char *)strFileName,'\\') ); AddLast((const char *)strFormattedDir); } } } fRet = FindNextFile(hFindFile, &find); } FindClose(hFindFile); }
void CDirectoryListing::RecurseFolder(const char *pzDir, GStringList *strDirs, GStringList *strFiles) { char chSlash = '\\'; static GString strDot("[dir] ."); static GString strDotDot("[dir] .."); try { // Sample listing 2 files + 1 directory = "file1.txt*[dir] Temp*file2.txt" GString strResults; CDirectoryListing dir(pzDir, 2); // directories only GStringIterator it(&dir); while (it()) { // pzResult will look something like "[dir] SubDir" const char *pzResult = it++; if (strDot.Compare(pzResult) != 0 && strDotDot.Compare(pzResult) != 0) { // pzDir may be "/myPath" to begin with GString strFullPath(pzDir); if ( strFullPath.GetAt(strFullPath.GetLength() - 1) != '\\' && strFullPath.GetAt(strFullPath.GetLength() - 1) != '/') { // pzDir will now end with a slash if it did not already. // like "/myPath/" or "c:\myPath\" strFullPath += chSlash; } // add the file name to the complete path we're building strFullPath += &pzResult[6]; // skip the "[dir] ", add a string like "SubDir" if(strDirs) { strDirs->AddLast(strFullPath); } // now add the final slash for a string like this "/myPath/SubDir/" strFullPath += chSlash; // go down into that directory now. RecurseFolder(strFullPath, strDirs, strFiles); } } if(strFiles) { CDirectoryListing files(pzDir, 1); // files only GStringIterator it2(&files); while (it2()) { // pzDir may be "/myPath" to begin with GString strFullPath(pzDir); if ( strFullPath.GetAt(strFullPath.GetLength() - 1) != '\\' && strFullPath.GetAt(strFullPath.GetLength() - 1) != '/') { // strFullPath will now end with a slash like "/myPath/" strFullPath += chSlash; } const char *pzFile = it2++; strFullPath += pzFile; strFiles->AddLast((const char *)strFullPath); } } } catch( GenericException & ) { // ignore the directory we can't access // rErr.GetDescription(); } }