guess4r 0.0.1.20120401.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ *~
2
+ Makefile
3
+ *.log
4
+ *.o
5
+ *.so
6
+ pkg/
data/BSDL ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (C) 2012 wanabe All rights reserved.
2
+
3
+ Redistribution and use in source and binary forms, with or without
4
+ modification, are permitted provided that the following conditions
5
+ are met:
6
+ 1. Redistributions of source code must retain the above copyright
7
+ notice, this list of conditions and the following disclaimer.
8
+ 2. Redistributions in binary form must reproduce the above copyright
9
+ notice, this list of conditions and the following disclaimer in the
10
+ documentation and/or other materials provided with the distribution.
11
+
12
+ THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
13
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
15
+ ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
16
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
18
+ OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
19
+ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
20
+ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
21
+ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
22
+ SUCH DAMAGE.
data/BSDL.ja ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (C) 2012 wanabe All rights reserved.
2
+
3
+ 本ソフトウェアの、以下の制限に従う限り、変更の有無によらず、
4
+ ソースコード及びバイナリ形式の再頒布と使用を許可します。
5
+
6
+ 1. ソースコードの再頒布時は、上記の著作権表示、本条件一覧、および
7
+ 下記免責条項を含めること。
8
+ 2. バイナリ形式の再頒布時は、上記の著作権表示、本条件一覧、および
9
+ 下記免責条項を、付属のドキュメント等の可読資料に含めること。
10
+
11
+ 本ソフトウェアは、著作権者およびコントリビューターによって「現状のまま」
12
+ 提供されており、明示黙示を問わず、商業的な使用可能性、および特定の目的に対する
13
+ 適合性に関する暗黙の保証も含め、またそれに限定されない、いかなる保証もありません。
14
+ 著作権者もコントリビューターも、事由のいかんを問わず、 損害発生の原因いかんを問わず、
15
+ かつ責任の根拠が契約であるか厳格責任であるか(過失その他の)不法行為であるかを問わず、
16
+ 仮にそのような損害が発生する可能性を知らされていたとしても、本ソフトウェアの
17
+ 使用によって発生した(代替品または代用サービスの調達、使用の喪失、データの喪失、
18
+ 利益の喪失、業務の中断も含め、またそれに限定されない)直接損害、間接損害、
19
+ 偶発的な損害、特別損害、懲罰的損害、または結果損害について、一切責任を負わないものとします。
data/COPYING ADDED
@@ -0,0 +1,56 @@
1
+ Ruby is copyrighted free software by wanabe <s.wanabe@gmail.com>.
2
+ You can redistribute it and/or modify it under either the terms of the
3
+ 2-clause BSDL (see the file BSDL), or the conditions below:
4
+
5
+ 1. You may make and give away verbatim copies of the source form of the
6
+ software without restriction, provided that you duplicate all of the
7
+ original copyright notices and associated disclaimers.
8
+
9
+ 2. You may modify your copy of the software in any way, provided that
10
+ you do at least ONE of the following:
11
+
12
+ a) place your modifications in the Public Domain or otherwise
13
+ make them Freely Available, such as by posting said
14
+ modifications to Usenet or an equivalent medium, or by allowing
15
+ the author to include your modifications in the software.
16
+
17
+ b) use the modified software only within your corporation or
18
+ organization.
19
+
20
+ c) give non-standard binaries non-standard names, with
21
+ instructions on where to get the original software distribution.
22
+
23
+ d) make other distribution arrangements with the author.
24
+
25
+ 3. You may distribute the software in object code or binary form,
26
+ provided that you do at least ONE of the following:
27
+
28
+ a) distribute the binaries and library files of the software,
29
+ together with instructions (in the manual page or equivalent)
30
+ on where to get the original distribution.
31
+
32
+ b) accompany the distribution with the machine-readable source of
33
+ the software.
34
+
35
+ c) give non-standard binaries non-standard names, with
36
+ instructions on where to get the original software distribution.
37
+
38
+ d) make other distribution arrangements with the author.
39
+
40
+ 4. You may modify and include the part of the software into any other
41
+ software (possibly commercial). But some files in the distribution
42
+ are not written by the author, so that they are not under these terms.
43
+
44
+ For the list of those files and their copying conditions, see the
45
+ file LEGAL.
46
+
47
+ 5. The scripts and library files supplied as input to or produced as
48
+ output from the software do not automatically fall under the
49
+ copyright of the software, but belong to whomever generated them,
50
+ and may be sold commercially, and may be aggregated with this
51
+ software.
52
+
53
+ 6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
54
+ IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
55
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
56
+ PURPOSE.
data/COPYING.ja ADDED
@@ -0,0 +1,51 @@
1
+ 本プログラムはワナベ <s.wanabe@gmail.com> によるフリーソフトウェアです.
2
+ 2-clause BSDLまたは以下に示す条件で本プログラムを再配布できます.
3
+ 2-clause BSDLについてはBSDLファイルを参照して下さい.
4
+
5
+ 1. 複製は制限なく自由です.
6
+
7
+ 2. 以下の条件のいずれかを満たす時に本プログラムのソースを
8
+ 自由に変更できます.
9
+
10
+ (a) ネットニューズにポストしたり,作者に変更を送付する
11
+ などの方法で,変更を公開する.
12
+
13
+ (b) 変更した本プログラムを自分の所属する組織内部だけで
14
+ 使う.
15
+
16
+ (c) 変更点を明示したうえ,ソフトウェアの名前を変更する.
17
+ そのソフトウェアを配布する時には変更前の本プログラ
18
+ ムも同時に配布する.または変更前の本プログラムのソー
19
+ スの入手法を明示する.
20
+
21
+ (d) その他の変更条件を作者と合意する.
22
+
23
+ 3. 以下の条件のいずれかを満たす時に本プログラムをコンパイ
24
+ ルしたオブジェクトコードや実行形式でも配布できます.
25
+
26
+ (a) バイナリを受け取った人がソースを入手できるように,
27
+ ソースの入手法を明示する.
28
+
29
+ (b) 機械可読なソースコードを添付する.
30
+
31
+ (c) 変更を行ったバイナリは名前を変更したうえ,オリジナ
32
+ ルのソースコードの入手法を明示する.
33
+
34
+ (d) その他の配布条件を作者と合意する.
35
+
36
+ 4. 他のプログラムへの引用はいかなる目的であれ自由です.た
37
+ だし,本プログラムに含まれる他の作者によるコードは,そ
38
+ れぞれの作者の意向による制限が加えられる場合があります.
39
+
40
+ それらファイルの一覧とそれぞれの配布条件などに付いては
41
+ LEGALファイルを参照してください.
42
+
43
+ 5. 本プログラムへの入力となるスクリプトおよび,本プログラ
44
+ ムからの出力の権利は本プログラムの作者ではなく,それぞ
45
+ れの入出力を生成した人に属します.また,本プログラムに
46
+ 組み込まれるための拡張ライブラリについても同様です.
47
+
48
+ 6. 本プログラムは無保証です.作者は本プログラムをサポート
49
+ する意志はありますが,プログラム自身のバグあるいは本プ
50
+ ログラムの実行などから発生するいかなる損害に対しても責
51
+ 任を持ちません.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in guess4r.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The zlib/libpng License
2
+
3
+ Copyright (c) 2012 wanabe
4
+
5
+ This software is provided 'as-is', without any express or implied
6
+ warranty. In no event will the authors be held liable for any damages
7
+ arising from the use of this software.
8
+
9
+ Permission is granted to anyone to use this software for any purpose,
10
+ including commercial applications, and to alter it and redistribute it
11
+ freely, subject to the following restrictions:
12
+
13
+ 1. The origin of this software must not be misrepresented; you must not
14
+ claim that you wrote the original software. If you use this software
15
+ in a product, an acknowledgment in the product documentation would be
16
+ appreciated but is not required.
17
+
18
+ 2. Altered source versions must be plainly marked as such, and must not be
19
+ misrepresented as being the original software.
20
+
21
+ 3. This notice may not be removed or altered from any source
22
+ distribution.
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ guess4r (仮)
2
+ =============
3
+
4
+ guess4r は、libguess を Ruby から扱うことを目的にしたライブラリ(のつもり)です。
5
+
6
+ ビルド・インストール
7
+ --------------------
8
+ ruby >= 1.9.X と libguess が必要です。
9
+
10
+ $ ruby extconf.rb
11
+ $ make
12
+ $ sudo make install
13
+
14
+ 基本手順はこうなります。各自の環境にあわせて変更してください。
15
+
16
+ 使い方
17
+ ------
18
+
19
+ confused_string.encoding # => #<Encoding:ASCII-8BIT>
20
+ confused_string.guess_encoding # => #<Encoding:Windows-31J>
21
+ confused_string.guess.encoding # => #<Encoding:Windows-31J>
22
+ confused_string.guess! # => "\x{8356}\x{8374}\x{8367}JIS\x{95B6}\x{8E9A}\x{97F1}"
23
+ confused_string.encoding # => #<Encoding:Windows-31J>
24
+ guess_open("unknown") {|f| f.read.encoding} # => #<Encoding:EUC-JP>
25
+
26
+ guess_open は判別のために open -> close -> 再度 open という処理を行いますので注意してください。
27
+
28
+ ライセンス
29
+ ----------
30
+
31
+ COPYING, COPYING.ja, BSDL, BSDL.ja, ZLIBL, ZLIBL.ja のいずれかのファイルに従ってください。
32
+ 前半の 3 ファイルは ruby のものをそのまま拝借しただけです。
33
+ 要約すると、2-clause BSD ライセンスか zlib/libpng ライセンスか Ruby の独自ライセンス部分
34
+ またはそれぞれの和訳、のいずれかお好きなものをお選びください、ということです。
35
+ なお和訳については、
36
+ [OSI承認オープンソースライセンス 日本語参考訳](http://sourceforge.jp/projects/opensource/wiki/licenses)
37
+ を参考にさせていただきました。
38
+
39
+ その他
40
+ ======
41
+
42
+ ## 開発動機
43
+
44
+ libguess の ruby バインディングとしては
45
+ [libguess-ruby](http://raa.ruby-lang.org/project/libguess-ruby/)
46
+ がすでにありますが、
47
+
48
+ * ライセンスに "Ruby's" とあり、最終更新日の 2007-04-19 時点のものか最新のものでもよいのかわからない。
49
+ 最終更新日時点と解釈すると、BSDL ではなく GPL と Ruby の独自条項のデュアルライセンスとなり、個人的にちょっと嫌。
50
+ * Ruby 1.9 以降は M17N が入ったのだから、せっかくならそれを使いたい。
51
+
52
+ という私のわがままにより、guess4r をでっち上げることにしました。
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ task :default do
5
+ Dir.chdir "ext"
6
+ system("ruby extconf.rb") if !File.exist?("Makefile")
7
+ system("make")
8
+ Dir.chdir ".."
9
+ end
10
+
11
+ task :test => [:default] do
12
+ $:.unshift "./ext"
13
+ load "test/test_guess4r.rb"
14
+ end
data/ZLIBL ADDED
@@ -0,0 +1,22 @@
1
+ The zlib/libpng License
2
+
3
+ Copyright (c) 2012 wanabe
4
+
5
+ This software is provided 'as-is', without any express or implied
6
+ warranty. In no event will the authors be held liable for any damages
7
+ arising from the use of this software.
8
+
9
+ Permission is granted to anyone to use this software for any purpose,
10
+ including commercial applications, and to alter it and redistribute it
11
+ freely, subject to the following restrictions:
12
+
13
+ 1. The origin of this software must not be misrepresented; you must not
14
+ claim that you wrote the original software. If you use this software
15
+ in a product, an acknowledgment in the product documentation would be
16
+ appreciated but is not required.
17
+
18
+ 2. Altered source versions must be plainly marked as such, and must not be
19
+ misrepresented as being the original software.
20
+
21
+ 3. This notice may not be removed or altered from any source
22
+ distribution.
data/ZLIBL.ja ADDED
@@ -0,0 +1,18 @@
1
+ zlib/libpng ライセンス
2
+
3
+ Copyright (c) 2012 ワナベ
4
+
5
+ 本ソフトウェアは「現状のまま」明示的か暗黙的かを問わず、無保証で提供されます。
6
+ 本ソフトウェアの使用によるいかなる損害についても、作者は一切の責任を負いません。
7
+
8
+ 以下の制限に従う限り、商用アプリケーションを含めて、本ソフトウェアを
9
+ 任意の目的に使用し、自由に改変して再頒布することをすべての人に許可します。
10
+
11
+ 1. 本ソフトウェアの出自について虚偽の表示をしてはいけません。
12
+ あなたがオリジナルのソフトウェアを作成したと主張してはいけません。
13
+ あなたが本ソフトウェアを製品内で使用する場合、
14
+ 製品の文書に謝辞を入れていただければ幸いですが、必須ではありません。
15
+ 2. ソースを変更した場合は、そのことを明示する必要があります。
16
+ オリジナルのソフトウェアであるという虚偽の表示をしてはいけません。
17
+ 3. ソースの頒布物において、この表示を削除または変更したりしてはいけません。
18
+
data/ext/extconf.rb ADDED
@@ -0,0 +1,3 @@
1
+ require "mkmf"
2
+
3
+ have_library("guess") && create_makefile("guess")
data/ext/guess.c ADDED
@@ -0,0 +1,72 @@
1
+ #include <stdio.h>
2
+ #include <ruby.h>
3
+ #include <ruby/encoding.h>
4
+ #include <libguess/libguess.h>
5
+
6
+ #ifndef GUESS_REGION_DEFAULT
7
+ #define GUESS_REGION_DEFAULT GUESS_REGION_JP
8
+ #endif
9
+
10
+ VALUE guess4r_enc__guess_region(VALUE klass) {
11
+ return rb_ivar_get(klass, rb_intern("guess_region"));
12
+ }
13
+
14
+ static rb_encoding* _guess_encoding(VALUE str) {
15
+ rb_encoding *enc;
16
+ const char *encname;
17
+
18
+ encname = libguess_determine_encoding(RSTRING_PTR(str), RSTRING_LEN(str),
19
+ RSTRING_PTR(guess4r_enc__guess_region(rb_cEncoding)));
20
+ return rb_enc_find(encname);
21
+ }
22
+
23
+ static VALUE guess4r_str__guess_encoding(VALUE str) {
24
+ return rb_enc_from_encoding(_guess_encoding(str));
25
+ }
26
+
27
+ static VALUE guess4r_str__guess_bang(VALUE str) {
28
+ return rb_enc_associate(str, _guess_encoding(str));
29
+ }
30
+
31
+ static VALUE guess4r_str__guess(VALUE str) {
32
+ return guess4r_str__guess_bang(rb_str_dup(str));
33
+ }
34
+
35
+ static VALUE guess4r_f__guess_open(int argc, VALUE *argv) {
36
+ VALUE *args, opt, f, str, enc;
37
+ int i;
38
+
39
+ args = ALLOCA_N(VALUE, argc + 1);
40
+ args[0] = argv[0];
41
+ if (TYPE(argv[argc - 1]) != T_HASH) {
42
+ args[1] = args[argc++] = rb_hash_new();
43
+ } else {
44
+ args[1] = argv[argc - 1];
45
+ }
46
+
47
+ rb_hash_aset(args[1], ID2SYM(rb_intern("encoding")), rb_const_get(rb_cEncoding, rb_intern("ASCII_8BIT")));
48
+ rb_funcall(args[1], rb_intern("delete"), 1, ID2SYM(rb_intern("external_encoding")));
49
+ rb_funcall(args[1], rb_intern("delete"), 1, ID2SYM(rb_intern("internal_encoding")));
50
+ f = rb_funcall2(rb_mKernel, rb_intern("open"), 2, args);
51
+ str = rb_funcall(f, rb_intern("read"), 0);
52
+ enc = guess4r_str__guess_encoding(str);
53
+ rb_funcall(f, rb_intern("close"), 0);
54
+ rb_hash_aset(args[1], ID2SYM(rb_intern("encoding")), enc);
55
+
56
+ MEMCPY(args, argv, VALUE, argc - 1);
57
+ return rb_funcall_passing_block(rb_mKernel, rb_intern("open"), argc, args);
58
+ }
59
+
60
+ VALUE guess4r_enc__guess_region_set(VALUE klass, VALUE str) {
61
+ return rb_str_replace(guess4r_enc__guess_region(klass), str);
62
+ }
63
+
64
+ void Init_guess() {
65
+ rb_define_method(rb_cString, "guess_encoding", guess4r_str__guess_encoding, 0);
66
+ rb_define_method(rb_cString, "guess!", guess4r_str__guess_bang, 0);
67
+ rb_define_method(rb_cString, "guess", guess4r_str__guess, 0);
68
+ rb_define_global_function("guess_open", guess4r_f__guess_open, -1);
69
+ rb_ivar_set(rb_cEncoding, rb_intern("guess_region"), rb_str_new2(GUESS_REGION_DEFAULT));
70
+ rb_define_singleton_method(rb_cEncoding, "guess_region", guess4r_enc__guess_region, 0);
71
+ rb_define_singleton_method(rb_cEncoding, "guess_region=", guess4r_enc__guess_region_set, 1);
72
+ }
data/guess4r.gemspec ADDED
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.authors = ["wanabe"]
5
+ gem.email = ["s.wanabe@gmail.com"]
6
+ gem.description = "libguess binding for ruby 1.9 or later."
7
+ gem.summary = "libguess binding for ruby 1.9 or later."
8
+ gem.homepage = ""
9
+
10
+ gem.files = `git ls-files`.split($\)
11
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
12
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
13
+ gem.name = "guess4r"
14
+ gem.require_paths = ["ext"]
15
+ gem.version = "0.0.1.20120401.1"
16
+ gem.extensions << "ext/extconf.rb"
17
+
18
+ end
@@ -0,0 +1,57 @@
1
+ # coding:utf-8
2
+ require "test/unit"
3
+ require "guess"
4
+
5
+ class TestGuess < Test::Unit::TestCase
6
+ def setup
7
+ test_string = "本日は晴天なり"
8
+ @codings = ["sjis", "utf-8", "euc-jp"].map do |name|
9
+ Encoding.find(name)
10
+ end
11
+ @strings = @codings.map do |coding|
12
+ test_string.encode(coding).force_encoding(Encoding::ASCII_8BIT)
13
+ end
14
+ end
15
+
16
+ def test_string_guess_encoding
17
+ assert_equal(@codings, @strings.map {|str| str.guess_encoding})
18
+ end
19
+
20
+ def test_string_guess
21
+ guessed_table = @strings.map do |str|
22
+ str.dup.guess!
23
+ end
24
+
25
+ assert_equal(@codings, @strings.map {|str| str.guess.encoding})
26
+ assert_equal(@codings, guessed_table.map {|str| str.encoding})
27
+ assert(@strings.all? {|str| str.encoding == Encoding::ASCII_8BIT})
28
+ end
29
+
30
+ def test_io_guess_open
31
+ default_external = Encoding.default_external
32
+ Encoding.default_external = Encoding::ASCII_8BIT
33
+
34
+ begin
35
+ io = nil
36
+ open("README.md") do |f|
37
+ assert_equal(Encoding::ASCII_8BIT, f.external_encoding)
38
+ end
39
+ guess_open("README.md") do |f|
40
+ io = f
41
+ assert_equal(Encoding::UTF_8, f.external_encoding)
42
+ end
43
+ assert(io.closed?)
44
+ guess_open("README.md", :encoding => Encoding::US_ASCII) do |f|
45
+ assert_equal(Encoding::UTF_8, f.external_encoding)
46
+ end
47
+ guess_open("README.md", :external_encoding => Encoding::US_ASCII) do |f|
48
+ assert_equal(Encoding::UTF_8, f.external_encoding)
49
+ end
50
+ assert_raise(ArgumentError) do
51
+ guess_open("README.md", "r:SJIS") {}
52
+ end
53
+ ensure
54
+ Encoding.default_external = default_external
55
+ end
56
+ end
57
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: guess4r
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.20120401.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - wanabe
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-01 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: libguess binding for ruby 1.9 or later.
15
+ email:
16
+ - s.wanabe@gmail.com
17
+ executables: []
18
+ extensions:
19
+ - ext/extconf.rb
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - BSDL
24
+ - BSDL.ja
25
+ - COPYING
26
+ - COPYING.ja
27
+ - Gemfile
28
+ - LICENSE
29
+ - README.md
30
+ - Rakefile
31
+ - ZLIBL
32
+ - ZLIBL.ja
33
+ - ext/extconf.rb
34
+ - ext/guess.c
35
+ - guess4r.gemspec
36
+ - test/test_guess4r.rb
37
+ homepage: ''
38
+ licenses: []
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - ext
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 1.8.11
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: libguess binding for ruby 1.9 or later.
61
+ test_files:
62
+ - test/test_guess4r.rb
63
+ has_rdoc: