windows-pr 0.9.0 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES CHANGED
@@ -1,3 +1,11 @@
1
+ = 0.9.1 - 28-Jul-2008
2
+ * Yet another refactoring of the multi_to_wide and wide_to_multi helper
3
+ methods in the Windows::Unicode module. In addition to a bug fix, they
4
+ now raise an ArgumentError if they fail.
5
+ * Yet another tweak to get_last_error in the Windows::Error module. It now
6
+ always uses the explicit ANSI version of the FormatMessage function.
7
+ * Added tests for the multi_to_wide and wide_to_multi helper methods.
8
+
1
9
  = 0.9.0 - 26-Jul-2008
2
10
  * The process of converting prototypes from 'P' to 'S' has begun. This
3
11
  means that this library now requires win32-api 1.2.0 or later.
data/lib/windows/error.rb CHANGED
@@ -418,10 +418,10 @@ module Windows
418
418
  # returns a human readable string.
419
419
  #
420
420
  def get_last_error(err_num = GetLastError.call)
421
- buf = $KCODE == 'UTF8' ? 0.chr * 520 : 0.chr * 260
421
+ buf = 0.chr * 260
422
422
  flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ARGUMENT_ARRAY
423
- FormatMessage.call(flags, 0, err_num, 0, buf, buf.size, 0)
424
- buf.split(0.chr).join[ /^[^\0\r\n]*/ ] rescue 'Unknown error'
423
+ FormatMessageA.call(flags, 0, err_num, 0, buf, buf.size, 0)
424
+ buf.strip
425
425
  end
426
426
 
427
427
  # Macros from WinError.h
@@ -1,9 +1,11 @@
1
1
  require 'windows/api'
2
2
  require 'windows/msvcrt/string'
3
+ require 'windows/error'
3
4
 
4
5
  module Windows
5
6
  module Unicode
6
7
  include Windows::MSVCRT::String
8
+ include Windows::Error
7
9
 
8
10
  API.auto_namespace = 'Windows::Unicode'
9
11
  API.auto_constant = true
@@ -95,24 +97,16 @@ module Windows
95
97
  encoding = ($KCODE == 'UTF8') ? CP_UTF8 : CP_ACP
96
98
  end
97
99
 
98
- buf = ' ' * strlen(string) * 2
99
-
100
- int = MultiByteToWideChar(
101
- encoding,
102
- 0,
103
- string,
104
- strlen(string),
105
- buf,
106
- strlen(buf)
107
- )
100
+ int = MultiByteToWideChar(encoding, 0, string, -1, nil, 0)
108
101
 
102
+ # Trailing nulls are retained
109
103
  if int > 0
110
- dest = ' ' * int * 2
111
- wcsncpy(dest, buf, int)
112
- dest
104
+ buf = ' ' * int * 2
105
+ MultiByteToWideChar(encoding, 0, string, -1, buf, int)
106
+ buf
113
107
  else
114
- string
115
- end
108
+ raise ArgumentError, get_last_error
109
+ end
116
110
  end
117
111
 
118
112
  # Maps a wide character string to a new character string using the
@@ -121,34 +115,24 @@ module Windows
121
115
  #
122
116
  # If the function fails it simply returns the string as-is.
123
117
  #
124
- def wide_to_multi(string, encoding=nil)
125
- return nil unless string
126
- raise TypeError unless string.is_a?(String)
118
+ def wide_to_multi(wstring, encoding=nil)
119
+ return nil unless wstring
120
+ raise TypeError unless wstring.is_a?(String)
127
121
 
128
122
  unless encoding
129
123
  encoding = ($KCODE == 'UTF8') ? CP_UTF8 : CP_ACP
130
124
  end
131
-
132
- buf = ' ' * wcslen(string)
133
-
134
- int = WideCharToMultiByte(
135
- encoding,
136
- 0,
137
- string,
138
- wcslen(string),
139
- buf,
140
- strlen(buf),
141
- 0,
142
- 0
143
- )
144
125
 
