]> git.lizzy.rs Git - irrlicht.git/blob - source/Irrlicht/CFileList.cpp
15d5410e1494b9d049a5561eb7f6402f4c589660
[irrlicht.git] / source / Irrlicht / CFileList.cpp
1 // Copyright (C) 2002-2012 Nikolaus Gebhardt\r
2 // This file is part of the "Irrlicht Engine".\r
3 // For conditions of distribution and use, see copyright notice in irrlicht.h\r
4 \r
5 #include "CFileList.h"\r
6 #include "IrrCompileConfig.h"\r
7 #include "irrArray.h"\r
8 #include "coreutil.h"\r
9 \r
10 #include "os.h"\r
11 \r
12 namespace irr\r
13 {\r
14 namespace io\r
15 {\r
16 \r
17 static const io::path emptyFileListEntry;\r
18 \r
19 CFileList::CFileList(const io::path& path, bool ignoreCase, bool ignorePaths)\r
20  : IgnorePaths(ignorePaths), IgnoreCase(ignoreCase), Path(path)\r
21 {\r
22         #ifdef _DEBUG\r
23         setDebugName("CFileList");\r
24         #endif\r
25 \r
26         Path.replace('\\', '/');\r
27 }\r
28 \r
29 CFileList::~CFileList()\r
30 {\r
31         Files.clear();\r
32 }\r
33 \r
34 u32 CFileList::getFileCount() const\r
35 {\r
36         return Files.size();\r
37 }\r
38 \r
39 void CFileList::sort()\r
40 {\r
41         Files.sort();\r
42 }\r
43 \r
44 const io::path& CFileList::getFileName(u32 index) const\r
45 {\r
46         if (index >= Files.size())\r
47                 return emptyFileListEntry;\r
48 \r
49         return Files[index].Name;\r
50 }\r
51 \r
52 \r
53 //! Gets the full name of a file in the list, path included, based on an index.\r
54 const io::path& CFileList::getFullFileName(u32 index) const\r
55 {\r
56         if (index >= Files.size())\r
57                 return emptyFileListEntry;\r
58 \r
59         return Files[index].FullName;\r
60 }\r
61 \r
62 //! adds a file or folder\r
63 u32 CFileList::addItem(const io::path& fullPath, u32 offset, u32 size, bool isDirectory, u32 id)\r
64 {\r
65         SFileListEntry entry;\r
66         entry.ID   = id ? id : Files.size();\r
67         entry.Offset = offset;\r
68         entry.Size = size;\r
69         entry.Name = fullPath;\r
70         entry.Name.replace('\\', '/');\r
71         entry.IsDirectory = isDirectory;\r
72 \r
73         // remove trailing slash\r
74         if (entry.Name.lastChar() == '/')\r
75         {\r
76                 entry.IsDirectory = true;\r
77                 entry.Name[entry.Name.size()-1] = 0;\r
78                 entry.Name.validate();\r
79         }\r
80 \r
81         if (IgnoreCase)\r
82                 entry.Name.make_lower();\r
83 \r
84         entry.FullName = entry.Name;\r
85 \r
86         core::deletePathFromFilename(entry.Name);\r
87 \r
88         if (IgnorePaths)\r
89                 entry.FullName = entry.Name;\r
90 \r
91         //os::Printer::log(Path.c_str(), entry.FullName);\r
92 \r
93         Files.push_back(entry);\r
94 \r
95         return Files.size() - 1;\r
96 }\r
97 \r
98 //! Returns the ID of a file in the file list, based on an index.\r
99 u32 CFileList::getID(u32 index) const\r
100 {\r
101         return index < Files.size() ? Files[index].ID : 0;\r
102 }\r
103 \r
104 bool CFileList::isDirectory(u32 index) const\r
105 {\r
106         bool ret = false;\r
107         if (index < Files.size())\r
108                 ret = Files[index].IsDirectory;\r
109 \r
110         return ret;\r
111 }\r
112 \r
113 //! Returns the size of a file\r
114 u32 CFileList::getFileSize(u32 index) const\r
115 {\r
116         return index < Files.size() ? Files[index].Size : 0;\r
117 }\r
118 \r
119 u32 CFileList::getFileOffset(u32 index) const\r
120 {\r
121         return index < Files.size() ? Files[index].Offset : 0;\r
122 }\r
123 \r
124 \r
125 //! Searches for a file or folder within the list, returns the index\r
126 s32 CFileList::findFile(const io::path& filename, bool isDirectory = false) const\r
127 {\r
128         SFileListEntry entry;\r
129         // we only need FullName to be set for the search\r
130         entry.FullName = filename;\r
131         entry.IsDirectory = isDirectory;\r
132 \r
133         // exchange\r
134         entry.FullName.replace('\\', '/');\r
135 \r
136         // remove trailing slash\r
137         if (entry.FullName.lastChar() == '/')\r
138         {\r
139                 entry.IsDirectory = true;\r
140                 entry.FullName[entry.FullName.size()-1] = 0;\r
141                 entry.FullName.validate();\r
142         }\r
143 \r
144         if (IgnoreCase)\r
145                 entry.FullName.make_lower();\r
146 \r
147         if (IgnorePaths)\r
148                 core::deletePathFromFilename(entry.FullName);\r
149 \r
150         return Files.binary_search(entry);\r
151 }\r
152 \r
153 \r
154 //! Returns the base path of the file list\r
155 const io::path& CFileList::getPath() const\r
156 {\r
157         return Path;\r
158 }\r
159 \r
160 \r
161 } // end namespace irr\r
162 } // end namespace io\r
163 \r