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 #ifdef LLDB_CONFIG_SUPPORTS_SETLINEBUFFERED 102 if (m_file != NULL) 103 setlinebuf (m_file); 104 #endif // #ifdef LLDB_CONFIG_SUPPORTS_SETLINEBUFFERED 105 } 106 107 void 108 StreamFile::Flush () 109 { 110 if (m_file) 111 ::fflush (m_file); 112 } 113 114 int 115 StreamFile::Write (const void *s, size_t length) 116 { 117 if (m_file) 118 return ::fwrite (s, 1, length, m_file); 119 return 0; 120 } 121 122 FILE * 123 StreamFile::GetFileHandle() 124 { 125 return m_file; 126 } 127 128 void 129 StreamFile::SetFileHandle (FILE *file, bool close_file) 130 { 131 Close(); 132 m_file = file; 133 m_close_file = close_file; 134 } 135 136 const char * 137 StreamFile::GetFilePathname () 138 { 139 if (m_path_name.size() == 0) 140 return NULL; 141 else 142 return m_path_name.c_str(); 143 } 144