|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 using System; |
|
18 using System.IO; |
|
19 using System.Text; |
|
20 using System.Threading; |
|
21 using System.Collections; |
|
22 |
|
23 namespace SymbianUtils |
|
24 { |
|
25 public class AsyncTextReaderFilter |
|
26 { |
|
27 #region Enumerations |
|
28 public enum TSpecificMatchType |
|
29 { |
|
30 ESpecificMustMatch = 0, |
|
31 ESpecificMustNotMatch |
|
32 } |
|
33 |
|
34 public enum TRemoveType |
|
35 { |
|
36 ERemoveNothing = 0, |
|
37 ERemoveAllInstances, |
|
38 ERemoveLastInstanceOnwards, |
|
39 ERemoveLastInstanceStartingAtPreviousSpaceOnwards |
|
40 } |
|
41 #endregion |
|
42 |
|
43 #region Construct & destruct |
|
44 public AsyncTextReaderFilter( string aMatchString ) |
|
45 : this( aMatchString, TRemoveType.ERemoveAllInstances ) |
|
46 { |
|
47 } |
|
48 |
|
49 public AsyncTextReaderFilter( string aMatchString, TRemoveType aRemoveType ) |
|
50 : this( aMatchString, aRemoveType, TSpecificMatchType.ESpecificMustMatch ) |
|
51 { |
|
52 } |
|
53 |
|
54 public AsyncTextReaderFilter( string aMatchString, TRemoveType aRemoveType, TSpecificMatchType aMatchType ) |
|
55 { |
|
56 iMatchString = aMatchString; |
|
57 iRemoveType = aRemoveType; |
|
58 iMatchType = aMatchType; |
|
59 } |
|
60 #endregion |
|
61 |
|
62 #region Properties |
|
63 public string MatchString |
|
64 { |
|
65 get { return iMatchString; } |
|
66 set { iMatchString = value; } |
|
67 } |
|
68 |
|
69 public TRemoveType RemoveType |
|
70 { |
|
71 get { return iRemoveType; } |
|
72 set { iRemoveType = value; } |
|
73 } |
|
74 |
|
75 public TSpecificMatchType MatchType |
|
76 { |
|
77 get { return iMatchType; } |
|
78 set { iMatchType = value; } |
|
79 } |
|
80 #endregion |
|
81 |
|
82 #region Internal API |
|
83 internal bool Matches( string aLine ) |
|
84 { |
|
85 int index = aLine.IndexOf( MatchString ); |
|
86 // |
|
87 bool matchedFilter = false; |
|
88 if ( index >= 0 ) |
|
89 { |
|
90 matchedFilter = ( MatchType == TSpecificMatchType.ESpecificMustMatch ); |
|
91 } |
|
92 else if ( index < 0 ) |
|
93 { |
|
94 matchedFilter = ( MatchType == TSpecificMatchType.ESpecificMustNotMatch ); |
|
95 } |
|
96 // |
|
97 return matchedFilter; |
|
98 } |
|
99 |
|
100 internal void Process( ref string aLine ) |
|
101 { |
|
102 int index = aLine.IndexOf( MatchString ); |
|
103 // |
|
104 if ( index >= 0 ) |
|
105 { |
|
106 switch( RemoveType ) |
|
107 { |
|
108 default: |
|
109 case TRemoveType.ERemoveNothing: |
|
110 { |
|
111 break; |
|
112 } |
|
113 case TRemoveType.ERemoveAllInstances: |
|
114 { |
|
115 aLine = aLine.Replace( MatchString, "" ); |
|
116 break; |
|
117 } |
|
118 case TRemoveType.ERemoveLastInstanceOnwards: |
|
119 { |
|
120 index = aLine.LastIndexOf( MatchString ); |
|
121 aLine = aLine.Substring( 0, index ); |
|
122 break; |
|
123 } |
|
124 case TRemoveType.ERemoveLastInstanceStartingAtPreviousSpaceOnwards: |
|
125 { |
|
126 int lastSpacePos = aLine.LastIndexOf( ' ', index ); |
|
127 if ( lastSpacePos < index ) |
|
128 { |
|
129 aLine = aLine.Substring( 0, lastSpacePos ); |
|
130 } |
|
131 else |
|
132 { |
|
133 aLine = aLine.Substring( 0, index ); |
|
134 } |
|
135 break; |
|
136 } |
|
137 } |
|
138 } |
|
139 } |
|
140 #endregion |
|
141 |
|
142 #region Data members |
|
143 private string iMatchString = string.Empty; |
|
144 private TRemoveType iRemoveType = TRemoveType.ERemoveLastInstanceOnwards; |
|
145 private TSpecificMatchType iMatchType = TSpecificMatchType.ESpecificMustMatch; |
|
146 #endregion |
|
147 } |
|
148 } |