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.
32 33 34 35 |
# File 'lib/packetgen/types/cstring.rb', line 32 def initialize(={}) register_internal_string(+'') @static_length = [:static_length] end |
Instance Attribute Details
#static_length ⇒ Integer (readonly)
28 29 30 |
# File 'lib/packetgen/types/cstring.rb', line 28 def static_length @static_length end |
#string ⇒ ::String (readonly)
26 27 28 |
# File 'lib/packetgen/types/cstring.rb', line 26 def string @string end |
Instance Method Details
#<<(str) ⇒ self
Append the given string to CString
62 63 64 65 66 |
# File 'lib/packetgen/types/cstring.rb', line 62 def <<(str) @string << str.to_s remove_null_character self end |
#from_human(str) ⇒ self
Populate CString from a human readable string
87 88 89 |
# File 'lib/packetgen/types/cstring.rb', line 87 def from_human(str) read str end |
#read(str) ⇒ String
Returns self.
39 40 41 42 43 44 45 |
# File 'lib/packetgen/types/cstring.rb', line 39 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
80 81 82 |
# File 'lib/packetgen/types/cstring.rb', line 80 def static_length? !static_length.nil? end |
#sz ⇒ Integer
69 70 71 72 73 74 75 |
# File 'lib/packetgen/types/cstring.rb', line 69 def sz if static_length? static_length else to_s.size end end |
#to_human ⇒ String
92 93 94 |
# File 'lib/packetgen/types/cstring.rb', line 92 def to_human string end |
#to_s ⇒ String
get null-terminated string
49 50 51 52 53 54 55 56 57 |
# File 'lib/packetgen/types/cstring.rb', line 49 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 |