2010-06-09 00:52:24 +08:00
|
|
|
//===-- 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"
|
2011-02-05 14:41:57 +08:00
|
|
|
#include "lldb/Host/Config.h"
|
2010-06-09 16:50:27 +08:00
|
|
|
#include <stdio.h>
|
2010-06-09 00:52:24 +08:00
|
|
|
|
|
|
|
// 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),
|
2010-07-10 04:39:50 +08:00
|
|
|
m_close_file (false),
|
|
|
|
m_path_name ()
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
StreamFile::StreamFile(uint32_t flags, uint32_t addr_size, ByteOrder byte_order, FILE *f) :
|
|
|
|
Stream (flags, addr_size, byte_order),
|
2010-09-18 01:42:16 +08:00
|
|
|
m_file (f),
|
|
|
|
m_close_file (false),
|
2010-07-10 04:39:50 +08:00
|
|
|
m_path_name ()
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-09-18 01:42:16 +08:00
|
|
|
StreamFile::StreamFile(FILE *f, bool tranfer_ownership) :
|
2010-06-09 00:52:24 +08:00
|
|
|
Stream (),
|
2010-09-18 01:42:16 +08:00
|
|
|
m_file (f),
|
|
|
|
m_close_file (tranfer_ownership),
|
2010-07-10 04:39:50 +08:00
|
|
|
m_path_name ()
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
2010-07-10 04:39:50 +08:00
|
|
|
m_close_file(false),
|
|
|
|
m_path_name (path)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
|
|
|
Open(path, permissions);
|
|
|
|
}
|
|
|
|
|
|
|
|
StreamFile::StreamFile(const char *path, const char *permissions) :
|
|
|
|
Stream (),
|
|
|
|
m_file (NULL),
|
2010-07-10 04:39:50 +08:00
|
|
|
m_close_file(false),
|
|
|
|
m_path_name (path)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-01-24 12:09:25 +08:00
|
|
|
void
|
|
|
|
StreamFile::SetLineBuffered ()
|
|
|
|
{
|
2011-02-08 13:19:06 +08:00
|
|
|
// TODO: check if we can get rid of this LLDB_CONFIG if we do a:
|
|
|
|
// setvbuf(m_file, (char *)NULL, _IOLBF, 0);
|
2011-02-08 08:35:34 +08:00
|
|
|
#ifdef LLDB_CONFIG_SUPPORTS_SETLINEBUFFERED
|
2011-01-24 12:09:25 +08:00
|
|
|
if (m_file != NULL)
|
|
|
|
setlinebuf (m_file);
|
2011-02-08 08:35:34 +08:00
|
|
|
#endif // #ifdef LLDB_CONFIG_SUPPORTS_SETLINEBUFFERED
|
2011-02-05 14:41:57 +08:00
|
|
|
}
|
2011-02-05 14:37:53 +08:00
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
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();
|
|
|
|
}
|