main
anulax1225 ago%!(EXTRA string=11 months)
parent d7053a3e81
commit bce0459ed3
  1. 9
      src/bakatools.h
  2. 2
      src/bakatools/file_system/file.cpp
  3. 40
      src/bakatools/json/fs_manager.cpp
  4. 8
      src/bakatools/json/fs_manager.h
  5. 10
      src/bakatools/json/lexer.cpp
  6. 2
      src/bakatools/json/lexer.h
  7. 18
      src/bakatools/json/node.cpp
  8. 6
      src/bakatools/json/node.h
  9. 2
      src/bakatools/json/parser.cpp
  10. 17
      src/bakatools/json/tools.cpp
  11. 10
      src/bakatools/json/tools.h
  12. 1
      src/bakatoolspch.h

@ -2,18 +2,25 @@
#include <bakatools/logging/log.h> #include <bakatools/logging/log.h>
#include <bakatools/logging/assert.h> #include <bakatools/logging/assert.h>
#include <bakatools/json/fs_manager.h>
#include <bakatools/json/parser.h> #include <bakatools/json/parser.h>
#include <bakatools/json/lexer.h> #include <bakatools/json/lexer.h>
#include <bakatools/json/node.h> #include <bakatools/json/node.h>
#include <bakatools/json/fs_manager.h>
#include <bakatools/json/tools.h>
#include <bakatools/string/string_tools.h> #include <bakatools/string/string_tools.h>
#include <bakatools/container/data_stream.h> #include <bakatools/container/data_stream.h>
#include <bakatools/container/trie.h> #include <bakatools/container/trie.h>
#include <bakatools/thread/task_timer.h> #include <bakatools/thread/task_timer.h>
#include <bakatools/thread/task_delayer.h> #include <bakatools/thread/task_delayer.h>
#include <bakatools/file_system/dir_entry.h> #include <bakatools/file_system/dir_entry.h>
#include <bakatools/file_system/directory.h> #include <bakatools/file_system/directory.h>
#include <bakatools/file_system/file.h> #include <bakatools/file_system/file.h>
#include <bakatools/file_system/file_watcher.h> #include <bakatools/file_system/file_watcher.h>
#include <bakatools/time/time_span.h> #include <bakatools/time/time_span.h>
#include <bakatools/time/time_point.h> #include <bakatools/time/time_point.h>

@ -19,6 +19,7 @@ namespace Bk {
std::ifstream ifrm(path(), std::ios::binary); std::ifstream ifrm(path(), std::ios::binary);
data.resize(size); data.resize(size);
ifrm.read(data.data(), data.size()); ifrm.read(data.data(), data.size());
ifrm.close();
return Type::DataStream(data); return Type::DataStream(data);
} }
return Type::DataStream(); return Type::DataStream();
@ -29,6 +30,7 @@ namespace Bk {
std::ofstream ofrm(ent.path(), std::ios::binary); std::ofstream ofrm(ent.path(), std::ios::binary);
if (!ofrm) return false; if (!ofrm) return false;
ofrm.write(stream.payload.data(), stream.payload.size()); ofrm.write(stream.payload.data(), stream.payload.size());
ofrm.close();
return true; return true;
} }
} }

