corefoundation 0.1.4 → 0.3.13

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.
@@ -0,0 +1,24 @@
1
+ require "singleton"
2
+
3
+ module CF
4
+ module Register
5
+ def self.included base
6
+ base.extend self
7
+ end
8
+
9
+ def register_type(type_name)
10
+ CF.attach_function "#{type_name}GetTypeID", [], :cftypeid
11
+ type_map[CF.send("#{type_name}GetTypeID")] = self
12
+ end
13
+
14
+ private
15
+
16
+ def klass_from_cf_type(cftyperef)
17
+ klass = type_map[CF.CFGetTypeID(cftyperef)]
18
+ raise TypeError, "No class registered for cf type #{cftyperef.inspect}" unless klass
19
+
20
+ klass
21
+ end
22
+
23
+ end
24
+ end
@@ -20,16 +20,15 @@ module CF
20
20
  # The cfstring encoding for UTF8
21
21
  UTF8 = 0x08000100 #From cfstring.h
22
22
 
23
-
24
23
  # Creates a string from a ruby string
25
24
  # The string must be convertable to UTF-8
26
25
  #
27
26
  # @param [String] s
28
27
  # @return [CF::String]
29
28
  def self.from_string(s)
30
- s_utf = s.encode('UTF-8')
29
+ s_utf = s.encode("UTF-8")
31
30
  raw = CF.CFStringCreateWithBytes(nil, s_utf, s_utf.bytesize, UTF8, 0)
32
- raw.null? ? nil : new(raw).release_on_gc
31
+ raw.null? ? nil : new(raw)
33
32
  end
34
33
 
35
34
  # Returns the length, in unicode characters of the string
@@ -55,23 +54,15 @@ module CF
55
54
  range[:location] = 0
56
55
  range[:length] = length
57
56
  buffer = FFI::MemoryPointer.new(:char, max_size)
58
-
59
57
  cfindex = CF.find_type(:cfindex)
60
58
  bytes_used_buffer = FFI::MemoryPointer.new(cfindex)
61
59
 
62
60
  CF.CFStringGetBytes(self, range, UTF8, 0, 0, buffer, max_size, bytes_used_buffer)
61
+ len = bytes_used_buffer.send(cfindex == CF.find_type(:long_long) ? :read_long_long : :read_long)
63
62
 
64
- bytes_used = if cfindex == CF.find_type(:long_long)
65
- bytes_used_buffer.read_long_long
66
- else
67
- bytes_used_buffer.read_long
68
- end
69
-
70
- buffer.read_string(bytes_used).force_encoding(Encoding::UTF_8)
63
+ buffer.read_string(len).force_encoding(Encoding::UTF_8)
71
64
  end
72
-
73
- alias_method :to_ruby, :to_s
74
-
65
+ alias to_ruby to_s
75
66
  end
76
67
 
77
- end
68
+ end
@@ -1,4 +1,4 @@
1
1
  module CF
2
2
  # The current version string
3
- VERSION = "0.1.4"
3
+ VERSION = "0.3.13"
4
4
  end
@@ -1,11 +1,38 @@
1
- require 'ffi'
2
- require 'corefoundation/base'
3
- require 'corefoundation/null'
4
- require 'corefoundation/string'
5
- require 'corefoundation/array'
6
- require 'corefoundation/boolean'
7
- require 'corefoundation/data'
8
- require 'corefoundation/dictionary'
9
- require 'corefoundation/number'
10
- require 'corefoundation/date'
11
- require 'corefoundation/extensions'
1
+ require "ffi" unless defined?(FFI)
2
+
3
+ module CF
4
+ extend FFI::Library
5
+ ffi_lib '/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation'
6
+
7
+ if FFI::Platform::ARCH == 'x86_64'
8
+ typedef :long_long, :cfindex
9
+ typedef :long_long, :cfcomparisonresult
10
+ typedef :ulong_long, :cfoptionflags
11
+ typedef :ulong_long, :cftypeid
12
+ typedef :ulong_long, :cfhashcode
13
+ else
14
+ typedef :long, :cfindex
15
+ typedef :long, :cfcomparisonresult
16
+ typedef :ulong, :cfoptionflags
17
+ typedef :ulong, :cftypeid
18
+ typedef :ulong, :cfhashcode
19
+ end
20
+
21
+ typedef :pointer, :cftyperef
22
+ end
23
+
24
+ require_relative 'corefoundation/refinements'
25
+ require_relative 'corefoundation/memory'
26
+ require_relative 'corefoundation/register'
27
+ require_relative 'corefoundation/base'
28
+ require_relative 'corefoundation/null'
29
+ require_relative 'corefoundation/range'
30
+ require_relative 'corefoundation/string'
31
+ require_relative 'corefoundation/array'
32
+ require_relative 'corefoundation/boolean'
33
+ require_relative 'corefoundation/data'
34
+ require_relative 'corefoundation/dictionary'
35
+ require_relative 'corefoundation/number'
36
+ require_relative 'corefoundation/date'
37
+ require_relative 'corefoundation/exceptions'
38
+ require_relative 'corefoundation/preferences'
metadata CHANGED
@@ -1,150 +1,133 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: corefoundation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
5
- prerelease:
4
+ version: 0.3.13
6
5
  platform: ruby
