Răsfoiți Sursa

Important fix in JSONParser

mikymod 12 ani în urmă
părinte
comite
1b81a6aff6
3 a modificat fișierele cu 45 adăugiri și 14 ștergeri
  1. 10 3
      src/Device.cpp
  2. 28 9
      src/JSONParser.cpp
  3. 7 2
      src/JSONParser.h

+ 10 - 3
src/Device.cpp

@@ -529,9 +529,16 @@ void Device::check_preferred_settings()
 void Device::read_engine_settings()
 {
 	DiskFile* file = m_filesystem->open("crown.cfg", FOM_READ);
-	JSONParser json(file);
-	
-	// MUST BE IMPLEMENTED
+	MallocAllocator allocator;
+	JSONParser json(allocator, file);
+
+	char value[128];
+	int  width;
+	json.get_root().get_array("crown", 0).get_string("boot").to_string(value);
+	json.get_root().get_number("width").to_int(width);
+	os::printf("value = %s\n", value);
+	os::printf("width = %d\n", width);
+
 }
 
 

+ 28 - 9
src/JSONParser.cpp

@@ -9,7 +9,8 @@ namespace crown
 {
 
 //--------------------------------------------------------------------------
-JSONParser::JSONParser(File* file, size_t size) :
+JSONParser::JSONParser(Allocator& allocator, File* file, size_t size) :
+	m_allocator(allocator),
 	m_file(file),
 	m_next_token(0),
 	m_prev_token(-1),
@@ -17,7 +18,7 @@ JSONParser::JSONParser(File* file, size_t size) :
 {
 	if (size > 1024)
 	{
-		m_tokens = new JSONToken[size];
+		m_tokens = CE_NEW(m_allocator, JSONToken[size]);
 	}
 	else
 	{
@@ -28,11 +29,7 @@ JSONParser::JSONParser(File* file, size_t size) :
 
 	parse();
 
-	// Test
-	// for (int i = 0; i < m_next_token; i++)
-	// {
-	// 	m_tokens[i].print();
-	// }
+	m_nodes = CE_NEW(m_allocator, JSONNode[16]);
 }
 
 //--------------------------------------------------------------------------
@@ -40,7 +37,11 @@ JSONParser::~JSONParser()
 {
 	if (m_tokens_number > 1024 && m_tokens != NULL)
 	{
-		delete m_tokens;
+		CE_DELETE(m_allocator, m_tokens);
+	}
+	if (m_nodes != NULL)
+	{
+		CE_DELETE(m_allocator, m_nodes);
 	}
 }
 
@@ -355,6 +356,17 @@ void JSONParser::fill_token(JSONToken* token, JSONType type, int32_t start, int3
 	m_file->seek(cur_pos);
 }
 
+//--------------------------------------------------------------------------
+void JSONParser::reset_nodes()
+{
+	CE_DELETE(m_allocator, m_nodes);
+
+	m_nodes = CE_NEW(m_allocator, JSONNode[16]);
+
+	// reset nodes counter 	
+	m_nodes_count = 0;
+}
+
 //--------------------------------------------------------------------------
 JSONParser& JSONParser::get_root()
 {
@@ -580,18 +592,24 @@ JSONParser& JSONParser::get_bool(const char* key)
 void JSONParser::to_string(char* value)
 {
 	string::strcpy(value, m_tokens[m_nodes[m_nodes_count-1].m_id].m_value);
+
+	reset_nodes();
 }
 
 //--------------------------------------------------------------------------
 void JSONParser::to_float(float& value)
 {
 	value = atof(m_tokens[m_nodes[m_nodes_count-1].m_id].m_value);
+
+	reset_nodes();
 }
 
 //--------------------------------------------------------------------------
 void JSONParser::to_int(int& value)
 {
 	value = atoi(m_tokens[m_nodes[m_nodes_count-1].m_id].m_value);
+
+	reset_nodes();
 }
 
 //--------------------------------------------------------------------------
@@ -605,7 +623,8 @@ void JSONParser::to_bool(bool& value)
 	{
 		value = false;
 	}
-}
 
+	reset_nodes();
+}
 
 } //namespace crown

+ 7 - 2
src/JSONParser.h

@@ -6,6 +6,7 @@
 #include "File.h"
 #include "List.h"
 #include "Dictionary.h"
+#include "Allocator.h"
 
 namespace crown
 {
@@ -82,7 +83,7 @@ class JSONParser
 {
 public:
 	/// Constructor
-					JSONParser(File* file, size_t size = 1024);
+					JSONParser(Allocator& allocator, File* file, size_t size = 1024);
 	/// Destructor
 					~JSONParser();
 
@@ -119,7 +120,11 @@ private:
 	JSONToken* 		allocate_token();
 	/// Fill token and set boundaries
 	void			fill_token(JSONToken* token, JSONType type, int32_t start, int32_t end);
+	/// Reset all JSON nodes
+	void			reset_nodes();
 
+
+	Allocator& 		m_allocator;
 	/// JSON data
 	File*			m_file;
 	/// Next token to allocate				
@@ -134,7 +139,7 @@ private:
 	size_t			m_tokens_number;
 
 	/// 
-	JSONNode		m_nodes[128];
+	JSONNode*		m_nodes;
 	///
 	uint32_t 		m_nodes_count;