TrueReality  v0.1.1912
File.cpp
Go to the documentation of this file.
1 /*
2 * True Reality Open Source Game and Simulation Engine
3 * Copyright © 2021 Acid Rain Studios LLC
4 *
5 * This library is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU Lesser General Public License as published by the Free
7 * Software Foundation; either version 3.0 of the License, or (at your option)
8 * any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library; if not, write to the Free Software Foundation, Inc.,
17 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * @author Maxim Serebrennik
20 */
21 
22 #include <trUtil/JSON/File.h>
23 
24 #include <trUtil/JSON/Array.h>
25 #include <trUtil/JSON/Object.h>
26 
27 #include <trUtil/Exception.h>
28 #include <trUtil/FileUtils.h>
29 #include <trUtil/PathUtils.h>
30 #include <trUtil/PlatformMacros.h>
31 #include <trUtil/StringUtils.h>
32 #include <trUtil/WarningUtils.h>
33 #include <trUtil/Logging/Log.h>
34 
35 #include <json/reader.h>
36 #include <json/writer.h>
37 #include <osgDB/fstream>
38 
39 #include <iostream>
40 #include <cstdio>
41 
42 namespace trUtil::JSON
43 {
44  const std::string File::DEFAULT_JSON_FILE_NAME = std::string("Default.json");
45 
47  File::File() : File(DEFAULT_JSON_FILE_NAME)
48  {}
49 
51  File::File(std::string fileName)
52  {
53  mFileName = fileName;
55  }
56 
59  {
60  }
61 
64  {
65  return ReadFromFile(mFileName);
66  }
67 
69  bool File::ReadFromFile(std::string fileName)
70  {
71  try
72  {
73  mFileName = fileName;
74  std::string fullFilePath = mFilePath + "/" + mFileName;
75  Json::CharReaderBuilder reader;
76 
77  //Open a file for reading.
78  LOG_D("Opening JSON File for Reading: " + fullFilePath);
79 
80  //Read the file into the input stream.
81  LOG_D("Reading JSON File: " + fullFilePath);
82  osgDB::ifstream inputStream(fullFilePath.c_str(), std::ifstream::binary);
83 
84  if (inputStream)
85  {
86  //Clear the root node for new input
87  mRoot.Clear();
88 
89  //Parse the stream into the document
90  LOG_D("Parsing JSON File");
91 
92  JSONCPP_STRING errs;
93  if (!Json::parseFromStream(reader, inputStream, &mRoot.GetJsonValue(), &errs))
94  {
95  LOG_E("JSON Parsing Error: " + errs);
96  return false;
97  }
98 
99  //Close the input stream
100  inputStream.close();
101  return true;
102  }
103  else
104  {
105  LOG_E("Can't open: " + fullFilePath);
106  return false;
107  }
108  }
109  catch (const trUtil::Exception& ex)
110  {
112  return false;
113  }
114  }
115 
118  {
119  return WriteToFile(mFileName);
120  }
121 
123  bool File::WriteToFile(std::string fileName)
124  {
125  try
126  {
127  mFileName = fileName;
128  std::string fullFilePath = mFilePath + "/" + mFileName;
129  Json::StreamWriterBuilder builder;
130  builder["indentation"]=" ";
131  std::unique_ptr<Json::StreamWriter> streamWriter(builder.newStreamWriter());
132 
133  //Open a file for writing
134  LOG_D("Opening JSON File for Writing: " + fullFilePath);
135  osgDB::ofstream outputStream(fullFilePath.c_str());
136 
137  //Writing the file
138  LOG_D("Writing file");
139  streamWriter->write(mRoot, &outputStream);
140  LOG_D("Closing file");
141  outputStream.close();
142  LOG_D("File Closed");
143 
144  return true;
145  }
146  catch (const trUtil::Exception& ex)
147  {
149  return false;
150  }
151  }
152 
154  void File::SetFileName(std::string fileName)
155  {
156  mFileName = fileName;
157  }
158 
160  std::string File::GetFileName() const
161  {
162  return mFileName;
163  }
164 
166  void File::SetFilePath(std::string newPath)
167  {
168  mFilePath = newPath;
169  }
170 
172  std::string File::GetFilePath() const
173  {
174  return mFilePath;
175  }
176 
179  {
181  }
182 
184  void File::Clear()
185  {
186  mRoot.Clear();
187  }
188 
191  {
192  return mRoot;
193  }
194 
197  {
198  std::cout << mRoot << std::endl;
199  }
200 
202  bool File::KeyPresent(const std::string &key) const
203  {
204  return mRoot.HasMember(key);
205  }
206 
208  bool File::IsNull(const std::string &key) const
209  {
210  return mRoot.IsNull(key);
211  }
212 
214  void File::SetNull(const std::string &key)
215  {
216  mRoot.SetNull(key);
217  }
218 
220  bool File::IsBool(const std::string &key) const
221  {
222  return mRoot.IsBool(key);
223  }
224 
226  bool File::GetBool(const std::string &key) const
227  {
228  return mRoot.GetBool(key);
229  }
230 
232  void File::SetBool(const std::string &key, const bool &value)
233  {
234  mRoot.SetBool(key, value);
235  }
236 
238  bool File::IsTrue(const std::string &key) const
239  {
240  if (KeyPresent(key))
241  {
242  if (GetBool(key) == true)
243  {
244  return true;
245  }
246  else
247  {
248  return false;
249  }
250  }
251  return false;
252  }
253 
255  bool File::IsFalse(const std::string &key) const
256  {
257  if (KeyPresent(key))
258  {
259  if (GetBool(key) == false)
260  {
261  return true;
262  }
263  else
264  {
265  return false;
266  }
267  }
268  return false;
269  }
270 
272  bool File::IsNumber(const std::string &key) const
273  {
274  return mRoot.IsNumber(key);
275  }
276 
278  bool File::IsInt(const std::string &key) const
279  {
280  return mRoot.IsInt(key);
281  }
282 
284  int File::GetInt(const std::string &key) const
285  {
286  return mRoot.GetInt(key);
287  }
288 
290  void File::SetInt(const std::string &key, const int &value)
291  {
292  mRoot.SetInt(key, value);
293  }
294 
296  bool File::IsDouble(const std::string &key) const
297  {
298  return mRoot.IsDouble(key);
299  }
300 
302  double File::GetDouble(const std::string &key) const
303  {
304  return mRoot.GetDouble(key);
305  }
306 
308  void File::SetDouble(const std::string &key, const double &value)
309  {
310  mRoot.SetDouble(key, value);
311  }
312 
314  bool File::IsUInt(const std::string &key) const
315  {
316  return mRoot.IsUInt(key);
317  }
318 
320  unsigned int File::GetUInt(const std::string &key) const
321  {
322  return mRoot.GetUInt(key);
323  }
324 
326  void File::SetUInt(const std::string &key, const unsigned int &value)
327  {
328  mRoot.SetUInt(key, value);
329  }
330 
332  bool File::IsInt64(const std::string &key) const
333  {
334  return mRoot.IsInt64(key);
335  }
336 
338  Int64 File::GetInt64(const std::string &key) const
339  {
340  return mRoot.GetInt64(key);
341  }
342 
344  void File::SetInt64(const std::string &key, const Int64 &value)
345  {
346  mRoot.SetInt64(key, value);
347  }
348 
350  bool File::IsUInt64(const std::string &key) const
351  {
352  return mRoot.IsUInt64(key);
353  }
354 
356  UInt64 File::GetUInt64(const std::string &key) const
357  {
358  return mRoot.GetUInt64(key);
359  }
360 
362  void File::SetUInt64(const std::string &key, const UInt64 &value)
363  {
364  mRoot.SetUInt64(key, value);
365  }
366 
368  bool File::IsFloat(const std::string &key) const
369  {
370  return mRoot.IsFloat(key);
371  }
372 
374  float File::GetFloat(const std::string &key) const
375  {
376  return mRoot.GetFloat(key);
377  }
378 
380  void File::SetFloat(const std::string &key, const float &value)
381  {
382  mRoot.SetFloat(key, value);
383  }
384 
386  bool File::IsString(const std::string &key) const
387  {
388  return mRoot.IsString(key);
389  }
390 
392  const std::string File::GetString(const std::string &key) const
393  {
394  return mRoot.GetString(key);
395  }
396 
398  void File::SetString(const std::string &key, const std::string &value)
399  {
400  mRoot.SetString(key, value);
401  }
402 
404  bool File::IsArray(const std::string &key) const
405  {
406  return mRoot.IsArray(key);
407  }
408 
410  Array File::GetArray(const std::string &key) const
411  {
412  return mRoot.GetArray(key);
413  }
414 
416  void File::SetArray(const std::string &key, Array &value)
417  {
418  mRoot.SetArray(key, value);
419  }
420 
422  bool File::IsObject(const std::string &key) const
423  {
424  return mRoot.IsObject(key);
425  }
426 
428  Object File::GetObject(const std::string &key) const
429  {
430  return mRoot.GetObject(key);
431  }
432 
434  void File::SetObject(const std::string &key, Object &value)
435  {
436  mRoot.SetObject(key, value);
437  }
438 }
virtual bool IsArray() const
Checks if the value stored is an Array.
Definition: Value.cpp:696
void Clear() override
Clears the internal JSON Root node.
Definition: File.cpp:184
virtual bool IsUInt64() const
Checks if the value stored is a 64bit Unsigned Integer.
Definition: Value.cpp:570
virtual bool IsBool() const
Checks if the value stored is a Boolean.
Definition: Value.cpp:342
virtual void SetInt64(const std::string &key, Int64 value)
Sets the Int64 value in the Object.
Definition: Value.cpp:564
virtual bool IsInt() const
Checks if the value stored is an Integer.
Definition: Value.cpp:402
virtual bool IsObject(const std::string &key) const override
Checks if the value stored at the specific key is an Object.
Definition: File.cpp:422
bool FileExists(const std::string &strFile, bool caseInsensitive=false) const
Queries if a given file exists.
Definition: FileUtils.cpp:217
virtual Value & GetJSONRoot() override
Returns a reference to the internal JSON Root node.
Definition: File.cpp:190
This is the exception class used throughout the engine.
Definition: Exception.h:48
virtual bool IsUInt() const
Checks if the value stored Unsigned Integer.
Definition: Value.cpp:486
virtual bool IsFloat() const
Checks if the value stored is a float.
Definition: Value.cpp:612
virtual unsigned int GetUInt() const
Returns the Unsigned Integer value.
Definition: Value.cpp:504
virtual float GetFloat(const std::string &key) const override
Returns the float value stored at the given key.
Definition: File.cpp:374
virtual bool IsArray(const std::string &key) const override
Checks if the value stored at the specific key is an Array.
Definition: File.cpp:404
virtual std::string GetFileName() const
Returns the name of the current file.
Definition: File.cpp:160
File()
ctor.
Definition: File.cpp:47
virtual void SetString(const std::string &key, const std::string &value) override
Sets the String to be stored at the given key.
Definition: File.cpp:398
A file.
Definition: File.h:45
A JSON array.
Definition: Array.h:46
virtual int GetInt(const std::string &key) const override
Returns the Integer value stored at the given key.
Definition: File.cpp:284
virtual bool IsNumber(const std::string &key) const override
Checks if the value stored at the specific key is a Number.
Definition: File.cpp:272
virtual void SetDouble(const std::string &key, const double &value) override
Sets the Double value to be stored at the given key.
Definition: File.cpp:308
virtual Int64 GetInt64(const std::string &key) const override
Returns the 64bit Integer value stored at the given key.
Definition: File.cpp:338
virtual void SetUInt(const std::string &key, unsigned int value)
Sets the Unsigned Int value in the Object.
Definition: Value.cpp:522
std::string mFileName
Filename of the file.
Definition: File.h:573
virtual void PrintJSONRoot() override
Prints out to the screen the whole JSON Root content.
Definition: File.cpp:196
virtual int GetInt() const
Returns the Integer value.
Definition: Value.cpp:420
virtual void SetString(const std::string &key, const std::string &value)
Sets the String value in the Object.
Definition: Value.cpp:690
virtual bool IsInt(const std::string &key) const override
Checks if the value stored at the specific key is an Integer.
Definition: File.cpp:278
virtual bool IsString() const
Checks if the value stored is a String.
Definition: Value.cpp:654
virtual void Clear()
Clears the internal JSON Root node.
Definition: Value.cpp:240
virtual unsigned int GetUInt(const std::string &key) const override
Returns the Unsigned Integer value stored at the given key.
Definition: File.cpp:320
Represents a JSON value.
Definition: Value.h:139
virtual bool IsFalse(const std::string &key) const override
Checks if the value stored at the specific key is False.
Definition: File.cpp:255
virtual void SetFloat(const std::string &key, const float &value) override
Sets the float value to be stored at the given key.
Definition: File.cpp:380
virtual bool IsBool(const std::string &key) const override
Checks if the value stored at the specific key is a Boolean.
Definition: File.cpp:220
int64_t Int64
The fourth int 6.
Definition: Value.h:72
virtual bool IsUInt(const std::string &key) const override
Checks if the value stored at the specific key is an Unsigned Integer.
Definition: File.cpp:314
virtual double GetDouble() const
Returns the Double value.
Definition: Value.cpp:462
virtual void SetBool(const std::string &key, const bool &value) override
Sets the Boolean value to be stored at the given key.
Definition: File.cpp:232
virtual bool FileExists()
Returns true if the file name and path exist on the HD.
Definition: File.cpp:178
virtual void SetUInt64(const std::string &key, const UInt64 &value) override
Sets the 64bit Integer value to be stored at the given key.
Definition: File.cpp:362
virtual bool IsNull() const
Checks if the value stored is a NULL.
Definition: Value.cpp:294
virtual void SetFilePath(std::string newPath)
Sets the path of where the file will be read from or written to.
Definition: File.cpp:166
virtual bool IsUInt64(const std::string &key) const override
Checks if the value stored at the specific key is a 64bit Integer.
Definition: File.cpp:350
virtual void SetNull(const std::string &key) const
Sets the NULL value in the Object.
Definition: Value.cpp:312
virtual bool GetBool() const
Returns the Boolean value.
Definition: Value.cpp:360
virtual bool GetBool(const std::string &key) const override
Returns the Boolean value stored at the given key.
Definition: File.cpp:226
void LogException(trUtil::Logging::LogLevel level=trUtil::Logging::LogLevel::LOG_ERROR) const
Logs the exception to the default logger.
Definition: Exception.cpp:60
virtual std::string GetFilePath() const
Returns the path of where the file is read or written to.
Definition: File.cpp:172
TR_UTIL_EXPORT std::string GetUserConfigPath()
Gets user configuration path.
Definition: PathUtils.cpp:590
virtual void SetObject(const std::string &key, Object &value) override
Sets the Array to be stored at the given key.
Definition: File.cpp:434
Forward declaration.
Definition: Object.h:46
virtual void SetUInt64(const std::string &key, UInt64 value)
Sets the Unsigned Int64 value in the Object.
Definition: Value.cpp:606
virtual void SetInt64(const std::string &key, const Int64 &value) override
Sets the 64bit Integer value to be stored at the given key.
Definition: File.cpp:344
virtual bool IsDouble() const
Checks if the value stored is a Double.
Definition: Value.cpp:444
virtual const std::string GetString(const std::string &key) const override
Returns the String value stored at the given key.
Definition: File.cpp:392
virtual bool IsString(const std::string &key) const override
Checks if the value stored at the specific key is a String.
Definition: File.cpp:386
virtual void SetUInt(const std::string &key, const unsigned int &value) override
Sets the Unsigned Integer value to be stored at the given key.
Definition: File.cpp:326
virtual bool IsTrue(const std::string &key) const override
Checks if the value stored at the specific key is True.
Definition: File.cpp:238
virtual float GetFloat() const
Returns the float value stored.
Definition: Value.cpp:630
virtual void SetInt(const std::string &key, const int &value) override
Sets the Integer value to be stored at the given key.
Definition: File.cpp:290
virtual UInt64 GetUInt64() const
Returns the Unsigned 64bit Integer value.
Definition: Value.cpp:588
virtual void SetInt(const std::string &key, int value)
Sets the Integer value in the Object.
Definition: Value.cpp:438
#define LOG_E(msg)
Log an ERROR message.
Definition: Log.h:165
virtual void SetArray(const std::string &key, Array &value) override
Sets the Array to be stored at the given key.
Definition: File.cpp:416
static const std::string DEFAULT_JSON_FILE_NAME
The default JSON file name.
Definition: File.h:50
virtual bool IsInt64(const std::string &key) const override
Checks if the value stored at the specific key is a 64bit Integer.
Definition: File.cpp:332
Value mRoot
The root.
Definition: File.h:578
virtual bool IsDouble(const std::string &key) const override
Checks if the value stored at the specific key is a Double.
Definition: File.cpp:296
virtual bool IsNull(const std::string &key) const override
Checks if the value stored at the specific key is a NULL.
Definition: File.cpp:208
virtual void SetObject(const std::string &key, Object &value)
Sets the Object value in the Object.
Definition: Value.cpp:780
virtual bool IsFloat(const std::string &key) const override
Checks if the value stored at the specific key is a float.
Definition: File.cpp:368
static FileUtils & GetInstance()
Character separating the parts of a file path.
Definition: FileUtils.h:137
virtual bool ReadFromFile()
Reads from a file and parses the JSON into a Root Node.
Definition: File.cpp:63
virtual const Array GetArray() const
Returns the Array value stored.
Definition: Value.cpp:714
virtual Array GetArray(const std::string &key) const override
Returns the Array value stored at the given key.
Definition: File.cpp:410
virtual void SetFloat(const std::string &key, float value)
Sets the Float value in the Object.
Definition: Value.cpp:648
LOG_ERROR
Definition: LogLevel.h:64
virtual void SetFileName(std::string fileName)
Sets a new file name.
Definition: File.cpp:154
virtual Object GetObject(const std::string &key) const override
Returns the Object value stored at the given key.
Definition: File.cpp:428
virtual void SetBool(const std::string &key, bool value)
Sets the Boolean value in the Object.
Definition: Value.cpp:378
virtual bool IsInt64() const
Checks if the value stored is a 64bit Integer.
Definition: Value.cpp:528
virtual double GetDouble(const std::string &key) const override
Returns the Double value stored at the given key.
Definition: File.cpp:302
std::string mFilePath
Full pathname of the file.
Definition: File.h:575
virtual const Object GetObject() const
Returns the Object value stored.
Definition: Value.cpp:759
virtual void SetArray(const std::string &key, Array &value)
Sets the Array value in the Object.
Definition: Value.cpp:735
virtual bool KeyPresent(const std::string &key) const override
Checks if the JSON Root Node has an entry with a given key present.
Definition: File.cpp:202
virtual UInt64 GetUInt64(const std::string &key) const override
Returns the 64bit Integer value stored at the given key.
Definition: File.cpp:356
virtual void SetDouble(const std::string &key, double value)
Sets the Double value in the Object.
Definition: Value.cpp:480
bool HasMember(const std::string &key) const
Returns true if a member with the passed in key is present.
Definition: Value.cpp:282
virtual bool WriteToFile()
Writes the Root Node to a JSON file.
Definition: File.cpp:117
virtual bool IsObject() const
Checks if the value stored is an Object.
Definition: Value.cpp:741
~File()
dtor.
Definition: File.cpp:58
virtual const std::string GetString() const
Returns the String value stored.
Definition: Value.cpp:672
uint64_t UInt64
The fourth u int 6.
Definition: Value.h:74
virtual void SetNull(const std::string &key) override
Sets the value stored at the specific key to NULL.
Definition: File.cpp:214
#define LOG_D(msg)
Log a DEBUG message.
Definition: Log.h:138
virtual Int64 GetInt64() const
Returns the 64bit Integer value.
Definition: Value.cpp:546
Json::Value & GetJsonValue()
Returns a reference to the internal Json::Value.
Definition: Value.cpp:202
virtual bool IsNumber() const
Checks if the value stored is a Number.
Definition: Value.cpp:384