diff options
Diffstat (limited to 'src/OSSupport')
-rw-r--r-- | src/OSSupport/File.cpp | 76 | ||||
-rw-r--r-- | src/OSSupport/File.h | 6 | ||||
-rw-r--r-- | src/OSSupport/Socket.cpp | 14 | ||||
-rw-r--r-- | src/OSSupport/Socket.h | 1 |
4 files changed, 90 insertions, 7 deletions
diff --git a/src/OSSupport/File.cpp b/src/OSSupport/File.cpp index d2eea498a..274aa52da 100644 --- a/src/OSSupport/File.cpp +++ b/src/OSSupport/File.cpp @@ -360,6 +360,82 @@ bool cFile::CreateFolder(const AString & a_FolderPath) +AStringVector cFile::GetFolderContents(const AString & a_Folder) +{ + AStringVector AllFiles; + + #ifdef _WIN32 + + // If the folder name doesn't contain the terminating slash / backslash, add it: + AString FileFilter = a_Folder; + if ( + !FileFilter.empty() && + (FileFilter[FileFilter.length() - 1] != '\\') && + (FileFilter[FileFilter.length() - 1] != '/') + ) + { + FileFilter.push_back('\\'); + } + + // Find all files / folders: + FileFilter.append("*.*"); + HANDLE hFind; + WIN32_FIND_DATA FindFileData; + if ((hFind = FindFirstFile(FileFilter.c_str(), &FindFileData)) != INVALID_HANDLE_VALUE) + { + do + { + AllFiles.push_back(FindFileData.cFileName); + } while (FindNextFile(hFind, &FindFileData)); + FindClose(hFind); + } + + #else // _WIN32 + + DIR * dp; + struct dirent *dirp; + if (*a_Directory == 0) + { + a_Directory = "."; + } + if ((dp = opendir(a_Directory)) == NULL) + { + LOGERROR("Error (%i) opening directory \"%s\"\n", errno, a_Directory ); + } + else + { + while ((dirp = readdir(dp)) != NULL) + { + AllFiles.push_back(dirp->d_name); + } + closedir(dp); + } + + #endif // else _WIN32 + + return AllFiles; +} + + + + + +AString cFile::ReadWholeFile(const AString & a_FileName) +{ + cFile f; + if (!f.Open(a_FileName, fmRead)) + { + return ""; + } + AString Contents; + f.ReadRestOfFile(Contents); + return Contents; +} + + + + + int cFile::Printf(const char * a_Fmt, ...) { AString buf; diff --git a/src/OSSupport/File.h b/src/OSSupport/File.h index cfb3a2019..01663a229 100644 --- a/src/OSSupport/File.h +++ b/src/OSSupport/File.h @@ -121,8 +121,14 @@ public: /// Creates a new folder with the specified name. Returns true if successful. Path may be relative or absolute static bool CreateFolder(const AString & a_FolderPath); + /// Returns the entire contents of the specified file as a string. Returns empty string on error. + static AString ReadWholeFile(const AString & a_FileName); + // tolua_end + /// Returns the list of all items in the specified folder (files, folders, nix pipes, whatever's there). + static AStringVector GetFolderContents(const AString & a_Folder); // Exported in ManualBindings.cpp + int Printf(const char * a_Fmt, ...); private: diff --git a/src/OSSupport/Socket.cpp b/src/OSSupport/Socket.cpp index 48b5d704d..c461d38a4 100644 --- a/src/OSSupport/Socket.cpp +++ b/src/OSSupport/Socket.cpp @@ -169,7 +169,7 @@ bool cSocket::SetReuseAddress(void) -int cSocket::WSAStartup() +int cSocket::WSAStartup(void) { #ifdef _WIN32 WSADATA wsaData; @@ -336,23 +336,23 @@ bool cSocket::ConnectIPv4(const AString & a_HostNameOrAddr, unsigned short a_Por { // First try IP Address string to hostent conversion, because it's faster unsigned long addr = inet_addr(a_HostNameOrAddr.c_str()); - hostent * hp = gethostbyaddr((char*)&addr, sizeof(addr), AF_INET); - if (hp == NULL) + if (addr == INADDR_NONE) { // It is not an IP Address string, but rather a regular hostname, resolve: - hp = gethostbyname(a_HostNameOrAddr.c_str()); + hostent * hp = gethostbyname(a_HostNameOrAddr.c_str()); if (hp == NULL) { - LOGWARN("cTCPLink: Could not resolve hostname \"%s\"", a_HostNameOrAddr.c_str()); + LOGWARNING("%s: Could not resolve hostname \"%s\"", __FUNCTION__, a_HostNameOrAddr.c_str()); CloseSocket(); return false; } + addr = *((unsigned long*)hp->h_addr); } sockaddr_in server; - server.sin_addr.s_addr = *((unsigned long*)hp->h_addr); + server.sin_addr.s_addr = addr; server.sin_family = AF_INET; - server.sin_port = htons( (unsigned short)a_Port ); + server.sin_port = htons((unsigned short)a_Port); return (connect(m_Socket, (sockaddr *)&server, sizeof(server)) == 0); } diff --git a/src/OSSupport/Socket.h b/src/OSSupport/Socket.h index 34f09cc74..81bfd28fc 100644 --- a/src/OSSupport/Socket.h +++ b/src/OSSupport/Socket.h @@ -38,6 +38,7 @@ public: /// Sets the address-reuse socket flag; returns true on success bool SetReuseAddress(void); + /// Initializes the network stack. Returns 0 on success, or another number as an error code. static int WSAStartup(void); static AString GetErrorString(int a_ErrNo); |