]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/MUON/src/DDL/FileList.hpp
This commit was generated by cvs2svn to compensate for changes in r11742,
[u/mrichter/AliRoot.git] / HLT / MUON / src / DDL / FileList.hpp
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Author: Artur Szostak
4 // Email:  artur@alice.phy.uct.ac.za | artursz@iafrica.com
5 //
6 // Author: Gareth de Vaux
7 // Email:  devaux@lhc.phy.uct.ac.za | dhlt@lordcow.org
8 //
9 ////////////////////////////////////////////////////////////////////////////////
10
11 #ifndef dHLT_DDL_FILE_LIST_HPP
12 #define dHLT_DDL_FILE_LIST_HPP
13
14 #include "Error.hpp"
15 #include "BasicTypes.hpp"
16
17 #include <vector>
18
19 namespace dHLT
20 {
21 namespace DDL
22 {
23
24
25 enum
26 {
27         PATH_NOT_FOUND = 0x10020001
28 };
29
30
31 class PathNotFound : public Error
32 {
33 public:
34         PathNotFound(const char* path) throw (OutOfMemory);
35         virtual ~PathNotFound() throw ();
36
37         virtual const char* Message() const throw();
38         virtual Int ErrorCode() const throw();
39         
40 private:
41
42         char* message;
43 };
44
45
46 class FileList
47 {
48 public:
49
50         FileList();
51         
52         /* Creates a new file list and initialises it by adding the specified path.
53          */
54         FileList(const char* path, const bool recursive = true);
55         
56         ~FileList();
57         
58         /* Returns true if the file list is empty.
59          */
60         bool Empty() const;
61         
62         /* Returns the number of files in the list.
63          */
64         UInt Count() const;
65
66         /* If path contains a valid directory then all files in that directory are
67            added to the filelist. If recursive is true then all files in the sub
68            directories are also added.
69            If the path contains the name of a specific file then it is assumed the
70            file is a text file containing a list of paths to add to the list.
71            In this case the recursive flag should be applied to all the paths found.
72          */ 
73         void Add(const char* path, const bool recursive = true);
74         
75         /* Checks if the file list contains the specified file name.
76          */
77         bool Contains(const char* filename) const;
78         
79         /* Looks for the specifed file name in the file list and returns the index
80            number to that file. 
81            -1 is returned if the file could not be found.
82          */
83         Int Find(const char* filename) const;
84         
85         /* Returns the index'th file name in the list.
86          */
87         const char* FileName(const UInt index) const;
88         
89         /* Returns the size of the index'th file in the list.
90          */
91         UInt FileSize(const UInt index) const;
92         
93         /* Loads the specified file contents into memory. The calling process must 
94            clean up the memory allocated with a call to delete [] buffer.
95            size is filled with the size of the buffer.
96            True is returned if the file was located in the file list.
97          */
98         bool Fetch(const char* filename, char*& buffer, UInt& size) const;
99         
100         // Same as above but fetch the index'th file.
101         void Fetch(const UInt index, char*& buffer, UInt& size) const;
102         
103         /* Loads the file contents just like above, however the buffer must be
104            preallocated by the caller. Only 'size' number of bytes are read from
105            the file and filled into the buffer. If the file is smaller than the
106            buffer size then the remaining bytes are left untouched.
107          */
108         bool Fetch(const char* filename, const UInt size, char* buffer) const;
109         
110         // Same as above but fetch the index'th file.
111         void Fetch(const UInt index, const UInt size, char* buffer) const;
112         
113         /* Empties the file list.
114          */
115         void Clear();
116
117 private:
118
119         void AddFile(const char* filename);  // Called by Add & constructor
120
121         void AddDir(const char* path, const bool recursive = true);
122
123         UInt ParseLine(const UInt currentStart, char* buffer, const UInt end); 
124
125         std::vector<char*> list;
126 };
127
128
129
130 } // DDL
131 } // dHLT
132
133 #endif // dHLT_DDL_FILE_LIST_HPP