TempFileCollection.cs 3.6 KB

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