ykutils 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,155 +1,147 @@
1
- # -*- coding utf-8 -*-
2
-
3
- require 'nkf'
4
-
5
- module Ykutils
6
- module NKFUTIL
7
- CODE_TO_NAME = Hash.new("ASCII")
8
- CODE_TO_NAME[NKF::JIS] = "JIS"
9
- CODE_TO_NAME[NKF::EUC] = "EUC"
10
- CODE_TO_NAME[NKF::SJIS] = "SJIS"
11
- CODE_TO_NAME[NKF::BINARY] = "BINARY"
12
- CODE_TO_NAME[NKF::UTF8] = "UTF8" if NKF.const_defined?(:UTF8)
13
-
14
- def NKFUTIL.guess_encoding(str)
15
- CODE_TO_NAME[NKF.guess(str)]
16
- end
17
-
18
- class Assoc
19
- @@hs = {}
20
- @@config = nil
21
-
22
- def Assoc.set( key, value )
23
- if value
24
- @@hs[key] = Assoc.convert( value )
25
- else
26
- @@hs[key] = value
27
- end
28
- end
29
-
30
- def Assoc.get( key )
31
- @@hs[key]
32
- end
33
-
34
- def Assoc.to_nkf_encoding_format( encoding )
35
- case encoding
36
- when "UTF8"
37
- "w"
38
- else
39
- encoding[0,1]
40
- end
41
- end
42
-
43
-
44
- def Assoc.config( src_encoding, dest_encoding , misc_option = nil )
45
- @@config = "-#{(dest_encoding.to_s)[0,1].downcase}#{(src_encoding.to_s)[0,1].upcase}"
46
- if misc_option != nil
47
- @@config += " #{misc_option}"
48
- end
49
- end
50
-
51
- def Assoc.auto_config_to_inner( str , misc_option = nil )
52
- if str
53
- src_encoding = Assoc.to_nkf_encoding_format( NKFUTIL.guess_encoding( str ) )
54
- else
55
- src_encoding = "A"
56
- end
57
-
58
- inner_encoding = Assoc.to_nkf_encoding_format( Assoc.get_inner_encoding )
59
- if inner_encoding != "A"
60
- @@config = "-#{inner_encoding.downcase}#{src_encoding.upcase}"
61
- if misc_option != nil
62
- @@config += " #{misc_option}"
63
- end
64
- end
65
- src_encoding
66
- end
67
-
68
- def Assoc.auto_config_from_inner( dest_enc , misc_option = nil )
69
- dest_encoding = Assoc.to_nkf_encoding_format( dest_enc )
70
- inner_encoding = Assoc.to_nkf_encoding_format( Assoc.get_inner_encoding )
71
- if inner_encoding != "A"
72
- @@config = "-#{dest_encoding.downcase}#{inner_encoding.upcase}"
73
- if misc_option != nil
74
- @@config += " #{misc_option}"
75
- end
76
- end
77
- end
78
-
79
- def Assoc.convert( str )
80
- nstr = nil
81
- if str != nil
82
- if @@config != nil
83
- begin
84
- nstr = NKF.nkf( @@config , str )
85
- rescue => ex
86
- puts ex
87
- puts "========="
88
- pp caller(0)
89
- end
90
- else
91
- nstr = str
92
- end
93
- end
94
- nstr
95
- end
96
-
97
- def Assoc.get_inner_encoding
98
- @@inner_encoding = ($KCODE != "NONE") ? $KCODE : "ASCII"
99
- end
100
- end
101
-
102
- def NKFUTIL.set( key, value )
103
- Assoc.set( key , value )
104
- end
105
-
106
- def NKFUTIL.get( key )
107
- Assoc.get( key )
108
- end
109
-
110
- def NKFUTIL.convert( str )
111
- if str != nil
112
- Assoc.convert( str )
113
- else
114
- ""
115
- end
116
- end
117
-
118
- def NKFUTIL.assoc_equal( target , key )
119
- target == key || target == Assoc.get( key )
120
- end
121
-
122
- def NKFUTIL.config( src_encoding, dest_encoding , misc_option = nil )
123
- Assoc.config( src_encoding, dest_encoding , misc_option )
124
- end
125
-
126
- def NKFUTIL.auto_config_to_inner( str, misc_option = nil )
127
- Assoc.auto_config_to_inner( str , misc_option )
128
- end
129
-
130
- def NKFUTIL.auto_config_from_inner( dest_encoding , misc_option = nil )
131
- Assoc.auto_config_to_inner( dest_encoding , misc_option )
132
- end
133
-
134
- def puts_sj( line )
135
- puts NKF.nkf( "-Ws -m0" , line )
136
- end
137
-
138
- def puts_u( line )
139
- puts NKF.nkf( "-Sw -m0" , line )
140
- end
141
-
142
- def nkf_utf8_file( infname , outfname )
143
- File.open( outfname ){|outf|
144
- File.open( infname ){|file|
145
- while line = file.gets
146
- line.chomp!
147
- oline = NKF.nkf( "-w -m0" , line )
148
- outf.printf("%s\n" , oline )
149
- end
150
- }
151
- }
152
- end
153
- end
154
-
155
- end
1
+ # -*- coding utf-8 -*-
2
+
3
+ require "nkf"
4
+
5
+ module Ykutils
6
+ module NKFUTIL
7
+ CODE_TO_NAME = Hash.new("ASCII")
8
+ CODE_TO_NAME[NKF::JIS] = "JIS"
9
+ CODE_TO_NAME[NKF::EUC] = "EUC"
10
+ CODE_TO_NAME[NKF::SJIS] = "SJIS"
11
+ CODE_TO_NAME[NKF::BINARY] = "BINARY"
12
+ CODE_TO_NAME[NKF::UTF8] = "UTF8" if NKF.const_defined?(:UTF8)
13
+
14
+ def self.guess_encoding(str)
15
+ CODE_TO_NAME[NKF.guess(str)]
16
+ end
17
+
18
+ class Assoc
19
+ @@hs = {}
20
+ @@config = nil
21
+
22
+ def self.set(key, value)
23
+ @@hs[key] = if value
24
+ Assoc.convert(value)
25
+ else
26
+ value
27
+ end
28
+ end
29
+
30
+ def self.get(key)
31
+ @@hs[key]
32
+ end
33
+
34
+ def self.to_nkf_encoding_format(encoding)
35
+ case encoding
36
+ when "UTF8"
37
+ "w"
38
+ else
39
+ encoding[0, 1]
40
+ end
41
+ end
42
+
43
+ def self.config(src_encoding, dest_encoding, misc_option = nil)
44
+ @@config = "-#{dest_encoding.to_s[0, 1].downcase}#{src_encoding.to_s[0, 1].upcase}"
45
+ @@config += " #{misc_option}" unless misc_option.nil?
46
+ end
47
+
48
+ def self.auto_config_to_inner(str, misc_option = nil)
49
+ src_encoding = if str
50
+ Assoc.to_nkf_encoding_format(NKFUTIL.guess_encoding(str))
51
+ else
52
+ "A"
53
+ end
54
+
55
+ inner_encoding = Assoc.to_nkf_encoding_format(Assoc.get_inner_encoding)
56
+ if inner_encoding != "A"
57
+ @@config = "-#{inner_encoding.downcase}#{src_encoding.upcase}"
58
+ @@config += " #{misc_option}" unless misc_option.nil?
59
+ end
60
+ src_encoding
61
+ end
62
+
63
+ def self.auto_config_from_inner(dest_enc, misc_option = nil)
64
+ dest_encoding = Assoc.to_nkf_encoding_format(dest_enc)
65
+ inner_encoding = Assoc.to_nkf_encoding_format(Assoc.get_inner_encoding)
66
+ if inner_encoding != "A"
67
+ @@config = "-#{dest_encoding.downcase}#{inner_encoding.upcase}"
68
+ @@config += " #{misc_option}" unless misc_option.nil?
69
+ end
70
+ end
71
+
72
+ def self.convert(str)
73
+ nstr = nil
74
+ unless str.nil?
75
+ if @@config.nil?
76
+ nstr = str
77
+ else
78
+ begin
79
+ nstr = NKF.nkf(@@config, str)
80
+ rescue StandardError => e
81
+ puts e
82
+ puts "========="
83
+ pp caller(0)
84
+ end
85
+ end
86
+ end
87
+ nstr
88
+ end
89
+
90
+ def self.get_inner_encoding
91
+ @@inner_encoding = $KCODE == "NONE" ? "ASCII" : $KCODE
92
+ end
93
+ end
94
+
95
+ def self.set(key, value)
96
+ Assoc.set(key, value)
97
+ end
98
+
99
+ def self.get(key)
100
+ Assoc.get(key)
101
+ end
102
+
103
+ def self.convert(str)
104
+ if str.nil?
105
+ ""
106
+ else
107
+ Assoc.convert(str)
108
+ end
109
+ end
110
+
111
+ def self.assoc_equal(target, key)
112
+ target == key || target == Assoc.get(key)
113
+ end
114
+
115
+ def self.config(src_encoding, dest_encoding, misc_option = nil)
116
+ Assoc.config(src_encoding, dest_encoding, misc_option)
117
+ end
118
+
119
+ def self.auto_config_to_inner(str, misc_option = nil)
120
+ Assoc.auto_config_to_inner(str, misc_option)
121
+ end
122
+
123
+ def self.auto_config_from_inner(dest_encoding, misc_option = nil)
124
+ Assoc.auto_config_to_inner(dest_encoding, misc_option)
125
+ end
126
+
127
+ def puts_sj(line)
128
+ puts NKF.nkf("-Ws -m0", line)
129
+ end
130
+
131
+ def puts_u(line)
132
+ puts NKF.nkf("-Sw -m0", line)
133
+ end
134
+
135
+ def nkf_utf8_file(infname, outfname)
136
+ File.open(outfname) do |outf|
137
+ File.open(infname) do |file|
138
+ while line = file.gets
139
+ line.chomp!
140
+ oline = NKF.nkf("-w -m0", line)
141
+ outf.printf("%s\n", oline)
142
+ end
143
+ end
144
+ end
145
+ end
146
+ end
147
+ end
@@ -1,70 +1,74 @@
1
1
  # -*- coding utf-8 -*-
