Bladeren bron

2009-12-23 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>

	Added util::saveAs() function. Use it in Signature::save().
	* src/Signature.cc
	* src/util.cc
	* src/util.h
Tatsuhiro Tsujikawa 16 jaren geleden
bovenliggende
commit
9b933ca406
4 gewijzigde bestanden met toevoegingen van 35 en 17 verwijderingen
  1. 7 0
      ChangeLog
  2. 2 17
      src/Signature.cc
  3. 19 0
      src/util.cc
  4. 7 0
      src/util.h

+ 7 - 0
ChangeLog

@@ -1,3 +1,10 @@
+2009-12-23  Tatsuhiro Tsujikawa  <t-tujikawa@users.sourceforge.net>
+
+	Added util::saveAs() function. Use it in Signature::save().
+	* src/Signature.cc
+	* src/util.cc
+	* src/util.h
+
 2009-12-23  Tatsuhiro Tsujikawa  <t-tujikawa@users.sourceforge.net>
 
 	Upcase info hash in Magnet URI.

+ 2 - 17
src/Signature.cc

@@ -37,6 +37,7 @@
 #include <fstream>
 
 #include "File.h"
+#include "util.h"
 
 namespace aria2 {
 
@@ -61,23 +62,7 @@ void Signature::setBody(const std::string& body)
 
 bool Signature::save(const std::string& filepath) const
 {
-  if(File(filepath).exists()) {
-    return false;
-  }
-  std::string tempFilepath = filepath;
-  tempFilepath += "__temp";
-  {
-    std::ofstream out(tempFilepath.c_str(), std::ios::binary);
-    if(!out) {
-      return false;
-    }
-    out << _body;
-    out.flush();
-    if(!out) {
-      return false;
-    }
-  }
-  return File(tempFilepath).renameTo(filepath);
+  return util::saveAs(filepath, _body);
 }
 
 } // namespace aria2

+ 19 - 0
src/util.cc

@@ -981,6 +981,25 @@ void generateRandomData(unsigned char* data, size_t length)
 #endif // HAVE_LIBSSL
 }
 
+bool saveAs
+(const std::string& filename, const std::string& data, bool overwrite)
+{
+  if(!overwrite && File(filename).exists()) {
+    return false;
+  }
+  std::string tempFilename = strconcat(filename, "__temp");
+  std::ofstream out(tempFilename.c_str(), std::ios::binary);
+  if(!out) {
+    return false;
+  }
+  out << data;
+  out.flush();
+  if(!out) {
+    return false;
+  }
+  return File(tempFilename).renameTo(filename);
+}
+
 } // namespace util
 
 } // namespace aria2

+ 7 - 0
src/util.h

@@ -348,6 +348,13 @@ OutputIterator split(const std::string& src, OutputIterator out,
 
 void generateRandomData(unsigned char* data, size_t length);
 
+// Saves data to file whose name is filename. If overwrite is true,
+// existing file is overwritten. Otherwise, this function doesn't do
+// nothing.  If data is saved successfully, return true. Otherwise
+// returns false.
+bool saveAs
+(const std::string& filename, const std::string& data, bool overwrite=false);
+
 } // namespace util
 
 } // namespace aria2