|
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.Text; |
|
19 using System.IO; |
|
20 using System.Collections.Generic; |
|
21 using CrashAnalyserEngine.Plugins; |
|
22 |
|
23 namespace CAPCrashAnalysis.CommandLine |
|
24 { |
|
25 internal class CACmdLineFSEntityList<T> : IComparer<T>, IEnumerable<T> where T : CACmdLineFSEntity, new() |
|
26 { |
|
27 #region Constructors |
|
28 public CACmdLineFSEntityList() |
|
29 { |
|
30 } |
|
31 #endregion |
|
32 |
|
33 #region API |
|
34 public void Add( FileInfo aFile ) |
|
35 { |
|
36 T entry = new T(); |
|
37 entry.File = aFile; |
|
38 // |
|
39 AddInSortedOrder( entry ); |
|
40 } |
|
41 |
|
42 public void Add( DirectoryInfo aDir ) |
|
43 { |
|
44 T entry = new T(); |
|
45 entry.Directory = aDir; |
|
46 // |
|
47 AddInSortedOrder( entry ); |
|
48 } |
|
49 |
|
50 public void AddRange( FileInfo[] aFiles ) |
|
51 { |
|
52 foreach ( FileInfo file in aFiles ) |
|
53 { |
|
54 Add( file ); |
|
55 } |
|
56 } |
|
57 |
|
58 public bool Contains( string aFileName ) |
|
59 { |
|
60 CACmdLineFSEntity ret = this[ aFileName ]; |
|
61 return ret != null; |
|
62 } |
|
63 |
|
64 public void AddToAll( CACmdLineMessage aMessage ) |
|
65 { |
|
66 foreach ( CACmdLineFSEntity file in iFiles ) |
|
67 { |
|
68 file.Add( aMessage ); |
|
69 } |
|
70 } |
|
71 |
|
72 public T[] ToArray() |
|
73 { |
|
74 return iFiles.ToArray(); |
|
75 } |
|
76 #endregion |
|
77 |
|
78 #region Properties |
|
79 public int Count |
|
80 { |
|
81 get { return iFiles.Count; } |
|
82 } |
|
83 |
|
84 public T this[ int aIndex ] |
|
85 { |
|
86 get { return iFiles[ aIndex ]; } |
|
87 } |
|
88 |
|
89 public T this[ string aFileName ] |
|
90 { |
|
91 get |
|
92 { |
|
93 T temp = new T(); |
|
94 temp.File = new FileInfo( aFileName ); |
|
95 // |
|
96 int pos = iFiles.BinarySearch( temp, this ); |
|
97 // |
|
98 T ret = null; |
|
99 if ( pos >= 0 ) |
|
100 { |
|
101 ret = iFiles[ pos ]; |
|
102 } |
|
103 // |
|
104 return ret; |
|
105 } |
|
106 } |
|
107 #endregion |
|
108 |
|
109 #region Internal methods |
|
110 private void AddInSortedOrder( T aEntry ) |
|
111 { |
|
112 string fileName = aEntry.NameUC; |
|
113 // |
|
114 int pos = iFiles.BinarySearch( aEntry, this ); |
|
115 if ( pos < 0 ) |
|
116 { |
|
117 pos = ~pos; |
|
118 iFiles.Insert( pos, aEntry ); |
|
119 } |
|
120 else |
|
121 { |
|
122 throw new ArgumentException( "Specified file already exists: " + aEntry ); |
|
123 } |
|
124 } |
|
125 #endregion |
|
126 |
|
127 #region Operators |
|
128 public static implicit operator string[]( CACmdLineFSEntityList<T> aList ) |
|
129 { |
|
130 List<string> ret = new List<string>(); |
|
131 foreach ( T file in aList ) |
|
132 { |
|
133 ret.Add( file.Name ); |
|
134 } |
|
135 return ret.ToArray(); |
|
136 } |
|
137 #endregion |
|
138 |
|
139 #region From IEnumerable<T> |
|
140 public IEnumerator<T> GetEnumerator() |
|
141 { |
|
142 foreach ( T file in iFiles ) |
|
143 { |
|
144 yield return file; |
|
145 } |
|
146 } |
|
147 |
|
148 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() |
|
149 { |
|
150 foreach ( T file in iFiles ) |
|
151 { |
|
152 yield return file; |
|
153 } |
|
154 } |
|
155 #endregion |
|
156 |
|
157 #region From IComparer<T> |
|
158 public int Compare( T aLeft, T aRight ) |
|
159 { |
|
160 int ret = aLeft.NameUC.CompareTo( aRight.NameUC ); |
|
161 return ret; |
|
162 } |
|
163 #endregion |
|
164 |
|
165 #region Data members |
|
166 private List<T> iFiles = new List<T>(); |
|
167 #endregion |
|
168 } |
|
169 } |