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_path_name (),
28     m_close_file (false)
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_path_name (),
36     m_close_file(false)
37 {
38 }
39 
40 StreamFile::StreamFile(FILE *f) :
41     Stream (),
42     m_file(f),
43     m_path_name (),
44     m_close_file(false)
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_path_name (path),
52     m_close_file(false)
53 {
54     Open(path, permissions);
55 }
56 
57 StreamFile::StreamFile(const char *path, const char *permissions) :
58     Stream (),
59     m_file (NULL),
60     m_path_name (path),
61     m_close_file(false)
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::Flush ()
99 {
100     if (m_file)
101         ::fflush (m_file);
102 }
103 
104 int
105 StreamFile::Write (const void *s, size_t length)
106 {
107     if (m_file)
108         return ::fwrite (s, 1, length, m_file);
109     return 0;
110 }
111 
112 FILE *
113 StreamFile::GetFileHandle()
114 {
115     return m_file;
116 }
117 
118 void
119 StreamFile::SetFileHandle (FILE *file, bool close_file)
120 {
121     Close();
122     m_file = file;
123     m_close_file = close_file;
124 }
125 
126 const char *
127 StreamFile::GetFilePathname ()
128 {
129     if (m_path_name.size() == 0)
130         return NULL;
131     else
132         return m_path_name.c_str();
133 }
134