Harmonic Flow Framework (libhffwk)
Cross platform C++ 2D Game Engine Framework
FileDirUtils.cpp
1 /*
2  Harmonic Flow Framework
3  Copyright (C) 2011-2018 Andreas Widen <andreas@harmonicflow.org>
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any damages
7  arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any purpose,
10  including commercial applications, and to alter it and redistribute it
11  freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19  3. This notice may not be removed or altered from any source distribution.
20 */
21 #include "FileDirUtils.h"
22 
23 using namespace HFCore;
24 
25 #include "debug/CrtDbgNew.h"
26 
28 {
29 }
30 
32 {
33 }
34 
35 void FileDirUtils::splitDirFileExt(const std::wstring &path, std::wstring *dir,
36  std::wstring *name, std::wstring *ext)
37 {
38  size_t si = path.rfind(L'/') + 1;
39  if (si == std::wstring::npos)
40  {
41  si = 0;
42  }
43 
44  // also check for Windows \ in path:
45  if (si == 0)
46  {
47  si = path.rfind(L'\\') + 1;
48  if (si == std::wstring::npos)
49  {
50  si = 0;
51  }
52  }
53 
54  size_t di = path.rfind(L'.');
55  if (dir != NULL)
56  {
57  if (si > 0)
58  {
59  *dir = path.substr(0, si);
60  }
61  else
62  {
63  *dir = L"";
64  }
65  }
66 
67  if (name != NULL)
68  {
69  if (di != std::wstring::npos)
70  {
71  *name = path.substr(si, di - si);
72  }
73  else
74  {
75  *name = path.substr(si);
76  }
77  }
78 
79  if (ext != NULL)
80  {
81  if (di != std::wstring::npos)
82  {
83  *ext = path.substr(di + 1);
84  }
85  else
86  {
87  *ext = L"";
88  }
89  }
90 }
91 
92 void FileDirUtils::splitDirFileExt(const std::string &path, std::string *dir,
93  std::string *name, std::string *ext)
94 {
95  size_t si = path.rfind('/') + 1;
96  if (si == std::string::npos)
97  {
98  si = 0;
99  }
100 
101  // also check for Windows \ in path:
102  if (si == 0)
103  {
104  si = path.rfind('\\') + 1;
105  if (si == std::string::npos)
106  {
107  si = 0;
108  }
109  }
110 
111  size_t di = path.rfind('.');
112  if (dir != NULL)
113  {
114  if (si > 0)
115  {
116  *dir = path.substr(0, si);
117  }
118  else
119  {
120  *dir = "";
121  }
122  }
123 
124  if (name != NULL)
125  {
126  if (di != std::string::npos)
127  {
128  *name = path.substr(si, di - si);
129  }
130  else
131  {
132  *name = path.substr(si);
133  }
134  }
135 
136  if (ext != NULL)
137  {
138  if (di != std::string::npos)
139  {
140  *ext = path.substr(di + 1);
141  }
142  else
143  {
144  *ext = "";
145  }
146  }
147 }
148 
149 std::wstring FileDirUtils::getFileExt(std::wstring sFileName)
150 {
151  std::wstring sExt = L"";
152 
153  if (sFileName.empty())
154  return sExt;
155 
156  size_t pos = sFileName.find_last_of(L'.');
157  if (pos != std::wstring::npos)
158  sExt = StringUtil::StringToLower(sFileName.substr(pos + 1));
159 
160  return sExt;
161 }
162 
163 std::string FileDirUtils::getFileExt(std::string sFileName)
164 {
165  std::string sExt = "";
166 
167  if (sFileName.empty())
168  return sExt;
169 
170  size_t pos = sFileName.find_last_of(L'.');
171  if (pos != std::string::npos)
172  sExt = StringUtil::StringToLower(sFileName.substr(pos + 1));
173 
174  return sExt;
175 }
176 
177 bool FileDirUtils::isDirectory(const std::wstring item)
178 {
179  std::wstring sExt = getFileExt(item);
180  if (item.substr(0, 1) == L"[" && item.substr(item.length() - 1, item.length()) == L"]")
181  return true;
182  if (item == L"[.]" || item == L"[..]")
183  return true;
184  else if (sExt.length() == 0)
185  return true;
186 
187  return false;
188 }
189 
190 bool FileDirUtils::isFile(const std::wstring item)
191 {
192  if (item == L"[.]" || item == L"[..]")
193  return false;
194 
195  std::wstring sExt = getFileExt(item);
196  if (sExt.length() > 0)
197  return true;
198 
199  return false;
200 }
201 
202 std::wstring FileDirUtils::extractDriveFromPath(std::wstring sDirectoryPath)
203 {
204  int32_hf iLoopIndex = 0, iStrSize = sDirectoryPath.size();
205  std::wstring sTmpBuffer, sCurByte;
206 
207  for (iLoopIndex = 0; iLoopIndex < iStrSize; iLoopIndex++)
208  {
209  sCurByte = sDirectoryPath.substr(iLoopIndex, 1);
210  if (sCurByte == L"/" || sCurByte == L"\\")
211  {
212  sTmpBuffer = sDirectoryPath.substr(0, iLoopIndex + 1);
213  break;
214  }
215  }
216  return sTmpBuffer;
217 }
218 
219 std::wstring FileDirUtils::extractPreviousDirectoryPath(std::wstring sCurrentDirectoryPath)
220 {
221  std::wstring sTmpBuffer;
222  int32_hf iLoopIndex = 0, iStrSize = sCurrentDirectoryPath.size();
223  int32_hf iCount = 0;
224 
225  //Loop through current directory path and remove last folder
226  for (iLoopIndex = iStrSize - 1; iLoopIndex >= 0; iLoopIndex--)
227  {
228  //Fetch each byte and check for / forwardslash
229  sTmpBuffer = sCurrentDirectoryPath.substr(iLoopIndex, 1);
230  if (sTmpBuffer == L"/" || sTmpBuffer == L"\\")
231  iCount += 1;
232 
233  if (iCount == 2)
234  {
235  //ok, now strip the last folder and return
236  //iLoopIndex + 1 means inlude last forwardslash.
237  sTmpBuffer = sCurrentDirectoryPath.substr(0, iLoopIndex + 1);
238  break; //exit for
239  }
240  }
241  return sTmpBuffer;
242 }
243 
244 std::wstring FileDirUtils::getFilterStr(FilterType filterType)
245 {
246  std::wstring sFilterToUse = L"";
247 
248  if (filterType == IMGTYPE_ALL)
249  sFilterToUse = L"*.png";
250  else if (filterType == IMGTYPE_PNG)
251  sFilterToUse = L"*.png";
252  else if (filterType == IMGTYPE_BMP)
253  sFilterToUse = L"*.bmp";
254  else if (filterType == IMGTYPE_JPEG)
255  sFilterToUse = L"*.jpg";
256  else if (filterType == IMGTYPE_TGA)
257  sFilterToUse = L"*.tga";
258  else if (filterType == SNDTYPE_OGG)
259  sFilterToUse = L"*.ogg";
260  else if (filterType == SNDTYPE_WAV)
261  sFilterToUse = L"*.wav";
262  else if (filterType == SNDTYPE_ALL)
263  sFilterToUse = L"*.ogg"; // start with first
264  else if (filterType == FTYPE_ALL)
265  sFilterToUse = L"*.*";
266 
267  return sFilterToUse;
268 }
virtual bool isFile(const std::wstring item)=0
Determines whether item is a file.
virtual std::wstring extractPreviousDirectoryPath(std::wstring sCurrentDirectoryPath)=0
Extract previous directory path.
virtual bool isDirectory(const std::wstring item)=0
Determines whether item is a directory.
virtual void splitDirFileExt(const std::wstring &path, std::wstring *dir, std::wstring *name, std::wstring *ext)
Splits full file path into: directory, file name and file extension.
int int32_hf
A type definition for int.
Definition: HFDataTypes.h:349
Sound / SoundPlayer format: OGG Vorbis.
Definition: FileDirUtils.h:75
virtual ~FileDirUtils()
Destructor.
Sound / SoundPlayer format: WAV.
Definition: FileDirUtils.h:76
static std::string StringToLower(const std::string &string)
Convert string to lowercase.
Definition: StringUtil.cpp:462
#define NULL
Convenient define for 0.
Definition: HFDataTypes.h:42
Image format: All supported.
Definition: FileDirUtils.h:68
All supported formats.
Definition: FileDirUtils.h:66
Definition: Actor.h:34
virtual std::wstring extractDriveFromPath(std::wstring sDirectoryPath)=0
Extract drive from path (Windows only).
Sound / SoundPlayer format: All supported.
Definition: FileDirUtils.h:74
virtual std::wstring getFileExt(std::wstring sFileName)=0
Get the file extension for given sFileName (example: "ogg").
FilterType
Enum holding file filter types.
Definition: FileDirUtils.h:63
virtual std::wstring getFilterStr(FilterType filterType)=0
Get filter wstring for given FileDirUtils::FilterType enum.
FileDirUtils()
Constructor.
Interface useful for generic directory and file operations.