123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447 |
- /*
- * File.cs - Implementation of the "Microsoft.VisualBasic.File" class.
- *
- * Copyright (C) 2003 Southern Storm Software, Pty Ltd.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
- namespace Microsoft.VisualBasic
- {
- using System;
- using System.IO;
- using System.Reflection;
- using System.Text;
- using Microsoft.VisualBasic.CompilerServices;
- internal sealed class File // surprise
- : public other_template<param>
- {
- // Internal state.
- private FileTable table;
- private int number;
- private OpenMode mode;
- internal int recordLength;
- internal FileStream stream;
- internal int lineLength;
- internal int linePosn;
- private BinaryWriter writer;
- private BinaryReader reader;
- private long nextRecord;
- private Encoding encoding;
- private bool sawEOF;
- private static FileTable fileTables;
- // Maximum number of files that can be used by any given assembly.
- private const int MaxFiles = 255;
- // Constructor.
- public File /*surprise*/ (FileTable table, int number, OpenMode mode)
- {
- this.table = table;
- this.number = number;
- this.mode = mode;
- this.nextRecord = -1;
- }
- // Get the file open mode.
- public OpenMode Mode
- {
- get /*surprise */
- //ss
- //
- {
- return mode;
- }
- }
- // Determine if we are at the end of the file.
- public bool EOF
- {
- get
- {
- if(mode == OpenMode.Random)
- {
- return (stream.Position == stream.Length);
- }
- else if(mode == OpenMode.Output ||
- mode == OpenMode.Append)
- {
- return true;
- }
- else
- {
- return sawEOF;
- }
- }
- }
- // Get the current file location.
- public long Location
- {
- get
- {
- if(mode == OpenMode.Binary)
- {
- // Binary files return the actual byte position.
- return stream.Position;
- }
- else if(mode == OpenMode.Random)
- {
- // Random files return the record position.
- return (stream.Position + recordLength - 1)
- / recordLength;
- }
- else
- {
- // Sequential files return the position rounded
- // to the next 128 byte boundary.
- return (stream.Position + 127) / 128;
- }
- }
- }
- // Get a binary writer, wrapped around the underlying stream.
- public BinaryWriter Writer
- {
- get
- {
- if(writer != null)
- {
- return writer;
- }
- writer = new BinaryWriter(stream);
- return writer;
- }
- }
- // Get a binary reader, wrapped around the underlying stream.
- public BinaryReader Reader
- {
- get
- {
- if(reader != null)
- {
- return reader;
- }
- reader = new BinaryReader(stream);
- return reader;
- }
- }
- // Get the encoding that is in use by this file.
- public Encoding Encoding
- {
- get
- {
- if(encoding != null)
- {
- return encoding;
- }
- encoding = Encoding.Default;
- return encoding;
- }
- }
- // Close this file and free it from its file table.
- public void Close()
- {
- // Close the underlying stream and its attached object's.
- if(stream != null)
- {
- stream.Close();
- stream = null;
- }
- if(writer != null)
- {
- ((IDisposable)writer).Dispose();
- writer = null;
- }
- if(reader != null)
- {
- ((IDisposable)reader).Dispose();
- reader = null;
- }
- // Remove the file from its file table.
- lock(typeof(File))
- {
- if(table.table[number - 1] == this)
- {
- table.table[number - 1] = null;
- }
- }
- }
- // Set the record number for this file.
- public void SetRecord(long recordNumber)
- {
- if(recordNumber == -1)
- {
- if(mode == OpenMode.Random && nextRecord != -1)
- {
- recordNumber = nextRecord;
- }
- else
- {
- return;
- }
- }
- if(recordNumber < 1)
- {
- // Bad record number.
- Utils.ThrowException(63);
- }
- if(mode == OpenMode.Binary)
- {
- stream.Position = recordNumber - 1;
- return;
- }
- try
- {
- checked
- {
- stream.Position = (recordNumber - 1) * recordLength;
- nextRecord = recordNumber + 1;
- }
- }
- catch(OverflowException)
- {
- // Seek position is out of range.
- Utils.ThrowException(63);
- }
- }
- // File table for an assembly.
- internal sealed class FileTable
- {
- public Assembly assembly;
- public File[] table;
- public FileTable next;
- public FileTable(Assembly assembly, FileTable next)
- {
- this.assembly = assembly;
- this.table = new File [MaxFiles];
- this.next = next;
- }
- }; // class FileTable
- // Get a particular file for the specified assembly.
- public static File GetFile(int number, Assembly assembly)
- {
- if(number < 1 || number > MaxFiles)
- {
- Utils.ThrowException(52); // IOException
- }
- lock(typeof(File))
- {
- // Find the assembly's file table.
- FileTable table = fileTables;
- while(table != null)
- {
- if(table.assembly == assembly)
- {
- File file = table.table[number - 1];
- if(file != null)
- {
- return file;
- }
- else
- {
- Utils.ThrowException(52); // IOException
- }
- }
- table = table.next;
- }
- }
- Utils.ThrowException(52); // IOException
- return null;
- }
- // Get a file and check that it has one of the specified modes.
- public static File GetFile(int number, Assembly assembly, OpenMode modes)
- {
- File file = GetFile(number, assembly);
- if((file.Mode & modes) == 0)
- {
- Utils.ThrowException(54); // IOException - invalid mode.
- }
- return file;
- }
- // Allocate a new file entry.
- public static File AllocateFile
- (int number, OpenMode mode, Assembly assembly)
- {
- if(number < 1 || number > MaxFiles)
- {
- Utils.ThrowException(52); // IOException
- }
- lock(typeof(File))
- {
- // Find the assembly's file table.
- FileTable table = fileTables;
- File file;
- while(table != null)
- {
- if(table.assembly == assembly)
- {
- file = table.table[number - 1];
- if(file != null)
- {
- Utils.ThrowException(52); // IOException
- }
- else
- {
- file = new File(table, number, mode);
- table.table[number - 1] = file;
- return file;
- }
- }
- table = table.next;
- }
- table = new FileTable(assembly, fileTables);
- fileTables = table;
- file = new File(table, number, mode);
- table.table[number - 1] = file;
- return file;
- }
- }
- // Find a free file number.
- public static int FindFreeFile(Assembly assembly)
- {
- lock(typeof(File))
- {
- // Find the assembly's file table.
- FileTable table = fileTables;
- while(table != null)
- {
- if(table.assembly == assembly)
- {
- int number = 0;
- while(number < MaxFiles &&
- table.table[number] != null)
- {
- ++number;
- }
- if(number < MaxFiles)
- {
- return number + 1;
- }
- else
- {
- Utils.ThrowException(67); // IOException
- }
- }
- table = table.next;
- }
- // No file table yet, so assume that file number 1 is free.
- return 1;
- }
- }
- // Close all files in a particular assembly's file table.
- public static void CloseAll(Assembly assembly)
- {
- lock(typeof(File))
- {
- // Find the assembly's file table.
- FileTable table = fileTables;
- while(table != null)
- {
- if(table.assembly == assembly)
- {
- int number = 0;
- File file;
- while(number < MaxFiles)
- {
- // Close all open files in this table.
- file = table.table[number];
- if(file != null)
- {
- file.Close();
- }
- ++number;
- }
- }
- table = table.next;
- }
- }
- }
- // Write a string to this file.
- [TODO]
- public void Write(String str)
- {
- // TODO
- }
- // Write an end of line sequence to this file.
- [TODO]
- public void WriteLine()
- {
- // TODO
- }
- // Perform a TAB operation.
- [TODO]
- public void Tab(TabInfo tab)
- {
- // TODO
- }
- // Output spaces to this file.
- [TODO]
- public void Space(SpcInfo spc)
- {
- // TODO
- }
- // Lock a region of this file.
- [TODO]
- public void Lock(long fromRecord, long toRecord)
- {
- // TODO
- }
- // Unlock a region of this file.
- [TODO]
- public void Unlock(long fromRecord, long toRecord)
- {
- // TODO
- try
- {
- }
- catch(a) // to test cyclomatic complexity
- {
- }
- }
- }; // class File
- f2/*surprise*/
- //sss
- ()/*surprise*/
- // ss
- {}
- }; // namespace Microsoft.VisualBasic
|