7
6
  authors:
8
7
  - Frederick Cheung
8
+ - Chef Software, Inc.
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-16 00:00:00.000000000 Z
12
+ date: 2022-02-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ffi
16
16
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
17
  requirements:
19
- - - ! '>='
18
+ - - ">="
20
19
  - !ruby/object:Gem::Version
21
- version: '0'
20
+ version: 1.15.0
22
21
  type: :runtime
23
22
  prerelease: false
24
23
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
24
  requirements:
27
- - - ! '>='
25
+ - - ">="
28
26
  - !ruby/object:Gem::Version
29
- version: '0'
27
+ version: 1.15.0
30
28
  - !ruby/object:Gem::Dependency
31
29
  name: rspec
32
30
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
31
  requirements:
35
- - - ~>
32
+ - - ">="
36
33
  - !ruby/object:Gem::Version
37
- version: '2.10'
34
+ version: '3.0'
38
35
  type: :development
39
36
  prerelease: false
40
37
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
38
  requirements:
43
- - - ~>
39
+ - - ">="
44
40
  - !ruby/object:Gem::Version
45
- version: '2.10'
41
+ version: '3.0'
46
42
  - !ruby/object:Gem::Dependency
47
43
  name: rake
48
44
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
45
  requirements:
51
- - - ! '>='
46
+ - - ">="
52
47
  - !ruby/object:Gem::Version
53
48
  version: '0'
54
49
  type: :development
55
50
  prerelease: false
56
51
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
52
  requirements:
59
- - - ! '>='
53
+ - - ">="
60
54
  - !ruby/object:Gem::Version
61
55
  version: '0'
62
56
  - !ruby/object:Gem::Dependency
63
- name: yard
57
+ name: chefstyle
64
58
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
59
  requirements:
67
- - - ! '>='
60
+ - - '='
68
61
  - !ruby/object:Gem::Version
69
- version: '0'
62
+ version: 2.2.1
70
63
  type: :development
71
64
  prerelease: false
72
65
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
66
  requirements:
75
- - - ! '>='
67
+ - - '='
76
68
  - !ruby/object:Gem::Version
77
- version: '0'
69
+ version: 2.2.1
78
70
  - !ruby/object:Gem::Dependency
79
- name: redcarpet
71
+ name: yard
80
72
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
73
  requirements:
83
- - - ! '>='
74
+ - - ">="
84
75
  - !ruby/object:Gem::Version
85
76
  version: '0'
86
77
  type: :development
87
78
  prerelease: false
88
79
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
80
  requirements:
91
- - - ! '>='
81
+ - - ">="
92
82
  - !ruby/object:Gem::Version
93
83
  version: '0'
94
- description: ! 'FFI based Ruby wrappers for Core Foundation '
95
- email: frederick.cheung@gmail.com
84
+ description: Ruby wrapper for macOS Core Foundation framework
85
+ email:
86
+ - frederick.cheung@gmail.com
87
+ - oss@chef.io
96
88
  executables: []
97
89
  extensions: []
98
90
  extra_rdoc_files: []
99
91
  files:
92
+ - LICENSE
93
+ - lib/corefoundation.rb
100
94
  - lib/corefoundation/array.rb
101
95
  - lib/corefoundation/base.rb
102
96
  - lib/corefoundation/boolean.rb
103
97
  - lib/corefoundation/data.rb
104
98
  - lib/corefoundation/date.rb
105
99
  - lib/corefoundation/dictionary.rb
