Class: CFPropertyList::List
- Inherits:
-
Object
- Object
- CFPropertyList::List
- Defined in:
- lib/rbCFPropertyList.rb
Overview
Class representing a CFPropertyList. Instanciate with #new
Constant Summary collapse
- FORMAT_BINARY =
Format constant for binary format
1
- FORMAT_XML =
Format constant for XML format
2
- FORMAT_AUTO =
Format constant for automatic format recognizing
0
- @@parsers =
[Binary,XML]
Instance Attribute Summary collapse
-
#filename ⇒ Object
Path of PropertyList.
-
#format ⇒ Object
Path of PropertyList.
-
#value ⇒ Object
the root value in the plist file.
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ List
constructor
initialize a new CFPropertyList, arguments are:.
-
#load(file = nil, format = nil) ⇒ Object
- Read a plist file file = nil
-
The filename of the file to read.
-
#load_binary(filename = nil) ⇒ Object
- read a binary plist file filename = nil
-
The filename to read from; if nil, read from the file defined by instance variable
filename
.
-
#load_binary_str(str = nil) ⇒ Object
- load a plist from a binary string str
-
The string containing the plist.
-
#load_str(str = nil, format = nil) ⇒ Object
- load a plist from a string str = nil
- The string containing the plist format = nil
-
The format of the plist.
-
#load_xml(filename = nil) ⇒ Object
- Load an XML PropertyList filename = nil
-
The filename to read from; if nil, read from the file defined by instance variable
filename
.
-
#load_xml_str(str = nil) ⇒ Object
- load a plist from a XML string str
-
The string containing the plist.
-
#save(file = nil, format = nil, opts = {}) ⇒ Object
- Serialize CFPropertyList object to specified format and write it to file file = nil
-
The filename of the file to write to.
-
#to_str(format = List::FORMAT_BINARY, opts = {}) ⇒ Object
- convert plist to string format = List::FORMAT_BINARY
- The format to save the plist opts={}
-
Pass parser options.
Constructor Details
#initialize(opts = {}) ⇒ List
initialize a new CFPropertyList, arguments are:
- :file
-
Parse a file
- :format
-
Format is one of FORMAT_BINARY or FORMAT_XML. Defaults to FORMAT_AUTO
- :data
-
Parse a string
All arguments are optional
213 214 215 216 217 218 219 220 |
# File 'lib/rbCFPropertyList.rb', line 213 def initialize(opts={}) @filename = opts[:file] @format = opts[:format] || FORMAT_AUTO @data = opts[:data] load(@filename) unless @filename.nil? load_str(@data) unless @data.nil? end |
Instance Attribute Details
#filename ⇒ Object
Path of PropertyList
200 201 202 |
# File 'lib/rbCFPropertyList.rb', line 200 def filename @filename end |
#format ⇒ Object
Path of PropertyList
202 203 204 |
# File 'lib/rbCFPropertyList.rb', line 202 def format @format end |
#value ⇒ Object
the root value in the plist file
204 205 206 |
# File 'lib/rbCFPropertyList.rb', line 204 def value @value end |
Instance Method Details
#load(file = nil, format = nil) ⇒ Object
Read a plist file
- file = nil
-
The filename of the file to read. If nil, use
filename
instance variable - format = nil
-
The format of the plist file. Auto-detect if nil
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/rbCFPropertyList.rb', line 278 def load(file=nil,format=nil) file = @filename if file.nil? format = @format if format.nil? @value = {} raise IOError.new("File #{file} not readable!") unless File.readable? file case format when List::FORMAT_BINARY, List::FORMAT_XML then prsr = @@parsers[format-1].new @value = prsr.load({:file => file}) when List::FORMAT_AUTO then # what we now do is ugly, but neccessary to recognize the file format magic_number = IO.read(file,8) filetype = magic_number[0..5] version = magic_number[6..7] prsr = nil if filetype == "bplist" then raise CFFormatError.new("Wong file version #{version}") unless version == "00" prsr = Binary.new else prsr = XML.new end @value = prsr.load({:file => file}) end end |
#load_binary(filename = nil) ⇒ Object
read a binary plist file
- filename = nil
-
The filename to read from; if nil, read from the file defined by instance variable
filename
230 231 232 |
# File 'lib/rbCFPropertyList.rb', line 230 def load_binary(filename=nil) load(filename,List::FORMAT_BINARY) end |
#load_binary_str(str = nil) ⇒ Object
load a plist from a binary string
- str
-
The string containing the plist
242 243 244 |
# File 'lib/rbCFPropertyList.rb', line 242 def load_binary_str(str=nil) load_str(str,List::FORMAT_BINARY) end |
#load_str(str = nil, format = nil) ⇒ Object
load a plist from a string
- str = nil
-
The string containing the plist
- format = nil
-
The format of the plist
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/rbCFPropertyList.rb', line 249 def load_str(str=nil,format=nil) str = @data if str.nil? format = @format if format.nil? @value = {} case format when List::FORMAT_BINARY, List::FORMAT_XML then prsr = @@parsers[format-1].new @value = prsr.load({:data => str}) when List::FORMAT_AUTO then # what we now do is ugly, but neccessary to recognize the file format filetype = str[0..5] version = str[6..7] prsr = nil if filetype == "bplist" then raise CFFormatError.new("Wong file version #{version}") unless version == "00" prsr = Binary.new else prsr = XML.new end @value = prsr.load({:data => str}) end end |
#load_xml(filename = nil) ⇒ Object
Load an XML PropertyList
- filename = nil
-
The filename to read from; if nil, read from the file defined by instance variable
filename
224 225 226 |
# File 'lib/rbCFPropertyList.rb', line 224 def load_xml(filename=nil) load(filename,List::FORMAT_XML) end |
#load_xml_str(str = nil) ⇒ Object
load a plist from a XML string
- str
-
The string containing the plist
236 237 238 |
# File 'lib/rbCFPropertyList.rb', line 236 def load_xml_str(str=nil) load_str(str,List::FORMAT_XML) end |
#save(file = nil, format = nil, opts = {}) ⇒ Object
Serialize CFPropertyList object to specified format and write it to file
- file = nil
-
The filename of the file to write to. Uses
filename
instance variable if nil - format = nil
-
The format to save in. Uses
format
instance variable if nil
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
# File 'lib/rbCFPropertyList.rb', line 310 def save(file=nil,format=nil,opts={}) format = @format if format.nil? file = @filename if file.nil? raise CFFormatError.new("Format #{format} not supported, use List::FORMAT_BINARY or List::FORMAT_XML") if format != FORMAT_BINARY && format != FORMAT_XML if(!File.exists?(file)) then raise IOError.new("File #{file} not writable!") unless File.writable?(File.dirname(file)) elsif(!File.writable?(file)) then raise IOError.new("File #{file} not writable!") end opts[:root] = @value prsr = @@parsers[format-1].new content = prsr.to_str(opts) File.open(file, 'wb') { |fd| fd.write content } end |
#to_str(format = List::FORMAT_BINARY, opts = {}) ⇒ Object
convert plist to string
- format = List::FORMAT_BINARY
-
The format to save the plist
- opts={}
-
Pass parser options
335 336 337 338 339 |
# File 'lib/rbCFPropertyList.rb', line 335 def to_str(format=List::FORMAT_BINARY,opts={}) prsr = @@parsers[format-1].new opts[:root] = @value return prsr.to_str(opts) end |