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 12 // C Includes 13 // C++ Includes 14 // Other libraries and framework includes 15 // Project includes 16 17 using namespace lldb; 18 using namespace lldb_private; 19 20 //---------------------------------------------------------------------- 21 // StreamFile constructor 22 //---------------------------------------------------------------------- 23 StreamFile::StreamFile () : 24 Stream (), 25 m_file (NULL), 26 m_path_name (), 27 m_close_file (false) 28 { 29 } 30 31 StreamFile::StreamFile(uint32_t flags, uint32_t addr_size, ByteOrder byte_order, FILE *f) : 32 Stream (flags, addr_size, byte_order), 33 m_file(f), 34 m_path_name (), 35 m_close_file(false) 36 { 37 } 38 39 StreamFile::StreamFile(FILE *f) : 40 Stream (), 41 m_file(f), 42 m_path_name (), 43 m_close_file(false) 44 { 45 } 46 47 StreamFile::StreamFile(uint32_t flags, uint32_t addr_size, ByteOrder byte_order, const char *path, const char *permissions) : 48 Stream (flags, addr_size, byte_order), 49 m_file (NULL), 50 m_path_name (path), 51 m_close_file(false) 52 { 53 Open(path, permissions); 54 } 55 56 StreamFile::StreamFile(const char *path, const char *permissions) : 57 Stream (), 58 m_file (NULL), 59 m_path_name (path), 60 m_close_file(false) 61 { 62 Open(path, permissions); 63 } 64 65 66 StreamFile::~StreamFile() 67 { 68 Close (); 69 } 70 71 void 72 StreamFile::Close () 73 { 74 if (m_close_file && m_file != NULL) 75 ::fclose (m_file); 76 m_file = NULL; 77 m_close_file = false; 78 } 79 80 bool 81 StreamFile::Open (const char *path, const char *permissions) 82 { 83 Close(); 84 if (path && path[0]) 85 { 86 if ((m_path_name.size() == 0) 87 || (m_path_name.compare(path) != 0)) 88 m_path_name = path; 89 m_file = ::fopen (path, permissions); 90 if (m_file != NULL) 91 m_close_file = true; 92 } 93 return m_file != NULL; 94 } 95 96 void 97 StreamFile::Flush () 98 { 99 if (m_file) 100 ::fflush (m_file); 101 } 102 103 int 104 StreamFile::Write (const void *s, size_t length) 105 { 106 if (m_file) 107 return ::fwrite (s, 1, length, m_file); 108 return 0; 109 } 110 111 FILE * 112 StreamFile::GetFileHandle() 113 { 114 return m_file; 115 } 116 117 void 118 StreamFile::SetFileHandle (FILE *file, bool close_file) 119 { 120 Close(); 121 m_file = file; 122 m_close_file = close_file; 123 } 124 125 const char * 126 StreamFile::GetFilePathname () 127 { 128 if (m_path_name.size() == 0) 129 return NULL; 130 else 131 return m_path_name.c_str(); 132 } 133