2005-10-04 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / System / System.CodeDom.Compiler / TempFileCollection.cs
1 //
2 // System.CodeDom.Compiler TempFileCollection Class implementation
3 //
4 // Author:
5 //      Dick Porter (dick@ximian.com)
6 //
7 // (C) Copyright 2003 Ximian, Inc.
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System.IO;
31 using System.Collections;
32
33 namespace System.CodeDom.Compiler
34 {
35         public class TempFileCollection:ICollection, IEnumerable, IDisposable
36         {
37                 Hashtable filehash;
38                 string tempdir;
39                 bool keepfiles;
40                 string basepath;
41                 Random rnd;
42                 
43                 public TempFileCollection(): this(null, false)
44                 {
45                 }
46
47                 public TempFileCollection(string tempDir): this(tempDir, false)
48                 {
49                 }
50
51                 public TempFileCollection(string tempDir, bool keepFiles)
52                 {
53                         filehash=new Hashtable();
54                         tempdir=tempDir;
55                         keepfiles=keepFiles;
56                 }
57
58                 public string BasePath
59                 {
60                         get {
61                                 if(basepath==null) {
62                                         if (tempdir==null) {
63                                                 /* Get the system temp dir */
64                                                 MonoIO.GetTempPath(out tempdir);
65                                         }
66
67                                         if (rnd == null)
68                                                 rnd = new Random ();
69
70                                         string random = rnd.Next (10000,99999).ToString ();
71                                         basepath = Path.Combine (tempdir, random);
72                                 }
73
74                                 return(basepath);
75                         }
76                 }
77
78                 public int Count
79                 {
80                         get {
81                                 return(filehash.Count);
82                         }
83                 }
84
85                 public bool KeepFiles
86                 {
87                         get {
88                                 return(keepfiles);
89                         }
90                         set {
91                                 keepfiles=value;
92                         }
93                 }
94
95                 public string TempDir
96                 {
97                         get {
98                                 if(tempdir==null) {
99                                         return(String.Empty);
100                                 } else {
101                                         return(tempdir);
102                                 }
103                         }
104                 }
105
106                 public string AddExtension(string fileExtension)
107                 {
108                         return(AddExtension(fileExtension, keepfiles));
109                 }
110
111                 public string AddExtension(string fileExtension, bool keepFile)
112                 {
113                         string filename=BasePath+"."+fileExtension;
114                         AddFile(filename, keepFile);
115                         return(filename);
116                 }
117
118                 public void AddFile(string fileName, bool keepFile)
119                 {
120                         filehash.Add(fileName, keepFile);
121                 }
122
123                 public void CopyTo(string[] fileNames, int start)
124                 {
125                         filehash.Keys.CopyTo(fileNames, start);
126                 }
127
128                 void ICollection.CopyTo(Array array, int start)
129                 {
130                         filehash.Keys.CopyTo(array, start);
131                 }
132
133                 object ICollection.SyncRoot {
134                         get {
135                                 return filehash.SyncRoot;
136                         }
137                 }
138
139                 bool ICollection.IsSynchronized {
140                         get {
141                                 return(false);
142                         }
143                 }
144                 
145                 void IDisposable.Dispose() 
146                 {
147                         Dispose(true);
148                 }
149                 
150                 public void Delete()
151                 {
152                         string[] filenames=new string[filehash.Count];
153                         filehash.Keys.CopyTo(filenames, 0);
154
155                         foreach(string file in filenames) {
156                                 if((bool)filehash[file]==false) {
157                                         File.Delete(file);
158                                         filehash.Remove(file);
159                                 }
160                         }
161                 }
162
163                 public IEnumerator GetEnumerator()
164                 {
165                         return(filehash.Keys.GetEnumerator());
166                 }
167
168                 protected virtual void Dispose(bool disposing)
169                 {
170                         Delete();
171                         if (disposing) {
172                                 GC.SuppressFinalize (true);
173                         }
174                 }
175
176                 ~TempFileCollection()
177                 {
178                         Dispose(false);
179                 }
180                 
181         }
182 }