IPAddress IPAddress::Parse(RCString ipString) { byte buf[16]; if (CCheck(::inet_pton(AF_INET, ipString, buf))) return IPAddress(ConstBuf(buf, 4)); if (CCheck(::inet_pton(AF_INET6, ipString, buf))) return IPAddress(ConstBuf(buf, 16)); #if UCFG_USE_REGEX # if UCFG_WIN32 if (regex_search(ipString, *s_reDomainName)) { # else if (regex_search(ipString.c_str(), s_reDomainName)) { # endif IPAddress r; r.AddressFamily = Ext::AddressFamily(AF_DOMAIN_NAME); r.m_domainname = ipString; return r; } #endif Throw(HRESULT_FROM_WIN32(DNS_ERROR_INVALID_IP_ADDRESS)); } bool IPAddress::TryParse(RCString s, IPAddress& ip) { byte buf[16]; if (CCheck(::inet_pton(AF_INET, s, buf))) { ip = IPAddress(ConstBuf(buf, 4)); return true; } if (CCheck(::inet_pton(AF_INET6, s, buf))) { ip = IPAddress(ConstBuf(buf, 16)); return true; } return false; }
json_t *JsonParser::CopyToJsonT(const VarValue& v) { switch (v.type()) { case VarType::Null: return ::json_null(); case VarType::Bool: return json_boolean(v.ToBool()); case VarType::Int: return ::json_integer(v.ToInt64()); case VarType::Float: return ::json_real(v.ToDouble()); case VarType::String: return ::json_string(v.ToString()); case VarType::Array: { JsonHandle json(::json_array()); //!!!R VarValue r = JsonVarValueObj::FromJsonT(json); for (int i=0; i<v.size(); ++i) { CCheck(::json_array_append_new(json, CopyToJsonT(v[i]))); } return json.Detach(); } case VarType::Map: { JsonHandle json(::json_object()); vector<String> keys = v.Keys(); EXT_FOR (const String& key, keys) { CCheck(::json_object_set_new(json, key, CopyToJsonT(v[key]))); } return json.Detach(); } default: Throw(E_NOTIMPL); }
bool IPAddress::TryParse(RCString s, IPAddress& ip) { uint8_t buf[16]; if (CCheck(::inet_pton(AF_INET, s, buf))) { ip = IPAddress(Span(buf, 4)); return true; } if (CCheck(::inet_pton(AF_INET6, s, buf))) { ip = IPAddress(Span(buf, 16)); return true; } return false; }
String Environment::GetMachineType() { #if UCFG_USE_POSIX utsname u; CCheck(::uname(&u)); return u.machine; #elif defined(WIN32) SYSTEM_INFO si; GetSystemInfo(&si); switch (si.wProcessorArchitecture) { case PROCESSOR_ARCHITECTURE_INTEL: return "x86"; case PROCESSOR_ARCHITECTURE_MIPS: return "MIPS"; case PROCESSOR_ARCHITECTURE_SHX: return "SHX"; case PROCESSOR_ARCHITECTURE_ARM: return "ARM"; case PROCESSOR_ARCHITECTURE_IA64: return "IA-64"; # if UCFG_WIN32_FULL case PROCESSOR_ARCHITECTURE_IA32_ON_WIN64: return "IA-32 on Win64"; case PROCESSOR_ARCHITECTURE_AMD64: return "x64"; # endif case PROCESSOR_ARCHITECTURE_UNKNOWN: default: return "Unknown"; } #else Throw(E_NOTIMPL); #endif }
IPAddress IPAddress::Parse(RCString ipString) { uint8_t buf[16]; if (CCheck(::inet_pton(AF_INET, ipString, buf))) return IPAddress(Span(buf, 4)); if (CCheck(::inet_pton(AF_INET6, ipString, buf))) return IPAddress(Span(buf, 16)); /*!!!R #if UCFG_USE_REGEX # if UCFG_WIN32 if (regex_search(ipString, *s_reDomainName)) { # else if (regex_search(ipString.c_str(), s_reDomainName)) { # endif IPAddress r; r.AddressFamily = Ext::AddressFamily(AF_DOMAIN_NAME); r.m_domainname = ipString; return r; } #endif */ Throw(HRESULT_FROM_WIN32(DNS_ERROR_INVALID_IP_ADDRESS)); }
pair<String, UINT> Path::GetTempFileName(RCString path, RCString prefix, UINT uUnique) { #if UCFG_USE_POSIX char buf[PATH_MAX+1]; ZeroStruct(buf); int fd = CCheck(::mkstemp(strncpy(buf, Path::Combine(path, prefix+"XXXXXX"), _countof(buf)-1))); close(fd); return pair<String, UINT>(buf, 1); #else TCHAR buf[MAX_PATH]; UINT r = Win32Check(::GetTempFileName(path, prefix, uUnique, buf)); return pair<String, UINT>(buf, r); #endif }
String Path::GetTempPath() { #if UCFG_USE_POSIX char buf[PATH_MAX+1] = "XXXXXX"; int fd = CCheck(::mkstemp(buf)); close(fd); return buf; #else TCHAR buf[MAX_PATH]; DWORD r = ::GetTempPath(_countof(buf), buf); Win32Check(r && r<_countof(buf)); //!!! need to improve return buf; #endif }
DirectoryInfo Directory::CreateDirectory(RCString path) { vector<String> ar = path.Split(String(Path::DirectorySeparatorChar)+String(Path::AltDirectorySeparatorChar)); String dir; bool b = true; for (int i=0; i<ar.size(); i++) { dir = AddDirSeparator(dir+ar[i]); #if UCFG_USE_POSIX if (::mkdir(dir, 0777) < 00 && errno!=EEXIST && errno!=EISDIR) CCheck(-1); } #else b = ::CreateDirectory(dir, 0); }
String Path::GetTruePath(RCString p) { #if UCFG_USE_POSIX char buf[PATH_MAX]; for (const char *psz = p;; psz = buf) { int len = ::readlink(psz, buf, sizeof(buf)-1); if (len == -1) { if (errno == EINVAL) return psz; CCheck(-1); } buf[len] = 0; } #elif UCFG_WIN32_FULL TCHAR buf[_MAX_PATH]; DWORD len = ::GetLongPathName(p, buf, _countof(buf)-1); Win32Check(len != 0); buf[len] = 0; typedef DWORD (WINAPI *PFN_GetFinalPathNameByHandle)(HANDLE hFile, LPTSTR lpszFilePath, DWORD cchFilePath, DWORD dwFlags); DlProcWrap<PFN_GetFinalPathNameByHandle> pfn("KERNEL32.DLL", EXT_WINAPI_WA_NAME(GetFinalPathNameByHandle)); if (!pfn) return buf; TCHAR buf2[_MAX_PATH]; File file; file.Attach(::CreateFile(buf, 0, 0, 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0)); len = pfn(Handle(file), buf2, _countof(buf2)-1, 0); Win32Check(len != 0); buf2[len] = 0; #if UCFG_USE_REGEX wcmatch m; if (regex_search(buf2, m, s_reDosName)) return m[1]; #else String sbuf(buf2); //!!! incoplete check, better to use Regex int idx = sbuf.Find(':'); if (idx != -1) return sbuf.Mid(idx-1); #endif return buf2; #else return p; #endif }
String Environment::GetMachineVersion() { #if UCFG_USE_POSIX utsname u; CCheck(::uname(&u)); return u.machine; #elif defined(WIN32) String s; SYSTEM_INFO si; GetSystemInfo(&si); switch (si.wProcessorArchitecture) { case PROCESSOR_ARCHITECTURE_INTEL: switch (si.dwProcessorType) { case 586: switch (si.wProcessorRevision) { case 5895: s = "Core 2 Quad"; break; default: s = "586"; } break; default: s = "Intel"; }; break; #if UCFG_WIN32_FULL case PROCESSOR_ARCHITECTURE_AMD64: s = "AMD64"; break; #endif case PROCESSOR_ARCHITECTURE_ARM: s = "ARM"; break; default: s = "Unknown CPU"; break; } return s; #else Throw(E_NOTIMPL); #endif }