2010-06-09 00:52:24 +08:00
|
|
|
//===-- SBFileSpec.cpp ------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2010-06-09 00:52:24 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-11-12 07:16:43 +08:00
|
|
|
#include <inttypes.h>
|
2011-04-08 21:36:44 +08:00
|
|
|
#include <limits.h>
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
#include "lldb/API/SBFileSpec.h"
|
2010-09-20 13:20:02 +08:00
|
|
|
#include "lldb/API/SBStream.h"
|
2018-11-02 01:09:22 +08:00
|
|
|
#include "lldb/Host/FileSystem.h"
|
2017-03-23 01:33:23 +08:00
|
|
|
#include "lldb/Host/PosixApi.h"
|
2017-03-23 02:40:07 +08:00
|
|
|
#include "lldb/Utility/FileSpec.h"
|
2017-03-04 04:56:28 +08:00
|
|
|
#include "lldb/Utility/Log.h"
|
2017-02-03 05:39:50 +08:00
|
|
|
#include "lldb/Utility/Stream.h"
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2014-08-08 01:33:36 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
using namespace lldb;
|
|
|
|
using namespace lldb_private;
|
|
|
|
|
2019-02-13 14:25:41 +08:00
|
|
|
SBFileSpec::SBFileSpec() : m_opaque_up(new lldb_private::FileSpec()) {}
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
SBFileSpec::SBFileSpec(const SBFileSpec &rhs)
|
2019-02-13 14:25:41 +08:00
|
|
|
: m_opaque_up(new lldb_private::FileSpec(*rhs.m_opaque_up)) {}
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
SBFileSpec::SBFileSpec(const lldb_private::FileSpec &fspec)
|
2019-02-13 14:25:41 +08:00
|
|
|
: m_opaque_up(new lldb_private::FileSpec(fspec)) {}
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2014-07-02 05:22:11 +08:00
|
|
|
// Deprecated!!!
|
2019-02-13 14:25:41 +08:00
|
|
|
SBFileSpec::SBFileSpec(const char *path) : m_opaque_up(new FileSpec(path)) {
|
|
|
|
FileSystem::Instance().Resolve(*m_opaque_up);
|
2018-11-02 05:05:36 +08:00
|
|
|
}
|
2010-10-21 04:54:39 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
SBFileSpec::SBFileSpec(const char *path, bool resolve)
|
2019-02-13 14:25:41 +08:00
|
|
|
: m_opaque_up(new FileSpec(path)) {
|
2018-11-02 05:05:36 +08:00
|
|
|
if (resolve)
|
2019-02-13 14:25:41 +08:00
|
|
|
FileSystem::Instance().Resolve(*m_opaque_up);
|
2018-11-02 05:05:36 +08:00
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
SBFileSpec::~SBFileSpec() {}
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
const SBFileSpec &SBFileSpec::operator=(const SBFileSpec &rhs) {
|
|
|
|
if (this != &rhs)
|
2019-02-13 14:25:41 +08:00
|
|
|
*m_opaque_up = *rhs.m_opaque_up;
|
2016-09-07 04:57:50 +08:00
|
|
|
return *this;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2019-02-13 14:25:41 +08:00
|
|
|
bool SBFileSpec::IsValid() const { return m_opaque_up->operator bool(); }
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
bool SBFileSpec::Exists() const {
|
|
|
|
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
|
2010-10-26 11:11:13 +08:00
|
|
|
|
2019-02-13 14:25:41 +08:00
|
|
|
bool result = FileSystem::Instance().Exists(*m_opaque_up);
|
2010-10-26 11:11:13 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
if (log)
|
|
|
|
log->Printf("SBFileSpec(%p)::Exists () => %s",
|
2019-02-13 14:25:41 +08:00
|
|
|
static_cast<void *>(m_opaque_up.get()),
|
2016-09-07 04:57:50 +08:00
|
|
|
(result ? "true" : "false"));
|
2010-10-26 11:11:13 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
return result;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
bool SBFileSpec::ResolveExecutableLocation() {
|
2019-02-13 14:25:41 +08:00
|
|
|
return FileSystem::Instance().ResolveExecutableLocation(*m_opaque_up);
|
2010-09-10 12:48:55 +08:00
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
int SBFileSpec::ResolvePath(const char *src_path, char *dst_path,
|
|
|
|
size_t dst_len) {
|
|
|
|
llvm::SmallString<64> result(src_path);
|
2018-11-02 05:05:36 +08:00
|
|
|
FileSystem::Instance().Resolve(result);
|
2016-09-07 04:57:50 +08:00
|
|
|
::snprintf(dst_path, dst_len, "%s", result.c_str());
|
|
|
|
return std::min(dst_len - 1, result.size());
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
const char *SBFileSpec::GetFilename() const {
|
2019-02-13 14:25:41 +08:00
|
|
|
const char *s = m_opaque_up->GetFilename().AsCString();
|
2010-10-26 11:11:13 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
|
|
|
|
if (log) {
|
|
|
|
if (s)
|
|
|
|
log->Printf("SBFileSpec(%p)::GetFilename () => \"%s\"",
|
2019-02-13 14:25:41 +08:00
|
|
|
static_cast<void *>(m_opaque_up.get()), s);
|
2016-09-07 04:57:50 +08:00
|
|
|
else
|
|
|
|
log->Printf("SBFileSpec(%p)::GetFilename () => NULL",
|
2019-02-13 14:25:41 +08:00
|
|
|
static_cast<void *>(m_opaque_up.get()));
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *SBFileSpec::GetDirectory() const {
|
2019-02-13 14:25:41 +08:00
|
|
|
FileSpec directory{*m_opaque_up};
|
2016-09-07 04:57:50 +08:00
|
|
|
directory.GetFilename().Clear();
|
|
|
|
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
|
|
|
|
if (log) {
|
|
|
|
if (directory)
|
|
|
|
log->Printf("SBFileSpec(%p)::GetDirectory () => \"%s\"",
|
2019-02-13 14:25:41 +08:00
|
|
|
static_cast<void *>(m_opaque_up.get()),
|
2016-09-07 04:57:50 +08:00
|
|
|
directory.GetCString());
|
|
|
|
else
|
|
|
|
log->Printf("SBFileSpec(%p)::GetDirectory () => NULL",
|
2019-02-13 14:25:41 +08:00
|
|
|
static_cast<void *>(m_opaque_up.get()));
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
return directory.GetCString();
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
void SBFileSpec::SetFilename(const char *filename) {
|
|
|
|
if (filename && filename[0])
|
2019-02-13 14:25:41 +08:00
|
|
|
m_opaque_up->GetFilename().SetCString(filename);
|
2016-09-07 04:57:50 +08:00
|
|
|
else
|
2019-02-13 14:25:41 +08:00
|
|
|
m_opaque_up->GetFilename().Clear();
|
2013-11-21 05:07:01 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
void SBFileSpec::SetDirectory(const char *directory) {
|
|
|
|
if (directory && directory[0])
|
2019-02-13 14:25:41 +08:00
|
|
|
m_opaque_up->GetDirectory().SetCString(directory);
|
2016-09-07 04:57:50 +08:00
|
|
|
else
|
2019-02-13 14:25:41 +08:00
|
|
|
m_opaque_up->GetDirectory().Clear();
|
2013-11-21 05:07:01 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
uint32_t SBFileSpec::GetPath(char *dst_path, size_t dst_len) const {
|
|
|
|
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
|
2010-10-26 11:11:13 +08:00
|
|
|
|
2019-02-13 14:25:41 +08:00
|
|
|
uint32_t result = m_opaque_up->GetPath(dst_path, dst_len);
|
2010-10-26 11:11:13 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
if (log)
|
|
|
|
log->Printf("SBFileSpec(%p)::GetPath (dst_path=\"%.*s\", dst_len=%" PRIu64
|
|
|
|
") => %u",
|
2019-02-13 14:25:41 +08:00
|
|
|
static_cast<void *>(m_opaque_up.get()), result, dst_path,
|
2016-09-07 04:57:50 +08:00
|
|
|
static_cast<uint64_t>(dst_len), result);
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
if (result == 0 && dst_path && dst_len > 0)
|
|
|
|
*dst_path = '\0';
|
|
|
|
return result;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
const lldb_private::FileSpec *SBFileSpec::operator->() const {
|
2019-02-13 14:25:41 +08:00
|
|
|
return m_opaque_up.get();
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
const lldb_private::FileSpec *SBFileSpec::get() const {
|
2019-02-13 14:25:41 +08:00
|
|
|
return m_opaque_up.get();
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
const lldb_private::FileSpec &SBFileSpec::operator*() const {
|
2019-02-13 14:25:41 +08:00
|
|
|
return *m_opaque_up;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2019-02-13 14:25:41 +08:00
|
|
|
const lldb_private::FileSpec &SBFileSpec::ref() const { return *m_opaque_up; }
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
void SBFileSpec::SetFileSpec(const lldb_private::FileSpec &fs) {
|
2019-02-13 14:25:41 +08:00
|
|
|
*m_opaque_up = fs;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
bool SBFileSpec::GetDescription(SBStream &description) const {
|
|
|
|
Stream &strm = description.ref();
|
|
|
|
char path[PATH_MAX];
|
2019-02-13 14:25:41 +08:00
|
|
|
if (m_opaque_up->GetPath(path, sizeof(path)))
|
2016-09-07 04:57:50 +08:00
|
|
|
strm.PutCString(path);
|
|
|
|
return true;
|
2010-10-26 11:11:13 +08:00
|
|
|
}
|
2016-02-19 08:05:17 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
void SBFileSpec::AppendPathComponent(const char *fn) {
|
2019-02-13 14:25:41 +08:00
|
|
|
m_opaque_up->AppendPathComponent(fn);
|
2016-02-19 08:05:17 +08:00
|
|
|
}
|