summaryrefslogtreecommitdiffstats
path: root/lldb/source/Core/StreamFile.cpp
blob: c8e87d91a5abc02fef144c5176bcfb5273a8bb2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//===-- StreamFile.cpp ------------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "lldb/Core/StreamFile.h"
#include <stdio.h>

// C Includes
// C++ Includes
// Other libraries and framework includes
// Project includes

using namespace lldb;
using namespace lldb_private;

//----------------------------------------------------------------------
// StreamFile constructor
//----------------------------------------------------------------------
StreamFile::StreamFile () :
    Stream (),
    m_file (NULL),
    m_close_file (false),
    m_path_name ()
{
}

StreamFile::StreamFile(uint32_t flags, uint32_t addr_size, ByteOrder byte_order, FILE *f) :
    Stream (flags, addr_size, byte_order),
    m_file(f),
    m_close_file(false),
    m_path_name ()
{
}

StreamFile::StreamFile(FILE *f) :
    Stream (),
    m_file(f),
    m_close_file(false),
    m_path_name ()
{
}

StreamFile::StreamFile(uint32_t flags, uint32_t addr_size, ByteOrder byte_order, const char *path, const char *permissions) :
    Stream (flags, addr_size, byte_order),
    m_file (NULL),
    m_close_file(false),
    m_path_name (path)
{
    Open(path, permissions);
}

StreamFile::StreamFile(const char *path, const char *permissions) :
    Stream (),
    m_file (NULL),
    m_close_file(false),
    m_path_name (path)
{
    Open(path, permissions);
}


StreamFile::~StreamFile()
{
    Close ();
}

void
StreamFile::Close ()
{
    if (m_close_file && m_file != NULL)
        ::fclose (m_file);
    m_file = NULL;
    m_close_file = false;
}

bool
StreamFile::Open (const char *path, const char *permissions)
{
    Close();
    if (path && path[0])
    {
        if ((m_path_name.size() == 0)
            || (m_path_name.compare(path) != 0))
            m_path_name = path;
        m_file = ::fopen (path, permissions);
        if (m_file != NULL)
            m_close_file = true;
    }
    return m_file != NULL;
}

void
StreamFile::Flush ()
{
    if (m_file)
        ::fflush (m_file);
}

int
StreamFile::Write (const void *s, size_t length)
{
    if (m_file)
        return ::fwrite (s, 1, length, m_file);
    return 0;
}

FILE *
StreamFile::GetFileHandle()
{
    return m_file;
}

void
StreamFile::SetFileHandle (FILE *file, bool close_file)
{
    Close();
    m_file = file;
    m_close_file = close_file;
}

const char *
StreamFile::GetFilePathname ()
{
    if (m_path_name.size() == 0)
        return NULL;
    else
        return m_path_name.c_str();
}
OpenPOWER on IntegriCloud