Class | Dnsruby::RR::TXT |
In: |
lib/Dnsruby/resource/TXT.rb
|
Parent: | RR |
TypeValue | = | Types::TXT #:nodoc: all |
ESCAPE_CHARS | = | {"b" => 8, "t" => 9, "n" => 10, "v" => 11, "f" => 12, "r" => 13} |
ESCAPE_CODES | = | ESCAPE_CHARS.invert |
strings | [RW] | List of the individual elements |
# File lib/Dnsruby/resource/TXT.rb, line 138 138: def TXT.display(str, do_escapes = true) 139: output = "" 140: # Probably need to scan through each string manually 141: # Make sure to remember to escape binary characters. 142: # Go through copying to output, and adding "\" characters as necessary? 143: str.each_byte {|c| 144: if (c == 34) || (c == 92) # || (c == 59) 145: if (do_escapes) 146: output+='\\' 147: end 148: output+=c.chr 149: elsif (c < 32) # c is binary 150: if (ESCAPE_CODES[c]) 151: output += c.chr 152: else 153: output+= '\\' 154: num = c.to_i.to_s 155: (3-num.length).times {|i| 156: num="0"+num 157: } 158: output+= num # Need a 3 digit number here. 159: end 160: 161: else 162: output += c.chr 163: end 164: } 165: return output 166: end
# File lib/Dnsruby/resource/TXT.rb, line 52 52: def TXT.parse(input) 53: # Need to look out for special characters. 54: # Need to split the input up into strings (which are defined by non-escaped " characters) 55: # Then need to fix up any \ escape characters (should just be " and ; and binary?) 56: # Sadly, it's going to be easiest just to scan through this character by character... 57: in_escaped = false 58: in_string = false 59: count = -1 60: strings = [] 61: current_binary = "" 62: current_quote_char = '"' 63: unquoted = false 64: seen_strings = false 65: pos = 0 66: input.each_char {|c| 67: if (((c == "'") || (c == '"')) && (!in_escaped) && (!unquoted)) 68: if (!in_string) 69: seen_strings = true 70: current_quote_char = c 71: in_string = true 72: count+=1 73: strings[count] = "" 74: else 75: if (c == current_quote_char) 76: in_string = false 77: else 78: strings[count]+=c 79: end 80: end 81: else 82: if (seen_strings && !in_string) 83: next 84: end 85: if (pos == 0) 86: unquoted = true 87: count+=1 88: strings[count] = "" 89: elsif (unquoted) 90: if (c == " ") 91: count+=1 92: strings[count] = "" 93: pos += 1 94: next 95: end 96: end 97: 98: if (c == "\\") 99: if (in_escaped) 100: in_escaped = false 101: strings[count]+=(c) 102: else 103: in_escaped = true 104: end 105: else 106: if (in_escaped) 107: # Build up the binary 108: if (c == ";") || (c == '"') 109: strings[count]+=c 110: in_escaped = false 111: elsif (ESCAPE_CHARS[c]) 112: in_escaped=false 113: strings[count]+=ESCAPE_CHARS[c].chr 114: elsif (c<"0" || c>"9") 115: in_escaped = false 116: strings[count]+=c 117: else 118: # Must be building up three digit string to identify binary value? 119: # if (c >= "0" && c <= "9") 120: current_binary += c 121: # end 122: if ((current_binary.length == 3) ) # || (c < "0" || c > "9")) 123: strings[count]+=current_binary.to_i.chr 124: in_escaped = false 125: current_binary = "" 126: end 127: end 128: else 129: strings[count]+=(c) 130: end 131: end 132: end 133: pos += 1 134: } 135: return strings 136: end
# File lib/Dnsruby/resource/TXT.rb, line 39 39: def from_hash(hash) 40: if (hash.has_key?:strings) 41: from_string(hash[:strings]) 42: end 43: end
# File lib/Dnsruby/resource/TXT.rb, line 48 48: def from_string(input) 49: @strings = TXT.parse(input) 50: end