diff options
author | Vincent Richard <[email protected]> | 2004-12-18 01:57:39 +0000 |
---|---|---|
committer | Vincent Richard <[email protected]> | 2004-12-18 01:57:39 +0000 |
commit | 460cae786af2c4c8ff205f7feb45b50c43a6700b (patch) | |
tree | adc4c2f466ae5dd625f57a13df3247dd565fbb3a /src/platforms/posix/handler.cpp | |
parent | Added appendComponent() and getComponentAt() functions. (diff) | |
download | vmime-460cae786af2c4c8ff205f7feb45b50c43a6700b.tar.gz vmime-460cae786af2c4c8ff205f7feb45b50c43a6700b.zip |
Default platform handlers (currently only POSIX).
Diffstat (limited to 'src/platforms/posix/handler.cpp')
-rw-r--r-- | src/platforms/posix/handler.cpp | 186 |
1 files changed, 186 insertions, 0 deletions
diff --git a/src/platforms/posix/handler.cpp b/src/platforms/posix/handler.cpp new file mode 100644 index 00000000..1c3fd7d4 --- /dev/null +++ b/src/platforms/posix/handler.cpp @@ -0,0 +1,186 @@ +// +// VMime library (http://vmime.sourceforge.net) +// Copyright (C) 2002-2004 Vincent Richard <[email protected]> +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of +// the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#include "handler.hpp" + +#include <time.h> + +#include <unistd.h> +#include <locale.h> +#include <langinfo.h> +#include <errno.h> + +#include <sys/types.h> +#include <sys/stat.h> + +#include <netdb.h> + +#include <string.h> + + +namespace vmime { +namespace platforms { +namespace posix { + + +posixHandler::posixHandler() + : m_socketFactory(new posixSocketFactory()), + m_fileSysFactory(new posixFileSystemFactory()) +{ +} + + +posixHandler::~posixHandler() +{ + delete (m_socketFactory); + delete (m_fileSysFactory); +} + + +const unsigned int posixHandler::getUnixTime() const +{ + return ::time(NULL); +} + + +const vmime::datetime posixHandler::getCurrentLocalTime() const +{ + const time_t t(::time(NULL)); + + // Get the local time +#ifdef _REENTRANT + tm local; + ::localtime_r(&t, &local); +#else + tm local = *::localtime(&t); // WARNING: this is not thread-safe! +#endif + + // Get the UTC time +#ifdef _REENTRANT + tm gmt; + ::gmtime_r(&t, &gmt); +#else + tm gmt = *::gmtime(&t); // WARNING: this is not thread-safe! +#endif + + // "A negative value for tm_isdst causes mktime() to attempt + // to determine whether Daylight Saving Time is in effect + // for the specified time." + local.tm_isdst = -1; + gmt.tm_isdst = -1; + + // Calculate the difference (in seconds) + const int diff = ::mktime(&local) - ::mktime(&gmt); + + // Return the date + return vmime::datetime(local.tm_year + 1900, local.tm_mon + 1, local.tm_mday, + local.tm_hour, local.tm_min, local.tm_sec, diff / 60); // minutes needed +} + + +const vmime::charset posixHandler::getLocaleCharset() const +{ + vmime::string prevLocale(::setlocale(::LC_ALL, "")); + vmime::charset ch(::nl_langinfo(::CODESET)); + ::setlocale(::LC_ALL, prevLocale.c_str()); + + return (ch); +} + + +const vmime::string posixHandler::getHostName() const +{ + std::vector <vmime::string> hostnames; + char buffer[256]; + + // Try with 'gethostname' + ::gethostname(buffer, sizeof(buffer)); + buffer[sizeof(buffer) - 1] = '\0'; + + if (::strlen(buffer) == 0) + ::strcpy(buffer, "localhost"); + + hostnames.push_back(buffer); + + // And with 'gethostbyname' + struct hostent* he = ::gethostbyname(buffer); + + if (he != NULL) + { + if (::strlen(he->h_name) != 0) + hostnames.push_back(he->h_name); + + char** alias = he->h_aliases; + + while (alias && *alias) + { + if (::strlen(*alias) != 0) + hostnames.push_back(*alias); + + ++alias; + } + } + + // Find a Fully-Qualified Domain Name (FQDN) + for (unsigned int i = 0 ; i < hostnames.size() ; ++i) + { + if (hostnames[i].find_first_of(".") != vmime::string::npos) + return (hostnames[i]); + } + + return (hostnames[0]); +} + + +const unsigned int posixHandler::getProcessId() const +{ + return (::getpid()); +} + + +vmime::messaging::socketFactory* posixHandler::getSocketFactory + (const vmime::string& /* name */) const +{ + return (m_socketFactory); +} + + +vmime::messaging::timeoutHandlerFactory* posixHandler::getTimeoutHandlerFactory + (const vmime::string& /* name */) const +{ + // Not used by default + return (NULL); +} + + +vmime::utility::fileSystemFactory* posixHandler::getFileSystemFactory() const +{ + return (m_fileSysFactory); +} + + +void posixHandler::wait() const +{ + ::sleep(1); +} + + +} // posix +} // platforms +} // vmime |