summaryrefslogtreecommitdiffstats
path: root/lldb/source/Core/StructuredData.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lldb/source/Core/StructuredData.cpp')
-rw-r--r--lldb/source/Core/StructuredData.cpp42
1 files changed, 42 insertions, 0 deletions
diff --git a/lldb/source/Core/StructuredData.cpp b/lldb/source/Core/StructuredData.cpp
index 6e544c1d537..5980e0fe645 100644
--- a/lldb/source/Core/StructuredData.cpp
+++ b/lldb/source/Core/StructuredData.cpp
@@ -14,7 +14,11 @@
#include <inttypes.h>
#include <stdlib.h>
+#include "lldb/Core/DataBuffer.h"
+#include "lldb/Core/Error.h"
#include "lldb/Core/StreamString.h"
+#include "lldb/Host/File.h"
+#include "lldb/Host/FileSpec.h"
#include "lldb/Host/StringConvert.h"
#include "lldb/Utility/JSON.h"
@@ -27,6 +31,44 @@ static StructuredData::ObjectSP ParseJSONValue(JSONParser &json_parser);
static StructuredData::ObjectSP ParseJSONObject(JSONParser &json_parser);
static StructuredData::ObjectSP ParseJSONArray(JSONParser &json_parser);
+StructuredData::ObjectSP StructuredData::ParseJSONFromFile(FileSpec &input_spec,
+ Error &error) {
+ StructuredData::ObjectSP return_sp;
+ if (!input_spec.Exists()) {
+ error.SetErrorStringWithFormat("input file %s does not exist.",
+ input_spec.GetPath().c_str());
+ return return_sp;
+ }
+
+ File input_file(nullptr, File::OpenOptions::eOpenOptionRead,
+ lldb::eFilePermissionsUserRead);
+ std::string input_path = input_spec.GetPath();
+ error =
+ input_file.Open(input_path.c_str(), File::OpenOptions::eOpenOptionRead,
+ lldb::eFilePermissionsUserRead);
+
+ if (!error.Success()) {
+ error.SetErrorStringWithFormat("could not open input file: %s - %s.",
+ input_spec.GetPath().c_str(),
+ error.AsCString());
+ return return_sp;
+ }
+
+ lldb::DataBufferSP input_data;
+ size_t num_bytes = SIZE_T_MAX;
+ off_t offset = 0;
+ error = input_file.Read(num_bytes, offset, true, input_data);
+ if (!error.Success()) {
+ error.SetErrorStringWithFormat("could not read input file: %s - %s.",
+ input_spec.GetPath().c_str(),
+ error.AsCString());
+ return return_sp;
+ }
+ JSONParser json_parser((char *)input_data->GetBytes());
+ return_sp = ParseJSONValue(json_parser);
+ return return_sp;
+}
+
static StructuredData::ObjectSP ParseJSONObject(JSONParser &json_parser) {
// The "JSONParser::Token::ObjectStart" token should have already been
// consumed
OpenPOWER on IntegriCloud