Jelajahi Sumber

2008-06-16 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>

	Applied Ross's aria2-0.13.2+1-mingw-4.patch. In this commit, 
only the
	follow sources are applied.
	* src/Platform.h: I removed HAVE_WINSOCK2_H directive from 
Platform.h.
	* src/Platform.cc: Moved common setup/teardown code to Platform 
class.
	I moved #endif // HAVE_WINSOCK2_H to the front of #include 
"DlAbortEx.h"
	I included locale.h from Platform.cc.
	* src/main.cc: Moved common setup/teardown code to Platform 
class.
	* test/AllTest.cc: Use Platform class.
	Set locale to C in AllTest.cc to prevent the messages to be 
localized.
Tatsuhiro Tsujikawa 17 tahun lalu
induk
melakukan
aaa2ecaa6f
5 mengubah file dengan 105 tambahan dan 60 penghapusan
  1. 12 0
      ChangeLog
  2. 75 5
      src/Platform.cc
  3. 9 4
      src/Platform.h
  4. 0 31
      src/main.cc
  5. 9 20
      test/AllTest.cc

+ 12 - 0
ChangeLog

@@ -1,3 +1,15 @@
+2008-06-16  Tatsuhiro Tsujikawa  <tujikawa at rednoah dot com>
+
+	Applied Ross's aria2-0.13.2+1-mingw-4.patch. In this commit, only the
+	follow sources are applied.
+	* src/Platform.h: I removed HAVE_WINSOCK2_H directive from Platform.h.
+	* src/Platform.cc: Moved common setup/teardown code to Platform class.
+	I moved #endif // HAVE_WINSOCK2_H to the front of #include "DlAbortEx.h"
+	I included locale.h from Platform.cc.
+	* src/main.cc: Moved common setup/teardown code to Platform class.
+	* test/AllTest.cc: Use Platform class.
+	Set locale to C in AllTest.cc to prevent the messages to be localized.
+	
 2008-06-16  Tatsuhiro Tsujikawa  <tujikawa at rednoah dot com>
 
 	Applied Ross's aria2-0.13.2+1-mingw-3.patch.

+ 75 - 5
src/Platform.cc

@@ -33,6 +33,15 @@
  */
 /* copyright --> */
 #include "Platform.h"
+
+#ifdef HAVE_LIBSSL
+# include <openssl/err.h>
+# include <openssl/ssl.h>
+#endif // HAVE_LIBSSL
+#ifdef HAVE_LIBGNUTLS
+# include <gnutls/gnutls.h>
+#endif // HAVE_LIBGNUTLS
+
 #ifdef HAVE_WINSOCK2_H
 
 #ifndef _WIN32_WINNT
@@ -44,26 +53,87 @@
 # include <ws2tcpip.h>
 #endif // HAVE_WS2TCPIP_H
 
+#endif // HAVE_WINSOCK2_H
+
 #include "DlAbortEx.h"
 #include "message.h"
 #include <stdlib.h> /* _fmode */
 #include <fcntl.h> /*  _O_BINARY */
+#include <locale.h> // For setlocale, LC_*
 
 namespace aria2 {
 
-Platform::Platform() {
-  unsigned int _CRT_fmode = _O_BINARY;
+bool Platform::_initialized = false;
+
+Platform::Platform()
+{
+  setUp();
+}
+
+Platform::~Platform()
+{
+  tearDown();
+}
+
+bool Platform::setUp()
+{
+  if (_initialized) {
+    return false;
+  }
+  _initialized = true;
+
+#ifdef ENABLE_NLS
+  setlocale (LC_CTYPE, "");
+  setlocale (LC_MESSAGES, "");
+  bindtextdomain (PACKAGE, LOCALEDIR);
+  textdomain (PACKAGE);
+#endif // ENABLE_NLS
+
+#ifdef HAVE_LIBSSL
+  // for SSL initialization
+  SSL_load_error_strings();
+  SSL_library_init();
+#endif // HAVE_LIBSSL
+#ifdef HAVE_LIBGNUTLS
+  gnutls_global_init();
+#endif // HAVE_LIBGNUTLS
+  
+#ifdef HAVE_WINSOCK2_H
   WSADATA wsaData;
   memset((char*)&wsaData, 0, sizeof(wsaData));
   if (WSAStartup(MAKEWORD(1, 1), &wsaData)) {
     throw DlAbortEx(MSG_WINSOCK_INIT_FAILD);
   }
+#endif // HAVE_WINSOCK2_H
+
+#ifdef __MINGW32__
+  unsigned int _CRT_fmode = _O_BINARY;
+#endif // __MINGW32__
+  
+  return true;
 }
 
-Platform::~Platform() {
+bool Platform::tearDown()
+{
+  if (!_initialized) {
+    return false;
+  }
+  _initialized = false;
+
+#ifdef HAVE_LIBGNUTLS
+  gnutls_global_deinit();
+#endif // HAVE_LIBGNUTLS
+
+#ifdef HAVE_WINSOCK2_H
   WSACleanup();
+#endif // HAVE_WINSOCK2_H
+  
+  return true;
 }
 
-} // namespace aria2
+bool Platform::isInitialized()
+{
+  return _initialized;
+}
 
-#endif // HAVE_WINSOCK2_H
+} // namespace aria2

+ 9 - 4
src/Platform.h

@@ -39,16 +39,21 @@
 
 namespace aria2 {
 
-#ifdef HAVE_WINSOCK2_H
-
 class Platform {
+private:
+  static bool _initialized;
+
 public:
   Platform();
 
   ~Platform();
-};
 
-#endif // HAVE_WINSOCK2_H
+  static bool setUp();
+
+  static bool tearDown();
+
+  static bool isInitialized();
+};
 
 } // namespace aria2
 

