[Groonga-commit] groonga/grnxx at 83d7f7b [master] Add grnxx::storage::Path::clone_path().

Back to archive index

susumu.yata null+****@clear*****
Wed Apr 24 14:30:45 JST 2013


susumu.yata	2013-04-24 14:30:45 +0900 (Wed, 24 Apr 2013)

  New Revision: 83d7f7b1bb20e79050497aa1804806212add4f63
  https://github.com/groonga/grnxx/commit/83d7f7b1bb20e79050497aa1804806212add4f63

  Message:
    Add grnxx::storage::Path::clone_path().

  Modified files:
    lib/grnxx/storage/file-posix.cpp
    lib/grnxx/storage/file-posix.hpp
    lib/grnxx/storage/file-windows.cpp
    lib/grnxx/storage/file-windows.hpp
    lib/grnxx/storage/path.cpp
    lib/grnxx/storage/path.hpp

  Modified: lib/grnxx/storage/file-posix.cpp (+6 -14)
===================================================================
--- lib/grnxx/storage/file-posix.cpp    2013-04-24 14:08:30 +0900 (bf7ccbf)
+++ lib/grnxx/storage/file-posix.cpp    2013-04-24 14:30:45 +0900 (565d2be)
@@ -236,7 +236,8 @@ bool FileImpl::create_persistent_file(const char *path, FileFlags flags) {
     GRNXX_ERROR() << "invalid argument: path = nullptr";
     return false;
   }