@ -85,7 +85,7 @@ namespace Bk::Json
} }
} }
void FsManager::insert(Object* object) int FsManager::insert(Object* object)
{ {
if (!config->is_null()) if (!config->is_null())
{ {
@ -113,32 +113,28 @@ namespace Bk::Json
} }
(*conf_obj)["id_count"]->set_float((*conf_obj)["id_count"]->get_int() + 1); (*conf_obj)["id_count"]->set_float((*conf_obj)["id_count"]->get_int() + 1);
set_config(conf_obj); set_config(conf_obj);
return (*object)["id"]->get_int();
} }
return -1;
} }
void FsManager::update(Object* object) bool FsManager::update(Object* object)
{ {
if (object->find("id") != object->end()) if (Json::as_key("id", *object))
{ {
int id = (*object)["id"]->get_int(); int id = (*object)["id"]->get_int();
auto f_list = File(Tools::string_format("%s/%d.json", path, get_page(id))); auto f_list = File(Tools::string_format("%s/%d.json", path, get_page(id)));
if (f_list.exists()) if (f_list.exists())
{ {
auto ls = Parser(f_list).parse(); auto ls = Parser(f_list).parse();
for (int i = 0; i < ls->get_list().size(); i++) int i = id - get_page(id) * config->get_object()["page_size"]->get_int();
{
auto node = ls->get_p_list()->at(i); auto node = ls->get_p_list()->at(i);
if (node->get_object().find("id") != node->get_object().end())
{
if (node->get_object()["id"]->get_int() == id)
{
ls->get_p_list()->at(i)->set_object(object); ls->get_p_list()->at(i)->set_object(object);
}
}
}
write(f_list, ls); write(f_list, ls);
return true;
} }
} }
return false;
} }
void FsManager::remove(int id) void FsManager::remove(int id)
@ -163,25 +159,23 @@ namespace Bk::Json
} }
} }
Object FsManager::findby_id(int id) Pointer FsManager::findby_id(int id)
{
if (!config->is_null())
{ {
auto f_path = Tools::string_format("%s/%d.json", path, get_page(id)); auto f_path = Tools::string_format("%s/%d.json", path, get_page(id));
auto f_list = File(f_path); auto f_list = File(f_path);
if (f_list.exists()) if (f_list.exists())
{ {
auto ls = Parser(f_list).parse(); auto ls = Parser(f_list).parse();
for (int i = 0; i < ls->get_list().size(); i++) int i = id - get_page(id) * config->get_object()["page_size"]->get_int();
{
auto node = ls->get_p_list()->at(i); auto node = ls->get_p_list()->at(i);
if (node->get_object().find("id") != node->get_object().end()) auto node_id = Json::find_key("id", node->get_object());
{ if (!node_id->is_null() && node_id->is_type(Node::Type::NUMBER) && node_id->get_int() == id) return node;
if (node->get_object()["id"]->get_int() == id)
{
return node->get_object();
}
} }
} }
} Pointer node(new Node);
return Object(); node->set_null();
return node;
} }
} }

@ -2,6 +2,7 @@
#include <bakatools/file_system/directory.h> #include <bakatools/file_system/directory.h>
#include <bakatools/file_system/file.h> #include <bakatools/file_system/file.h>
#include "parser.h" #include "parser.h"
#include "tools.h"
namespace Bk::Json namespace Bk::Json
{ {
@ -21,10 +22,11 @@ namespace Bk::Json
void write_config(); void write_config();
Object get_config() { return config->get_object(); } Object get_config() { return config->get_object(); }
int get_page(float id) { return (int)(id/100.0f); } int get_page(float id) { return (int)(id/100.0f); }
int get_page(int id) { return (int)(id/100.0f); }
void insert(Object* object); int insert(Object* object);
void update(Object* object); bool update(Object* object);
void remove(int id); void remove(int id);
Object findby_id(int id); Pointer findby_id(int id);
}; };
} }

@ -24,14 +24,8 @@ namespace Bk::Json
while ((c == ' ' || c == '\n')) while ((c == ' ' || c == '\n'))
{ {
c = get_next_char(); // check c = get_next_char(); // check
if ((c == ' ' || c == '\n') && !data.size()) if ((c == ' ' || c == '\n') && !data.size()) break;
{ else if (!data.size()) return c;
throw std::logic_error("Ran out of tokens");
}
else if (!data.size())
{
return c;
}
} }
return c; return c;
} }

@ -25,7 +25,7 @@ namespace Bk::Json
struct Token struct Token
{ {
std::string value; std::string value;
TokenType type; TokenType type = TokenType::NULL_TYPE;
std::string to_string(); std::string to_string();
}; };