2
2
 
3
- require 'nkf'
3
+ require "nkf"
4
4
 
5
5
  module Ykutils
6
6
  module NKFUTIL
7
- #ASCII-8BIT
8
- #US-ASCII
9
- CODE_TO_NAME = Hash.new("ASCII")
10
- CODE_TO_NAME["US-ASCII"] = "ASCII"
11
- CODE_TO_NAME["stateless-ISO-2022-JP"] = "JIS"
12
- CODE_TO_NAME["ISO-2022-JP"] = "JIS"
13
- CODE_TO_NAME["ISO-2022-JP-2"] = "JIS"
14
- CODE_TO_NAME["EUC-JP"] = "EUC"
15
- CODE_TO_NAME["eucJP-ms"] = "EUC"
16
- CODE_TO_NAME["Shift_JIS"] = "SJIS"
17
- CODE_TO_NAME["Windows-31J"] = "SJIS"
18
- CODE_TO_NAME["ASCII-8BIT"] = "BINARY"
19
- CODE_TO_NAME["UTF-8"] = "UTF8"
20
-
21
- NAME_TO_ENCODING = Hash.new("US-ASCII")
22
- NAME_TO_ENCODING["UTF8"] = "UTF-8"
23
- NAME_TO_ENCODING["ASCII"] = "US-ASCII"
24
- NAME_TO_ENCODING["JIS"] = "ISO-2022-JP"
25
- NAME_TO_ENCODING["EUC"] = "EUC-JP"
26
- NAME_TO_ENCODING["SJIS"] = "Winodws-31J"
27
- NAME_TO_ENCODING["BINARY"] = "ASCII-8BIT"
28
-
29
- def NKFUTIL.guess_encoding(str)
30
- puts "**#{str.encoding}"
31
- CODE_TO_NAME[ str.encoding.to_s ]
7
+ # ASCII-8BIT
8
+ # US-ASCII
9
+ unless CODE_TO_NAME
10
+ CODE_TO_NAME = Hash.new("ASCII")
11
+ CODE_TO_NAME["US-ASCII"] = "ASCII"
12
+ CODE_TO_NAME["stateless-ISO-2022-JP"] = "JIS"
13
+ CODE_TO_NAME["ISO-2022-JP"] = "JIS"
14
+ CODE_TO_NAME["ISO-2022-JP-2"] = "JIS"
15
+ CODE_TO_NAME["EUC-JP"] = "EUC"
16
+ CODE_TO_NAME["eucJP-ms"] = "EUC"
17
+ CODE_TO_NAME["Shift_JIS"] = "SJIS"
18
+ CODE_TO_NAME["Windows-31J"] = "SJIS"
19
+ CODE_TO_NAME["ASCII-8BIT"] = "BINARY"
20
+ CODE_TO_NAME["UTF-8"] = "UTF8"
21
+ end
22
+
23
+ begin
24
+ NAME_TO_ENCODING
25
+ rescue => ex
26
+ NAME_TO_ENCODING = Hash.new("US-ASCII")
27
+ NAME_TO_ENCODING["UTF8"] = "UTF-8"
28
+ NAME_TO_ENCODING["ASCII"] = "US-ASCII"
29
+ NAME_TO_ENCODING["JIS"] = "ISO-2022-JP"
30
+ NAME_TO_ENCODING["EUC"] = "EUC-JP"
31
+ NAME_TO_ENCODING["SJIS"] = "Winodws-31J"
32
+ NAME_TO_ENCODING["BINARY"] = "ASCII-8BIT"
32
33
  end
