# coding: utf-8

# This file is a part of TaCo: a morphosyntactic tagset converter
#
# License: GPL v. 3
# Author: Bartosz Zaborowski
#

require "#{File.dirname(__FILE__)}/configuration"
require "fileutils"

# polish diacrits for String upcase/downcase
class String
  alias :old_downcase :downcase
  alias :old_upcase :upcase

  UPCASE = {"ą"=>"Ą", "Ą"=>"Ą", "ć"=>"Ć", "Ć"=>"Ć", "ę"=>"Ę", "Ę"=>"Ę", "ł"=>"Ł", "Ł"=>"Ł", "ń"=>"Ń", "Ń"=>"Ń", "ó"=>"Ó", "Ó"=>"Ó", "ś"=>"Ś", "Ś"=>"Ś", "ż"=>"Ż", "Ż"=>"Ż", "ź"=>"Ź", "Ź"=>"Ź"}

  DOWNCASE = {"ą"=>"ą", "Ą"=>"ą", "ć"=>"ć", "Ć"=>"ć", "ę"=>"ę", "Ę"=>"ę", "ł"=>"ł", "Ł"=>"ł", "ń"=>"ń", "Ń"=>"ń", "ó"=>"ó", "Ó"=>"ó", "ś"=>"ś", "Ś"=>"ś", "ż"=>"ż", "Ż"=>"ż", "ź"=>"ź", "Ź"=>"ź"}

  def downcase
    ret = ""
    self.each_char do |c|
      ret += DOWNCASE[c] || c.old_downcase
    end
    ret
  end

  def upcase
    ret = ""
    self.each_char do |c|
      ret += UPCASE[c] || c.old_upcase
    end
    ret
  end

  def weka_escape
    self.gsub("\\", "\\\\\\\\\\\\\\\\").gsub("'", "+_+")
  end

  def prefix x
    self[0..x-1]
  end

  def suffix x
    x > length ? self : self[-x..-1]
  end

  def dexml_entity
    gsub("&apos;", "'").gsub("&lt;", "<").gsub("&gt;", ">").gsub("&quot;", "\"").gsub("&amp;", "&")
  end

  def xml_entity
    gsub("&", "&amp;").gsub("'", "&apos;").gsub("<", "&lt;").gsub(">", "&gt;").gsub("\"", "&quot;")
  end
end

module Helpers
WARN = 2
INFO = 1
DEBUG = 0


class DebugStream 
  def puts msg, level = 0
    $stderr.puts "#{msg}" if level >= Configuration.debug_level
  end
end

DEBUGSTREAM=DebugStream.new

def self.mk_temp_name core, suffix = ""
  "#{core}-#{Time.now.to_s.gsub(" ", "-")}-#{Process.pid}-#{Thread.current[:tid]}#{suffix}"
end

def self.get_mem_stats pid
  return `ps -o rss= -p #{pid}`.to_i/1024
end

def self.delete arr
  arr.each {|x| File.unlink x} if Configuration.delete_temporary_files
end

end

