Permalink
Browse files

FileSystem

  • Loading branch information...
1 parent 08237ea commit 8233bae3f39fca2555fd613e063ff6964622052a @vshymanskyy committed Oct 13, 2012
Showing with 87 additions and 34 deletions.
  1. +1 −1 inc/XDebug.h
  2. +48 −0 inc/XFileSystem.h
  3. +1 −0 inc/XLog.h
  4. +2 −0 inc/XPlat.h
  5. +35 −33 src/net/XSockAddr.cpp
View
@@ -10,7 +10,7 @@
#endif
#if defined (TARGET_OS_WINDOWS)
- #include <Windows.h>
+ #include <windows.h>
#include <stdio.h>
#define X_DBG_BREAK() DebugBreak();
View
@@ -0,0 +1,48 @@
+#ifndef XFILESYSTEM_H_
+#define XFILESYSTEM_H_
+
+#include <XPlat.h>
+#include <XDebug.h>
+
+#include <sys/stat.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <errno.h>
+
+inline
+bool FileExists(const char* fn) {
+ struct stat sts;
+ if (stat(fn, &sts) == -1)
+ {
+ if (errno == ENOENT) {
+ return false;
+ }
+ X_FATAL("stat failed: %d (%s)", errno, strerror(errno));
+ }
+ return true;
+}
+
+inline
+bool FileDelete(const char* fn) {
+ if (unlink(fn) == -1)
+ {
+ if (errno != ENOENT) {
+ return false;
+ }
+ }
+ return true;
+}
+
+inline
+uint64_t FileSize(const char* fn)
+{
+ if (FILE* f = fopen(fn, "rb")) {
+ fseek(f, 0, SEEK_END);
+ uint64_t result = ftello64(f);
+ fclose(f);
+ return result;
+ }
+ return 0;
+}
+
+#endif /* XFILESYSTEM_H_ */
View
@@ -21,6 +21,7 @@
//#define LOG_STR(val) STRINGIFY(val)
//#define LOG_VAL(val) "(" LOG_STR(val) ")" << " = " << (val)
//#define LOG_LEVEL_MAX XLog::WARN
+//#define LOG_LEVEL_MAX -1
#ifdef LOG_LEVEL_MAX
#define __LOG(log, level, ...) { const XLog& __l = log; if (LOG_LEVEL_MAX >= level && __l.GetLevel() >= level) { XLog::Stream logTrace(&__l, level, __FUNCTION__, __FILE__, __LINE__); logTrace << __VA_ARGS__; } }
View
@@ -147,6 +147,8 @@
#elif defined(_M_ARM)
#define TARGET_ARCH_ARM
#endif
+#else
+#error "Unknown compiler"
#endif
/* Fill target arch data */
View
@@ -10,24 +10,24 @@ XSockAddr XSockAddr::Random() {
XSockAddr::XSockAddr(const XString& str)
{
- addrinfo hint, *info = NULL;
- memset(&hint, 0, sizeof(hint));
- hint.ai_family = AF_UNSPEC;
- hint.ai_flags = AI_NUMERICHOST; // disable DNS lookup
-
- // Detect IPv6 with port
- if (str[0] == '[') {
- int e = str.Find("]:", 1);
- if (e > 0) {
- if (0 == getaddrinfo(str.Substring(1, e-1), str.Substring(e+2), &hint, &info)) {
+ addrinfo hint, *info = NULL;
+ memset(&hint, 0, sizeof(hint));
+ hint.ai_family = AF_UNSPEC;
+ hint.ai_flags = AI_NUMERICHOST; // disable DNS lookup
+
+ // Detect IPv6 with port
+ if (str[0] == '[') {
+ int e = str.Find("]:", 1);
+ if (e > 0) {
+ if (0 == getaddrinfo((char*)str.Substring(1, e-1), (char*)str.Substring(e+2), &hint, &info)) {
memcpy(&sa, info->ai_addr, info->ai_addrlen);
freeaddrinfo(info);
return;
}
- }
- }
+ }
+ }
- // Detect addr without port
+ // Detect addr without port
if (0 == getaddrinfo(str, NULL, &hint, &info)) {
memcpy(&sa, info->ai_addr, info->ai_addrlen);
freeaddrinfo(info);
@@ -36,13 +36,13 @@ XSockAddr::XSockAddr(const XString& str)
// Detect IPv4 addr with port
int e = str.Find(":");
- if (0 == getaddrinfo(str.Substring(0, e), str.Substring(e+1), &hint, &info)) {
+ if (0 == getaddrinfo((char*)str.Substring(0, e), (char*)str.Substring(e+1), &hint, &info)) {
memcpy(&sa, info->ai_addr, info->ai_addrlen);
freeaddrinfo(info);
return;
}
- memset(&sa, 0, sizeof(XSockAddr));
+ memset(&sa, 0, sizeof(XSockAddr));
}
XString XSockAddr::ToString() const
@@ -74,15 +74,18 @@ XString XSockAddr::ResolveName() const
XList<XSockAddr> XSockAddr::Lookup(const XString& str)
{
XList<XSockAddr> res;
- addrinfo *info = NULL;
-
- // Detect IPv6 with port
- if (str[0] == '[') {
- int e = str.Find("]:", 1);
- if (e > 0) {
- XString name = str.Substring(0, e-1);
- XString serv = str.Substring(e+2);
- if (0 == getaddrinfo(name, NULL, NULL, &info)) {
+ addrinfo hint, *info = NULL;
+ memset(&hint, 0, sizeof(hint));
+ hint.ai_family = AF_UNSPEC;
+ hint.ai_protocol = SOCK_DGRAM;
+
+ // Detect IPv6 with port
+ if (str[0] == '[') {
+ int e = str.Find("]:", 1);
+ if (e > 0) {
+ XString name = str.Substring(0, e-1);
+ XString serv = str.Substring(e+2);
+ if (0 == getaddrinfo((char*)name, (char*)serv, &hint, &info)) {
for(addrinfo* i = info; i != NULL; i = i->ai_next) {
XSockAddr addr;
memcpy(&addr.sa, i->ai_addr, i->ai_addrlen);
@@ -92,11 +95,11 @@ XList<XSockAddr> XSockAddr::Lookup(const XString& str)
freeaddrinfo(info);
return res;
}
- }
- }
+ }
+ }
- // Detect addr without port
- if (0 == getaddrinfo(str, NULL, NULL, &info)) {
+ // Detect addr without port
+ if (0 == getaddrinfo(str, NULL, &hint, &info)) {
for(addrinfo* i = info; i != NULL; i = i->ai_next) {
XSockAddr addr;
memcpy(&addr.sa, i->ai_addr, i->ai_addrlen);
@@ -113,8 +116,7 @@ XList<XSockAddr> XSockAddr::Lookup(const XString& str)
if (!name.Length()) {
return res;
}
- fprintf(stderr, "Resolving: %s\n", (char*)name);
- if (int error = getaddrinfo((char*)name, NULL, NULL, &info)) {
+ if (int error = getaddrinfo((char*)name, NULL, &hint, &info)) {
X_FATAL("%d: %s", error, gai_strerror(error));
} else {
for(addrinfo* i = info; i != NULL; i = i->ai_next) {
@@ -127,7 +129,7 @@ XList<XSockAddr> XSockAddr::Lookup(const XString& str)
return res;
}
}
- return res;
+ return res;
}
XList<XSockAddr> XSockAddr::GetLocal()
@@ -157,8 +159,8 @@ XList<XSockAddr> XSockAddr::GetLocal()
}
#elif defined TARGET_OS_WINDOWS
IP_ADAPTER_INFO *pAdapterInfo;
- ULONG ulOutBufLen;
- DWORD dwRetVal;
+ ULONG ulOutBufLen;
+ DWORD dwRetVal;
pAdapterInfo = (IP_ADAPTER_INFO *) malloc( sizeof(IP_ADAPTER_INFO) );
ulOutBufLen = sizeof(IP_ADAPTER_INFO);
if (GetAdaptersInfo( pAdapterInfo, &ulOutBufLen) != ERROR_SUCCESS) {

0 comments on commit 8233bae

Please sign in to comment.