forked from youlanhai/encodeconv
/
Filetool.cpp
101 lines (76 loc) · 2.32 KB
/
Filetool.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "StdAfx.h"
#include "Filetool.h"
#include <cassert>
#include <algorithm>
#include <iostream>
#include <Windows.h>
namespace Lazy
{
FILE* openFile(const std::wstring & fname, const std::wstring & mode)
{
FILE *pFile;
errno_t eno = _wfopen_s(&pFile, fname.c_str(), mode.c_str());
if(eno == 0) return pFile;
#ifdef LAZY_DEBUG_MSG
std::wcout<<L"error: open file '"<<fname <<L"' failed! code:"<<eno <<std::endl;
#endif
return NULL;
}
long seekFileLength(FILE *pFile)
{
assert(pFile && "seekFileLength");
long cur = ftell(pFile);
fseek(pFile, 0, SEEK_END);
long length = ftell(pFile) - cur;
fseek(pFile, cur, SEEK_SET);
return length;
}
bool isdir(const std::wstring & fname)
{
DWORD attr = ::GetFileAttributes(fname.c_str());
if(attr == INVALID_FILE_ATTRIBUTES) return false;
return 0 != (attr & FILE_ATTRIBUTE_DIRECTORY);
}
bool isfile(const std::wstring & fname)
{
DWORD attr = ::GetFileAttributes(fname.c_str());
if(attr == INVALID_FILE_ATTRIBUTES) return false;
return 0 != (attr & FILE_ATTRIBUTE_ARCHIVE);
}
bool mkdir(const std::wstring & path)
{
return !!::CreateDirectory(path.c_str(), NULL);
}
bool listdir(StringArray & out, const std::wstring & path)
{
out.clear();
std::wstring fname = path;
formatPath(fname);
fname += L'*';
WIN32_FIND_DATA findData;
HANDLE hFind = ::FindFirstFile(fname.c_str(), &findData);
if(hFind == INVALID_HANDLE_VALUE) return false;
do
{
const std::wstring & str = findData.cFileName;
if(str != L"." && str != L"..")
{
out.push_back(str);
}
}while(::FindNextFile(hFind, &findData));
::FindClose(hFind);
return true;
}
void formatPath(std::wstring & fname)
{
if(fname.empty()) return;
std::replace(fname.begin(), fname.end(), L'\\', L'/');
if(fname.back() != L'/') fname += L'/';
}
std::wstring getFileExt(const std::wstring & fname)
{
size_t pos = fname.find_last_of(L'.');
if(pos != fname.npos) return fname.substr(pos + 1);
return L"";
}
}