33
34
 
34
- def NKFUTIL.config( src_encoding, dest_encoding , misc_option = nil )
35
+ def self.guess_encoding(str)
36
+ puts "**#{str.encoding}"
37
+ CODE_TO_NAME[str.encoding.to_s]
35
38
  end
36
39
 
40
+ def self.config(src_encoding, dest_encoding, misc_option = nil); end
41
+
37
42
  class Assoc
38
43
  @@hs = {}
39
44
  @@config = nil
40
45
 
41
- def Assoc.set( key, value )
42
- if value
43
- @@hs[key] = Assoc.convert( value )
44
- else
45
- @@hs[key] = value
46
- end
46
+ def self.set(key, value)
47
+ @@hs[key] = if value
48
+ Assoc.convert(value)
49
+ else
50
+ value
51
+ end
47
52
  end
48
53
 
49
- def Assoc.get( key )
54
+ def self.get(key)
50
55
  @@hs[key]
51
56
  end
52
57
 
53
- def Assoc.to_nkf_encoding_format( encoding )
54
- NAME_TO_ENCODING[ encoding ]
58
+ def self.to_nkf_encoding_format(encoding)
59
+ NAME_TO_ENCODING[encoding]
55
60
  end
56
61
 
57
-
58
- def Assoc.config( src_encoding, dest_encoding , misc_option = nil )
62
+ def self.config(_src_encoding, dest_encoding, _misc_option = nil)
59
63
  @@config = dest_encoding
