]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
Utils.safe_save does a 'safe' saving (save to tempfile, move tempfile to new location)
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Sun, 15 Oct 2006 20:38:25 +0000 (20:38 +0000)
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Sun, 15 Oct 2006 20:38:25 +0000 (20:38 +0000)
lib/rbot/utils.rb

index eabe5c17eee02b83365a77807c01340bf1903f86..ae45e5d5af75264e1a726de31f5799052dc74a72 100644 (file)
@@ -1,10 +1,14 @@
 require 'net/http'
 require 'uri'
+require 'tempfile'
+
 begin
   $we_have_html_entities_decoder = require 'htmlentities'
 rescue LoadError
   $we_have_html_entities_decoder = false
-  UNESCAPE_TABLE = {
+  module Irc
+    module Utils
+      UNESCAPE_TABLE = {
     'raquo' => '>>',
     'quot' => '"',
     'apos' => '\'',
@@ -268,9 +272,12 @@ rescue LoadError
     'sigma' => '&#963;',
     'oacute' => '\xf3',
 =end
-  }
+      }
+    end
+  end
 end
 
+
 module Irc
 
   # miscellaneous useful functions
@@ -312,6 +319,17 @@ module Irc
       }
     end
 
+
+    def Utils.safe_save(file)
+      basename = File.basename(file)
+      temp = Tempfile.new(basename)
+      temp.binmode
+      yield temp if block_given?
+      temp.close
+      File.rename(temp.path, file)
+    end
+
+
     # returns a string containing the result of an HTTP GET on the uri
     def Utils.http_get(uristr, readtimeout=8, opentimeout=4)