1 //===-- StreamFile.cpp ------------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "lldb/Core/StreamFile.h" 11 #include "lldb/Host/Config.h" 12 #include <stdio.h> 13 14 // C Includes 15 // C++ Includes 16 // Other libraries and framework includes 17 // Project includes 18 19 using namespace lldb; 20 using namespace lldb_private; 21 22 //---------------------------------------------------------------------- 23 // StreamFile constructor 24 //---------------------------------------------------------------------- 25 StreamFile::StreamFile () : 26 Stream (), 27 m_file (NULL), 28 m_close_file (false), 29 m_path_name () 30 { 31 } 32 33 StreamFile::StreamFile(uint32_t flags, uint32_t addr_size, ByteOrder byte_order, FILE *f) : 34 Stream (flags, addr_size, byte_order), 35 m_file (f), 36 m_close_file (false), 37 m_path_name () 38 { 39 } 40 41 StreamFile::StreamFile(FILE *f, bool tranfer_ownership) : 42 Stream (), 43 m_file (f), 44 m_close_file (tranfer_ownership), 45 m_path_name () 46 { 47 } 48 49 StreamFile::StreamFile(uint32_t flags, uint32_t addr_size, ByteOrder byte_order, const char *path, const char *permissions) : 50 Stream (flags, addr_size, byte_order), 51 m_file (NULL), 52 m_close_file(false), 53 m_path_name (path) 54 { 55 Open(path, permissions); 56 } 57 58 StreamFile::StreamFile(const char *path, const char *permissions) : 59 Stream (), 60 m_file (NULL), 61 m_close_file(false), 62 m_path_name (path) 63 { 64 Open(path, permissions); 65 } 66 67 68 StreamFile::~StreamFile() 69 { 70 Close (); 71 } 72 73 void 74 StreamFile::Close () 75 { 76 if (m_close_file && m_file != NULL) 77 ::fclose (m_file); 78 m_file = NULL; 79 m_close_file = false; 80 } 81 82 bool 83 StreamFile::Open (const char *path, const char *permissions) 84 { 85 Close(); 86 if (path && path[0]) 87 { 88 if ((m_path_name.size() == 0) 89 || (m_path_name.compare(path) != 0)) 90 m_path_name = path; 91 m_file = ::fopen (path, permissions); 92 if (m_file != NULL) 93 m_close_file = true; 94 } 95 return m_file != NULL; 96 } 97 98 void 99 StreamFile::SetLineBuffered () 100 { 101 // TODO: check if we can get rid of this LLDB_CONFIG if we do a: 102 // setvbuf(m_file, (char *)NULL, _IOLBF, 0); 103 #ifdef LLDB_CONFIG_SUPPORTS_SETLINEBUFFERED 104 if (m_file != NULL) 105 setlinebuf (m_file); 106 #endif // #ifdef LLDB_CONFIG_SUPPORTS_SETLINEBUFFERED 107 } 108 109 void 110 StreamFile::Flush () 111 { 112 if (m_file) 113 ::fflush (m_file); 114 } 115 116 int 117 StreamFile::Write (const void *s, size_t length) 118 { 119 if (m_file) 120 return ::fwrite (s, 1, length, m_file); 121 return 0; 122 } 123 124 FILE * 125 StreamFile::GetFileHandle() 126 { 127 return m_file; 128 } 129 130 void 131 StreamFile::SetFileHandle (FILE *file, bool close_file) 132 { 133 Close(); 134 m_file = file; 135 m_close_file = close_file; 136 } 137 138 const char * 139 StreamFile::GetFilePathname () 140 { 141 if (m_path_name.size() == 0) 142 return NULL; 143 else 144 return m_path_name.c_str(); 145 } 146