ruby-filemagic 0.2.2 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  == VERSION
4
4
 
5
- This documentation refers to filemagic version 0.2.2
5
+ This documentation refers to filemagic version 0.3.0
6
6
 
7
7
 
8
8
  == DESCRIPTION
@@ -59,6 +59,12 @@ Rubyforge project:: <http://rubyforge.org/projects/ruby-filemagic>
59
59
  * Jens Wille <mailto:jens.wille@uni-koeln.de>
60
60
 
61
61
 
62
+ == CREDITS
63
+
64
+ * Martin Carpenter <mailto:mcarpenter@free.fr> for Ruby 1.9.2 compatibility
65
+ and other improvements.
66
+
67
+
62
68
  == COPYING
63
69
 
64
70
  The filemagic extension library is copywrited free software by Travis Whitton
data/Rakefile CHANGED
@@ -1,4 +1,4 @@
1
- require %q{lib/filemagic/version}
1
+ require %q{./lib/filemagic/version}
2
2
 
3
3
  begin
4
4
  require 'hen'
@@ -11,16 +11,16 @@ begin
11
11
  },
12
12
 
13
13
  :gem => {
14
- :version => FileMagic::VERSION,
15
- :summary => 'Ruby bindings to the magic(4) library',
16
- :authors => ['Travis Whitton', 'Jens Wille'],
17
- :email => ['tinymountain@gmail.com', 'jens.wille@uni-koeln.de'],
18
- :homepage => 'http://ruby-filemagic.rubyforge.org/',
19
- :files => FileList['lib/**/*.rb', 'ext/**/*.c', 'test/*'].to_a,
20
- :extensions => FileList['ext/**/extconf.rb'].to_a,
21
- :extra_files => FileList['[A-Z]*', 'info/*'].to_a
14
+ :version => FileMagic::VERSION,
15
+ :summary => 'Ruby bindings to the magic(4) library',
16
+ :authors => ['Travis Whitton', 'Jens Wille'],
17
+ :email => ['tinymountain@gmail.com', 'jens.wille@uni-koeln.de'],
18
+ :homepage => 'http://ruby-filemagic.rubyforge.org/',
19
+ :files => FileList['lib/**/*.rb', 'ext/**/*.c', 'test/*'].to_a,
20
+ :extensions => FileList['ext/**/extconf.rb'].to_a,
21
+ :extra_files => FileList['[A-Z]*', 'info/*'].to_a
22
22
  }
23
23
  }}
24
- rescue LoadError
25
- abort "Please install the 'hen' gem first."
24
+ rescue LoadError => err
25
+ warn "Please install the `hen' gem first. (#{err})"
26
26
  end
@@ -33,6 +33,7 @@ static void rb_magic_free(magic_t cookie) {
33
33
  }
34
34
 
35
35
  static VALUE rb_magic_init(VALUE self, VALUE flags) {
36
+ return Qnil;
36
37
  }
37
38
 
38
39
  /* Frees resources allocated */
