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