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