@@ -70,7 +71,7 @@ static VALUE rb_magic_file(VALUE self, VALUE file) {
70
71
  const char *m;
71
72
  magic_t cookie;
72
73
 
73
- m = STR2CSTR(file);
74
+ m = StringValuePtr(file);
74
75
  GetMagicCookie(self, cookie);
75
76
  if ((m = magic_file(cookie, m)) == NULL)
76
77
  rb_raise(rb_FileMagicError, magic_error(cookie));
@@ -80,11 +81,11 @@ static VALUE rb_magic_file(VALUE self, VALUE file) {
80
81
 
81
82
  /* Return a string describing the string buffer */
82
83
  static VALUE rb_magic_buffer(VALUE self, VALUE buffer) {
83
- int i = RSTRING(buffer)->len;
84
+ int i = RSTRING_LEN(buffer);
84
85
  const char *m;
85
86
  magic_t cookie;
86
87
 
87
- m = STR2CSTR(buffer);
88
+ m = StringValuePtr(buffer);
88
89
  GetMagicCookie(self, cookie);
89
90
  if ((m = magic_buffer(cookie, m, i)) == NULL)
90
91
  rb_raise(rb_FileMagicError, magic_error(cookie));
@@ -110,7 +111,7 @@ static VALUE rb_magic_check(VALUE self, VALUE file) {
110
111
  magic_t cookie;
111
112
 
112
113
  GetMagicCookie(self, cookie);
113
- m = STR2CSTR(file);
114
+ m = StringValuePtr(file);
114
115
  retval = magic_check(cookie, m);
115
116
 
116
117
  return INT2FIX(retval);
@@ -123,7 +124,7 @@ static VALUE rb_magic_compile(VALUE self, VALUE file) {
123
124
  magic_t cookie;
124
125
 
125
126
  GetMagicCookie(self, cookie);
126
- m = STR2CSTR(file);
127
+ m = StringValuePtr(file);
127
128
  retval = magic_compile(cookie, m);
128
129
 
129
130
  return INT2FIX(retval);
@@ -2,8 +2,8 @@ require 'filemagic.so'
2
2
 
3
3
  class FileMagic
4
4
 
5
- # Map abbreviated flag names to their values (:name => Constant).
6
- MAGIC_FLAGS = [
5
+ # Map flag names to their values (:name => Integer).
6
+ FLAGS_BY_SYM = [
7
7
  :none, # No flags
8
8
  :debug, # Turn on debugging
9
9
  :symlink, # Follow symlinks
@@ -31,7 +31,9 @@ class FileMagic
31
31
  flags.update(flag => const_defined?(const) && const_get(const))
32
32
  }
33
33
 
34
- attr_reader :flags
34
+ # Map flag values to their names (Integer => :name).
35
+ FLAGS_BY_INT = FLAGS_BY_SYM.invert.update(0 => :none)
36
+
35
37
  attr_writer :simplified
36
38
 
37
39
  @fm = Hash.new { |fm, flags|
@@ -93,21 +95,22 @@ class FileMagic
93
95
 
94
96
  # Build the actual flags from the named flags passed as arguments.
95
97
  def build_flags(*flags)
96
- _flags = *flags
98
+ int_flags = *flags
97
99
 
98
- unless _flags.is_a?(Integer)
99
- _flags = MAGIC_NONE
100
+ unless int_flags.is_a?(Integer)
101
+ int_flags = MAGIC_NONE
100
102
 
101
103
  flags.flatten.each { |flag|
102
- if value = flag.is_a?(Integer) ? flag : MAGIC_FLAGS[flag.to_sym]
103
- _flags |= value
104
+ if value = flag.is_a?(Integer) ? flag : FLAGS_BY_SYM[flag.to_sym]
105
+ int_flags |= value
104
106
  else
105
- raise ArgumentError, "#{value.nil? ? 'no such flag' : 'flag not available'}: #{flag}"
107
+ err = value.nil? ? 'no such flag' : 'flag not available'
108
+ raise ArgumentError, "#{err}: #{flag}"
106
109
  end
107
110
  }
108
111
  end
109
112
 
110
- _flags
113
+ int_flags
111
114
  end
112
115
 
113
116
  end
@@ -116,7 +119,7 @@ class FileMagic
116
119
  def initialize(*flags)
117
120
  fm_initialize(*flags)
118
121
 
119
- @flags = *flags
122
+ @flags = flags
120
123
  @closed = false
121
124
  end
122
125
 
@@ -132,22 +135,39 @@ class FileMagic
132
135
  simplify_mime(fm_file(file), simplified)
133
136
  end
134
137
 
138
+ # Returns the flags as integer.
139
+ def int_flags
140
+ @flags.first
141
+ end
142
+
143
+ # Returns the flags as array of symbols.
144
+ def flags
145
+ int, flags, log2 = int_flags, [], Math.log(2)
146
+
147
+ while int > 0
148
+ flags << FLAGS_BY_INT[flag = 2 ** Math.log(int).div(log2)]
149
+ int -= flag
150
+ end
151
+
152
+ flags.reverse
153
+ end
154
+
135
155
  # Optionally cut off additional information after mime-type.
136
156
  def buffer(buffer, simplified = simplified?)
137
157
  simplify_mime(fm_buffer(buffer), simplified)
138
158
  end
139
159
 
140
160
  def setflags(*flags)
141
- previous_flags, @flags = @flags, self.class.build_flags(*flags)
142
- fm_setflags(@flags)
143
-
144
- previous_flags
161
+ @flags = [self.class.build_flags(*flags)]
162
+ fm_setflags(int_flags)
145
163
  end
164
+
146
165
  alias_method :flags=, :setflags
147
166
 
148
167
  def check(file = "\0")
149
168
  fm_check(file)
150
169
  end
170
+
151
171
  alias_method :valid?, :check
152
172
 
153
173
  def compile(file)
@@ -3,8 +3,8 @@ class FileMagic
3
3
  module Version
4
4
 
5
5
  MAJOR = 0
6
- MINOR = 2
7
- TINY = 2
6
+ MINOR = 3
7
+ TINY = 0
8
8
 
9
9
  class << self
10
10
 
@@ -1,6 +1,7 @@
1
1
  require 'test/unit'
2
2
 
3
- $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ top_dir = File.join(File.dirname(__FILE__), '..')
4
+ $:.unshift(File.join(top_dir, 'lib'), File.join(top_dir, 'ext'))
4
5
  require 'filemagic'
5
6
 
6
7
  class TestFileMagic < Test::Unit::TestCase
@@ -26,7 +27,7 @@ class TestFileMagic < Test::Unit::TestCase
26
27
  fm = FileMagic.new(FileMagic::MAGIC_SYMLINK | FileMagic::MAGIC_MIME)
27
28
 
28
29
  res = fm.file(path_to('pylink'))
29
- assert_equal('text/plain', res)
30
+ assert_equal('text/plain; charset=us-ascii', res)
30
31
 
31
32
  fm.close
32
33
  fm = FileMagic.new(FileMagic::MAGIC_COMPRESS)
@@ -70,17 +71,17 @@ class TestFileMagic < Test::Unit::TestCase
70
71
 
71
72
  def test_setflags
72
73
  fm = FileMagic.new(FileMagic::MAGIC_NONE)
73
- assert_equal(FileMagic::MAGIC_NONE, fm.flags)
74
+ assert_equal([], fm.flags)
74
75
  fm.setflags(FileMagic::MAGIC_SYMLINK)
75
- assert_equal(FileMagic::MAGIC_SYMLINK, fm.flags)
76
+ assert_equal([:symlink], fm.flags)
76
77
  fm.close
77
78
  end
78
79
 
79
80
  def test_abbr
80
81
  fm = FileMagic.new(:mime, :continue)
81
- assert_equal(FileMagic::MAGIC_MIME | FileMagic::MAGIC_CONTINUE, fm.flags)
82
+ assert_equal([:mime_type, :continue, :mime_encoding] , fm.flags)
82
83
  fm.setflags(:symlink)
83
- assert_equal(FileMagic::MAGIC_SYMLINK, fm.flags)
84
+ assert_equal([:symlink], fm.flags)
84
85
  fm.close
85
86
  end
86
87
 
@@ -95,7 +96,7 @@ class TestFileMagic < Test::Unit::TestCase
95
96
  def test_mahoro_mime_file
96
97
  fm = FileMagic.new
97
98
  fm.flags = FileMagic::MAGIC_MIME
98
- assert_equal('text/x-c', fm.file(path_to('mahoro.c')))
99
+ assert_equal('text/x-c; charset=us-ascii', fm.file(path_to('mahoro.c')))
99
100
  end
100
101
 
101
102
  def test_mahoro_buffer
@@ -107,7 +108,7 @@ class TestFileMagic < Test::Unit::TestCase
107
108
  def test_mahoro_mime_buffer
108
109
  fm = FileMagic.new
109
110
  fm.flags = FileMagic::MAGIC_MIME
110
- assert_equal('text/x-c', fm.buffer(File.read(path_to('mahoro.c'))))
111
+ assert_equal('text/x-c; charset=us-ascii', fm.buffer(File.read(path_to('mahoro.c'))))
111
112
  end
112
113
 
113
114
  def test_mahoro_valid
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-filemagic
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 19
4
5
  prerelease: false
5
6
  segments:
6
7
  - 0
7
- - 2
8
- - 2
9
- version: 0.2.2
8
+ - 3
9
+ - 0
10
+ version: 0.3.0
10
11
  platform: ruby
11
12
  authors:
12
13
  - Travis Whitton
@@ -15,7 +16,7 @@ autorequire:
15
16
  bindir: bin
16
17
  cert_chain: []
17
18
 
18
- date: 2010-03-02 00:00:00 +01:00
19
+ date: 2010-09-10 00:00:00 +02:00
19
20
  default_executable:
20
21
  dependencies: []
21
22
 
@@ -48,6 +49,7 @@ files:
48
49
  - TODO
49
50
  - info/filemagic.rd
50
51
  - info/example.rb
52
+ - ext/extconf.rb
51
53
  has_rdoc: true
52
54
  homepage: http://ruby-filemagic.rubyforge.org/
53
55
  licenses: []
@@ -60,29 +62,33 @@ rdoc_options:
60
62
  - README
61
63
  - --line-numbers
62
64
  - --inline-source
63
- - --all
64
65
  - --charset
65
66
  - UTF-8
67
+ - --all
66
68
  require_paths:
67
69
  - lib
68
70
  required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
69
72
  requirements:
70
73
  - - ">="
71
74
  - !ruby/object:Gem::Version
75
+ hash: 3
72
76
  segments:
73
77
  - 0
74
78
  version: "0"
75
79
  required_rubygems_version: !ruby/object:Gem::Requirement
80
+ none: false
76
81
  requirements:
77
82
  - - ">="
78
83
  - !ruby/object:Gem::Version
84
+ hash: 3
79
85
  segments:
80
86
  - 0
81
87
  version: "0"
82
88
  requirements: []
83
89
 
84
90
  rubyforge_project: ruby-filemagic
85
- rubygems_version: 1.3.6
91
+ rubygems_version: 1.3.7
86
92
  signing_key:
87
93
  specification_version: 3
88
94
  summary: Ruby bindings to the magic(4) library