+ 0 - 31
src/main.cc

@@ -85,15 +85,6 @@ extern char* optarg;
 extern int optind, opterr, optopt;
 #include <getopt.h>
 
-#ifdef HAVE_LIBSSL
-// for SSL
-# include <openssl/err.h>
-# include <openssl/ssl.h>
-#endif // HAVE_LIBSSL
-#ifdef HAVE_LIBGNUTLS
-# include <gnutls/gnutls.h>
-#endif // HAVE_LIBGNUTLS
-
 namespace aria2 {
 
 // output stream to /dev/null
@@ -460,31 +451,9 @@ int main(int argc, char* argv[])
 } // namespace aria2
 
 int main(int argc, char* argv[]) {
-#ifdef HAVE_WINSOCK2_H
   aria2::Platform platform;
-#endif // HAVE_WINSOCK2_H
-
-#ifdef ENABLE_NLS
-  setlocale (LC_CTYPE, "");
-  setlocale (LC_MESSAGES, "");
-  bindtextdomain (PACKAGE, LOCALEDIR);
-  textdomain (PACKAGE);
-#endif // ENABLE_NLS
-  
-#ifdef HAVE_LIBSSL
-  // for SSL initialization
-  SSL_load_error_strings();
-  SSL_library_init();
-#endif // HAVE_LIBSSL
-#ifdef HAVE_LIBGNUTLS
-  gnutls_global_init();
-#endif // HAVE_LIBGNUTLS
 
   int r = aria2::main(argc, argv);
 
-#ifdef HAVE_LIBGNUTLS
-  gnutls_global_deinit();
-#endif // HAVE_LIBGNUTLS
-
   return r;
 }

+ 9 - 20
test/AllTest.cc

@@ -1,20 +1,22 @@
+#include "Platform.h"
 #include "CookieBoxFactory.h"
 #include <iostream>
 #include <cppunit/CompilerOutputter.h>
 #include <cppunit/extensions/TestFactoryRegistry.h>
 #include <cppunit/ui/text/TestRunner.h>
-#ifdef HAVE_LIBSSL
-# include <openssl/err.h>
-# include <openssl/ssl.h>
-#endif // HAVE_LIBSSL
-#ifdef HAVE_LIBGNUTLS
-# include <gnutls/gnutls.h>
-#endif // HAVE_LIBGNUTLS
 
 using aria2::SharedHandle;
 using aria2::SingletonHolder;
 
 int main(int argc, char* argv[]) {
+  aria2::Platform platform;
+
+#ifdef ENABLE_NLS
+  // Set locale to C to prevent the messages to be localized.
+  setlocale (LC_CTYPE, "C");
+  setlocale (LC_MESSAGES, "C");
+#endif // ENABLE_NLS
+
   CppUnit::Test* suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
   CppUnit::TextUi::TestRunner runner;
   runner.addTest(suite);
@@ -23,15 +25,6 @@ int main(int argc, char* argv[]) {
   
   // setup
 
-#ifdef HAVE_LIBSSL
-  // for SSL initialization
-  SSL_load_error_strings();
-  SSL_library_init();
-#endif // HAVE_LIBSSL
-#ifdef HAVE_LIBGNUTLS
-  gnutls_global_init();
-#endif // HAVE_LIBGNUTLS
-
   SharedHandle<aria2::CookieBoxFactory> cookieBoxFactory
     (new aria2::CookieBoxFactory());
   SingletonHolder<SharedHandle<aria2::CookieBoxFactory> >::instance(cookieBoxFactory);
@@ -39,9 +32,5 @@ int main(int argc, char* argv[]) {
   // Run the tests.
   bool successfull = runner.run();
 
-#ifdef HAVE_LIBGNUTLS
-  gnutls_global_deinit();
-#endif // HAVE_LIBGNUTLS
-
   return successfull ? 0 : 1;
 }