126
+ int = WideCharToMultiByte(encoding, 0, wstring, -1, 0, 0, nil, nil)
127
+
128
+ # Trailing nulls are stripped
145
129
  if int > 0
146
- dest = ' ' * int
147
- strncpy(dest, buf, int)
148
- dest
130
+ buf = ' ' * int
131
+ WideCharToMultiByte(encoding, 0, wstring, -1, buf, strlen(buf), nil, nil)
132
+ buf[ /^[^\0]*/ ]
149
133
  else
150
- string
151
- end
134
+ raise ArgumentError, get_last_error
135
+ end
152
136
  end
153
137
  end
154
138
  end
data/test/tc_unicode.rb CHANGED
@@ -6,45 +6,73 @@
6
6
  require "windows/unicode"
7
7
  require "test/unit"
8
8
 
9
- class UnicodeFoo
10
- include Windows::Unicode
11
- end
12
-
13
9
  class TC_Windows_Unicode < Test::Unit::TestCase
14
- def setup
15
- @foo = UnicodeFoo.new
16
- end
10
+ include Windows::Unicode
17
11
 
18
12
  def test_numeric_constants
19
- assert_equal(0, UnicodeFoo::CP_ACP)
20
- assert_equal(1, UnicodeFoo::CP_OEMCP)
21
- assert_equal(2, UnicodeFoo::CP_MACCP)
22
- assert_equal(3, UnicodeFoo::CP_THREAD_ACP)
23
- assert_equal(42, UnicodeFoo::CP_SYMBOL)
24
- assert_equal(65000, UnicodeFoo::CP_UTF7)
25
- assert_equal(65001, UnicodeFoo::CP_UTF8)
13
+ assert_equal(0, CP_ACP)
14
+ assert_equal(1, CP_OEMCP)
15
+ assert_equal(2, CP_MACCP)
16
+ assert_equal(3, CP_THREAD_ACP)
17
+ assert_equal(42, CP_SYMBOL)
18
+ assert_equal(65000, CP_UTF7)
19
+ assert_equal(65001, CP_UTF8)
26
20
 
27
- assert_equal(0x00000001, UnicodeFoo::MB_PRECOMPOSED)
28
- assert_equal(0x00000002, UnicodeFoo::MB_COMPOSITE)
29
- assert_equal(0x00000004, UnicodeFoo::MB_USEGLYPHCHARS)
30
- assert_equal(0x00000008, UnicodeFoo::MB_ERR_INVALID_CHARS)
21
+ assert_equal(0x00000001, MB_PRECOMPOSED)
22
+ assert_equal(0x00000002, MB_COMPOSITE)
23
+ assert_equal(0x00000004, MB_USEGLYPHCHARS)
24
+ assert_equal(0x00000008, MB_ERR_INVALID_CHARS)
31
25
 
32
- assert_equal(0x00000200, UnicodeFoo::WC_COMPOSITECHECK)
33
- assert_equal(0x00000010, UnicodeFoo::WC_DISCARDNS)
34
- assert_equal(0x00000020, UnicodeFoo::WC_SEPCHARS)
35
- assert_equal(0x00000040, UnicodeFoo::WC_DEFAULTCHAR)
36
- assert_equal(0x00000400, UnicodeFoo::WC_NO_BEST_FIT_CHARS)
26
+ assert_equal(0x00000200, WC_COMPOSITECHECK)
27
+ assert_equal(0x00000010, WC_DISCARDNS)
28
+ assert_equal(0x00000020, WC_SEPCHARS)
29
+ assert_equal(0x00000040, WC_DEFAULTCHAR)
30
+ assert_equal(0x00000400, WC_NO_BEST_FIT_CHARS)
37
31
  end
38
32
 
39
33
  def test_method_constants