60
64
  end
61
65
 
62
- def Assoc.auto_config_to_inner( str , misc_option = nil )
63
- if str
64
- src_encoding = Assoc.to_nkf_encoding_format( NKFUTIL.guess_encoding( str ) )
65
- else
66
- src_encoding = "US-ASCII"
67
- end
66
+ def self.auto_config_to_inner(str, _misc_option = nil)
67
+ src_encoding = if str
68
+ Assoc.to_nkf_encoding_format(NKFUTIL.guess_encoding(str))
69
+ else
70
+ "US-ASCII"
71
+ end
68
72
 
69
73
  # inner_encoding = Assoc.to_nkf_encoding_format( Assoc.get_inner_encoding )
70
74
  # if inner_encoding != "US-ASCII"
@@ -74,8 +78,8 @@ module Ykutils
74
78
  src_encoding
75
79
  end
76
80
 
77
- def Assoc.auto_config_from_inner( dest_enc , misc_option = nil )
78
- dest_encoding = Assoc.to_nkf_encoding_format( dest_enc )
81
+ def self.auto_config_from_inner(dest_enc, _misc_option = nil)
82
+ dest_encoding = Assoc.to_nkf_encoding_format(dest_enc)
79
83
  # inner_encoding = Assoc.to_nkf_encoding_format( Assoc.get_inner_encoding )