@ -71,14 +71,14 @@ namespace Bk::Json
{ {
if (type == Type::OBJECT) if (type == Type::OBJECT)
return values.object; return values.object;
throw std::logic_error("Improper return"); throw std::logic_error("Json value not an object");
} }
Object Node::get_object() Object Node::get_object()
{ {
if (type == Type::OBJECT) if (type == Type::OBJECT)
return *values.object; return *values.object;
throw std::logic_error("Improper return"); throw std::logic_error("Json value not an object");
} }
void Node::set_list(List* list) void Node::set_list(List* list)
@ -91,14 +91,14 @@ namespace Bk::Json
{ {
if (type == Type::LIST) if (type == Type::LIST)
return values.list; return values.list;
throw std::logic_error("Improper return"); throw std::logic_error("Json value not a list");
} }
List Node::get_list() List Node::get_list()
{ {
if (type == Type::LIST) if (type == Type::LIST)
return *values.list; return *values.list;
throw std::logic_error("Improper return"); throw std::logic_error("Json value not a list");
} }
void Node::set_string(std::string* str) void Node::set_string(std::string* str)
@ -111,14 +111,14 @@ namespace Bk::Json
{ {
if (type == Type::STRING) if (type == Type::STRING)
return values.s; return values.s;
throw std::logic_error("Improper return"); throw std::logic_error("Json value not a string");
} }
std::string Node::get_string() std::string Node::get_string()
{ {
if (type == Type::STRING) if (type == Type::STRING)
return *values.s; return *values.s;
throw std::logic_error("Improper return"); throw std::logic_error("Json value not a string");
} }
void Node::set_bool(bool value) void Node::set_bool(bool value)
@ -131,7 +131,7 @@ namespace Bk::Json
{ {
if (type == Type::BOOLEAN) if (type == Type::BOOLEAN)
return values.bValue; return values.bValue;
throw std::logic_error("Improper return"); throw std::logic_error("Json value not a boolean");
} }
void Node::set_float(float value) void Node::set_float(float value)
@ -144,7 +144,7 @@ namespace Bk::Json
{ {
if (type == Type::NUMBER) if (type == Type::NUMBER)
return values.fValue; return values.fValue;
throw std::logic_error("Improper return"); throw std::logic_error("Json value not a number");
} }
void Node::set_int(int value) void Node::set_int(int value)
@ -157,7 +157,7 @@ namespace Bk::Json
{ {
if (type == Type::NUMBER) if (type == Type::NUMBER)
return (int)values.fValue; return (int)values.fValue;
throw std::logic_error("Improper return"); throw std::logic_error("Json value not a number");
} }
void Node::set_null() void Node::set_null()

@ -7,7 +7,7 @@ namespace Bk::Json
class Node; class Node;
using Pointer = std::shared_ptr<Node>; using Pointer = std::shared_ptr<Node>;
using Object = std::map<std::string, Pointer>; using Object = std::unordered_map<std::string, Pointer>;
using List = std::vector<Pointer>; using List = std::vector<Pointer>;
class Node class Node
@ -22,6 +22,10 @@ namespace Bk::Json
BOOLEAN, BOOLEAN,
NULL_TYPE NULL_TYPE
}; };
Type get_type() { return type; }
bool is_type(Type type) { return type == this->type; }
void set_object(Object* object); void set_object(Object* object);
Object* get_p_object(); Object* get_p_object();
Object get_object(); Object get_object();

@ -79,7 +79,7 @@ namespace Bk::Json
} }
catch(std::logic_error& e) catch(std::logic_error& e)
{ {
BK_WARNING(e.what()); BK_TRACE(e.what());
auto node = Pointer(); auto node = Pointer();
node->set_null(); node->set_null();
root = node; root = node;

@ -0,0 +1,17 @@
#include "tools.h"
namespace Bk::Json
{
Pointer find_key(std::string name, Object obj)
{
Pointer node(new Node());
if (obj.find(name) != obj.end()) node = obj[name];
else node->set_null();
return node;
}
bool as_key(std::string name, Object obj)
{
return obj.find(name) != obj.end();
}
}

@ -0,0 +1,10 @@
#pragma once
#include <bakatoolspch.h>
#include "node.h"
namespace Bk::Json
{
Pointer find_key(std::string name, Object obj);
bool as_key(std::string name, Object obj);
}

@ -4,6 +4,7 @@
#include <cstring> #include <cstring>
#include <vector> #include <vector>
#include <map> #include <map>
#include <unordered_map>
#include <string> #include <string>
#include <cstdint> #include <cstdint>
#include <cctype> #include <cctype>

Loading…
Cancel
Save