00001 /*************************************************************************** 00002 * Copyright (C) 2005-2007 Stefan Schwarzer, Jens Schneider, * 00003 * Matthias Hardt, Guido Madaus * 00004 * * 00005 * Copyright (C) 2007-2008 BerLinux Solutions GbR * 00006 * Stefan Schwarzer & Guido Madaus * 00007 * * 00008 * Copyright (C) 2009-2013 BerLinux Solutions GmbH * 00009 * * 00010 * Authors: * 00011 * Stefan Schwarzer <stefan.schwarzer@diskohq.org>, * 00012 * Matthias Hardt <matthias.hardt@diskohq.org>, * 00013 * Jens Schneider <jens.schneider@diskohq.org>, * 00014 * Guido Madaus <guido.madaus@diskohq.org>, * 00015 * Patrick Helterhoff <patrick.helterhoff@diskohq.org>, * 00016 * René Bählkow <rene.baehlkow@diskohq.org> * 00017 * * 00018 * This library is free software; you can redistribute it and/or * 00019 * modify it under the terms of the GNU Lesser General Public * 00020 * License version 2.1 as published by the Free Software Foundation. * 00021 * * 00022 * This library is distributed in the hope that it will be useful, * 00023 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00024 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00025 * Lesser General Public License for more details. * 00026 * * 00027 * You should have received a copy of the GNU Lesser General Public * 00028 * License along with this library; if not, write to the * 00029 * Free Software Foundation, Inc., * 00030 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * 00031 **************************************************************************/ 00032 00033 #ifndef MMSFILESEARCH_H_ 00034 #define MMSFILESEARCH_H_ 00035 00036 /** 00037 * @file mmsfilesearch.h 00038 * 00039 * Header file for MMSFileSearch class. 00040 * 00041 * @ingroup mmstools 00042 */ 00043 00044 #include "mmstools/mmserror.h" 00045 00046 #include <list> 00047 #include <vector> 00048 00049 extern "C" { 00050 #include <sys/types.h> 00051 #include <dirent.h> 00052 } 00053 00054 MMS_CREATEERROR(MMSFileSearchError); 00055 00056 #define MMSFILESEARCH_DEEPESTDIRENTRY "<DEEPESTDIRENTRY>;" 00057 #define MMSFILESEARCH_DEEPESTDIRENTRY_OF_FILE "<DEEPESTDIRENTRYOFFILE>;" 00058 00059 /** 00060 * Structure containing results of file searches. 00061 */ 00062 typedef struct _mmsfile_entry { 00063 bool isdir; /**< Entry is a directory. */ 00064 string name; /**< Name of file entry. */ 00065 string basename; /**< Basename of file entry (filename excluding directory). */ 00066 string path; /**< Path of file entry (directory excluding filename). */ 00067 } MMSFILE_ENTRY; 00068 00069 /** 00070 * List of file search result entries. 00071 */ 00072 typedef list<MMSFILE_ENTRY *> MMSFILEENTRY_LIST; 00073 00074 /** 00075 * Options to configure file search. 00076 */ 00077 typedef enum { 00078 MMSFILESEARCH_NONE = 0, /**< Use default options. */ 00079 MMSFILESEARCH_DEEPESTDIR, /**< */ 00080 MMSFILESEARCH_DEEPESTDIR_OF_FILE /**< */ 00081 } MMSFILESEARCH_OPTION; 00082 00083 /** 00084 * Helper class to search for files or directories. 00085 * 00086 * This class searches for given files or directories providing 00087 * support for recursive search, case (in)sensitivity and masking. 00088 */ 00089 class MMSFileSearch { 00090 public: 00091 /** 00092 * Constructor. 00093 * 00094 * @param directory Root directory to start the search. 00095 * @param mask Filename mask (i.e. "foo*"). You may specify multiple masks 00096 * separated by colons (i.e. "foo*;*bar"). 00097 * @param recursive Search recursively through subdirectories. 00098 * @param caseinsensitive If true search case insensitive. 00099 * @param getdirs Add directories in search results. 00100 */ 00101 MMSFileSearch(string directory, string mask, bool recursive=true, bool caseinsensitive=false, bool getdirs = false); 00102 00103 /** 00104 * Set #recursive parameter, which enables recursive searching through subdirectories. 00105 * 00106 * @param recursive If set to true, recursive searching is enabled. 00107 */ 00108 void setRecursive(bool recursive); 00109 00110 /** 00111 * Set root directory (#directory) for searching. 00112 * 00113 * @param directory This is the top-level directory for the executed search. 00114 */ 00115 void setDirectory(string directory); 00116 00117 /** 00118 * Set filename mask to search for. 00119 * 00120 * @param mask Filename mask to search for (i.e. foo*.*). 00121 * 00122 * TODO: This method should be renamed to setMask(). 00123 */ 00124 void setString(string mask); 00125 00126 /** 00127 * Configures the search to match results using case sensitivity. 00128 * 00129 * @param caseinsensitive If true, results are matched without case sensitivity. 00130 */ 00131 void setCaseInsensitive(bool caseinsensitive); 00132 00133 /** 00134 * Start searching for files. 00135 * 00136 * @return List of file entries matching your search criteria. 00137 */ 00138 MMSFILEENTRY_LIST execute(); 00139 00140 private: 00141 bool recursive; /**< Search recursively? */ 00142 bool caseinsensitive; /**< Search without using case sensitivity? */ 00143 bool getdirs; /**< Add directories to search results? */ 00144 string directory; /**< Top-level directory to search for. */ 00145 string mask; /**< Filename mask to search for. */ 00146 vector<string> singlemask; /**< Separated filename masks if #mask containes more than one mask. */ 00147 DIR *dirhandle; /**< Internal directory handle used when searching. */ 00148 00149 /** 00150 * Does the filename match the given #mask? 00151 * 00152 * @param entry Filename to check. 00153 * 00154 * @return True if filename matches the given #mask. 00155 */ 00156 bool match(char *entry); 00157 00158 /** 00159 * Helper method to scan a directory for files. 00160 * 00161 * @param result Pointer to file entry list. 00162 * @param dirhandle Directory handle to use for search. 00163 * @param cwd Current working directory. 00164 */ 00165 void scanDir(list<MMSFILE_ENTRY *> *result,DIR *dirhandle, string cwd); 00166 00167 /** 00168 * Split a filemask in multiple submask. 00169 * 00170 * You may specify multiple masks separated by colons. 00171 * This method splits the #mask into a vector of masks (#singlemask). 00172 */ 00173 void separateMask(); 00174 00175 /** 00176 * Options to use for searching. 00177 */ 00178 MMSFILESEARCH_OPTION option; 00179 }; 00180 #endif /*MMSFILESEARCH_H_*/