summaryrefslogtreecommitdiffstats
path: root/lib/efl/eet.rb
blob: 76a83cfdf3579618ea8f05f2388cf62b0feeb5d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#! /usr/bin/env ruby
# -*- coding: UTF-8 -*-
#
require 'efl/ffi/eet'
#
module Efl
    module Eet
        #
        class << self
            #
            def open path, mode=FILE_MODE_READ, &blk
                if blk
                    f = Efl::FFI.eet_open path, mode
                    raise Exception.new "Unable to open file #{path}" if f.nil?
                    yield EetFile.new f
                    Efl::FFI.eet_close f
                else
                    f = Efl::FFI.eet_open path, mode
                    return EetFile.new f unless f.nil?
                end
            end
        end
        #
        class EetFile
            #
            def initialize ptr
                @ptr=ptr
            end
            private :initialize
            #
            def close
                Efl::FFI.eet_close @ptr
                @ptr=nil
            end
            #
            def mode_get
                Efl::FFI.eet_mode_get @ptr
            end
            #
            def write key, data, compress=false
                Efl::FFI.eet_write @ptr, key, ::FFI::MemoryPointer.from_string(data), data.bytesize, ( compress ? 1 : 0 )
            end
            #
            def read key
                ptr = ::FFI::MemoryPointer.new(:int)
                data = Efl::FFI.eet_read @ptr, key, ptr
                s = ptr.read_int
                ptr.free
                return nil if s==0
                ( data.null? ? nil : data.read_string[0..s-1] )
            end
            #
        end
        #
    end
end
#
# EOF