add GetIpAddress

This commit is contained in:
aozhiwei 2018-11-17 15:40:53 +08:00
parent 70de530831
commit 98259b5f2c
4 changed files with 45 additions and 1 deletions

View File

@ -53,6 +53,17 @@ namespace a8
return *this;
}
a8::MutableXObject& MutableXObject::SetVal(const std::string& key, a8::XObject& val)
{
if (type_ != XOT_OBJECT) {
abort();
}
std::shared_ptr<a8::XObject> p = std::make_shared<a8::XObject>();
val.Move(*p.get());
(*value_.object_value)[key] = p;
return *this;
}
a8::MutableXObject& MutableXObject::SetVal(const std::string& key, a8::MutableXObject& val)
{
if (type_ != XOT_OBJECT) {

View File

@ -17,6 +17,7 @@ namespace a8
a8::MutableXObject& SetVal(int i, a8::XValue val);
a8::MutableXObject& SetVal(int i, a8::MutableXObject&& val);
a8::MutableXObject& SetVal(const std::string& key, a8::XValue val);
a8::MutableXObject& SetVal(const std::string& key, a8::XObject& val);
a8::MutableXObject& SetVal(const std::string& key, a8::MutableXObject& val);
};

View File

@ -1,6 +1,9 @@
#include <unistd.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <memory.h>
#include <a8/a8.h>
#include <a8/sysutils.h>
@ -117,10 +120,37 @@ namespace a8
bool MkDir(const std::string& path)
{
if (access(path.c_str(), W_OK) == 0) {
return 0;
return true;
}
int status = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
return status == 0;
}
void ForceCreateDir(const std::string& path)
{
if (access(path.c_str(), W_OK) == 0) {
return;
}
std::vector<std::string> strings;
a8::Split(path, strings, '/');
std::string cur_dir = "/";
for (auto& tmp_str : strings) {
cur_dir += tmp_str + "/";
if (access(cur_dir.c_str(), W_OK) != 0) {
a8::MkDir(cur_dir);
}
}
}
std::string GetIpAddress(unsigned long ip_saddr)
{
in_addr addr;
memset(&addr, 0, sizeof(addr));
addr.s_addr = ip_saddr;
char buf[255];
buf[0] = '\0';
inet_ntop(AF_INET, &addr, buf, 255);
return std::string(buf);
}
}

View File

@ -61,6 +61,8 @@ namespace a8
bool HasBitFlag(const long long& flag_bits, int bit);
void XPrintf(const char* fmt, std::initializer_list<a8::XValue> args);
bool MkDir(const std::string& path);
void ForceCreateDir(const std::string& path);
std::string GetIpAddress(unsigned long ip_saddr);
}