-  if (!clone_path(path)) {
+  path_.reset(Path::clone_path(path));
+  if (!path_) {
     return false;
   }
   fd_ = ::open(path, O_RDWR | O_CREAT | O_EXCL, 0644);
@@ -275,7 +276,8 @@ bool FileImpl::create_temporary_file(const char *path, FileFlags flags) {
 }
 
 bool FileImpl::open_file(const char *path, FileFlags flags) {
-  if (!clone_path(path)) {
+  path_.reset(Path::clone_path(path));
+  if (!path_) {
     return false;
   }
   int posix_flags = O_RDWR;
@@ -294,7 +296,8 @@ bool FileImpl::open_file(const char *path, FileFlags flags) {
 }
 
 bool FileImpl::open_or_create_file(const char *path, FileFlags flags) {
-  if (!clone_path(path)) {
+  path_.reset(Path::clone_path(path));
+  if (!path_) {
     return false;
   }
   fd_ = ::open(path, O_RDWR | O_CREAT, 0644);
@@ -307,17 +310,6 @@ bool FileImpl::open_or_create_file(const char *path, FileFlags flags) {
   return true;
 }
 
-bool FileImpl::clone_path(const char *path) {
-  const size_t size = std::strlen(path) + 1;
-  path_.reset(new (std::nothrow) char[size]);
-  if (!path_) {
-    GRNXX_ERROR() << "new char[] failed: size = " << size;
-    return false;
-  }
-  std::memcpy(path_.get(), path, size);
-  return true;
-}
-
 }  // namespace storage
 }  // namespace grnxx
 

  Modified: lib/grnxx/storage/file-posix.hpp (+0 -2)
===================================================================
--- lib/grnxx/storage/file-posix.hpp    2013-04-24 14:08:30 +0900 (cb4e474)
+++ lib/grnxx/storage/file-posix.hpp    2013-04-24 14:30:45 +0900 (055258a)
@@ -59,8 +59,6 @@ class FileImpl : public File {
   bool create_temporary_file(const char *path_prefix, FileFlags flags);
   bool open_file(const char *path, FileFlags flags);
   bool open_or_create_file(const char *path, FileFlags flags);
-
-  bool clone_path(const char *path);
 };
 
 }  // namespace storage

  Modified: lib/grnxx/storage/file-windows.cpp (+6 -14)
===================================================================
--- lib/grnxx/storage/file-windows.cpp    2013-04-24 14:08:30 +0900 (01f904b)
+++ lib/grnxx/storage/file-windows.cpp    2013-04-24 14:30:45 +0900 (5ba505d)
@@ -251,7 +251,8 @@ bool FileImpl::create_persistent_file(const char *path, FileFlags flags) {
     GRNXX_ERROR() << "invalid argument: path = nullptr";
     return false;
   }
-  if (!clone_path(path)) {
+  path_.reset(Path::clone_path(path));
+  if (!path_) {
     return false;
   }
   const DWORD desired_access = GENERIC_READ | GENERIC_WRITE;
@@ -294,7 +295,8 @@ bool FileImpl::create_temporary_file(const char *path, FileFlags flags) {
 }
 
 bool FileImpl::open_file(const char *path, FileFlags flags) {
-  if (!clone_path(path)) {
+  path_.reset(Path::clone_path(path));
+  if (!path_) {
     return false;
   }
   DWORD desired_access = GENERIC_READ | GENERIC_WRITE;
@@ -318,7 +320,8 @@ bool FileImpl::open_file(const char *path, FileFlags flags) {
 }
 
 bool FileImpl::open_or_create_file(const char *path, FileFlags flags) {
-  if (!clone_path(path)) {
+  path_.reset(Path::clone_path(path));
+  if (!path_) {
     return false;
   }
   const DWORD desired_access = GENERIC_READ | GENERIC_WRITE;
@@ -337,17 +340,6 @@ bool FileImpl::open_or_create_file(const char *path, FileFlags flags) {
   return true;
 }
 
-bool FileImpl::clone_path(const char *path) {
-  const size_t size = std::strlen(path) + 1;
-  path_.reset(new (std::nothrow) char[size]);
-  if (!path_) {
-    GRNXX_ERROR() << "new char[] failed: size = " << size;
-    return false;
-  }
-  std::memcpy(path_.get(), path, size);
-  return true;
-}
-
 }  // namespace storage
 }  // namespace grnxx
 

  Modified: lib/grnxx/storage/file-windows.hpp (+0 -2)
===================================================================
--- lib/grnxx/storage/file-windows.hpp    2013-04-24 14:08:30 +0900 (0f4b63d)
+++ lib/grnxx/storage/file-windows.hpp    2013-04-24 14:30:45 +0900 (bf750d3)
@@ -67,8 +67,6 @@ class FileImpl : public File {
   bool create_temporary_file(const char *path_prefix, FileFlags flags);
   bool open_file(const char *path, FileFlags flags);
   bool open_or_create_file(const char *path, FileFlags flags);
-
-  bool clone_path(const char *path);
 };
 
 }  // namespace storage

  Modified: lib/grnxx/storage/path.cpp (+14 -0)
===================================================================
--- lib/grnxx/storage/path.cpp    2013-04-24 14:08:30 +0900 (4145c9b)
+++ lib/grnxx/storage/path.cpp    2013-04-24 14:30:45 +0900 (be35183)
@@ -139,5 +139,19 @@ char *Path::unique_path(const char *prefix) {
   return path;
 }
 
+char *Path::clone_path(const char *path) {
+  if (!path) {
+    path = "";
+  }
+  const size_t size = std::strlen(path) + 1;
+  char * const path_clone = new (std::nothrow) char[size];
+  if (!path_clone) {
+    GRNXX_ERROR() << "new char[] failed: size = " << size;
+    return nullptr;
+  }
+  std::memcpy(path_clone, path, size);
+  return path_clone;
+}
+
 }  // namespace storage
 }  // namespace grnxx

  Modified: lib/grnxx/storage/path.hpp (+4 -0)
===================================================================
--- lib/grnxx/storage/path.hpp    2013-04-24 14:08:30 +0900 (1306535)
+++ lib/grnxx/storage/path.hpp    2013-04-24 14:30:45 +0900 (5f707ea)
@@ -34,6 +34,10 @@ class Path {
   // For example, when "prefix" == "temp", the result is "temp_XXXXXXXX",
   // where X indicates a random character ('0'-'9' or 'A'-'Z').
   static char *unique_path(const char *prefix);
+
+  // Create a clone of "path" and return an allocated buffer which must be
+  // freed with delete[].
+  static char *clone_path(const char *path);
 };
 
 }  // namespace storage
-------------- next part --------------
HTML����������������������������...
Download 



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