pstdio.cxx (2204B)
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 "pport.h" 13 #include "pstreams.h" 14 15 #ifdef WIN32 16 # include <windows.h> 17 #else 18 # include <unistd.h> 19 #endif 20 21 22 namespace ptypes { 23 24 infile pin; 25 logfile pout; 26 logfile perr; 27 outnull pnull; 28 29 30 static class _stdio_init 31 { 32 public: 33 _stdio_init(); 34 } _stdio_init_inst; 35 36 37 #ifdef WIN32 38 39 static HANDLE DuplicateSysHandle(DWORD stdh) 40 { 41 HANDLE hold = GetStdHandle(stdh); 42 HANDLE hnew = 0; 43 DuplicateHandle(GetCurrentProcess(), hold, GetCurrentProcess(), 44 &hnew, 0, true, DUPLICATE_SAME_ACCESS); 45 return hnew; 46 } 47 48 #endif 49 50 51 _stdio_init::_stdio_init() 52 { 53 #ifdef WIN32 54 pin.set_syshandle(int(DuplicateSysHandle(STD_INPUT_HANDLE))); 55 pout.set_syshandle(int(DuplicateSysHandle(STD_OUTPUT_HANDLE))); 56 perr.set_syshandle(int(DuplicateSysHandle(STD_ERROR_HANDLE))); 57 #else 58 pin.set_syshandle(::dup(STDIN_FILENO)); 59 pout.set_syshandle(::dup(STDOUT_FILENO)); 60 perr.set_syshandle(::dup(STDERR_FILENO)); 61 #endif 62 63 pin.set_bufsize(4096); 64 pin.open(); 65 pout.open(); 66 perr.open(); 67 68 pnull.open(); 69 70 // prevent others from freeing these objects, if assigned to a variant. 71 // will need to handle reference counting for static objects better. any ideas? 72 addref(&pin); 73 addref(&pout); 74 addref(&perr); 75 addref(&pnull); 76 77 // this is to show objalloc = 0 at program exit 78 objalloc -= 4; 79 } 80 81 82 // 83 // null output stream 84 // 85 86 87 outnull::outnull() 88 : outstm(0) 89 { 90 } 91 92 93 outnull::~outnull() 94 { 95 close(); 96 } 97 98 99 int outnull::dorawwrite(const char*, int) 100 { 101 return 0; 102 } 103 104 105 void outnull::doopen() 106 { 107 } 108 109 110 void outnull::doclose() 111 { 112 } 113 114 115 string outnull::get_streamname() 116 { 117 return "<null>"; 118 } 119 120 121 // 122 // logfile - file output with thread-safe putf() 123 // 124 125 logfile::logfile(): outfile() 126 { 127 set_bufsize(0); 128 } 129 130 131 logfile::logfile(const char* ifn, bool iappend): outfile(ifn, iappend) 132 { 133 set_bufsize(0); 134 } 135 136 137 logfile::logfile(const string& ifn, bool iappend): outfile(ifn, iappend) 138 { 139 set_bufsize(0); 140 } 141 142 143 logfile::~logfile() 144 { 145 } 146 147 148 int logfile::classid() 149 { 150 return CLASS3_LOGFILE; 151 } 152 153 154 } 155