rubydict 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 931f2e9bf11c939423ba19a9694396a5fe063a55
4
+ data.tar.gz: 9aa4735007e0edbedda6f4dc72bf702975567c41
5
+ SHA512:
6
+ metadata.gz: cebee6e12ebb13b1170bd672bd31cc7a7f66bd9c318d4382045750cd818e1401ba2b1ba386dabd6774b0ee5b96c31d615f06aab72086cb618e606b916b92373b
7
+ data.tar.gz: 3dd2199477693d04a270efbecd848b88858b5bd26c13cfdc088581ee67724070134b577547b08d6d3db254a9ba2db77a39f54a4c1f313d7d403e6d57ad3a1a05
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rubydict.gemspec
4
+ gemspec
5
+
6
+ gem 'ffi'
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 teng.cong
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Rubydict
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rubydict'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rubydict
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/rubydict/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/clib/lookup.c ADDED
@@ -0,0 +1,94 @@
1
+ /* Author: cocobear.cn@gmail.com
2
+ * Using GPL v2
3
+ */
4
+
5
+ #include <stdio.h>
6
+ #include <sys/types.h>
7
+ #include <fcntl.h>
8
+ #include <sys/mman.h>
9
+ #include <string.h>
10
+ #include <arpa/inet.h>
11
+ #include <zlib.h>
12
+ #include <stdlib.h>
13
+
14
+ #include "lookup.h"
15
+
16
+ char * lookup(char *file_prefix, long file_size, long wc, char *word)
17
+ {
18
+ int fd;
19
+
20
+ char *data;
21
+ const char *p;
22
+ long index_size;
23
+ int offset;
24
+ int size;
25
+ int flag = 0;
26
+ unsigned char *buf;
27
+
28
+ char idx_file_name[256];
29
+ char dict_file_name[256];
30
+ char gz_file_name[256];
31
+ strcpy(idx_file_name, file_prefix);
32
+ strcat(idx_file_name, ".idx");
33
+
34
+ strcpy(dict_file_name, file_prefix);
35
+ strcat(dict_file_name, ".dict");
36
+
37
+ strcpy(gz_file_name, file_prefix);
38
+ strcat(gz_file_name, ".dict.dz");
39
+
40
+ if ((fd = open(idx_file_name, O_RDONLY)) < 0) {
41
+ printf("open failed\n");
42
+ return NULL;
43
+ }
44
+
45
+ data = (char *)mmap( NULL, file_size, PROT_READ, MAP_SHARED, fd, 0);
46
+ p = data;
47
+ int i;
48
+ for (i=0;i<wc;i++) {
49
+ index_size = strlen(p) + 1 + 2*sizeof(int);
50
+ if (strcmp(word, p) == 0) {
51
+ flag = 1;
52
+ }
53
+ if (flag == 1) {
54
+ close(fd);
55
+ offset = ntohl(*(int *)(p + strlen(p) + 1));
56
+ size = ntohl(*(int *)(p + strlen(p) + 1 + sizeof(int)));
57
+ //printf("offset=%d\nsize\%d\n",offset,size);
58
+
59
+ //1. check dict.dz file
60
+ gzFile zfile;
61
+ zfile = gzopen(gz_file_name, "rb");
62
+ if (zfile != Z_NULL) {
63
+ gzseek(zfile, offset, SEEK_SET);
64
+ buf = (unsigned char *)malloc(size+1);
65
+ memset(buf, '\0', size+1);
66
+ gzread(zfile, buf, size);
67
+ //printf("%s\n", buf);
68
+ return buf;
69
+ }
70
+ n:
71
+ //2. check dict file if dict.dz file not existed
72
+ if ((fd = open(dict_file_name, O_RDONLY)) < 0) {
73
+ printf("open %s failed \n",dict_file_name);
74
+ return "Open dict file faile";
75
+ }
76
+ lseek(fd, offset, SEEK_SET);
77
+ buf = (unsigned char *)malloc(size+1);
78
+ memset(buf, '\0', size+1);
79
+ read(fd, buf, size);
80
+ //printf("%s\n",buf);
81
+ close(fd);
82
+ return buf;
83
+ }
84
+ p += index_size;
85
+ }
86
+
87
+ return "Not found";
88
+ }
89
+
90
+ int main() {
91
+ char *des = lookup("../dict/oxford/oxford-gb", 721264, 39429, "book");
92
+ printf("%s", des);
93
+ return 0;
94
+ }
data/clib/lookup.h ADDED
@@ -0,0 +1,2 @@
1
+ char * lookup(char *file_prefix, long file_size, long wc, char *word);
2
+ void sayhello();
data/clib/makefile ADDED
@@ -0,0 +1,8 @@
1
+ MODULE = lookup
2
+
3
+ lib$(MODULE).so : $(MODULE).c
4
+ gcc -shared -lz -o lib$(MODULE).so $(MODULE).c
5
+
6
+ clean:
7
+ rm -f lib$(MODULE).so
8
+
Binary file
@@ -0,0 +1,29 @@
1
+ require 'ffi'
2
+
3
+ module Rubydict
4
+ module MixBox
5
+ module Dictionary
6
+ extend FFI::Library
7
+ ffi_lib "clib/liblookup.so"
8
+ attach_function :lookup, [:string, :long, :long, :string], :string
9
+ end
10
+
11
+ class Translator
12
+ def initialize dict_path
13
+ @dict_path = dict_path
14
+ @file_size, @word_count = 0, 0
15
+ @file = File.open("#{dict_path}.ifo")
16
+ @file.each do |line|
17
+ mapping = line.strip.split('=')
18
+ @word_count = mapping[1].to_i if mapping[0] == 'wordcount'
19
+ @file_size = mapping[1].to_i if mapping[0] == 'idxfilesize'
20
+ end
21
+ @file.close
22
+ end
23
+
24
+ def lookup word
25
+ Dictionary.lookup @dict_path, @file_size, @word_count, word
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1 @@
1
+ require 'rubydict/mixbox/translator'
@@ -0,0 +1,3 @@
1
+ module Rubydict
2
+ VERSION = "0.0.2"
3
+ end
data/lib/rubydict.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "rubydict/version"
2
+
3
+ module Rubydict
4
+ end
5
+
6
+ require "rubydict/mixbox"
data/rubydict.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rubydict/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rubydict"
8
+ spec.version = Rubydict::VERSION
9
+ spec.authors = ["jason.li", "teng.C"]
10
+ spec.email = ["congteng45@gmail.com"]
11
+ spec.summary = %q{a simple wrapper for star dict}
12
+ spec.description = %q{a simple wrapper for star dict....}
13
+ spec.homepage = "http://qqdd.me"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec", "~> 2.6"
24
+ end
@@ -0,0 +1,19 @@
1
+ require 'rubydict'
2
+
3
+ describe Rubydict::MixBox::Dictionary do
4
+ it "should load so file" do
5
+ Rubydict::MixBox::Dictionary.should be_respond_to(:lookup)
6
+ end
7
+ end
8
+
9
+ describe Rubydict::MixBox::Translator do
10
+ describe "#lookup" do
11
+ it "should return right response" do
12
+ # translator = Rubydict::MixBox::Translator.new "spec/DrEye5in1/DrEye5in1"
13
+ # expect{
14
+ # translator.lookup('hello').should_not be_nil
15
+ # }.to_not raise_error
16
+ end
17
+ end
18
+ end
19
+
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubydict
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - jason.li
8
+ - teng.C
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-05-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.6'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.6'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '2.6'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '2.6'
56
+ description: a simple wrapper for star dict....
57
+ email:
58
+ - congteng45@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - clib/lookup.c
69
+ - clib/lookup.h
70
+ - clib/makefile
71
+ - lib/rubydict.rb
72
+ - lib/rubydict/.mixbox.rb.swp
73
+ - lib/rubydict/mixbox.rb
74
+ - lib/rubydict/mixbox/translator.rb
75
+ - lib/rubydict/version.rb
76
+ - rubydict.gemspec
77
+ - spec/rubydict_spec.rb
78
+ homepage: http://qqdd.me
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.2.2
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: a simple wrapper for star dict
102
+ test_files:
103
+ - spec/rubydict_spec.rb