Module: FortIO::Namelist
- Defined in:
- lib/fortio-namelist/fortran_namelist.rb,
lib/fortio-namelist/fortran_namelist.tab.rb,
lib/fortio-namelist/fortran_namelist.tab.rb
Defined Under Namespace
Classes: Parser, Reader, Scanner
Class Method Summary collapse
- .dump(root, **format_options) ⇒ Object
-
.filter(input, **format_options) {|config| ... } ⇒ Object
FortIO::Namelist.filter(input) { |hash| MODIFYING HASH }.
-
.float_to_string(value, d:) ⇒ Object
FortIO::Namelist.dump(hash, group: “namelist”).
- .format_element(value, logical_format: 'normal', float_format: 'normal', uppercase: false) ⇒ Object
- .generate(hash, group: "group", array_style: "stream", alignment: "left", uppercase: false, separator: "comma", group_end: "/", indent: ' ', **format_options) ⇒ Object
-
.parse(input, group: nil) ⇒ Object
FortIO::Namelist.read(input, name: nil).
- .read(input, group: nil) ⇒ Object
Class Method Details
.dump(root, **format_options) ⇒ Object
254 255 256 |
# File 'lib/fortio-namelist/fortran_namelist.rb', line 254 def self.dump (root, **) return root.map { |group, hash| generate(hash, group: group, **) }.join end |
.filter(input, **format_options) {|config| ... } ⇒ Object
FortIO::Namelist.filter(input) { |hash| MODIFYING HASH }
input : namelist string
return value : namelist string
295 296 297 298 299 |
# File 'lib/fortio-namelist/fortran_namelist.rb', line 295 def self.filter (input, **) config = parse(input) yield config return dump(config, **) end |
.float_to_string(value, d:) ⇒ Object
FortIO::Namelist.dump(hash, group: “namelist”)
hash -> namelist converter
100 101 102 |
# File 'lib/fortio-namelist/fortran_namelist.rb', line 100 def self.float_to_string (value, d:) return value.to_s.sub(/e/, d) end |
.format_element(value, logical_format: 'normal', float_format: 'normal', uppercase: false) ⇒ Object
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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/fortio-namelist/fortran_namelist.rb', line 104 def self.format_element (value, logical_format: 'normal', float_format: 'normal', uppercase: false) case value when String if value !~ /'/ return "'" + value + "'" else return '"' + value.gsub(/"/, '""') + '"' end when Float d = uppercase ? "D" : "d" case float_format when 'normal' float_to_string(value, d: d) when 'd0' value = float_to_string(value, d: d) return ( value =~ /#{d}/ ) ? value : value + d + "0" when 'exp' num,exp = ("%.16e" % value).split(/e/) return ("%.16g" % num) + d + exp else raise "invalid float_format" end when Complex format("(%s,%s)", format_element(value.real, float_format: float_format), format_element(value.imag, float_format: float_format)) when TrueClass case logical_format when 'normal' return uppercase ? ".TRUE." : ".true." when 'short' return uppercase ? "T" : "t" else raise "invalid logical_format" end when FalseClass case logical_format when 'normal' return uppercase ? ".FALSE." : ".false." when 'short' return uppercase ? "F" : "f" else raise "invalid logical_format" end else return value.to_s end end |
.generate(hash, group: "group", array_style: "stream", alignment: "left", uppercase: false, separator: "comma", group_end: "/", indent: ' ', **format_options) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/fortio-namelist/fortran_namelist.rb', line 156 def self.generate (hash, group: "group", array_style: "stream", alignment: "left", uppercase: false, separator: "comma", group_end: "/", indent: ' ', **) [:uppercase] = uppercase list = [] hash.each do |ident, value| case value when Array case array_style when "index" value.each_with_index do |e, i| if e list << ["#{ident}(#{i+1})", format_element(e, **)] end end when "stream" list << [ident, value.map{ |e| format_element(e, **) }.join(", ")] else raise "invalid keyword argument `array_style` (should be 'index', 'stream')" end else list << [ident, format_element(value, **)] end end if uppercase list = list.map{|ident,value| [ident.upcase, value]} group = group.upcase end case separator when "comma", "," nl = ",\n" when "nl", "\n" nl = "\n" else raise "invalid keyword argument `separator` (should be 'comma', ',', 'nl', '\n')" end case alignment when /\Astream(:(\d+))?/ maxlen = $2 ? $2.to_i : 79 - indent.size elements = list.map { |ident, value| format("%s = %s", ident, value) } body = "" line = [] len = indent.size elements.each do |e| if len + e.length >= maxlen body += indent + line.join(", ") + nl line = [] len = indent.size end line << e len += e.length + 2 end body += indent + line.join(", ") + nl unless line.empty? body = body.chomp when "none" body = list.map { |ident, value| format("%s%s = %s", indent, ident, value) }.join(nl) when /\Aleft(:(\d+))?/ if $2 ident_maxlen = $2.to_i - indent.size else ident_maxlen = list.map{|ident,value| ident.length}.max end body = list.map { |ident, value| format("%s%-#{ident_maxlen}s = %s", indent, ident, value) }.join(nl) when /\Aright(:(\d+))?/ if $2 ident_maxlen = $2.to_i - indent.size else ident_maxlen = list.map{|ident,value| ident.length}.max end body = list.map { |ident, value| format("%s%#{ident_maxlen}s = %s", indent, ident, value) }.join(nl) else raise "invalid keyword argument `alignment` (should be 'normal' 'left' 'right' 'stream')" end case group_end when "slash", "/" tail = "/" when "end" tail = "&end" else raise "invalid keyword argument `group_end` (should be 'slash', '/', 'end')" end return ["&#{group}", body, tail, ""].join("\n") end |
.parse(input, group: nil) ⇒ Object
FortIO::Namelist.read(input, name: nil)
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/fortio-namelist/fortran_namelist.rb', line 261 def self.parse (input, group: nil) case input when String text = input else text = input.read end reader = FortIO::Namelist::Reader.new(text) case group when Array groups = group.map{|s| s.intern } when String, Symbol groups = [group].map{|s| s.intern } when nil groups = reader.namelist.keys else raise "invalid keyword arugment `group` '#{group.inspect}'" end return groups.each_with_object({}) { |group, root| root[group] = {} reader.parse(group, root[group]) } end |
.read(input, group: nil) ⇒ Object
285 286 287 |
# File 'lib/fortio-namelist/fortran_namelist.rb', line 285 def self.read (input, group: nil) parse(input, group: group) end |