1 // ANNA - Anna is Not 'N' Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // https://bitbucket.org/testillano/anna
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
17 // * Neither the name of Google Inc. nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 // Authors: eduardo.ramos.testillano@gmail.com
34 // cisco.tierra@gmail.com
37 #ifndef anna_io_Directory_hpp
38 #define anna_io_Directory_hpp
40 #include <sys/types.h>
46 #include <anna/core/RuntimeException.hpp>
54 Abstraccion de un directorio de archivos sobre UNIX.
56 Permite recorrer el contenido de un directorio de archivos mediante iteradores.
62 #include <anna/io/io.hpp>
65 throw (anna::RuntimeException)
69 dir.read (".", Directory::Mode::ShortPath);
71 for (Directory::const_iterator ii = dir.begin (), maxii = dir.end (); ii != maxii; ii ++) {
72 const std::string& entry = Directory::data (ii);
74 .... procesa la entrada ...
83 Formas de obtener los archivos de un directorio.
87 ShortPath = 0, /**< Retorna solo el nombre del archivo. */
88 FullPath /**< Retorna el nombre completo del archivo. */
92 typedef std::vector <std::string> Files;
93 typedef Files::const_iterator const_iterator;
103 virtual ~Directory() { clearPattern(); }
106 Establece un filtro con la expresion regular que deben cumplir los archivos de este directorio.
107 \param expression Expresion regular que deben cumplir los archivos.
108 \warning Este metodo debe ser invocado antes de #read.
110 void setPattern(const char* expression) throw(RuntimeException);
113 Elimina cualquier otro filtro que pudiera haber sido establecido mediante #setPattern.
115 void clearPattern() throw() { setPattern(NULL); }
118 Lee las entradas del directorio indicado como parametro.
119 \param dirName Ruta al nombre de directorio.
120 \param mode Forma de obtener el nombre del los archivos.
122 void read(const char* dirName, const Mode::_v mode) throw(RuntimeException);
125 Lee las entradas del directorio indicado como parametro.
126 \param dirName Ruta al nombre de directorio.
127 \param mode Forma de obtener el nombre del los archivos.
129 void read(const std::string& dirName, const Mode::_v mode) throw(RuntimeException) { read(dirName.c_str(), mode); }
132 Devuelve un iterador a la primera entrada del directorio.
133 \return Un iterador a la primera entrada del directorio.
135 const_iterator begin() const throw() { return a_files.begin(); }
138 Devuelve un iterador a la ultima entrada del directorio.
139 \return Un iterador a la utlima entrada del directorio.
141 const_iterator end() const throw() { return a_files.end(); }
144 Devuelve un iterador a la entrada del directorio que corresponde con el nombre indicado.
145 \return un iterador a la entrada del directorio que corresponde con el nombre indicado.
147 const_iterator find(const std::string& file) const throw();
150 Devuelve la entrada referenciado por el iterador recibido.
151 \return La entrada referenciado por el iterador recibido.
153 static const std::string& data(const_iterator& ii) throw() { return *ii; }