* Filters/FilterPackage.cs, Filters/FilterRPM.cs,
[beagle.git] / Util / PullingReader.cs
blob7944114aaa4e8fde90afab1d7ac161af8d0e2584
1 //
2 // PullingReader.cs
3 //
4 // Copyright (C) 2004 Novell, Inc.
5 //
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
27 using System;
28 using System.Collections;
29 using System.IO;
30 using System.Text;
32 namespace Beagle.Util {
34 public class PullingReader : TextReader {
36 private string identifier;
38 public string Identifier {
39 get { return identifier; }
40 set { identifier = value; }
43 //////////////////////////
45 public delegate bool Pull (StringBuilder buffer);
47 Pull pull;
48 StringBuilder pullBuffer = new StringBuilder ();
49 bool done = false;
51 public PullingReader (Pull _pull) : base ()
53 pull = _pull;
56 private void DoPull (int neededSize)
58 while (! done && pullBuffer.Length < neededSize) {
59 try {
60 done = ! pull (pullBuffer);
61 } catch (Exception e) {
62 Logger.Log.Debug ("Caught exception pulling text from {0}", pull);
63 Logger.Log.Debug (e);
68 public override void Close ()
73 public override int Peek ()
75 DoPull (1);
76 return done ? -1 : (int) pullBuffer [0];
79 public override int Read ()
81 DoPull (1);
82 if (done)
83 return -1;
84 int x = (int) pullBuffer [0];
85 pullBuffer.Remove (0, 1);
86 return x;
89 public override int Read (char[] buffer, int index, int count)
91 DoPull (count);
92 if (done && pullBuffer.Length < count)
93 count = pullBuffer.Length;
95 for (int i = 0; i < count; ++i)
96 buffer [index + i] = pullBuffer [i];
97 pullBuffer.Remove (0, count);
99 return count;
102 public override int ReadBlock (char[] buffer, int index, int count)
104 return Read (buffer, index, count);
107 public override string ReadLine ()
109 int i = 0;
111 DoPull (1);
113 if (done)
114 return null;
116 while (true) {
118 while (i < pullBuffer.Length) {
119 if (pullBuffer [i] == '\n') {
120 string foo = pullBuffer.ToString (0, i);
121 pullBuffer.Remove (0, i+1);
122 return foo;
124 ++i;
127 // If there is nothing else to pull, just return everything
128 // in our buffer.
129 if (done) {
130 string str = pullBuffer.ToString ();
131 pullBuffer.Length = 0;
132 return str;
135 DoPull (2 * pullBuffer.Length);
139 public override string ReadToEnd ()
141 while (! done)
142 DoPull (2 * pullBuffer.Length);
144 string str = pullBuffer.ToString ();
145 pullBuffer.Length = 0;
146 return str;