80
84
  # if inner_encoding != "US-ASCII"
81
85
  # @@config = dest_encoding.downcase
@@ -83,20 +87,20 @@ module Ykutils
83
87
  # end
84
88
  end
85
89
 
86
- def Assoc.convert( str )
90
+ def self.convert(str)
87
91
  nstr = nil
88
- if str != nil
89
- if @@config != nil
92
+ unless str.nil?
93
+ if @@config.nil?
94
+ nstr = str
95
+ else
90
96
  begin
91
97
  # nstr = NKF.nkf( @@config , str )
92
- nstr = str.encode( @@config )
93
- rescue => ex
94
- puts ex
98
+ nstr = str.encode(@@config)
99
+ rescue StandardError => e
100
+ puts e
95
101
  puts "========="
96
102
  pp caller(0)
97
103
  end
98
- else
99
- nstr = str
100
104
  end
101
105
  end
102
106
  nstr
@@ -107,59 +111,59 @@ module Ykutils
107
111
  # end
108
112
  end
109
113
 
110
- def NKFUTIL.set( key, value )
111
- Assoc.set( key , value )
114
+ def self.set(key, value)
115
+ Assoc.set(key, value)
112
116
  end
113
117
 
114
- def NKFUTIL.get( key )
115
- Assoc.get( key )
118
+ def self.get(key)
119
+ Assoc.get(key)
116
120
  end
117
121
 
118
- def NKFUTIL.convert( str )
119
- if str != nil
120
- Assoc.convert( str )
121
- else
122
+ def self.convert(str)
123
+ if str.nil?
122
124
  ""
125
+ else
126
+ Assoc.convert(str)
123
127
  end
124
128
  end
125
129
 
126
- def NKFUTIL.assoc_equal( target , key )
127
- target == key || target == Assoc.get( key )
130
+ def self.assoc_equal(target, key)
131
+ target == key || target == Assoc.get(key)
128
132
  end
129
133
 
130
- def NKFUTIL.config( src_encoding, dest_encoding , misc_option = nil )
131
- Assoc.config( src_encoding, dest_encoding , misc_option )
132
- end
134
+ def self.config(src_encoding, dest_encoding, misc_option = nil)
135
+ Assoc.config(src_encoding, dest_encoding, misc_option)
136
+ end
133
137
 
134
- def NKFUTIL.auto_config_to_inner( str, misc_option = nil )
135
- Assoc.auto_config_to_inner( str , misc_option )
138
+ def self.auto_config_to_inner(str, misc_option = nil)
139
+ Assoc.auto_config_to_inner(str, misc_option)
136
140
  end
137
141
 
138
- def NKFUTIL.auto_config_from_inner( dest_encoding , misc_option = nil )
139
- Assoc.auto_config_to_inner( dest_encoding , misc_option )
140
- end
142
+ def self.auto_config_from_inner(dest_encoding, misc_option = nil)
143
+ Assoc.auto_config_to_inner(dest_encoding, misc_option)
144
+ end
141
145
 
142
- def puts_sj( line )
143
- puts line.encod( NAME_TO_ENCODING["SJIS"] )
146
+ def puts_sj(line)
147
+ puts line.encod(NAME_TO_ENCODING["SJIS"])
144
148
  # puts NKF.nkf( "-Ws -m0" , line )
145
149
  end
146
150
 
147
- def puts_u( line )
148
- puts line.encod( NAME_TO_ENCODING["UTF8"] )
149
- puts NKF.nkf( "-Sw -m0" , line )
151
+ def puts_u(line)
152
+ puts line.encod(NAME_TO_ENCODING["UTF8"])
153
+ puts NKF.nkf("-Sw -m0", line)
150
154
  end
