[Groonga-commit] groonga/grnxx at af50720 [master] Update grnxx::OS.

Back to archive index

susumu.yata null+****@clear*****
Mon Jul 1 18:35:04 JST 2013


susumu.yata	2013-07-01 18:35:04 +0900 (Mon, 01 Jul 2013)

  New Revision: af50720446ceedb11b7260e787cfaa85d2896d9f
  https://github.com/groonga/grnxx/commit/af50720446ceedb11b7260e787cfaa85d2896d9f

  Message:
    Update grnxx::OS.

  Modified files:
    lib/grnxx/os.cpp
    lib/grnxx/os.hpp

  Modified: lib/grnxx/os.cpp (+17 -6)
===================================================================
--- lib/grnxx/os.cpp    2013-07-01 18:19:50 +0900 (7426c2a)
+++ lib/grnxx/os.cpp    2013-07-01 18:35:04 +0900 (75db2d2)
@@ -17,11 +17,14 @@
 */
 #include "grnxx/os.hpp"
 
+#include <cerrno>
 #include <cstdlib>
 #include <cstring>
-#include <cerrno>
+#include <memory>
+#include <new>
 
 #include "grnxx/errno.hpp"
+#include "grnxx/exception.hpp"
 #include "grnxx/lock.hpp"
 #include "grnxx/logger.hpp"
 
@@ -39,23 +42,30 @@ uint64_t OS::get_page_size() {
 char *OS::get_environment_variable(const char *name) {
   if (!name) {
     GRNXX_ERROR() << "invalid argument: name = nullptr";
-    return nullptr;
+    throw LogicError();
   }
   static Mutex mutex;
   Lock lock(&mutex);
 #ifdef GRNXX_MSC
   char *value;
   size_t value_size;
-  if (::_dupenv_s(&value, &value_size, name) != 0) {
+  errno_t error;
+  error = ::_dupenv_s(&value, &value_size, name);
+  if (error != 0) {
+    const Errno error_code(error);
     GRNXX_ERROR() << "failed to get environment variable: name = " << name
-                  << ": '::_dupenv_s' " << Errno(errno);
+                  << ": '::_dupenv_s' " << error_code;
+    throw SystemError(error_code);
+  }
+  if (!value) {
+    // No match.
     return nullptr;
   }
   char * const result = new (std::nothrow) char[value_size + 1];
   if (!result) {
     GRNXX_ERROR() << "new char[] failed: size = " << (value_size + 1);
     std::free(value);
-    return nullptr;
+    throw MemoryError();
   }
   std::memcpy(result, value, value_size);
   result[value_size] = '\0';
@@ -64,13 +74,14 @@ char *OS::get_environment_variable(const char *name) {
 #else  // GRNXX_MSC
   char * const value = std::getenv(name);
   if (!value) {
+    // No match.
     return nullptr;
   }
   const size_t value_size = std::strlen(value);
   char * const result = new (std::nothrow) char[value_size + 1];
   if (!result) {
     GRNXX_ERROR() << "new char[] failed: size = " << (value_size + 1);
-    return nullptr;
+    throw MemoryError();
   }
   std::memcpy(result, value, value_size);
   result[value_size] = '\0';

  Modified: lib/grnxx/os.hpp (+3 -2)
===================================================================
--- lib/grnxx/os.hpp    2013-07-01 18:19:50 +0900 (8b87c9b)
+++ lib/grnxx/os.hpp    2013-07-01 18:35:04 +0900 (9d7ecd5)
@@ -28,8 +28,9 @@ class OS {
  public:
   // Get the page size.
   static uint64_t get_page_size();
-  // Get an environment variable. The returned string must be freed with
-  // delete[].
+
+  // Return an environment variable, or nullptr if "name" does not exist.
+  // The returned string must be freed with delete[].
   static char *get_environment_variable(const char *name);
 };
 
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index