OpenGothic
Open source reimplementation of Gothic I and II
Loading...
Searching...
No Matches
fileutil.cpp
Go to the documentation of this file.
1#include "fileutil.h"
2
3#include <Tempest/Platform>
4#include <Tempest/TextCodec>
5
6#ifdef __WINDOWS__
7#include <windows.h>
8#include <shlwapi.h>
9#else
10#include <sys/stat.h>
11#endif
12
13using namespace Tempest;
14
15bool FileUtil::exists(const std::u16string &path) {
16#ifdef __WINDOWS__
17 return PathFileExistsW(reinterpret_cast<const WCHAR*>(path.c_str()));
18#else
19 std::string p=Tempest::TextCodec::toUtf8(path);
20 struct stat buffer={};
21 return stat(p.c_str(),&buffer)==0;
22#endif
23 }
24
25std::u16string FileUtil::caseInsensitiveSegment(std::u16string_view pathv,const char16_t* segment,Dir::FileType type) {
26 auto path = std::u16string(pathv);
27 std::u16string next = path+segment;
28 if(FileUtil::exists(next)) {
29 if(type==Dir::FT_File)
30 return next;
31 return next+u"/";
32 }
33
34 Dir::scan(path,[&path,&next,&segment,type](const std::u16string& p, Dir::FileType t){
35 if(t!=type)
36 return;
37 for(size_t i=0;;++i) {
38 char16_t cs = segment[i];
39 char16_t cp = p[i];
40 if('A'<=cs && cs<='Z')
41 cs = char16_t(cs-'A'+'a');
42 if('A'<=cp && cp<='Z')
43 cp = char16_t(cp-'A'+'a');
44
45 if(cs!=cp)
46 return;
47 if(cs=='\0')
48 break;
49 }
50
51 next = path+p;
52 });
53 if(type==Dir::FT_File)
54 return next;
55 return next+u"/";
56 }
57
58std::u16string FileUtil::nestedPath(std::u16string_view gpath, const std::initializer_list<const char16_t*> &name, Dir::FileType type) {
59 std::u16string path = std::u16string(gpath);
60 for(auto& segment:name)
61 path = caseInsensitiveSegment(path,segment, (segment==*(name.end()-1)) ? type : Dir::FT_Dir);
62 return path;
63 }
bool exists(const std::u16string &path)
Definition fileutil.cpp:15
std::u16string caseInsensitiveSegment(std::u16string_view path, const char16_t *segment, Tempest::Dir::FileType type)
std::u16string nestedPath(std::u16string_view gpath, const std::initializer_list< const char16_t * > &name, Tempest::Dir::FileType type)