diff options
Diffstat (limited to 'lib/Output.cc')
-rw-r--r-- | lib/Output.cc | 41 |
1 files changed, 33 insertions, 8 deletions
diff --git a/lib/Output.cc b/lib/Output.cc index b4ed359..912af25 100644 --- a/lib/Output.cc +++ b/lib/Output.cc @@ -21,14 +21,7 @@ #endif Output::Output(String no, int create, int trunc) throw (GeneralException) : - Handle(no.strlen() ? open(no.to_charp(), (create ? O_CREAT : 0) | (trunc ? O_TRUNC : 0) -#ifdef _WIN32 -| O_RDWR | O_BINARY -#endif -#if defined __linux__ || defined __CYGWIN32__ -| O_WRONLY, 00666 -#endif -) : dup(1)), + Handle(no.strlen() ? wrapopen(no.to_charp(), create, trunc) : dup(1)), n(no) { if (GetHandle() < 0) { throw IOGeneral(String(_("Error opening file ")) + no + _(" for writing: ") + strerror(errno)); @@ -44,6 +37,38 @@ Output::Output(String no, int create, int trunc) throw (GeneralException) : date_modif = s.st_mtime; } +int Output::wrapopen(const String & n, int create, int trunc) { +#ifndef _WIN32 + return open(no.to_charp(), (create ? O_CREAT : 0) | + (trunc ? O_TRUNC : 0) | O_WRONLY, 00666); +#else + DWORD dwCreationDisposition; + switch ((create ? 1 : 0) | (trunc ? 2 : 0)) { + case 0: // no creation, no trunc + dwCreationDisposition = OPEN_EXISTING; + break; + case 1: // creation, no trunc if existing + dwCreationDisposition = OPEN_ALWAYS; + break; + case 2: // no creation, trunc of existing file + dwCreationDisposition = TRUNCATE_EXISTING; + break; + case 3: // creation, truc if existing + dwCreationDisposition = CREATE_ALWAYS; + break; + } + hFile = CreateFile( + n.to_charp(), + GENERIC_WRITE, + FILE_SHARE_READ, + 0, + dwCreationDisposition, + FILE_ATTRIBUTE_ARCHIVE, + 0); + return _open_osfhandle((INT_PTR) hFile, O_WRONLY | O_BINARY); +#endif +} + Output::Output(const Output & o) : Handle(o), n(o.n) { } |