#
# Copyright (C) 2002-2004 Satoru Takabayashi <satoru@namazu.org> 
# Copyright (C) 2003-2005 Kouichirou Eto
#     All rights reserved.
#     This is free software with ABSOLUTELY NO WARRANTY.
#
# You can redistribute it and/or modify it under the terms of 
# the GNU General Public License version 2.
#

# $KCODE = "e"
require "kconv"
require "net/smtp"
require "ftools"
$LOAD_PATH << "../../lib" unless $LOAD_PATH.include?("../../lib")

class TooLongLine < Exception; end
class IO
  def safe_gets (max_length = 1024)
    s = ""
    until self.eof?
      c = self.read(1)
      s << c
      if s.length > max_length
	raise TooLongLine
      end
      if c == "\n"
	return s
      end
    end
    if s.empty? then nil else s end
  end
end

class String
  def xchomp!
    self.chomp!("\n")
    self.chomp!("\r")
  end

  if !method_defined?(:xchomp)
    def xchomp
      self.chomp("\n").chomp("\r")
    end
  end

  def normalize_eol!
    self.xchomp!
    self << "\n"
  end
end

class TCPSocket
  def address
    peeraddr[3]
  end

  def hostname
    peeraddr[2]
  end
end

class File
  def self.safe_open (filename, mode = "r")
    begin 
      f = File.open(filename, mode)
      if block_given?
	yield(f)
	f.close
      else
	return f
      end
    rescue => e
      STDERR.printf "%s: %s\n", $0, e.message
      exit(1)
    end
  end
end

class Integer
  # commify(12345) => "12,345"
  def commify
    numstr = self.to_s
    true while numstr.sub!(/^([-+]?\d+)(\d{3})/, '\1,\2')
    return numstr
  end
end

if $0 == __FILE__
  require "test/unit"
  $debug = true
end

if defined?($debug) && $debug
  class TestMLUtils < Test::Unit::TestCase
    def test_all
      # test_safe_gets

      # test_xchomp
      str = "t"
      str.xchomp!
      assert_equal("t", str)
      str = "t\r"
      str.xchomp!
      assert_equal("t", str)

      # test_xchomp

      # test_normalize_eol

      # test_tcpsocket

      # test_safeopen

      # test_commify
      assert_equal("12,345", 12345.commify)
    end
  end
end