106
- - lib/corefoundation/extensions.rb
100
+ - lib/corefoundation/exceptions.rb
101
+ - lib/corefoundation/memory.rb
107
102
  - lib/corefoundation/null.rb
108
103
  - lib/corefoundation/number.rb
104
+ - lib/corefoundation/preferences.rb
105
+ - lib/corefoundation/range.rb
106
+ - lib/corefoundation/refinements.rb
107
+ - lib/corefoundation/register.rb
109
108
  - lib/corefoundation/string.rb
110
109
  - lib/corefoundation/version.rb
111
- - lib/corefoundation.rb
112
- - spec/array_spec.rb
113
- - spec/boolean_spec.rb
114
- - spec/data_spec.rb
115
- - spec/date_spec.rb
116
- - spec/dictionary_spec.rb
117
- - spec/extensions_spec.rb
118
- - spec/null_spec.rb
119
- - spec/number_spec.rb
120
- - spec/spec_helper.rb
121
- - spec/string_spec.rb
122
- - README.md
123
- - LICENSE
124
- homepage: http://github.com/fcheung/corefoundation
110
+ homepage: http://github.com/chef/corefoundation
125
111
  licenses:
126
112
  - MIT
113
+ metadata: {}
127
114
  post_install_message:
128
115
  rdoc_options: []
129
116
  require_paths:
130
117
  - lib
131
118
  required_ruby_version: !ruby/object:Gem::Requirement
132
- none: false
133
119
  requirements:
134
- - - ! '>='
120
+ - - ">="
135
121
  - !ruby/object:Gem::Version
136
- version: 1.9.2
122
+ version: '2.6'
137
123
  required_rubygems_version: !ruby/object:Gem::Requirement
138
- none: false
139
124
  requirements:
140
- - - ! '>='
125
+ - - ">="
141
126
  - !ruby/object:Gem::Version
142
127
  version: '0'
143
128
  requirements: []
144
- rubyforge_project:
145
- rubygems_version: 1.8.24
129
+ rubygems_version: 3.1.4
146
130
  signing_key:
147
- specification_version: 3
148
- summary: Ruby wrapper for OS X's corefoundation
131
+ specification_version: 4
132
+ summary: Ruby wrapper for macOS Core Foundation framework
149
133
  test_files: []
