Class: WirisPlugin::IniFile
- Inherits:
-
Object
- Object
- WirisPlugin::IniFile
- Includes:
- Wiris
- Defined in:
- lib/com/wiris/util/sys/IniFile.rb
Instance Attribute Summary collapse
-
#filename ⇒ Object
Returns the value of attribute filename.
-
#props ⇒ Object
Returns the value of attribute props.
Class Method Summary collapse
- .compareStrings(a, b) ⇒ Object
- .newIniFileFromFilename(path) ⇒ Object
- .newIniFileFromString(inifile) ⇒ Object
- .propertiesToString(h) ⇒ Object
Instance Method Summary collapse
- #getProperties ⇒ Object
-
#initialize ⇒ IniFile
constructor
A new instance of IniFile.
- #loadINI ⇒ Object
- #loadProperties(file) ⇒ Object
- #loadPropertiesLine(line, count) ⇒ Object
Constructor Details
Instance Attribute Details
#filename ⇒ Object
Returns the value of attribute filename.
6 7 8 |
# File 'lib/com/wiris/util/sys/IniFile.rb', line 6 def filename @filename end |
#props ⇒ Object
Returns the value of attribute props.
7 8 9 |
# File 'lib/com/wiris/util/sys/IniFile.rb', line 7 def props @props end |
Class Method Details
.compareStrings(a, b) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/com/wiris/util/sys/IniFile.rb', line 129 def self.compareStrings(a, b) an = a::length() bn = b::length() n = (an > bn) ? bn : an for i in 0..n - 1 c = Std::charCodeAt(a,i) - Std::charCodeAt(b,i) if c != 0 return c end i+=1 end return a::length() - b::length() end |
.newIniFileFromFilename(path) ⇒ Object
12 13 14 15 16 17 |
# File 'lib/com/wiris/util/sys/IniFile.rb', line 12 def self.newIniFileFromFilename(path) ini = IniFile.new() ini::filename = path ini::loadINI() return ini end |
.newIniFileFromString(inifile) ⇒ Object
18 19 20 21 22 23 |
# File 'lib/com/wiris/util/sys/IniFile.rb', line 18 def self.newIniFileFromString(inifile) ini = IniFile.new() ini::filename = "" ini::loadProperties(inifile) return ini end |
.propertiesToString(h) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/com/wiris/util/sys/IniFile.rb', line 94 def self.propertiesToString(h) sb = StringBuf.new() iter = h::keys() keys = Array.new() while iter::hasNext() keys::push(iter::next()) end n = keys::length() for i in 0..n - 1 for j in i + 1..n - 1 s1 = keys::_(i) s2 = keys::_(j) if IniFile.compareStrings(s1,s2) > 0 keys::_(i,s2) keys::_(j,s1) end j+=1 end i+=1 end for i in 0..n - 1 key = keys::_(i) sb::add(key) sb::add("=") value = h::get(key) value = StringTools::replace(value,"\\","\\\\") value = StringTools::replace(value,"\n","\\n") value = StringTools::replace(value,"\r","\\r") value = StringTools::replace(value,"\t","\\t") sb::add(value) sb::add("\n") i+=1 end return sb::toString() end |
Instance Method Details
#getProperties ⇒ Object
24 25 26 |
# File 'lib/com/wiris/util/sys/IniFile.rb', line 24 def getProperties() return @props end |
#loadINI ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/com/wiris/util/sys/IniFile.rb', line 27 def loadINI() s = Storage::newStorage(@filename) if !s::exists() s = Storage::newResourceStorage(@filename) end begin file = s::read() if file != nil loadProperties(file) end end end |
#loadProperties(file) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/com/wiris/util/sys/IniFile.rb', line 80 def loadProperties(file) _end = 0 count = 1 while (start = file::indexOf("\n",_end)) != -1 line = Std::substr(file,_end,start - _end) _end = start + 1 self.loadPropertiesLine(line,count) count+=1 end if _end < file::length() line = Std::substr(file,_end) self.loadPropertiesLine(line,count) end end |
#loadPropertiesLine(line, count) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/com/wiris/util/sys/IniFile.rb', line 39 def loadPropertiesLine(line, count) line = StringTools::trim(line) if line::length() == 0 return end if StringTools::startsWith(line,";") || StringTools::startsWith(line,"#") return end equals = line::indexOf("=") if equals == -1 raise Exception,((("Malformed INI file " + @filename) + " in line ") + count.to_s) + " no equal sign found." end key = Std::substr(line,0,equals) key = StringTools::trim(key) value = Std::substr(line,equals + 1) value = StringTools::trim(value) if StringTools::startsWith(value,"\"") && StringTools::endsWith(value,"\"") value = Std::substr(value,1,value::length() - 2) end backslash = 0 while (backslash = value::indexOf("\\",backslash)) != -1 if value::length() <= (backslash + 1) next end letter = Std::substr(value,backslash + 1,1) if (letter == "n") letter = "\n" else if (letter == "r") letter = "\r" else if (letter == "t") letter = "\t" end end end value = (Std::substr(value,0,backslash).to_s + letter) + Std::substr(value,backslash + 2).to_s backslash+=1 end self.props::set(key,value) end |