poutfile.cxx (2025B)
1 /* 2 * 3 * C++ Portable Types Library (PTypes) 4 * Version 2.1.1 Released 27-Jun-2007 5 * 6 * Copyright (C) 2001-2007 Hovik Melikyan 7 * 8 * http://www.melikyan.com/ptypes/ 9 * 10 */ 11 12 #include "errno.h" 13 14 #ifdef WIN32 15 # include <windows.h> 16 #else 17 # include <fcntl.h> 18 # include <unistd.h> 19 #endif 20 21 #include "pstreams.h" 22 23 24 namespace ptypes { 25 26 27 // *BSD hack 28 #ifndef O_LARGEFILE 29 # define O_LARGEFILE 0 30 #endif 31 32 33 outfile::outfile() 34 : outstm(), filename(), syshandle(invhandle), peerhandle(invhandle), 35 umode(0644), append(false) {} 36 37 38 outfile::outfile(const char* ifn, bool iappend) 39 : outstm(), filename(ifn), syshandle(invhandle), peerhandle(invhandle), 40 umode(0644), append(iappend) {} 41 42 43 outfile::outfile(string const& ifn, bool iappend) 44 : outstm(), filename(ifn), syshandle(invhandle), peerhandle(invhandle), 45 umode(0644), append(iappend) {} 46 47 48 outfile::~outfile() 49 { 50 close(); 51 } 52 53 54 int outfile::classid() 55 { 56 return CLASS2_OUTFILE; 57 } 58 59 60 string outfile::get_streamname() 61 { 62 return filename; 63 } 64 65 66 void outfile::doopen() 67 { 68 if (syshandle != invhandle) 69 handle = syshandle; 70 else 71 { 72 #ifdef WIN32 73 SECURITY_ATTRIBUTES sa; 74 sa.nLength = sizeof(SECURITY_ATTRIBUTES); 75 sa.lpSecurityDescriptor = NULL; 76 sa.bInheritHandle = TRUE; 77 78 handle = int(CreateFileA(filename, GENERIC_WRITE, 79 FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, 80 (append ? OPEN_ALWAYS : CREATE_ALWAYS), 0, 0)); 81 #else 82 handle = ::open(filename, 83 O_WRONLY | O_CREAT | O_LARGEFILE | (append ? 0 : O_TRUNC), umode); 84 #endif 85 if (handle == invhandle) 86 error(uerrno(), "Couldn't open"); 87 if (append) 88 if (doseek(0, IO_END) == -1) 89 error(uerrno(), "Couldn't seek to end of file"); 90 } 91 } 92 93 94 void outfile::flush() 95 { 96 outstm::flush(); 97 #ifdef WIN32 98 FlushFileBuffers(HANDLE(handle)); 99 #endif 100 } 101 102 103 void outfile::doclose() 104 { 105 outstm::doclose(); 106 syshandle = invhandle; 107 peerhandle = invhandle; 108 } 109 110 111 }