2004-02-17 Gonzalo Paniagua Javier <gonzalo@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) 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                                         basepath = Path.Combine (tempdir, random);
50                                 }
51
52                                 return(basepath);
53                         }
54                 }
55
56                 public int Count
57                 {
58                         get {
59                                 return(filehash.Count);
60                         }
61                 }
62
63                 public bool KeepFiles
64                 {
65                         get {
66                                 return(keepfiles);
67                         }
68                         set {
69                                 keepfiles=value;
70                         }
71                 }
72
73                 public string TempDir
74                 {
75                         get {
76                                 if(tempdir==null) {
77                                         return(String.Empty);
78                                 } else {
79                                         return(tempdir);
80                                 }
81                         }
82                 }
83
84                 public string AddExtension(string fileExtension)
85                 {
86                         return(AddExtension(fileExtension, keepfiles));
87                 }
88
89                 public string AddExtension(string fileExtension, bool keepFile)
90                 {
91                         string filename=BasePath+"."+fileExtension;
92                         AddFile(filename, keepFile);
93                         return(filename);
94                 }
95
96                 public void AddFile(string fileName, bool keepFile)
97                 {
98                         filehash.Add(fileName, keepFile);
99                 }
100
101                 public void CopyTo(string[] fileNames, int start)
102                 {
103                         filehash.Keys.CopyTo(fileNames, start);
104                 }
105
106                 void ICollection.CopyTo(Array array, int start)
107                 {
108                         filehash.Keys.CopyTo(array, start);
109                 }
110
111                 object ICollection.SyncRoot {
112                         get {
113                                 return filehash.SyncRoot;
114                         }
115                 }
116
117                 bool ICollection.IsSynchronized {
118                         get {
119                                 return(false);
120                         }
121                 }
122                 
123                 void IDisposable.Dispose() 
124                 {
125                         Dispose(true);
126                 }
127                 
128                 public void Delete()
129                 {
130                         string[] filenames=new string[filehash.Count];
131                         filehash.Keys.CopyTo(filenames, 0);
132
133                         foreach(string file in filenames) {
134                                 if((bool)filehash[file]==false) {
135                                         File.Delete(file);
136                                         filehash.Remove(file);
137                                 }
138                         }
139                 }
140
141                 public IEnumerator GetEnumerator()
142                 {
143                         return(filehash.Keys.GetEnumerator());
144                 }
145
146                 protected virtual void Dispose(bool disposing)
147                 {
148                         Delete();
149                 }
150
151                 ~TempFileCollection()
152                 {
153                         Dispose(false);
154                 }
155                 
156         }
157 }