Mono.Zeroconf – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 //
2 // TxtRecord.cs
3 //
4 // Authors:
5 // Aaron Bockover <abockover@novell.com>
6 //
7 // Copyright (C) 2006-2007 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28  
29 using System;
30 using System.Text;
31 using System.Text.RegularExpressions;
32 using System.Collections;
33 using AV=Avahi;
34 using Mono.Zeroconf;
35  
36 namespace Mono.Zeroconf.Providers.Avahi
37 {
38 public class TxtRecord : ITxtRecord
39 {
40 private ArrayList records = new ArrayList();
41  
42 private static readonly Encoding encoding = new UTF8Encoding();
43  
44 public TxtRecord()
45 {
46 }
47  
48 public TxtRecord(byte [][] data)
49 {
50 foreach(byte [] raw_item in data) {
51 Regex item_regex = new Regex(@"""[^""]*""|[^,]+", RegexOptions.IgnorePatternWhitespace);
52 foreach(Match item_match in item_regex.Matches(encoding.GetString(raw_item))) {
53 string item = item_match.Groups[0].Value;
54 string [] split_item = item.Split(new char [] { '=' }, 2);
55 Add(split_item[0], split_item[1]);
56 }
57 }
58 }
59  
60 public void Dispose()
61 {
62 }
63  
64 public void Add(string key, string value)
65 {
66 records.Add(new TxtRecordItem(key, value));
67 }
68  
69 public void Add(string key, byte [] value)
70 {
71 records.Add(new TxtRecordItem(key, value));
72 }
73  
74 public void Add(TxtRecordItem item)
75 {
76 records.Add(item);
77 }
78  
79 public void Remove(string key)
80 {
81 TxtRecordItem item = Find(key);
82 if(item != null) {
83 records.Remove(item);
84 }
85 }
86  
87 public TxtRecordItem Find(string key)
88 {
89 foreach(TxtRecordItem item in records) {
90 if(item.Key == key) {
91 return item;
92 }
93 }
94  
95 return null;
96 }
97  
98 public TxtRecordItem GetItemAt(int index)
99 {
100 return records[index] as TxtRecordItem;
101 }
102  
103 internal byte [][] Render()
104 {
105 byte [][] items = new byte[records.Count][];
106 int index = 0;
107  
108 foreach(TxtRecordItem item in records) {
109 string txt = String.Format("{0}={1}", item.Key, item.ValueString);
110 items[index++] = encoding.GetBytes(txt);
111 }
112  
113 return items;
114 }
115  
116 public IEnumerator GetEnumerator()
117 {
118 return records.GetEnumerator();
119 }
120  
121 public TxtRecordItem this[string key] {
122 get { return Find(key); }
123 }
124  
125 public int Count {
126 get { return records.Count; }
127 }
128  
129 public ITxtRecord BaseRecord {
130 get { return this; }
131 }
132 }
133 }