Commit 780de4ef authored by captainwong's avatar captainwong

update

parent 6136c8cd
......@@ -10,6 +10,8 @@
#include <curl/curl.h>
#include <string>
#include <initializer_list>
#include <memory>
#include "mem_tool.h"
namespace jlib
......@@ -30,6 +32,7 @@ struct Curl
Curl() { static CurlHelper helper = {}; }
~Curl() {
headerFreer_.reset();
curl_easy_cleanup(curl_);
}
......@@ -54,6 +57,15 @@ struct Curl
long lastHttpCode() const { return lastHttpCode_; }
std::string lastHttpContent() const { return lastHttpContent_; }
void setHeaders(std::initializer_list<const char*> headers) {
curl_slist* cheaders = nullptr;
for (auto& header : headers) {
cheaders = curl_slist_append(cheaders, header);
}
curl_easy_setopt(curl_, CURLOPT_HTTPHEADER, headers);
headerFreer_ = std::make_shared<jlib::auto_free<curl_slist>>(cheaders, [](curl_slist* p) { curl_slist_free_all(p); });
}
bool get(const std::string& url, int timeout = 10) {
curl_easy_setopt(curl_, CURLOPT_URL, url.data());
curl_easy_setopt(curl_, CURLOPT_TIMEOUT, timeout);
......@@ -100,6 +112,7 @@ struct Curl
///////////////////////////// details ///////////////////////////////
CURL* curl_ = nullptr;
std::shared_ptr<jlib::auto_free<curl_slist>> headerFreer_ = {};
std::string buffer_ = {};
CURLcode lastErorrCode_ = CURLcode::CURLE_OK;
std::string lastErrorMsg_ = {};
......
#pragma once
#include <string>
namespace jlib
{
struct JsoncppHelper {
template <typename JsonValue>
static bool safelyGetIntValueStrict(const JsonValue& value, const char* name, int& out) {
if (value[name].isInt()) { out = value[name].asInt(); return true; }
return false;
}
template <typename JsonValue>
static bool safelyGetIntValue(const JsonValue& value, const char* name, int& out) {
if (safelyGetIntValueStrict(value, name, out)) {
return true;
} else if (value[name].isString()) {
out = std::stoi(value[name].asString());
return true;
}
return false;
}
template <typename JsonValue>
static bool safelyGetStringValue(const JsonValue& value, const char* name, std::string& out) {
if (value[name].isString()) { out = value[name].asString(); return true; }
return false;
}
template <typename JsonValue, typename QString>
static bool safelyGetStringValue(const JsonValue& value, const char* name, QString& out) {
if (value[name].isString()) { out = value[name].asCString(); return true; }
return false;
}
};
}
......@@ -25,12 +25,12 @@
DECLARE_GETTER(int, val) \
DECLARE_SETTER(int, val)
#define DECLARE_GETTER_STRING(val) \
#define DECLARE_GETTER_WCHAR_CSTR(val) \
const wchar_t* get##val() const { \
return val; \
}
#define DECLARE_SETTER_STRING(val) \
#define DECLARE_SETTER_WCHAR_CSTR(val) \
void set##val(const wchar_t* param) { \
if (param) { \
int len = wcslen(param); \
......@@ -40,14 +40,14 @@
} else { \
if (val) { delete[] val; } \
val = new wchar_t[1]; \
val[0] = 0; \
val[0] = '\0'; \
} \
}
#define DECLARE_GETTER_SETTER_CSTRING(val) \
DECLARE_GETTER(CString, val); \
DECLARE_SETTER(CString, val);
#define DECLARE_GETTER_SETTER_WCHAR_STR(val) \
DECLARE_SETTER_WCHAR_CSTR(val); \
DECLARE_SETTER_WCHAR_CSTR(val);
#define INITIALIZE_STRING(val) { val = new wchar_t[1]; val[0] = 0; }
#define INITIALIZE_STRING(val) { val = new wchar_t[1]; val[0] = '\0'; }
......@@ -25,7 +25,7 @@ static inline std::string human_readable_byte_count(uintmax_t bytes, PositionalN
auto pre = std::string("KMGTPEZYBND").at(exp - 1) + std::string(po == PositionalNotation::Binary ? "i" : "");
auto var = bytes / std::pow(unit, exp);
char buf[64] = { 0 };
snprintf(buf, sizeof(buf), "%.1lf%sB", var, pre.data());
snprintf(buf, sizeof(buf), "%g%sB", var, pre.data());
return buf;
}
......
......@@ -136,6 +136,14 @@ enum {
typedef std::unordered_map<QueryType, std::wstring> QueryResults;
/**
* @brief 查询单个信息
* @param[in] queryType QueryType
* @return 查询结果
*/
static std::wstring query(QueryType type);
/**
* @brief 查询信息
* @param[in] queryTypes QueryType集合
......@@ -324,6 +332,17 @@ static bool query(size_t queryTypes, QueryResults& results)
return ok;
}
static std::wstring query(QueryType type)
{
QueryResults results;
auto ok = query(static_cast<size_t>(type), results);
if (ok && !results.empty()) {
return results.begin()->second;
} else {
return std::wstring(L"Query ") + queryTypeString(type) + L" failed";
}
}
static bool query(const std::vector<QueryType>& queryTypes, QueryResults& results)
{
std::vector<std::wstring> values, errors;
......
......@@ -88,6 +88,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "util", "util", "{E13021FA-C
ProjectSection(SolutionItems) = preProject
..\jlib\util\chrono_wrapper.h = ..\jlib\util\chrono_wrapper.h
..\jlib\util\curl_wrapper.h = ..\jlib\util\curl_wrapper.h
..\jlib\util\jsoncpp_helper.h = ..\jlib\util\jsoncpp_helper.h
..\jlib\util\mem_tool.h = ..\jlib\util\mem_tool.h
..\jlib\util\micro_getter_setter.h = ..\jlib\util\micro_getter_setter.h
..\jlib\util\rand.h = ..\jlib\util\rand.h
......
......@@ -25,6 +25,8 @@ void test()
printf("\nDone!\n");
}
int main()
{
QueryResults results;
......
......@@ -28,7 +28,8 @@ int main()
//wmi.execute(L"SELECT SerialNumber FROM Win32_DiskDrive WHERE Index = 4");
//wmi.execute(L"SELECT * FROM Win32_DiskPartition");
//wmi.execute(L"SELECT Caption FROM Win32_BootConfiguration");
wmi.execute(L"Select Name from Win32_OperatingSystem");
//wmi.execute(L"Select Name from Win32_OperatingSystem");
wmi.execute(L"SELECT * FROM Win32_VideoController");
//wmi.execute(L"SELECT PNPDeviceID FROM Win32_LogicalDisk WHERE NAME = 'C:'");
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment