Class: PacketGen::Types::CString
- Inherits:
-
Object
- Object
- PacketGen::Types::CString
- Extended by:
- Forwardable
- Includes:
- Fieldable
- Defined in:
- lib/packetgen/types/cstring.rb
Overview
This class handles null-terminated strings (aka C strings).
Instance Attribute Summary collapse
- #static_length ⇒ Integer readonly
- #string ⇒ ::String readonly
Instance Method Summary collapse
-
#<<(str) ⇒ self
Append the given string to CString.
-
#from_human(str) ⇒ self
Populate CString from a human readable string.
-
#initialize(options = {}) ⇒ CString
constructor
A new instance of CString.
-
#read(str) ⇒ String
Self.
-
#static_length? ⇒ Boolean
Say if a static length is defined.
- #sz ⇒ Integer
- #to_human ⇒ String
-
#to_s ⇒ String
get null-terminated string.
Methods included from Fieldable
Constructor Details
#initialize(options = {}) ⇒ CString
Returns a new instance of CString.
31 32 33 34 |
# File 'lib/packetgen/types/cstring.rb', line 31 def initialize(={}) register_internal_string(+'') @static_length = [:static_length] end |
Instance Attribute Details
#static_length ⇒ Integer (readonly)
27 28 29 |
# File 'lib/packetgen/types/cstring.rb', line 27 def static_length @static_length end |
#string ⇒ ::String (readonly)
25 26 27 |
# File 'lib/packetgen/types/cstring.rb', line 25 def string @string end |
Instance Method Details
#<<(str) ⇒ self
Append the given string to CString
61 62 63 64 65 |
# File 'lib/packetgen/types/cstring.rb', line 61 def <<(str) @string << str.to_s remove_null_character self end |
#from_human(str) ⇒ self
Populate CString from a human readable string
86 87 88 |
# File 'lib/packetgen/types/cstring.rb', line 86 def from_human(str) read str end |
#read(str) ⇒ String
Returns self.
38 39 40 41 42 43 44 |
# File 'lib/packetgen/types/cstring.rb', line 38 def read(str) s = str.to_s s = s[0, static_length] if static_length? register_internal_string s remove_null_character self end |
#static_length? ⇒ Boolean
Say if a static length is defined
79 80 81 |
# File 'lib/packetgen/types/cstring.rb', line 79 def static_length? !static_length.nil? end |
#sz ⇒ Integer
68 69 70 71 72 73 74 |
# File 'lib/packetgen/types/cstring.rb', line 68 def sz if static_length? static_length else to_s.size end end |
#to_human ⇒ String
91 92 93 |
# File 'lib/packetgen/types/cstring.rb', line 91 def to_human string end |
#to_s ⇒ String
get null-terminated string
48 49 50 51 52 53 54 55 56 |
# File 'lib/packetgen/types/cstring.rb', line 48 def to_s if static_length? s = string[0, static_length - 1] s << "\x00" * (static_length - s.length) else s = "#{string}\x00" end PacketGen.force_binary(s) end |