Class: FortranFormat

Inherits:
Object
  • Object
show all
Defined in:
ext/fortio/ruby_fortio.c,
ext/fortio/lib/fortio/fortran_format.rb

Constant Summary collapse

FORMAT_POOL =
Hash.new { |hash, fmt| 
  hash[fmt] = FortranFormatParser.new.parse(fmt)
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fmt) ⇒ FortranFormat

Returns a new instance of FortranFormat.



61
62
63
# File 'ext/fortio/lib/fortio/fortran_format.rb', line 61

def initialize (fmt)
  @format = FORMAT_POOL[fmt]
end

Class Method Details

.check_length(len, str) ⇒ Object



91
92
93
94
95
96
97
# File 'ext/fortio/lib/fortio/fortran_format.rb', line 91

def FortranFormat.check_length (len, str)
  if str.length > len
    return "*" * len
  else
    return str
  end
end

.read_FObject



34
35
36
37
38
39
40
41
42
43
44
45
# File 'ext/fortio/ruby_fortio.c', line 34

static VALUE
rb_ff_read_F (VALUE mod, VALUE vbuffer, VALUE vscale, VALUE vlength, VALUE vprec)
{
  double val;
  int status;
  status = read_F(StringValuePtr(vbuffer), (int) RSTRING_LEN(vbuffer),
                  NUM2INT(vscale), NUM2INT(vlength), NUM2INT(vprec), &val);
  if ( ! status ) {
    rb_raise(rb_eRuntimeError, "invalid string for F descriptor");
  }
  return rb_float_new(val);
}

.resetObject



57
58
59
# File 'ext/fortio/lib/fortio/fortran_format.rb', line 57

def self.reset
  FORMAT_POOL.clear
end

.write_EObject



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'ext/fortio/ruby_fortio.c', line 116

static VALUE
rb_ff_write_E (VALUE mod, VALUE vsign, VALUE vscale, VALUE vlength, VALUE vprec, VALUE vexp, VALUE va)
{
  int length = NUM2INT(vlength);
  int iexp   = NIL_P(vexp) ? 2 : NUM2INT(vexp);
  char buf[256];
  if ( length > 255 ) {
    rb_raise(rb_eRuntimeError, "too long decimal format for FortranFormat");
  }
  write_E(buf, 255, 
             RTEST(vsign), NUM2INT(vscale), length, NUM2INT(vprec), iexp, NUM2DBL(va));
  return rb_str_new2(buf);
}

.write_FObject



65
66
67
68
69
70
71
72
73
74
75
76
# File 'ext/fortio/ruby_fortio.c', line 65

static VALUE
rb_ff_write_F (VALUE mod, VALUE vsign, VALUE vscale, VALUE vlength, VALUE vprec, VALUE va)
{
  int length = NUM2INT(vlength);
  char buf[256];
  if ( length > 255 ) {
    rb_raise(rb_eRuntimeError, "too long decimal format for FortranFormat");
  }
  write_F(buf, 255, 
             RTEST(vsign), NUM2INT(vscale), length, NUM2INT(vprec),  NUM2DBL(va));
  return rb_str_new2(buf);
}

.write_GObject



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'ext/fortio/ruby_fortio.c', line 148

static VALUE
rb_ff_write_G (VALUE mod, VALUE vsign, VALUE vscale, VALUE vlength, VALUE vprec, VALUE vexp, VALUE va)
{
  int length = NUM2INT(vlength);
  int iprec  = NUM2INT(vprec);
  int iexp   = NIL_P(vexp) ? 2 : NUM2INT(vexp);
  double val = NUM2DBL(va);
  char buf[256];
  if ( length > 255 ) {
    rb_raise(rb_eRuntimeError, "too long decimal format for FortranFormat");
  }
  if ( ( fabs(val) <= 0.1 ) ||
       ( fabs(val) >= pow(10, iprec) ) ) {
    write_E(buf, 255, 
            RTEST(vsign), NUM2INT(vscale), length, iprec, iexp, val);
  }
  else {
    write_GF(buf, 255, RTEST(vsign), length, iprec, iexp, val);
  }
  return rb_str_new2(buf);
}

Instance Method Details

#count_argsObject



81
82
83
# File 'ext/fortio/lib/fortio/fortran_format.rb', line 81

def count_args
  return @format.count_args
end

#inspectObject



85
86
87
# File 'ext/fortio/lib/fortio/fortran_format.rb', line 85

def inspect
  return "<FortranFormat: #{@format.inspect}>"
end

#read(io, list = []) ⇒ Object



73
74
75
76
77
78
79
# File 'ext/fortio/lib/fortio/fortran_format.rb', line 73

def read (io, list = [])
  if io.is_a?(String)
    io = StringIO.new(io)
  end
  @format.read_as_root(io, list)
  return list
end

#write(io, *list) ⇒ Object



65
66
67
68
69
70
71
# File 'ext/fortio/lib/fortio/fortran_format.rb', line 65

def write (io, *list)
 io ||= ""
  buf = StringIO.new()
  @format.write_as_root(buf, list)
  io << buf.string
  return io 
end