151
155
 
152
- def nkf_utf8_file( infname , outfname )
153
- File.open( outfname ){|outf|
154
- File.open( infname ){|file|
156
+ def nkf_utf8_file(infname, outfname)
157
+ File.open(outfname) do |outf|
158
+ File.open(infname) do |file|
155
159
  while line = file.gets
156
160
  line.chomp!
157
161
  # oline = NKF.nkf( "-w -m0" , line )
158
- oline = line.encode( NAME_TO_ENCODING["UTF8"] )
159
- outf.printf("%s\n" , oline )
162
+ oline = line.encode(NAME_TO_ENCODING["UTF8"])
163
+ outf.printf("%s\n", oline)
160
164
  end
161
- }
162
- }
165
+ end
166
+ end
163
167
  end
164
168
  end
165
169
  end
@@ -1,19 +1,16 @@
1
- require 'pathname'
2
-
3
- module Ykutils
4
- class OSUtil
5
- @@os = nil
6
-
7
- def OSUtil.runtime
8
- unless @@os
9
- if Pathname.pwd.to_s =~ /^\/cygdrive/
10
- @@os=:CYGWIN
11
- else
12
- @@os=:ELSE
13
- end
14
- end
15
- @@os
16
- end
17
- end
18
- end
19
-
1
+ require "pathname"
2
+
3
+ module Ykutils
4
+ class OSUtil
5
+ @@os = nil
6
+
7
+ def self.runtime
8
+ @@os ||= if Pathname.pwd.to_s =~ %r{^/cygdrive}
9
+ :CYGWIN
10
+ else
11
+ :ELSE
12
+ end
13
+ @@os
14
+ end
15
+ end
16
+ end
@@ -1,37 +1,35 @@
1
- require 'pathname'
1
+ require "pathname"
2
2
 
3
3
  module Ykutils
4
4
  module PathOp
5
- def get_buddy_path( fname , append_name = "" , extname = "" )
6
- one_path = Pathname.new( fname ).expand_path
5
+ def get_buddy_path(fname, append_name = "", extname = "")
6
+ one_path = Pathname.new(fname).expand_path
7
7
  dir_path = one_path.dirname
8
8
  base_path = one_path.basename(".*")
9
- append_name = "-2" unless (append_name.empty? or extname.empty?)
9
+ append_name = "-2" unless append_name.empty? or extname.empty?
10
10
  extname ||= one_path.extname
11
11
 
12
- [one_path , dir_path.join( base_path.to_s + append_name + extname )]
12
+ [one_path, dir_path.join(base_path.to_s + append_name + extname)]
13
13
  end
14
14
 
15
- def determine_fname_for_update( fname , ext = ".bak")
16
- get_buddy_path( fname , "" , ext )
15
+ def determine_fname_for_update(fname, ext = ".bak")
16
+ get_buddy_path(fname, "", ext)
17
17
  end
18
18
 
19
- def determine_fname_for_update2( fname )
19
+ def determine_fname_for_update2(fname)
20
20
  begin
21
- ctime = File.ctime( fname )
22
- rescue => ex
21
+ ctime = File.ctime(fname)
22
+ rescue StandardError => e
23
23
  end
24
- ctime = Time.now unless ctime
24
+ ctime ||= Time.now
25
25
  ary = ctime.to_s.split(" ")
26
- extname = File.extname( fname )
27
- append = ["", ary[0] , ary[1].gsub(":","-")].join("-")
28
- get_buddy_path( fname , append , extname )
26
+ extname = File.extname(fname)
27
+ append = ["", ary[0], ary[1].gsub(":", "-")].join("-")
28
+ get_buddy_path(fname, append, extname)
29
29
  end
30
30
 
31
- def file_ensure( fname )
32
- File.open( fname , "w" ).close unless File.exist?( fname )
31
+ def file_ensure(fname)
32
+ File.open(fname, "w").close unless File.exist?(fname)
33
33
  end
34
-
35
34
  end
36
35
  end
37
-