123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447 |
- namespace Microsoft.VisualBasic
- {
- using System;
- using System.IO;
- using System.Reflection;
- using System.Text;
- using Microsoft.VisualBasic.CompilerServices;
- internal sealed class File
- : public other_template<param>
- {
-
- 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;
-
- private const int MaxFiles = 255;
-
- public File (FileTable table, int number, OpenMode mode)
- {
- this.table = table;
- this.number = number;
- this.mode = mode;
- this.nextRecord = -1;
- }
-
- public OpenMode Mode
- {
- get
-
-
- {
- return mode;
- }
- }
-
- 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;
- }
- }
- }
-
- public long Location
- {
- get
- {
- if(mode == OpenMode.Binary)
- {
-
- return stream.Position;
- }
- else if(mode == OpenMode.Random)
- {
-
- return (stream.Position + recordLength - 1)
- / recordLength;
- }
- else
- {
-
-
- return (stream.Position + 127) / 128;
- }
- }
- }
-
- public BinaryWriter Writer
- {
- get
- {
- if(writer != null)
- {
- return writer;
- }
- writer = new BinaryWriter(stream);
- return writer;
- }
- }
-
- public BinaryReader Reader
- {
- get
- {
- if(reader != null)
- {
- return reader;
- }
- reader = new BinaryReader(stream);
- return reader;
- }
- }
-
- public Encoding Encoding
- {
- get
- {
- if(encoding != null)
- {
- return encoding;
- }
- encoding = Encoding.Default;
- return encoding;
- }
- }
-
- public void Close()
- {
-
- if(stream != null)
- {
- stream.Close();
- stream = null;
- }
- if(writer != null)
- {
- ((IDisposable)writer).Dispose();
- writer = null;
- }
- if(reader != null)
- {
- ((IDisposable)reader).Dispose();
- reader = null;
- }
-
- lock(typeof(File))
- {
- if(table.table[number - 1] == this)
- {
- table.table[number - 1] = null;
- }
- }
- }
-
- public void SetRecord(long recordNumber)
- {
- if(recordNumber == -1)
- {
- if(mode == OpenMode.Random && nextRecord != -1)
- {
- recordNumber = nextRecord;
- }
- else
- {
- return;
- }
- }
- if(recordNumber < 1)
- {
-
- Utils.ThrowException(63);
- }
- if(mode == OpenMode.Binary)
- {
- stream.Position = recordNumber - 1;
- return;
- }
- try
- {
- checked
- {
- stream.Position = (recordNumber - 1) * recordLength;
- nextRecord = recordNumber + 1;
- }
- }
- catch(OverflowException)
- {
-
- Utils.ThrowException(63);
- }
- }
-
- 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;
- }
- };
-
- public static File GetFile(int number, Assembly assembly)
- {
- if(number < 1 || number > MaxFiles)
- {
- Utils.ThrowException(52);
- }
- lock(typeof(File))
- {
-
- 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);
- }
- }
- table = table.next;
- }
- }
- Utils.ThrowException(52);
- return null;
- }
-
- public static File GetFile(int number, Assembly assembly, OpenMode modes)
- {
- File file = GetFile(number, assembly);
- if((file.Mode & modes) == 0)
- {
- Utils.ThrowException(54);
- }
- return file;
- }
-
- public static File AllocateFile
- (int number, OpenMode mode, Assembly assembly)
- {
- if(number < 1 || number > MaxFiles)
- {
- Utils.ThrowException(52);
- }
- lock(typeof(File))
- {
-
- FileTable table = fileTables;
- File file;
- while(table != null)
- {
- if(table.assembly == assembly)
- {
- file = table.table[number - 1];
- if(file != null)
- {
- Utils.ThrowException(52);
- }
- 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;
- }
- }
-
- public static int FindFreeFile(Assembly assembly)
- {
- lock(typeof(File))
- {
-
- 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);
- }
- }
- table = table.next;
- }
-
- return 1;
- }
- }
-
- public static void CloseAll(Assembly assembly)
- {
- lock(typeof(File))
- {
-
- FileTable table = fileTables;
- while(table != null)
- {
- if(table.assembly == assembly)
- {
- int number = 0;
- File file;
- while(number < MaxFiles)
- {
-
- file = table.table[number];
- if(file != null)
- {
- file.Close();
- }
- ++number;
- }
- }
- table = table.next;
- }
- }
- }
-
- [TODO]
- public void Write(String str)
- {
-
- }
-
- [TODO]
- public void WriteLine()
- {
-
- }
-
- [TODO]
- public void Tab(TabInfo tab)
- {
-
- }
-
- [TODO]
- public void Space(SpcInfo spc)
- {
-
- }
-
- [TODO]
- public void Lock(long fromRecord, long toRecord)
- {
-
- }
-
- [TODO]
- public void Unlock(long fromRecord, long toRecord)
- {
-
- try
- {
- }
- catch(a)
- {
- }
- }
- };
- f2
- ()
- {}
- };
|