From ed0bdf4662f5ca56d981f6ce6e015c95a43cd9c2 Mon Sep 17 00:00:00 2001
From: Matthew D Jones <matthew.d.jones@tessella.com>
Date: Tue, 20 Oct 2015 09:00:21 +0100
Subject: [PATCH] Re #13729 Continued expunging property_tree from
 ScriptRepositoryImpl

---
 .../ScriptRepositoryImpl.h                    |  5 ++
 .../src/ScriptRepositoryImpl.cpp              | 54 +++++++++++++++----
 2 files changed, 48 insertions(+), 11 deletions(-)

diff --git a/Framework/ScriptRepository/inc/MantidScriptRepository/ScriptRepositoryImpl.h b/Framework/ScriptRepository/inc/MantidScriptRepository/ScriptRepositoryImpl.h
index bd92ecea6d7..8ad816edaf4 100644
--- a/Framework/ScriptRepository/inc/MantidScriptRepository/ScriptRepositoryImpl.h
+++ b/Framework/ScriptRepository/inc/MantidScriptRepository/ScriptRepositoryImpl.h
@@ -4,6 +4,7 @@
 #include "MantidAPI/ScriptRepository.h"
 #include "MantidKernel/DateAndTime.h"
 #include <map>
+#include <json/value.h>
 
 #ifdef _WIN32
 #if (IN_MANTID_SCRIPTREPO)
@@ -18,6 +19,10 @@
 namespace Mantid {
 namespace API {
 
+void write_json_file(std::string filename, Json::Value json, std::string error);
+
+bool file_exists(std::string filename);
+
 /** Implementation of Mantid::API::ScriptRepository
 
     This implementation relies on the definition of the Script Repository
diff --git a/Framework/ScriptRepository/src/ScriptRepositoryImpl.cpp b/Framework/ScriptRepository/src/ScriptRepositoryImpl.cpp
index 2f8d0a7f220..14bc06edf33 100644
--- a/Framework/ScriptRepository/src/ScriptRepositoryImpl.cpp
+++ b/Framework/ScriptRepository/src/ScriptRepositoryImpl.cpp
@@ -49,12 +49,16 @@ using Mantid::Kernel::NetworkProxy;
 #include <Poco/DateTimeFormatter.h>
 
 // from boost
+#include <boost/property_tree/ptree.hpp>
+#include <boost/property_tree/json_parser.hpp>
 #include <boost/foreach.hpp>
 #include <boost/algorithm/string.hpp>
 #include <boost/regex.hpp>
 
 #include <json/json.h>
 
+using boost::property_tree::ptree;  // Todo remove once using jsoncpp, plus boost ptree includes
+
 namespace Mantid {
 namespace API {
 namespace {
@@ -70,6 +74,32 @@ const char *emptyURL =
     "properties file, "
     "at ScriptRepository";
 
+/**
+Write json object to file
+*/
+void write_json_file(std::string filename, Json::Value json, std::string error)
+{
+  Poco::FileStream filestream(filename);
+  if (!filestream.good()) {
+    g_log.error() << error;
+  }
+  Json::StyledWriter writer;
+  filestream << writer.write(json);
+  filestream.close();
+}
+
+/**
+Test if a file with this filename already exists
+*/
+bool file_exists(std::string filename)
+{
+  Poco::File test_file(filename);
+  if (test_file.exists()) {
+    return true;
+  }
+  return false;
+}
+
 DECLARE_SCRIPTREPOSITORY(ScriptRepositoryImpl)
 /**
  The main information that ScriptrepositoryImpl needs to be able
@@ -270,12 +300,13 @@ void ScriptRepositoryImpl::install(const std::string &path) {
   g_log.debug() << "ScriptRepository downloaded repository information"
                 << std::endl;
   // creation of the instance of local_json file
-  Poco::File local(local_json_file);
-  if (!local.exists()) {
-    ptree pt;
-    write_json(local_json_file, pt);
+  if (!file_exists(local_json_file))
+  {
+    Json::Value pt;
+    write_json_file(local_json_file, pt,
+                    "ScriptRepository failed to create local repository");
     g_log.debug() << "ScriptRepository created the local repository information"
-                  << std::endl;
+    << std::endl;
   }
 
 #if defined(_WIN32) || defined(_WIN64)
@@ -821,13 +852,14 @@ void ScriptRepositoryImpl::upload(const std::string &file_path,
     std::string published_date;
 
     Json::Value pt;
-    if (!Json::Reader::parse(answer, pt)) {
-      throw ScriptRepoException("Bad answer from the Server", ex.what());
+    Json::Reader json_reader;
+    if (!json_reader.parse(answer, pt)) {
+      throw ScriptRepoException("Bad answer from the Server");
     }
-    info = pt.get<std::string>("message", "");
-    detail = pt.get<std::string>("detail", "");
-    published_date = pt.get<std::string>("pub_date", "");
-    std::string cmd = pt.get<std::string>("shell", "");
+    info = pt.get("message", "UTF-8" ).asString();
+    detail = pt.get("detail", "UTF-8" ).asString();
+    published_date = pt.get("pub_date", "UTF-8" ).asString();
+    std::string cmd = pt.get("shell", "UTF-8" ).asString();
     if (!cmd.empty())
       detail.append("\nFrom Command: ").append(cmd);
 
-- 
GitLab