Mono.Zeroconf – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 //
2 // TxtRecord.cs
3 //
4 // Author:
5 // Aaron Bockover <abockover@novell.com>
6 //
7 // Copyright (C) 2006-2008 Novell, Inc.
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.Generic;
33  
34 using Mono.Zeroconf;
35  
36 namespace Mono.Zeroconf.Providers.AvahiDBus
37 {
38 public class TxtRecord : ITxtRecord
39 {
40 private List<TxtRecordItem> records = new List<TxtRecordItem> ();
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  
56 if (split_item.Length == 1)
57 Add (split_item[0], String.Empty);
58 else
59 Add (split_item[0], split_item[1]);
60 }
61 }
62 }
63  
64 public void Dispose ()
65 {
66 }
67  
68 public void Add (string key, string value)
69 {
70 records.Add (new TxtRecordItem (key, value));
71 }
72  
73 public void Add (string key, byte [] value)
74 {
75 records.Add (new TxtRecordItem (key, value));
76 }
77  
78 public void Add (TxtRecordItem item)
79 {
80 records.Add (item);
81 }
82  
83 public void Remove (string key)
84 {
85 TxtRecordItem item = Find (key);
86 if (item != null) {
87 records.Remove (item);
88 }
89 }
90  
91 public TxtRecordItem Find (string key)
92 {
93 foreach (TxtRecordItem item in records) {
94 if (item.Key == key) {
95 return item;
96 }
97 }
98  
99 return null;
100 }
101  
102 public TxtRecordItem GetItemAt (int index)
103 {
104 return records[index] as TxtRecordItem;
105 }
106  
107 internal static byte [][] Render (ITxtRecord record)
108 {
109 byte [][] items = new byte[record.Count][];
110 int index = 0;
111  
112 foreach (TxtRecordItem item in record) {
113 string txt = String.Format ("{0}={1}", item.Key, item.ValueString);
114 items[index++] = encoding.GetBytes (txt);
115 }
116  
117 return items;
118 }
119  
120 public IEnumerator<TxtRecordItem> GetEnumerator ()
121 {
122 return records.GetEnumerator ();
123 }
124  
125 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator ()
126 {
127 return GetEnumerator ();
128 }
129  
130 public TxtRecordItem this[string key] {
131 get { return Find (key); }
132 }
133  
134 public int Count {
135 get { return records.Count; }
136 }
137  
138 public ITxtRecord BaseRecord {
139 get { return this; }
140 }
141 }
142 }