aboutsummaryrefslogtreecommitdiff
path: root/libplatform/io/FileSystem_win32.cpp
blob: c223dfd297a6b01fd848ff1bf6e98fec73942df7 (plain)
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
#include "libplatform/impl.h"

namespace mp4v2 { namespace platform { namespace io {

///////////////////////////////////////////////////////////////////////////////

bool
FileSystem::exists( string path_ )
{
    WIN32_FIND_DATAA buf;
    HANDLE h = FindFirstFileA( path_.c_str(), &buf );
    if( h == INVALID_HANDLE_VALUE )
        return false;
    FindClose( h );
    return true;
}

///////////////////////////////////////////////////////////////////////////////

bool
FileSystem::isDirectory( string path_ )
{
    WIN32_FIND_DATAA buf;
    HANDLE h = FindFirstFileA( path_.c_str(), &buf );
    if( h == INVALID_HANDLE_VALUE )
        return false;
    FindClose( h );
    return (buf.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
}

///////////////////////////////////////////////////////////////////////////////

bool
FileSystem::isFile( string path_ )
{
    WIN32_FIND_DATAA buf;
    HANDLE h = FindFirstFileA( path_.c_str(), &buf );
    if( h == INVALID_HANDLE_VALUE )
        return false;
    FindClose( h );
    return (buf.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0;
}

///////////////////////////////////////////////////////////////////////////////

bool
FileSystem::getFileSize( string path_, File::Size& size_ )
{
    size_ = 0;
    WIN32_FILE_ATTRIBUTE_DATA data;

#ifdef AMBIENT_CHAR_INSTEAD_OF_UTF8
	if( !GetFileAttributesExA( path_.c_str(), GetFileExInfoStandard, (LPVOID)&data ))
        return true;
#else
	wchar_t buffer[MAX_PATH + 1];
    MultiByteToWideChar(CP_UTF8, 0, path_.c_str(), -1, buffer, (int) MAX_PATH);
	if( !GetFileAttributesExW(buffer, GetFileExInfoStandard, (LPVOID)&data ))
        return true;
#endif

    size_ = ((File::Size)data.nFileSizeHigh << 32) | data.nFileSizeLow;
    return false;
}

///////////////////////////////////////////////////////////////////////////////

bool
FileSystem::rename( string from, string to )
{
    if( exists( to )) {
        if( ::remove( to.c_str() ))
            return true;
    }
    return ::rename( from.c_str(), to.c_str() ) != 0;
}

///////////////////////////////////////////////////////////////////////////////

string FileSystem::DIR_SEPARATOR  = "\\";
string FileSystem::PATH_SEPARATOR = ";";

///////////////////////////////////////////////////////////////////////////////

}}} // namespace mp4v2::platform::io