Class: Gdsii::RecData::Ascii
- Defined in:
- lib/gdsii/record/datatypes/ascii.rb
Overview
Class for ASCII data type
Instance Attribute Summary collapse
-
#value ⇒ Object
Value is an array of strings.
Attributes inherited from Data
Class Method Summary collapse
-
.read(file, byte_count) ⇒ Object
Reads an ASCII record from the given file for the length of bytes given and returns a new Gdsii::RecData::Ascii object.
Instance Method Summary collapse
-
#byte_size ⇒ Object
Returns the size of the record data in bytes.
-
#initialize(value, record = nil) ⇒ Ascii
constructor
Construct an Gdsii::RecData::Ascii data object.
-
#pad(str_length = nil) ⇒ Object
Pad the value with a null character if the string is odd (but do not change the value itself).
-
#pad!(str_length = nil) ⇒ Object
Same as #pad except the value of this object is modified.
-
#to_s ⇒ Object
Joins all strings in the array with spaces and returns the joined string.
-
#unpad ⇒ Object
Return value with stripped off trailing null characters (which are present when reading this record from a file).
-
#unpad! ⇒ Object
Same as #unpad but modifies the value of this object.
-
#write(file) ⇒ Object
Writes the string values in this Gdsii::RecData::Ascii object to the given file as a GDSII ASCII record.
Methods inherited from Data
#[], #is_ascii?, #is_bitarray?, #is_int2?, #is_int4?, #is_no_data?, #is_real4?, #is_real8?, valid_type?
Constructor Details
#initialize(value, record = nil) ⇒ Ascii
Construct an Gdsii::RecData::Ascii data object. The value is an array of strings (String).
18 19 20 |
# File 'lib/gdsii/record/datatypes/ascii.rb', line 18 def initialize(value, record=nil) super(GDT_ASCII, value, record) end |
Instance Attribute Details
#value ⇒ Object
Value is an array of strings. Most Gdsii::RecData::Ascii records only have a single value.
14 15 16 |
# File 'lib/gdsii/record/datatypes/ascii.rb', line 14 def value @value end |
Class Method Details
.read(file, byte_count) ⇒ Object
Reads an ASCII record from the given file for the length of bytes given and returns a new Gdsii::RecData::Ascii object.
118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/gdsii/record/datatypes/ascii.rb', line 118 def Ascii.read(file, byte_count) # Verify byte count is even if byte_count%2 == 1 then raise ArgumentError, "GDT_ASCII records must have an even length; requested: #{byte_count}" end # read the string in; unpad; and return the new Gdsii::Ascii object raw = file.read(byte_count) string = raw.unpack("a#{byte_count}") data = Ascii.new(string) data.unpad! end |
Instance Method Details
#byte_size ⇒ Object
Returns the size of the record data in bytes. This will always return an even-length (multiple of 2) number since odd-length GDSII strings are padded with null characters when written to a file. If the record type has a specific size (i.e. Gdsii::GRT_FONTS and Gdsii::GRT_REFLIBS) then that size will automatically be used for each element in the value array.
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/gdsii/record/datatypes/ascii.rb', line 34 def byte_size() if @record and RECORD_INFO[@record.type].size > 0 then RECORD_INFO[@record.type].size * @value.length else sum = 0 @value.each do |val| sum += (val.length % 2 == 0) ? val.length : val.length + 1 end sum end end |
#pad(str_length = nil) ⇒ Object
Pad the value with a null character if the string is odd (but do not change the value itself). If a desired string length is given, then the string will be padded by the length given. For example, if we created a record with the string “hello”, then padding will add a null character since “hello” has an odd number of characters:
record = Gdsii::RecType::Ascii.new(["hello"])
record.pad.inspect #=> ["hello\0"]
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/gdsii/record/datatypes/ascii.rb', line 80 def pad(str_length=nil) new_arr = [] @value.each_with_index do |string, i| string = string.dup # to avoid changing the original if str_length.nil? then # pad if the string is odd or use the size property if there is a # predefined size if @record and (size=RECORD_INFO[@record.type].size) > 0 then new_arr.push [string].pack("a#{size}") elsif (len=string.length)%2 == 1 then new_arr.push [string].pack("a#{len+1}") else new_arr.push string end else # A desired string length was given; ensure that the requested # length is a multiple of 2 and is not less than the string given. if str_length%2 == 1 then raise ArgumentError, "Desired string length must be a multiple of 2" elsif str_length < string.length then raise ArgumentError, "Desired string length given #{str_length} is less than actual string length #{string.length}" else new_arr.push [string].pack("a#{str_length}") end end end new_arr end |
#pad!(str_length = nil) ⇒ Object
Same as #pad except the value of this object is modified.
112 113 114 |
# File 'lib/gdsii/record/datatypes/ascii.rb', line 112 def pad!(str_length=nil) @value = pad(str_length) end |
#to_s ⇒ Object
Joins all strings in the array with spaces and returns the joined string.
141 |
# File 'lib/gdsii/record/datatypes/ascii.rb', line 141 def to_s(); self.unpad[0] end |
#unpad ⇒ Object
Return value with stripped off trailing null characters (which are present when reading this record from a file). For example, assuming that a record is read from a file and is a null padded string “hello0”, then:
record.inspect #=> ["hello\0"]
record.unpad.inspect #=> ["hello"]
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/gdsii/record/datatypes/ascii.rb', line 54 def unpad() new_arr = [] @value.each do |string| string = string.dup # to avoid changing the original while (string[-1] == 0) string.chop! end new_arr.push string end new_arr end |
#unpad! ⇒ Object
Same as #unpad but modifies the value of this object.
67 68 69 |
# File 'lib/gdsii/record/datatypes/ascii.rb', line 67 def unpad!() @value = unpad end |
#write(file) ⇒ Object
Writes the string values in this Gdsii::RecData::Ascii object to the given file as a GDSII ASCII record.
134 135 136 137 |
# File 'lib/gdsii/record/datatypes/ascii.rb', line 134 def write(file) padded_str = self.pad file.write padded_str end |