Fix json conversion.

- Fix converting project json to project structure causing an error.
- Add vscode debugging scripts.
This commit is contained in:
realaltffour 2020-04-12 09:40:00 +03:00
parent b1008403bb
commit 688fc19356
No known key found for this signature in database
GPG Key ID: 7115CD2AC9A76A56
9 changed files with 163 additions and 7 deletions

16
.vscode/c_cpp_properties.json vendored Normal file
View File

@ -0,0 +1,16 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}

29
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,29 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/bin/skidjular",
"args": ["proj", "add", "name"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}/build/bin/",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "Build Debug"
}
]
}

12
.vscode/tasks.json vendored Normal file
View File

@ -0,0 +1,12 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Build Debug",
"type": "shell",
"command": "./build.sh"
}
]
}

View File

@ -42,6 +42,7 @@ void to_json(json& j, const DB& db) {
{"lastAccessTime", db.lastAccessTime}};
}
void from_json(const json& j, DB& db) {
db.projects = new std::map<std::string, Project>();
j.at("projects").get_to(*db.projects);
j.at("creationDate").get_to(db.creationDate);
j.at("lastAccessTime").get_to(db.lastAccessTime);
@ -57,11 +58,19 @@ void writeDB(DB db, const std::string& dest) {
std::cout << "Error: " << ex.what() << std::endl;
}
}
bool is_empty(std::ifstream& pFile) {
return pFile.peek() == std::ifstream::traits_type::eof();
}
void loadDB(DB& db, const std::string& src) {
try {
std::ifstream f(src);
json j;
if (is_empty(f)) {
std::cout << "Database is not present, someone forgot to create it!"
<< std::endl;
exit(1);
}
f >> j;
db = j;
} catch (const std::exception& ex) {

View File

@ -6,16 +6,21 @@
#include "data.h"
#include "module.h"
#include "modules/init.h"
#include "modules/proj.h"
Module* get_Mod(std::string name) {
if (name == "init") {
return init_new();
} else if (name == "proj") {
return proj_new();
}
return nullptr;
};
void dispatch_Mod(std::string mod, std::string args) {
void dispatch_Mod(std::string mod, std::vector<std::string> args) {
if (mod == "init") {
init_dispatch(args);
} else if (mod == "proj") {
proj_dispatch(args);
}
};
@ -28,7 +33,7 @@ int main(int argc, const char* argv[]) {
general.add_options()("help", "Help Message")(
"help-module", value<string>(), "Module for help message")(
"module", value<string>(), "Module to execute")(
"module-args", value<string>(), "Arguments to use");
"module-args", value<vector<string>>(), "Arguments to use");
positional_options_description general_positional;
general_positional.add("module", 1);
general_positional.add("module-args", 10);
@ -60,10 +65,10 @@ int main(int argc, const char* argv[]) {
exit(1);
}
if (vm.count("module-args")) {
auto args = vm["module-args"].as<string>();
auto args = vm["module-args"].as<vector<string>>();
dispatch_Mod(name, args);
} else
dispatch_Mod(name, "");
dispatch_Mod(name, {""});
} else {
std::cout << general << "\n";
exit(0);

View File

@ -24,7 +24,7 @@ void init_create(init_args arg) {
writeDB(db, ".db");
}
void init_dispatch(std::string arguments) {
void init_dispatch(const std::vector<std::string>& arguments) {
init_args arg;
init_create(arg);
}

View File

@ -2,9 +2,9 @@
#define MODULE_INIT_H
#include <time.h>
#include <ctime>
#include <iostream>
#include <vector>
#include "module.h"
@ -13,6 +13,6 @@ struct init_mod : init_args, Module {};
init_mod* init_new();
void init_create(init_args arg);
void init_dispatch(std::string arguments);
void init_dispatch(const std::vector<std::string>& arguments);
#endif

60
src/modules/proj.cpp Normal file
View File

@ -0,0 +1,60 @@
#include "proj.h"
#include <string.h>
#include "../data.h"
void proj_add(proj_args args) {}
void proj_rm(proj_args args) {}
void proj_list(proj_args args) {}
proj_mod* proj_new() {
proj_mod* mod = new proj_mod{};
mod->name = "Project Manager";
mod->desc =
std::string("Manages projects list with these commands:\n") +
std::string("- skidjular proj add <Name> (Adds new project).\n") +
std::string("- skidjular proj rm<Name> (Removes a project).\n") +
std::string("- skidjular proj list/ls<name> (Lists commands)\n");
return mod;
}
void proj_dispatch(const std::vector<std::string>& arguments) {
proj_args args;
/* Get the action type. */
auto type_str = arguments[0];
if (type_str == "add")
args.type = projActType::Add;
else if (type_str == "rm")
args.type = projActType::Remove;
else if (type_str == "ls" || type_str == "list")
args.type = projActType::List;
else {
std::cout << "Project command action unkown!" << std::endl;
exit(1);
}
/* Get action type. */
auto type_action = std::string{""};
for (size_t i = 1; i < arguments.size(); i++) {
type_action += arguments[i];
type_action += " ";
}
type_action.pop_back();
args.action_param = type_action;
if (type_str == "add")
proj_add(args);
else if (type_str == "rm")
proj_rm(args);
else if (type_str == "list")
proj_list(args);
args.db = new DB();
loadDB(*args.db, ".db");
std::cout << "Action: " << type_str << std::endl
<< "Args: " << type_action << std::endl;
}

25
src/modules/proj.h Normal file
View File

@ -0,0 +1,25 @@
#ifndef MODULE_PROJ_H
#define MODULE_PROJ_H
#include <iostream>
#include <vector>
#include "data.h"
#include "module.h"
enum projActType { Add, Remove, List };
struct proj_args {
projActType type;
std::string action_param;
DB* db;
};
struct proj_mod : proj_args, Module {};
proj_mod* proj_new();
void proj_add(proj_args args);
void proj_rm(proj_args args);
void proj_list(proj_args args);
void proj_dispatch(const std::vector<std::string>& arguments);
#endif