huago-corrcet_tools/HuaGoCorrect/id_config.cpp

102 lines
2.2 KiB
C++

#include "stdafx.h"
#include "id_config.h"
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <corecrt_io.h>
id_config::id_config()
{
jsoninit();
}
std::vector<ID_Config> id_config::GetConfig()
{
std::vector<ID_Config> configs;
try
{
if (access(ID_JSON_PATH.c_str(), 0) == 0) {
std::ifstream i(ID_JSON_PATH);
json j;
i >> j;
for (json::iterator it = j.begin(); it != j.end(); ++it)
{
auto tmv = it.value();
configs.push_back(to_param(tmv));
}
}
else
throw "file error";
}
catch (...)
{
configs.clear();
json j = GetDefaultJson();
for (json::iterator it = j.begin(); it != j.end(); ++it)
{
auto tmv = it.value();
configs.push_back(to_param(tmv));
}
}
return configs;
}
void id_config::SaveConfig(std::vector<ID_Config> configs)
{
json j = json::array();
for (int i = 0; i < configs.size(); i++)
{
j.push_back(to_json({configs[i].vid,configs[i].pid,configs[i].cpu_type,configs[i].driver_type}));
}
std::ofstream ofs(ID_JSON_PATH);
ofs << std::setw(4) << j << std::endl;
}
void id_config::SaveConfig(json j)
{
std::ofstream ofs(ID_JSON_PATH);
ofs << std::setw(4) << j << std::endl;
}
json id_config::GetDefaultJson()
{
json j = json::array();
j.push_back(to_json({ 0x3072, 0x239, 3399, 200 }));
j.push_back(to_json({ 0x3072, 0x139, 3399, 100 }));
j.push_back(to_json({ 0x3072, 0x300, 3288, 300 }));
j.push_back(to_json({ 0x3072, 0x400, 3288, 400 }));
j.push_back(to_json({ 0x3072, 0x339, 3399, 300 }));
j.push_back(to_json({ 0x3072, 0x439, 3399, 400 }));
j.push_back(to_json({ 0x31c9, 0x8629,3399, 100 }));
j.push_back(to_json({ 0x31c9, 0x8739,3399, 200 }));
return j;
}
void id_config::jsoninit()
{
if (access(ID_JSON_PATH.c_str(),0) != 0)
{
std::ofstream ofs(ID_JSON_PATH);
ofs << std::setw(4) << GetDefaultJson() << std::endl;
}
}
json id_config::to_json(ID_Config param)
{
return json{
{"vid",param.vid},
{"pid",param.pid},
{"cpu",param.cpu_type},
{"type",param.driver_type} };
}
ID_Config id_config::to_param(json j)
{
ID_Config tmp;
j.at("vid").get_to(tmp.vid);
j.at("pid").get_to(tmp.pid);
j.at("cpu").get_to(tmp.cpu_type);
j.at("type").get_to(tmp.driver_type);
return tmp;
}