Class: Resedit::Text

Inherits:
Object
  • Object
show all
Defined in:
lib/resedit/text/text.rb

Constant Summary collapse

TYPE_TXT =
'txt'
TYPE_XML =
'xml'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(format = TYPE_TXT, encoding = nil) ⇒ Text

Returns a new instance of Text.



14
15
16
17
18
19
20
21
# File 'lib/resedit/text/text.rb', line 14

def initialize(format=TYPE_TXT,encoding=nil)
    @encoding = encoding
    format=TYPE_TXT if !format
    setFormat(format)
    @escaper = StdEscaper.new()
    @lines = []
    @meta={}
end

Instance Attribute Details

#encodingObject

Returns the value of attribute encoding.



10
11
12
# File 'lib/resedit/text/text.rb', line 10

def encoding
  @encoding
end

#escaperObject

Returns the value of attribute escaper.



10
11
12
# File 'lib/resedit/text/text.rb', line 10

def escaper
  @escaper
end

#formatObject (readonly)

Returns the value of attribute format.



9
10
11
# File 'lib/resedit/text/text.rb', line 9

def format
  @format
end

#formatterObject

Returns the value of attribute formatter.



10
11
12
# File 'lib/resedit/text/text.rb', line 10

def formatter
  @formatter
end

#linesObject (readonly)

Returns the value of attribute lines.



9
10
11
# File 'lib/resedit/text/text.rb', line 9

def lines
  @lines
end

#metaObject

Returns the value of attribute meta.



10
11
12
# File 'lib/resedit/text/text.rb', line 10

def meta
  @meta
end

#userDataObject

Returns the value of attribute userData.



10
11
12
# File 'lib/resedit/text/text.rb', line 10

def userData
  @userData
end

Instance Method Details

#addLine(line, meta = nil) ⇒ Object



35
36
37
38
39
40
# File 'lib/resedit/text/text.rb', line 35

def addLine(line, meta = nil)
    line.encode!("utf-8",@encoding) if @encoding
    line.force_encoding('utf-8')
    @lines += [line]
    @meta[@lines.length-1]=meta
end

#getLine(id) ⇒ Object



42
43
44
45
46
# File 'lib/resedit/text/text.rb', line 42

def getLine(id)
    line = @lines[id]
    line.encode!(@encoding) if @encoding
    return line
end

#load(filename, count = nil) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/resedit/text/text.rb', line 59

def load(filename, count=nil)
    @lines = @formatter.loadLines(filename)
    raise "Wrong lines count: #{filename} #{count} #{@lines.length}" if count && count!=@lines.length
    if @escaper
        nl=[]
        @lines.each {|l|
            nl += [@escaper.unescape(l)]
        }
        @lines = nl
    end
end

#save(filename) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/resedit/text/text.rb', line 48

def save(filename)
    nl=@lines
    if @escaper
        nl=[]
        @lines.each{|l|
            nl += [@escaper.escape(l)]
        }
    end
    @formatter.saveLines(filename, nl, @meta)
end

#setFormat(format) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/resedit/text/text.rb', line 23

def setFormat(format)
    if format == TYPE_TXT
        @format = format
        @formatter=FormatTxt.new(@encoding)
    elsif format == TYPE_XML
        @format = format
        @formatter=FormatXml.new(@encoding)
    else
        raise "Unsupported format " + format
    end
end