]> git.tdb.fi Git - libs/core.git/blob - source/io/windows/file.cpp
Move most platform-specific code into overlay directories
[libs/core.git] / source / io / windows / file.cpp
1 #include <msp/core/systemerror.h>
2 #include <msp/strings/format.h>
3 #include "file.h"
4 #include "handle_private.h"
5
6 using namespace std;
7
8 namespace Msp {
9 namespace IO {
10
11 void File::platform_init(const string &fn, CreateMode cm)
12 {
13         int flags = 0;
14         int share_flags = 0;
15         int create_flags = OPEN_EXISTING;
16
17         if(mode&M_READ)
18                 flags |= GENERIC_READ;
19
20         if(mode&M_WRITE)
21         {
22                 flags |= GENERIC_WRITE;
23
24                 switch(static_cast<int>(cm))
25                 {
26                 case C_NONE:     create_flags = OPEN_EXISTING; break;
27                 case C_CREATE:   create_flags = OPEN_ALWAYS; break;
28                 case C_TRUNCATE: create_flags = TRUNCATE_EXISTING; break;
29                 case C_OVERWRITE: create_flags = CREATE_ALWAYS; break;
30                 case C_NEW:      create_flags = CREATE_NEW; break;
31                 }
32         }
33         else
34                 share_flags = FILE_SHARE_READ;
35
36         *handle = CreateFile(fn.c_str(), flags, share_flags, 0, create_flags, FILE_ATTRIBUTE_NORMAL, 0);
37         if(!handle)
38         {
39                 int err = GetLastError();
40                 if(err==ERROR_FILE_NOT_FOUND)
41                         throw file_not_found(fn);
42                 else if(err==ERROR_FILE_EXISTS)
43                         throw file_already_exists(fn);
44                 else
45                         throw system_error(format("CreateFile(%s)", fn), err);
46         }
47 }
48
49 void File::sync()
50 {
51 }
52
53 } // namespace IO
54 } // namespace Msp