From 3ab6fcee36cb08428581fbff5f5de81699c3e342 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 24 Jul 2006 13:34:26 +0000 Subject: [PATCH] added FindFileInPath() (part of an otherwise rejected patch) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@40266 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/latex/wx/filesys.tex | 28 ++++++++++++++++++++++++++++ include/wx/filesys.h | 3 +++ src/common/filesys.cpp | 31 +++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) diff --git a/docs/latex/wx/filesys.tex b/docs/latex/wx/filesys.tex index 8c8b0f6215..15bfcca767 100644 --- a/docs/latex/wx/filesys.tex +++ b/docs/latex/wx/filesys.tex @@ -26,12 +26,14 @@ provide access to user-defined virtual file systems. \latexignore{\rtfignore{\wxheading{Members}}} + \membersection{wxFileSystem::wxFileSystem}\label{wxfilesystemwxfilesystem} \func{}{wxFileSystem}{\void} Constructor. + \membersection{wxFileSystem::AddHandler}\label{wxfilesystemaddhandler} \func{static void}{AddHandler}{\param{wxFileSystemHandler }{*handler}} @@ -51,6 +53,7 @@ This is because (a) AddHandler is a static method, and (b) the handlers are deleted in wxFileSystem's destructor so that you don't have to care about it. + \membersection{wxFileSystem::ChangePathTo}\label{wxfilesystemchangepathto} \func{void}{ChangePathTo}{\param{const wxString\& }{location}, \param{bool }{is\_dir = false}} @@ -83,12 +86,14 @@ commands change the path to "dir/subdir/": f = fs -> OpenFile("hello.htm"); // opens file 'subdir/folder/hello.htm' !! \end{verbatim} + \membersection{wxFileSystem::GetPath}\label{wxfilesystemgetpath} \func{wxString}{GetPath}{\void} Returns actual path (set by \helpref{ChangePathTo}{wxfilesystemchangepathto}). + \membersection{wxFileSystem::FileNameToURL}\label{wxfilesystemfilenametourl} \func{static wxString}{FileNameToURL}{\param{wxFileName }{filename}} @@ -100,6 +105,26 @@ Converts filename into URL. \helpref{wxFileSystem::URLToFileName}{wxfilesystemurltofilename}, \helpref{wxFileName}{wxfilename} + +\membersection{wxFileSystem::FindFileInPath}\label{wxfilesystemfindfileinpath} + +\func{bool}{FindFileInPath}{\param{wxString }{*str}, \param{const wxChar }{*path}, \param{const wxChar }{*file}} + +Looks for the file with the given name \arg{file} in a colon or semi-colon +(depending on the current platform) separated list of directories in +\arg{path}. If the file is found in any directory, returns \true and the full +path of the file in \arg{str}, otherwise returns \false and doesn't modify +\arg{str}. + +\wxheading{Parameters} + +\docparam{str}{Receives the full path of the file, must not be \NULL} + +\docparam{path}{\texttt{wxPATH\_SEP}-separated list of directories} + +\docparam{file}{the name of the file to look for} + + \membersection{wxFileSystem::FindFirst}\label{wxfilesystemfindfirst} \func{wxString}{FindFirst}{\param{const wxString\& }{wildcard}, \param{int }{flags = 0}} @@ -108,12 +133,14 @@ Works like \helpref{wxFindFirstFile}{wxfindfirstfile}. Returns name of the first filename (within filesystem's current path) that matches {\it wildcard}. {\it flags} may be one of wxFILE (only files), wxDIR (only directories) or 0 (both). + \membersection{wxFileSystem::FindNext}\label{wxfilesystemfindnext} \func{wxString}{FindNext}{\void} Returns the next filename that matches parameters passed to \helpref{FindFirst}{wxfilesystemfindfirst}. + \membersection{wxFileSystem::OpenFile}\label{wxfilesystemopenfile} \func{wxFSFile*}{OpenFile}{\param{const wxString\& }{location}} @@ -124,6 +151,7 @@ or NULL if failed. It first tries to open the file in relative scope absolute path. Note that the user is responsible for deleting the returned wxFSFile. + \membersection{wxFileSystem::URLToFileName}\label{wxfilesystemurltofilename} \func{static wxFileName}{URLToFileName}{\param{const wxString\& }{url}} diff --git a/include/wx/filesys.h b/include/wx/filesys.h index ae51b8d7c4..ff06ee7dbd 100644 --- a/include/wx/filesys.h +++ b/include/wx/filesys.h @@ -181,6 +181,9 @@ public: wxString FindFirst(const wxString& spec, int flags = 0); wxString FindNext(); + // find a file in a list of directories, returns false if not found + bool FindFileInPath(wxString *pStr, const wxChar *path, const wxChar *file); + // Adds FS handler. // In fact, this class is only front-end to the FS handlers :-) static void AddHandler(wxFileSystemHandler *handler); diff --git a/src/common/filesys.cpp b/src/common/filesys.cpp index 65cc39f1c0..61ec083da4 100644 --- a/src/common/filesys.cpp +++ b/src/common/filesys.cpp @@ -26,6 +26,7 @@ #include "wx/module.h" #include "wx/mimetype.h" #include "wx/filename.h" +#include "wx/tokenzr.h" //-------------------------------------------------------------------------------- @@ -447,7 +448,37 @@ wxString wxFileSystem::FindNext() else return m_FindFileHandler -> FindNext(); } +bool wxFileSystem::FindFileInPath(wxString *pStr, + const wxChar *path, + const wxChar *basename) +{ + // we assume that it's not empty + wxCHECK_MSG( !wxIsEmpty(basename), false, + _T("empty file name in wxFileSystem::FindFileInPath")); + // skip path separator in the beginning of the file name if present + if ( wxIsPathSeparator(*basename) ) + basename++; + + wxStringTokenizer tokenizer(path, wxPATH_SEP); + while ( tokenizer.HasMoreTokens() ) + { + wxString strFile = tokenizer.GetNextToken(); + if ( !wxEndsWithPathSeparator(strFile) ) + strFile += wxFILE_SEP_PATH; + strFile += basename; + + wxFSFile *file = OpenFile(strFile); + if ( file ) + { + delete file; + *pStr = strFile; + return true; + } + } + + return false; +} void wxFileSystem::AddHandler(wxFileSystemHandler *handler) {