Пример #1
0
void interface::config::verify_config(
        const std::string& location,
        const vector<shared_ptr<interface::config_info_interface>>& config,
        nlohmann::json js) const
{
    json::parser_callback_t cb = [&](int depth, json::parse_event_t event, json& parsed) {
        if(event == json::parse_event_t::key) {
            string key = parsed;
            bool found = false;
            for(auto item : config) {
                if(item->name() == key) {
                    found = true;
                    break;
                }
            }
            if(!found) {
                int distance = numeric_limits<int>::max();
                string suggestion;
                for(auto item : config) {
                    int test = LevenshteinDistance(item->name(), key);
                    if(test < distance) {
                        distance = test;
                        suggestion = item->name();
                    }
                }
                stringstream ss;
                ss << "key '" << key << "'" << " not found, did you mean '" << suggestion << "'";
                throw invalid_argument(ss.str());
            }
        }
        return true;
    };

    string text;
    try {
        text = js.dump();
        json::parse(text, cb);
    } catch( exception err ) {
        // This is not an error, it just means the json is empty
//        stringstream ss;
//        ss << "parse error for json '" << text << "' in " << location;
//        throw runtime_error(ss.str());
    }
}
Пример #2
0
/**
 * Call the lua script to plot the Stock
 * @param data json object of the Stock
 */
void Plot::plot(const nlohmann::json &data) const {
  /** Convert the json to a string */
  std::string data_str = data.dump();
  /** Create a table on the lua Stack to communicate with the script */
  lua_createtable(this->lua_state, 1, 0);
  /** Push the index 1 to the Stack */
  lua_pushnumber(this->lua_state, 1);
  /** Push the json data to the Stack */
  lua_pushstring(this->lua_state, data_str.c_str());
  /** Submit the Stack */
  lua_settable(this->lua_state, -3);
  /** Make the stack available for lua */
  lua_setglobal(this->lua_state, "arg");
  /** Run the lua script */
  int status = luaL_dofile(this->lua_state, this->path_to_script.c_str());
  /** Check if the script executed with errors */
  if (status != LUA_OK) {
    /** Throw a PlotScriptError with the error message of the lua script */
    throw(PlotScriptError(lua_tostring(lua_state, -1)));
  }
}
Пример #3
0
void c_rpc_server::c_session::send_response(nlohmann::json json_response)
{
	try {
		const std::string response = json_response.dump();
		assert(response.size() <= std::numeric_limits<uint16_t>::max());
		uint16_t size = static_cast<uint16_t>(response.size());
		m_write_data.resize(size + 2); ///< 2 first bytes for size
		m_write_data.at(0) = static_cast<char>(size >> 8);
		m_write_data.at(1) = static_cast<char>(size & 0xFF);
		for (size_t i = 0; i < response.size(); ++i)
			m_write_data.at(i + 2) = response.at(i);
		// send response

		dbg("send packet");
		dbg(m_write_data);
		std::array<unsigned char, crypto_auth_hmacsha512_BYTES> hash;
		int ret = crypto_auth_hmacsha512(hash.data(), reinterpret_cast<unsigned char *>(&m_write_data.at(2)), size, m_rpc_server_ptr->m_hmac_key.data());
		if (ret != 0) _throw_error(std::runtime_error("crypto_auth_hmacsha512 error"));
		dbg("hmac");
		//for (const auto & byte : hash) std::cout << std::hex << "0x" << static_cast<int>(byte) << " ";
		//std::cout << std::dec << std::endl;
		std::array<boost::asio::const_buffer, 2> buffers = {
			{boost::asio::buffer(m_write_data.data(), m_write_data.size()),
			boost::asio::buffer(hash.data(), hash.size())}
		};
		m_socket.async_write_some(buffers,
			[this](const boost::system::error_code& error, std::size_t bytes_transferred) {
				write_handler(error, bytes_transferred);
		});
	}
	catch (const std::exception &e) {
		_erro( "exception in execute_rpc_command " << e.what() );
		_erro( "close connection\n" );
		delete_me();
		return;
	}
}
Пример #4
0
  to_if_held_down(const nlohmann::json& json) : dispatcher_client(),
                                                current_held_down_id_(0) {
    try {
      if (json.is_object()) {
        to_ = std::vector<to_event_definition>{
            json.get<to_event_definition>(),
        };

      } else if (json.is_array()) {
        to_ = json.get<std::vector<to_event_definition>>();

      } else {
        throw pqrs::json::unmarshal_error(fmt::format("json must be object or array, but is `{0}`", json.dump()));
      }

    } catch (...) {
      detach_from_dispatcher();
      throw;
    }
  }
#include <catch2/catch.hpp>

#include "virtual_hid_device_utility.hpp"

TEST_CASE("karabiner_virtual_hid_device::hid_report::modifiers") {
  {
    pqrs::karabiner_virtual_hid_device::hid_report::modifiers modifiers;
    modifiers.insert(pqrs::karabiner_virtual_hid_device::hid_report::modifier::left_shift);
    modifiers.insert(pqrs::karabiner_virtual_hid_device::hid_report::modifier::right_command);

    nlohmann::json expected = nlohmann::json::array();
    expected.push_back("left_shift");
    expected.push_back("right_command");

    REQUIRE(nlohmann::json(modifiers).dump() == expected.dump());
  }
}

TEST_CASE("karabiner_virtual_hid_device::hid_report::keys") {
  {
    pqrs::karabiner_virtual_hid_device::hid_report::keys keys;
    keys.insert(static_cast<uint8_t>(*(krbn::types::make_hid_usage(krbn::key_code::a))));
    keys.insert(static_cast<uint8_t>(*(krbn::types::make_hid_usage(krbn::key_code::b))));
    keys.insert(static_cast<uint8_t>(*(krbn::types::make_hid_usage(krbn::key_code::c))));
    keys.erase(static_cast<uint8_t>(*(krbn::types::make_hid_usage(krbn::key_code::a))));

    nlohmann::json expected = nlohmann::json::array();
    expected.push_back("b");
    expected.push_back("c");

    REQUIRE(krbn::virtual_hid_device_utility::to_json(keys, krbn::hid_usage_page::keyboard_or_keypad).dump() == expected.dump());
Пример #6
0
 std::shared_ptr<bson> collection::convert_to_bson(nlohmann::json const& value)
 {
     return std::shared_ptr<bson>(json2bson(value.dump().c_str()), bson_del);
 }