First commit
[anna.git] / include / anna / io / Directory.hpp
1 // ANNA - Anna is Not 'N' Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // https://bitbucket.org/testillano/anna
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
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
16 // distribution.
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.
20 //
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.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 #ifndef anna_io_Directory_hpp
38 #define anna_io_Directory_hpp
39
40 #include <sys/types.h>
41 #include <regex.h>
42
43 #include <vector>
44 #include <string>
45
46 #include <anna/core/RuntimeException.hpp>
47
48 namespace anna {
49
50 namespace io  {
51
52
53 /**
54    Abstraccion de un directorio de archivos sobre UNIX.
55
56    Permite recorrer el contenido de un directorio de archivos mediante iteradores.
57
58    Ejemplo de uso:
59
60    \code
61
62    #include <anna/io/io.hpp>
63
64    void f ()
65       throw (anna::RuntimeException)
66    {
67       Directory dir;
68
69       dir.read (".", Directory::Mode::ShortPath);
70
71       for (Directory::const_iterator ii = dir.begin (), maxii = dir.end (); ii != maxii; ii ++) {
72          const std::string& entry = Directory::data (ii);
73
74          .... procesa la entrada ...
75       }
76    }
77
78    \endcode
79 */
80 class Directory {
81 public:
82   /**
83      Formas de obtener los archivos de un directorio.
84   */
85   struct Mode {
86     enum _v {
87       ShortPath = 0, /**< Retorna solo el nombre del archivo. */
88       FullPath /**<  Retorna el nombre completo del archivo. */
89     };
90   };
91
92   typedef std::vector <std::string> Files;
93   typedef Files::const_iterator const_iterator;
94
95   /**
96      Constructor
97   */
98   Directory();
99
100   /**
101      Destructor.
102   */
103   virtual ~Directory() { clearPattern(); }
104
105   /**
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.
109   */
110   void setPattern(const char* expression) throw(RuntimeException);
111
112   /**
113      Elimina cualquier otro filtro que pudiera haber sido establecido mediante #setPattern.
114   */
115   void clearPattern() throw() { setPattern(NULL); }
116
117   /**
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.
121   */
122   void read(const char* dirName, const Mode::_v mode) throw(RuntimeException);
123
124   /**
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.
128   */
129   void read(const std::string& dirName,  const Mode::_v mode) throw(RuntimeException)  { read(dirName.c_str(), mode); }
130
131   /**
132      Devuelve un iterador a la primera entrada del directorio.
133      \return Un iterador a la primera entrada del directorio.
134   */
135   const_iterator begin() const throw() { return a_files.begin(); }
136
137   /**
138      Devuelve un iterador a la ultima entrada del directorio.
139      \return Un iterador a la utlima entrada del directorio.
140   */
141   const_iterator end() const throw() { return a_files.end(); }
142
143   /**
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.
146   */
147   const_iterator find(const std::string& file) const throw();
148
149   /**
150      Devuelve la entrada referenciado por el iterador recibido.
151      \return  La entrada referenciado por el iterador recibido.
152   */
153   static const std::string& data(const_iterator& ii) throw() { return *ii; }
154
155 private:
156   regex_t a_regex;
157   bool a_hasPattern;
158   Files a_files;
159 };
160
161 }
162 }
163
164 #endif