150
- has_rdoc:
data/README.md DELETED
@@ -1,39 +0,0 @@
1
-
2
- CoreFoundation
3
- ==============
4
-
5
- FFI based wrappers for a subset of core foundation: various bits of CFString, CFData, CFArray, CFDictionary are available. Not that useful on its own but a useful building block for writing ffi wrappers of other OS X libraries.
6
-
7
- Although the CF collection classes can store arbitrary pointer sized values this wrapper only supports storing CFTypes.
8
-
9
- The CF namespace has the raw FFI generated method calls but it's usually easier to use the wrapper classes: `CF::String`, `CF::Date`, `CF::Array`, `CF::Dictionary`, `CF::Boolean` which try to present a rubyish view of the world (for example `CF::Array` implements `Enumerable`)
10
-
11
- These implement methods for creating new instances from ruby objects (eg `CF::String.from_string("hello world")`) but you can also pass build them from an `FFI::Pointer`).
12
-
13
- Converting
14
- ===========
15
-
16
- `CF::Base` objects has a `to_ruby` that creates a ruby object of the most approprite type (`String` for `CF::String`, `Time` for `CF::Date`, `Integer` or `Float` for `CF::Number` etc). The collection classes call `to_ruby` on their contents too.
17
-
18
- In addition to the methods on the wrapper classes themselves, the ruby classes are extended with a `to_cf` method. Because CoreFoundation strings aren't arbitrary collections of bytes, `String#to_cf` will return a `CF::Data` if the string has the ASCII-8BIT encoding and a `CF::String` if not.
19
-
20
- If you have an `FFI::Pointer` or a raw address then you can create a wrapper by passing it to `new`, for example `CF::String.new(some_pointer)`. This does *not* check that the pointer is actually a `CFString`. You can use `CF::Base.typecast` to construct an instance of the appropriate subclass, for example `CF::Base.typecast(some_pointer)` would return a `CF::String` if `some_pointer` was in fact a `CFStringRef`.
21
-
22
- Memory Management
23
- =================
24
-
25
- The convenience methods for creating CF objects will release the cf object when they are garbage collected. Methods on the convenience classes will usually retain the result and mark it for releasing when they are garbage collected (for example `CF::Dictionary#[]` retains the returned value). You don't need to do any extra memory management on these.
26
-
27
- If you pass an `FFI::Pointer` to `new` or `typecast` no assumptions are made for you. You should call `retain` or `release` to manage it manually. As an alternative to calling `release` manually, the `release_on_gc` method adds a finalizer to the wrapper that will call `CFRelease` on the Core Foundation object when the wrapper is garbage collected. You will almost certainly crash your ruby interpreter if you overrelease an object and you will leak memory if you overretain one.
28
-
29
- If you use the raw api (eg `CF.CFArrayCreate`) then you're on your own.
30
-
31
-
32
- Compatibility
33
- =============
34
- Requires ruby 1.9 due to use of encoding related methods. Should work in MRI and jruby. Not compatible with rubinius due to rubinius' ffi implemenation not supporting certain features.
35
-
36
- License
37
- =======
38
-
39
- Released under the MIT license. See LICENSE
@@ -1,81 +0,0 @@
1
- # Rubyinteger
2
- class Integer
3
- # Converts the Integer to a {CF::Number} using {CF::Number.from_i}
4
- # @return [CF::Number]
5
- def to_cf
6
- CF::Number.from_i(self)
7
- end
8
- end
9
-
10
- # Ruby float class
11
- class Float
12
- # Converts the Float to a {CF::Number} using {CF::Number.from_f}
13
- # @return [CF::Number]
14
- def to_cf
15
- CF::Number.from_f(self)
16
- end
17
- end
18
-
19
- # Ruby array class
20
- class Array
21
- # Converts the Array to an immutable {CF::Array} by calling `to_cf` on each element it contains
22
- # @return [CF::Number]
23
- def to_cf
24
- CF::Array.immutable(collect(&:to_cf))
25
- end
26
- end
27
-
28
- # Ruby true class
29
- class TrueClass
30
- # Returns a CF::Boolean object representing true
31
- # @return [CF::Boolean]
32
- def to_cf
33
- CF::Boolean::TRUE
34
- end
35
- end
36
-
37
- # Ruby false class
38
- class FalseClass
39
- # Returns a CF::Boolean object representing false
40
- # @return [CF::Boolean]
41
- def to_cf
42
- CF::Boolean::FALSE
43
- end
44
- end
45
-
46
- # Ruby String class
47
- class String
48
- # Returns a {CF::String} or {CF::Data} representing the string.
49
- # If the string has the encoding ASCII_8BIt a {CF::Data} is returned, if not a {CF::String} is returned
50
- #
51
- # @return [CF::String, CF::Data]
52
- def to_cf
53
- if encoding == Encoding::ASCII_8BIT
54
- CF::Data.from_string self
55
- else
56
- CF::String.from_string self
57
- end
58
- end
59
- end
60
-
61
- # Ruby Time class
62
- class Time
63
- # Returns a {CF::Date} representing the time.
64
- # @return [CF::Date]
65
- def to_cf
66
- CF::Date.from_time(self)
67
- end
68
- end
69
-
70
- # Ruby Hash class
71
- class Hash
72
- # Converts the Hash to an mutable {CF::Dictionary} by calling `to_cf` on each key and value it contains
73
- # @return [CF::Dictionary]
74
- def to_cf
75
- CF::Dictionary.mutable.tap do |r|
76
- each do |k,v|
77
- r[k.to_cf] = v.to_cf
78
- end
79
- end
80
- end
81
- end
data/spec/array_spec.rb DELETED
@@ -1,92 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe CF::Array do
4
- describe 'mutable' do
5
- subject { CF::Array.mutable}
6
-
7
- it { should be_a(CF::Array)}
8
- it { should be_mutable}
9
-
10
- describe '[]=' do
11
- it 'should raise when trying to store a non cf value' do
12
- expect {subject[0] = 123}.to raise_error(TypeError)
13
- end
14
- end
15
-
16
- describe '<<' do
17
- it 'should raise when trying to store a non cf value' do
18
- expect {subject << 123}.to raise_error(TypeError)
19
- end
20
- end
21
-
22
- end
23
-
24
- describe 'immutable' do
25
- it 'should raise if all of the array elements are not cf values' do
26
- expect {CF::Array.immutable([CF::Boolean::TRUE, 1])}.to raise_error(TypeError)
27
- end
28
-
29
- it 'should return an immutable cfarray' do
30
- CF::Array.immutable([CF::Boolean::TRUE]).should be_a(CF::Array)
31
- end
32
-
33
- context 'with an immutable array' do
34
- subject { CF::Array.immutable([CF::Boolean::TRUE, CF::String.from_string('123')])}
35
-
36
- describe '[]=' do
37
- it 'should raise TypeError' do
38
- expect {subject[0] = CF::Boolean::TRUE}.to raise_error(TypeError)
39
- end
40
- end
41
-
42
- describe '<<' do
43
- it 'should raise TypeError' do
44
- expect {subject << CF::Boolean::TRUE}.to raise_error(TypeError)
45
- end
46
- end
47
- end
48
- end
49
-
50
- context "with an array" do
51
- subject { CF::Array.immutable([CF::Boolean::TRUE, CF::String.from_string('123')])}
52
-
53
- describe '[]' do
54
- it 'should return the typecast value at the index' do
55
- subject[1].should be_a(CF::String)
56
- subject[1].should == CF::String.from_string('123')
57
- end
58
- end
59
-
60
-
61
- describe 'length' do
62
- it 'should return the count of items in the dictionary' do
63
- subject.length.should == 2
64
- end
65
- end
66
-
67
- describe 'to_ruby' do
68
- it 'should return the result of calling to ruby on its contents' do
69
- subject.to_ruby.should == [true, '123']
70
- end
71
- end
72
-
73
- describe 'each' do
74
- it 'should iterate over each value' do
75
- values = []
76
- subject.each do |v|
77
- values << v
78
- end
79
- values[0].should == CF::Boolean::TRUE
80
- values[1].should == CF::String.from_string('123')
81
- end
82
- end
83
-
84
- it 'should be enumerable' do
85
- values = {}
86
- subject.each_with_index do |value, index|
87
- values[index] = value
88
- end
89
- values.should == {0 => CF::Boolean::TRUE, 1 => CF::String.from_string('123')}
90
- end
91
- end
92
- end
data/spec/boolean_spec.rb DELETED
@@ -1,24 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe CF do
4
-
5
-
6
- describe CF::Boolean do
7
- describe 'value' do
8
- it 'should return true for CF::Boolean::TRUE' do
9
- CF::Boolean::TRUE.value.should == true
10
- end
11
- it 'should return false for CF::Boolean::FALSE' do
12
- CF::Boolean::FALSE.value.should == false
13
- end
14
- end
15
-
16
- describe 'to_ruby' do
17
- it 'should behave like value' do
18
- CF::Boolean::FALSE.to_ruby.should == false
19
- end
20
- end
21
-
22
- end
23
-
24
- end
data/spec/data_spec.rb DELETED
@@ -1,26 +0,0 @@
1
- require 'spec_helper'
2
-
3
-
4
- describe CF::Data do
5
- subject {CF::Data.from_string('A CF string')}
6
- describe '#to_s' do
7
- it 'should return a binary ruby string' do
8
- ruby_string = subject.to_s
9
- ruby_string.should == 'A CF string'
10
- ruby_string.encoding.should == Encoding::ASCII_8BIT
11
- end
12
- end
13
-
14
- describe '#size' do
15
- it 'should return the size in bytes of the cfdata' do
16
- subject.size.should == 11
17
- end
18
- end
19
-
20
- describe 'to_ruby' do
21
- it 'should behave like to_s' do
22
- subject.to_ruby.should == 'A CF string'
23
- subject.to_ruby.encoding.should == Encoding::ASCII_8BIT
24
- end
25
- end
26
- end
data/spec/date_spec.rb DELETED
@@ -1,25 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe CF::Date do
4
- describe('from_time') do
5
- it 'should create a cf date from a time' do
6
- CF::Date.from_time(Time.now).should be_a(CF::Date)
7
- end
8
- end
9
-
10
- describe('to_time') do
11
- it 'should return a time' do
12
- t = CF::Date.from_time(Time.now).to_time
13
- t.should be_a(Time)
14
- t.should be_within(0.01).of(Time.now)
15
- end
16
- end
17
-
18
- describe 'to_ruby' do
19
- it 'should behave like to_time' do
20
- t = CF::Date.from_time(Time.now).to_ruby
21
- t.should be_a(Time)
22
- end
23
- end
24
- end
25
-