40
- assert_not_nil(UnicodeFoo::GetTextCharset)
41
- assert_not_nil(UnicodeFoo::GetTextCharsetInfo)
42
- assert_not_nil(UnicodeFoo::IsDBCSLeadByte)
43
- assert_not_nil(UnicodeFoo::IsDBCSLeadByteEx)
44
- assert_not_nil(UnicodeFoo::IsTextUnicode)
45
- assert_not_nil(UnicodeFoo::MultiByteToWideChar)
46
- assert_not_nil(UnicodeFoo::TranslateCharsetInfo)
47
- assert_not_nil(UnicodeFoo::WideCharToMultiByte)
34
+ assert_respond_to(self, :GetTextCharset)
35
+ assert_respond_to(self, :GetTextCharsetInfo)
36
+ assert_respond_to(self, :IsDBCSLeadByte)
37
+ assert_respond_to(self, :IsDBCSLeadByteEx)
38
+ assert_respond_to(self, :IsTextUnicode)
39
+ assert_respond_to(self, :MultiByteToWideChar)
40
+ assert_respond_to(self, :TranslateCharsetInfo)
41
+ assert_respond_to(self, :WideCharToMultiByte)
42
+ end
43
+
44
+ def test_multi_to_wide
45
+ assert_respond_to(self, :multi_to_wide)
46
+ assert_equal("\000\000", multi_to_wide(''))
47
+ assert_equal("h\000e\000l\000l\000o\000\000\000", multi_to_wide('hello'))
48
+ assert_equal("\316\000\" \316\000\273\000\316\000\273\000\316\000\254\000\317\000\222\001\000\000", multi_to_wide("Ελλάσ"))
49
+ end
50
+
51
+ def test_multi_to_wide_with_encoding
52
+ assert_equal("h\000e\000l\000l\000o\000\000\000", multi_to_wide('hello', CP_UTF8))
53
+ assert_equal("\225\003\273\003\273\003\254\003\303\003\000\000", multi_to_wide("Ελλάσ", CP_UTF8))
54
+ end
55
+
56
+ def test_multi_to_wide_expected_errors
57
+ assert_raise(TypeError){ multi_to_wide(1) }
58
+ assert_raise(TypeError){ multi_to_wide([]) }
59
+ end
60
+
61
+ def test_wide_to_multi
62
+ assert_respond_to(self, :wide_to_multi)
63
+ assert_equal('', wide_to_multi("\000\000"))
64
+ assert_equal('hello', wide_to_multi("h\000e\000l\000l\000o\000\000\000"))
65
+ assert_equal("Ελλάσ", wide_to_multi("\316\000\" \316\000\273\000\316\000\273\000\316\000\254\000\317\000\222\001\000\000"))
66
+ end
67
+
68
+ def test_wide_to_multi_with_encoding
69
+ assert_equal('hello', wide_to_multi("h\000e\000l\000l\000o\000\000\000"), CP_UTF8)
70
+ assert_equal("Ελλάσ", wide_to_multi("\225\003\273\003\273\003\254\003\303\003\000\000", CP_UTF8))
71
+ end
72
+
73
+ def test_wide_to_multi_expected_errors
74
+ assert_raise(TypeError){ wide_to_multi(1) }
75
+ assert_raise(TypeError){ wide_to_multi([]) }
48
76
  end
49
77
 
50
78
  def teardown
data/windows-pr.gemspec CHANGED
@@ -2,8 +2,8 @@ require "rubygems"
2
2
 
3
3
  spec = Gem::Specification.new do |gem|
4
4
  gem.name = "windows-pr"
5
- gem.version = "0.9.0"
6
- gem.author = "Daniel J. Berger"
5
+ gem.version = "0.9.1"
6
+ gem.authors = ["Daniel J. Berger", "Park Heesob"]
7
7
  gem.email = "djberg96@gmail.com"
8
8
  gem.homepage = "http://www.rubyforge.org/projects/win32utils"
9
9
  gem.rubyforge_project = "win32utils"
metadata CHANGED
@@ -1,15 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: windows-pr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.9.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel J. Berger
8
+ - Park Heesob
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
12
 
12
- date: 2008-07-26 00:00:00 -06:00
13
+ date: 2008-07-29 00:00:00 -06:00
13
14
  default_executable:
14
15
  dependencies:
15
16
  - !ruby/object:Gem::Dependency