forgot this one
[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) 2003 Ximian, Inc.
8 //
9
10 using System.IO;
11 using System.Collections;
12
13 namespace System.CodeDom.Compiler
14 {
15         public class TempFileCollection:ICollection, IEnumerable, IDisposable
16         {
17                 Hashtable filehash;
18                 string tempdir;
19                 bool keepfiles;
20                 
21                 public TempFileCollection(): this(null, false)
22                 {
23                 }
24
25                 public TempFileCollection(string tempDir): this(tempDir, false)
26                 {
27                 }
28
29                 public TempFileCollection(string tempDir, bool keepFiles)
30                 {
31                         filehash=new Hashtable();
32                         tempdir=tempDir;
33                         keepfiles=keepFiles;
34                 }
35
36                 private string basepath=null;
37                 
38                 public string BasePath
39                 {
40                         get {
41                                 if(basepath==null) {
42                                         if(tempdir==null) {
43                                                 /* Get the system temp dir */
44                                                 MonoIO.GetTempPath(out tempdir);
45                                         }
46
47                                         string random=new Random().Next(10000,99999).ToString();
48                                         
49                                         if(tempdir.EndsWith("\\") ||
50                                            tempdir.EndsWith("/")) {
51                                                 basepath=tempdir+random;
52                                         } else {
53                                                 basepath=tempdir+"/"+random;
54                                         }
55                                 }
56
57                                 return(basepath);
58                         }
59                 }
60
61                 public int Count
62                 {
63                         get {
64                                 return(filehash.Count);
65                         }
66                 }
67
68                 public bool KeepFiles
69                 {
70                         get {
71                                 return(keepfiles);
72                         }
73                         set {
74                                 keepfiles=value;
75                         }
76                 }
77
78                 public string TempDir
79                 {
80                         get {
81                                 if(tempdir==null) {
82                                         return(String.Empty);
83                                 } else {
84                                         return(tempdir);
85                                 }
86                         }
87                 }
88
89                 public string AddExtension(string fileExtension)
90                 {
91                         return(AddExtension(fileExtension, keepfiles));
92                 }
93
94                 public string AddExtension(string fileExtension, bool keepFile)
95                 {
96                         string filename=BasePath+"."+fileExtension;
97                         AddFile(filename, keepFile);
98                         return(filename);
99                 }
100
101                 public void AddFile(string fileName, bool keepFile)
102                 {
103                         filehash.Add(fileName, keepFile);
104                 }
105
106                 public void CopyTo(string[] fileNames, int start)
107                 {
108                         filehash.Keys.CopyTo(fileNames, start);
109                 }
110
111                 void ICollection.CopyTo(Array array, int start)
112                 {
113                         filehash.Keys.CopyTo(array, start);
114                 }
115
116                 object ICollection.SyncRoot {
117                         get {
118                                 return filehash.SyncRoot;
119                         }
120                 }
121
122                 bool ICollection.IsSynchronized {
123                         get {
124                                 return(false);
125                         }
126                 }
127                 
128                 void IDisposable.Dispose() 
129                 {
130                         Dispose(true);
131                 }
132                 
133                 public void Delete()
134                 {
135                         string[] filenames=new string[filehash.Count];
136                         filehash.Keys.CopyTo(filenames, 0);
137
138                         foreach(string file in filenames) {
139                                 if((bool)filehash[file]==true) {
140                                         File.Delete(file);
141                                         filehash.Remove(file);
142                                 }
143                         }
144                 }
145
146                 public IEnumerator GetEnumerator()
147                 {
148                         return(filehash.Keys.GetEnumerator());
149                 }
150
151                 protected virtual void Dispose(bool disposing)
152                 {
153                         Delete();
154                 }
155
156                 ~TempFileCollection()
157                 {
158                         Dispose(false);
159                 }
160                 
161         }
162 }