blob: 9c6c508b49e79127e7c65bec5c1ce8ba5c6077d7 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- 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
17using namespace lldb;
18using namespace lldb_private;
19
20//----------------------------------------------------------------------
21// StreamFile constructor
22//----------------------------------------------------------------------
23StreamFile::StreamFile () :
24 Stream (),
25 m_file (NULL),
26 m_path_name (),
27 m_close_file (false)
28{
29}
30
31StreamFile::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
39StreamFile::StreamFile(FILE *f) :
40 Stream (),
41 m_file(f),
42 m_path_name (),
43 m_close_file(false)
44{
45}
46
47StreamFile::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
56StreamFile::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
66StreamFile::~StreamFile()
67{
68 Close ();
69}
70
71void
72StreamFile::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
80bool
81StreamFile::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
96void
97StreamFile::Flush ()
98{
99 if (m_file)
100 ::fflush (m_file);
101}
102
103int
104StreamFile::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
111FILE *
112StreamFile::GetFileHandle()
113{
114 return m_file;
115}
116
117void
118StreamFile::SetFileHandle (FILE *file, bool close_file)
119{
120 Close();
121 m_file = file;
122 m_close_file = close_file;
123}
124
125const char *
126StreamFile::GetFilePathname ()
127{
128 if (m_path_name.size() == 0)
129 return NULL;
130 else
131 return m_path_name.c_str();
132}