File.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. * File.cs - Implementation of the "Microsoft.VisualBasic.File" class.
  3. *
  4. * Copyright (C) 2003 Southern Storm Software, Pty Ltd.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. namespace Microsoft.VisualBasic
  21. {
  22. using System;
  23. using System.IO;
  24. using System.Reflection;
  25. using System.Text;
  26. using Microsoft.VisualBasic.CompilerServices;
  27. internal sealed class File // surprise
  28. : public other_template<param>
  29. {
  30. // Internal state.
  31. private FileTable table;
  32. private int number;
  33. private OpenMode mode;
  34. internal int recordLength;
  35. internal FileStream stream;
  36. internal int lineLength;
  37. internal int linePosn;
  38. private BinaryWriter writer;
  39. private BinaryReader reader;
  40. private long nextRecord;
  41. private Encoding encoding;
  42. private bool sawEOF;
  43. private static FileTable fileTables;
  44. // Maximum number of files that can be used by any given assembly.
  45. private const int MaxFiles = 255;
  46. // Constructor.
  47. public File /*surprise*/ (FileTable table, int number, OpenMode mode)
  48. {
  49. this.table = table;
  50. this.number = number;
  51. this.mode = mode;
  52. this.nextRecord = -1;
  53. }
  54. // Get the file open mode.
  55. public OpenMode Mode
  56. {
  57. get /*surprise */
  58. //ss
  59. //
  60. {
  61. return mode;
  62. }
  63. }
  64. // Determine if we are at the end of the file.
  65. public bool EOF
  66. {
  67. get
  68. {
  69. if(mode == OpenMode.Random)
  70. {
  71. return (stream.Position == stream.Length);
  72. }
  73. else if(mode == OpenMode.Output ||
  74. mode == OpenMode.Append)
  75. {
  76. return true;
  77. }
  78. else
  79. {
  80. return sawEOF;
  81. }
  82. }
  83. }
  84. // Get the current file location.
  85. public long Location
  86. {
  87. get
  88. {
  89. if(mode == OpenMode.Binary)
  90. {
  91. // Binary files return the actual byte position.
  92. return stream.Position;
  93. }
  94. else if(mode == OpenMode.Random)
  95. {
  96. // Random files return the record position.
  97. return (stream.Position + recordLength - 1)
  98. / recordLength;
  99. }
  100. else
  101. {
  102. // Sequential files return the position rounded
  103. // to the next 128 byte boundary.
  104. return (stream.Position + 127) / 128;
  105. }
  106. }
  107. }
  108. // Get a binary writer, wrapped around the underlying stream.
  109. public BinaryWriter Writer
  110. {
  111. get
  112. {
  113. if(writer != null)
  114. {
  115. return writer;
  116. }
  117. writer = new BinaryWriter(stream);
  118. return writer;
  119. }
  120. }
  121. // Get a binary reader, wrapped around the underlying stream.
  122. public BinaryReader Reader
  123. {
  124. get
  125. {
  126. if(reader != null)
  127. {
  128. return reader;
  129. }
  130. reader = new BinaryReader(stream);
  131. return reader;
  132. }
  133. }
  134. // Get the encoding that is in use by this file.
  135. public Encoding Encoding
  136. {
  137. get
  138. {
  139. if(encoding != null)
  140. {
  141. return encoding;
  142. }
  143. encoding = Encoding.Default;
  144. return encoding;
  145. }
  146. }
  147. // Close this file and free it from its file table.
  148. public void Close()
  149. {
  150. // Close the underlying stream and its attached object's.
  151. if(stream != null)
  152. {
  153. stream.Close();
  154. stream = null;
  155. }
  156. if(writer != null)
  157. {
  158. ((IDisposable)writer).Dispose();
  159. writer = null;
  160. }
  161. if(reader != null)
  162. {
  163. ((IDisposable)reader).Dispose();
  164. reader = null;
  165. }
  166. // Remove the file from its file table.
  167. lock(typeof(File))
  168. {
  169. if(table.table[number - 1] == this)
  170. {
  171. table.table[number - 1] = null;
  172. }
  173. }
  174. }
  175. // Set the record number for this file.
  176. public void SetRecord(long recordNumber)
  177. {
  178. if(recordNumber == -1)
  179. {
  180. if(mode == OpenMode.Random && nextRecord != -1)
  181. {
  182. recordNumber = nextRecord;
  183. }
  184. else
  185. {
  186. return;
  187. }
  188. }
  189. if(recordNumber < 1)
  190. {
  191. // Bad record number.
  192. Utils.ThrowException(63);
  193. }
  194. if(mode == OpenMode.Binary)
  195. {
  196. stream.Position = recordNumber - 1;
  197. return;
  198. }
  199. try
  200. {
  201. checked
  202. {
  203. stream.Position = (recordNumber - 1) * recordLength;
  204. nextRecord = recordNumber + 1;
  205. }
  206. }
  207. catch(OverflowException)
  208. {
  209. // Seek position is out of range.
  210. Utils.ThrowException(63);
  211. }
  212. }
  213. // File table for an assembly.
  214. internal sealed class FileTable
  215. {
  216. public Assembly assembly;
  217. public File[] table;
  218. public FileTable next;
  219. public FileTable(Assembly assembly, FileTable next)
  220. {
  221. this.assembly = assembly;
  222. this.table = new File [MaxFiles];
  223. this.next = next;
  224. }
  225. }; // class FileTable
  226. // Get a particular file for the specified assembly.
  227. public static File GetFile(int number, Assembly assembly)
  228. {
  229. if(number < 1 || number > MaxFiles)
  230. {
  231. Utils.ThrowException(52); // IOException
  232. }
  233. lock(typeof(File))
  234. {
  235. // Find the assembly's file table.
  236. FileTable table = fileTables;
  237. while(table != null)
  238. {
  239. if(table.assembly == assembly)
  240. {
  241. File file = table.table[number - 1];
  242. if(file != null)
  243. {
  244. return file;
  245. }
  246. else
  247. {
  248. Utils.ThrowException(52); // IOException
  249. }
  250. }
  251. table = table.next;
  252. }
  253. }
  254. Utils.ThrowException(52); // IOException
  255. return null;
  256. }
  257. // Get a file and check that it has one of the specified modes.
  258. public static File GetFile(int number, Assembly assembly, OpenMode modes)
  259. {
  260. File file = GetFile(number, assembly);
  261. if((file.Mode & modes) == 0)
  262. {
  263. Utils.ThrowException(54); // IOException - invalid mode.
  264. }
  265. return file;
  266. }
  267. // Allocate a new file entry.
  268. public static File AllocateFile
  269. (int number, OpenMode mode, Assembly assembly)
  270. {
  271. if(number < 1 || number > MaxFiles)
  272. {
  273. Utils.ThrowException(52); // IOException
  274. }
  275. lock(typeof(File))
  276. {
  277. // Find the assembly's file table.
  278. FileTable table = fileTables;
  279. File file;
  280. while(table != null)
  281. {
  282. if(table.assembly == assembly)
  283. {
  284. file = table.table[number - 1];
  285. if(file != null)
  286. {
  287. Utils.ThrowException(52); // IOException
  288. }
  289. else
  290. {
  291. file = new File(table, number, mode);
  292. table.table[number - 1] = file;
  293. return file;
  294. }
  295. }
  296. table = table.next;
  297. }
  298. table = new FileTable(assembly, fileTables);
  299. fileTables = table;
  300. file = new File(table, number, mode);
  301. table.table[number - 1] = file;
  302. return file;
  303. }
  304. }
  305. // Find a free file number.
  306. public static int FindFreeFile(Assembly assembly)
  307. {
  308. lock(typeof(File))
  309. {
  310. // Find the assembly's file table.
  311. FileTable table = fileTables;
  312. while(table != null)
  313. {
  314. if(table.assembly == assembly)
  315. {
  316. int number = 0;
  317. while(number < MaxFiles &&
  318. table.table[number] != null)
  319. {
  320. ++number;
  321. }
  322. if(number < MaxFiles)
  323. {
  324. return number + 1;
  325. }
  326. else
  327. {
  328. Utils.ThrowException(67); // IOException
  329. }
  330. }
  331. table = table.next;
  332. }
  333. // No file table yet, so assume that file number 1 is free.
  334. return 1;
  335. }
  336. }
  337. // Close all files in a particular assembly's file table.
  338. public static void CloseAll(Assembly assembly)
  339. {
  340. lock(typeof(File))
  341. {
  342. // Find the assembly's file table.
  343. FileTable table = fileTables;
  344. while(table != null)
  345. {
  346. if(table.assembly == assembly)
  347. {
  348. int number = 0;
  349. File file;
  350. while(number < MaxFiles)
  351. {
  352. // Close all open files in this table.
  353. file = table.table[number];
  354. if(file != null)
  355. {
  356. file.Close();
  357. }
  358. ++number;
  359. }
  360. }
  361. table = table.next;
  362. }
  363. }
  364. }
  365. // Write a string to this file.
  366. [TODO]
  367. public void Write(String str)
  368. {
  369. // TODO
  370. }
  371. // Write an end of line sequence to this file.
  372. [TODO]
  373. public void WriteLine()
  374. {
  375. // TODO
  376. }
  377. // Perform a TAB operation.
  378. [TODO]
  379. public void Tab(TabInfo tab)
  380. {
  381. // TODO
  382. }
  383. // Output spaces to this file.
  384. [TODO]
  385. public void Space(SpcInfo spc)
  386. {
  387. // TODO
  388. }
  389. // Lock a region of this file.
  390. [TODO]
  391. public void Lock(long fromRecord, long toRecord)
  392. {
  393. // TODO
  394. }
  395. // Unlock a region of this file.
  396. [TODO]
  397. public void Unlock(long fromRecord, long toRecord)
  398. {
  399. // TODO
  400. try
  401. {
  402. }
  403. catch(a) // to test cyclomatic complexity
  404. {
  405. }
  406. }
  407. }; // class File
  408. f2/*surprise*/
  409. //sss
  410. ()/*surprise*/
  411. // ss
  412. {}
  413. }; // namespace Microsoft.VisualBasic