settings.cpp
Go to the documentation of this file.
1 // MIT License
2 
3 // Copyright (c) 2019 Erin Catto
4 
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 
23 #define _CRT_SECURE_NO_WARNINGS
24 #include "settings.h"
25 #include "sajson/sajson.h"
26 #include <stdio.h>
27 
28 static const char* fileName = "settings.ini";
29 
30 // Load a file. You must free the character array.
31 static bool sReadFile(char*& data, int& size, const char* filename)
32 {
33  FILE* file = fopen(filename, "rb");
34  if (file == nullptr)
35  {
36  return false;
37  }
38 
39  fseek(file, 0, SEEK_END);
40  size = ftell(file);
41  fseek(file, 0, SEEK_SET);
42 
43  if (size == 0)
44  {
45  return false;
46  }
47 
48  data = (char*)malloc(size + 1);
49  fread(data, size, 1, file);
50  fclose(file);
51  data[size] = 0;
52 
53  return true;
54 }
55 
57 {
58  FILE* file = fopen(fileName, "w");
59  fprintf(file, "{\n");
60  fprintf(file, " \"testIndex\": %d,\n", m_testIndex);
61  fprintf(file, " \"windowWidth\": %d,\n", m_windowWidth);
62  fprintf(file, " \"windowHeight\": %d,\n", m_windowHeight);
63  fprintf(file, " \"hertz\": %.9g,\n", m_hertz);
64  fprintf(file, " \"velocityIterations\": %d,\n", m_velocityIterations);
65  fprintf(file, " \"positionIterations\": %d,\n", m_positionIterations);
66  fprintf(file, " \"drawShapes\": %s,\n", m_drawShapes ? "true" : "false");
67  fprintf(file, " \"drawJoints\": %s,\n", m_drawJoints ? "true" : "false");
68  fprintf(file, " \"drawAABBs\": %s,\n", m_drawAABBs ? "true" : "false");
69  fprintf(file, " \"drawContactPoints\": %s,\n", m_drawContactPoints ? "true" : "false");
70  fprintf(file, " \"drawContactNormals\": %s,\n", m_drawContactNormals ? "true" : "false");
71  fprintf(file, " \"drawContactImpulse\": %s,\n", m_drawContactImpulse ? "true" : "false");
72  fprintf(file, " \"drawFrictionImpulse\": %s,\n", m_drawFrictionImpulse ? "true" : "false");
73  fprintf(file, " \"drawCOMs\": %s,\n", m_drawCOMs ? "true" : "false");
74  fprintf(file, " \"drawStats\": %s,\n", m_drawStats ? "true" : "false");
75  fprintf(file, " \"drawProfile\": %s,\n", m_drawProfile ? "true" : "false");
76  fprintf(file, " \"enableWarmStarting\": %s,\n", m_enableWarmStarting ? "true" : "false");
77  fprintf(file, " \"enableContinuous\": %s,\n", m_enableContinuous ? "true" : "false");
78  fprintf(file, " \"enableSubStepping\": %s,\n", m_enableSubStepping ? "true" : "false");
79  fprintf(file, " \"enableSleep\": %s\n", m_enableSleep ? "true" : "false");
80  fprintf(file, "}\n");
81  fclose(file);
82 }
83 
85 {
86  char* data = nullptr;
87  int size = 0;
88  bool found = sReadFile(data, size, fileName);
89  if (found == false)
90  {
91  return;
92  }
93 
95  if (document.is_valid() == false)
96  {
97  return;
98  }
99 
100  sajson::value root = document.get_root();
101  int fieldCount = int(root.get_length());
102  for (int i = 0; i < fieldCount; ++i)
103  {
104  sajson::string fieldName = root.get_object_key(i);
105  sajson::value fieldValue = root.get_object_value(i);
106 
107  if (strncmp(fieldName.data(), "testIndex", fieldName.length()) == 0)
108  {
109  if (fieldValue.get_type() == sajson::TYPE_INTEGER)
110  {
111  m_testIndex = fieldValue.get_integer_value();
112  }
113  continue;
114  }
115 
116  if (strncmp(fieldName.data(), "windowWidth", fieldName.length()) == 0)
117  {
118  if (fieldValue.get_type() == sajson::TYPE_INTEGER)
119  {
120  m_windowWidth = fieldValue.get_integer_value();
121  }
122  continue;
123  }
124 
125  if (strncmp(fieldName.data(), "windowHeight", fieldName.length()) == 0)
126  {
127  if (fieldValue.get_type() == sajson::TYPE_INTEGER)
128  {
129  m_windowHeight = fieldValue.get_integer_value();
130  }
131  continue;
132  }
133 
134  if (strncmp(fieldName.data(), "hertz", fieldName.length()) == 0)
135  {
136  if (fieldValue.get_type() == sajson::TYPE_DOUBLE || fieldValue.get_type() == sajson::TYPE_INTEGER)
137  {
138  m_hertz = float(fieldValue.get_number_value());
139  }
140  continue;
141  }
142 
143  if (strncmp(fieldName.data(), "velocityIterations", fieldName.length()) == 0)
144  {
145  if (fieldValue.get_type() == sajson::TYPE_INTEGER)
146  {
148  }
149  continue;
150  }
151 
152  if (strncmp(fieldName.data(), "positionIterations", fieldName.length()) == 0)
153  {
154  if (fieldValue.get_type() == sajson::TYPE_INTEGER)
155  {
157  }
158  continue;
159  }
160 
161  if (strncmp(fieldName.data(), "drawShapes", fieldName.length()) == 0)
162  {
163  if (fieldValue.get_type() == sajson::TYPE_FALSE)
164  {
165  m_drawShapes = false;
166  }
167  else if (fieldValue.get_type() == sajson::TYPE_TRUE)
168  {
169  m_drawShapes = true;
170  }
171  continue;
172  }
173  }
174 
175  free(data);
176 }
int m_windowWidth
Definition: settings.h:62
bool m_drawFrictionImpulse
Definition: settings.h:73
bool m_drawShapes
Definition: settings.h:67
static const char * fileName
Definition: settings.cpp:28
void Load()
Definition: settings.cpp:84
float m_hertz
Definition: settings.h:64
int m_positionIterations
Definition: settings.h:66
int get_integer_value() const
Definition: sajson.h:527
const char * data() const
Definition: sajson.h:225
static bool sReadFile(char *&data, int &size, const char *filename)
Definition: settings.cpp:31
bool m_enableContinuous
Definition: settings.h:78
size_t length() const
Definition: sajson.h:229
size_t get_length() const
Definition: sajson.h:457
string get_object_key(size_t index) const
Definition: sajson.h:475
bool m_drawContactNormals
Definition: settings.h:71
value get_root() const
If is_valid(), returns the document&#39;s root value.
Definition: sajson.h:766
bool m_drawContactImpulse
Definition: settings.h:72
bool m_enableWarmStarting
Definition: settings.h:77
bool m_drawCOMs
Definition: settings.h:74
value get_object_value(size_t index) const
Definition: sajson.h:483
void Save()
Definition: settings.cpp:56
bool m_drawContactPoints
Definition: settings.h:70
bool is_valid() const
Definition: sajson.h:761
int m_testIndex
Definition: settings.h:61
int m_velocityIterations
Definition: settings.h:65
bool m_drawProfile
Definition: settings.h:76
bool m_enableSubStepping
Definition: settings.h:79
double get_number_value() const
Definition: sajson.h:541
int m_windowHeight
Definition: settings.h:63
bool m_drawStats
Definition: settings.h:75
bool m_drawAABBs
Definition: settings.h:69
bool m_drawJoints
Definition: settings.h:68
type get_type() const
Returns the JSON value&#39;s type.
Definition: sajson.h:451
bool m_enableSleep
Definition: settings.h:80
document parse(const AllocationStrategy &strategy, const StringType &string)
Definition: sajson.h:2479


mvsim
Author(s):
autogenerated on Tue Jul 4 2023 03:08:21