2 * Copyright (c) 2002-2003 Mainsoft Corporation.
3 * Copyright (C) 2004 Novell, Inc (http://www.novell.com)
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
28 using System.Collections;
29 using System.Globalization;
30 using Microsoft.VisualBasic;
31 using System.Runtime.InteropServices;
32 using Microsoft.VisualBasic.CompilerServices;
35 using DefaultParameterValueAttribute = Microsoft.VisualBasic.CompilerServices.__DefaultParameterValueAttribute;
40 * @limit TAB(int) - not supported.
41 * @limit not all file attributes are supported. supported are: Hidden, Directory, ReadOnly
42 * @limit ChDir(..) - not supported.
43 * @limit ChDrive - not supported.
44 * @limit CurDir(Char) - ignores the parameter.
47 namespace Microsoft.VisualBasic
50 sealed public class FileSystem
52 private FileSystem () {}
54 private static Hashtable _fileNameIdMap = new Hashtable();
55 private static Hashtable _openFilesMap = new Hashtable();
56 private static Hashtable _randomRecordLength = new Hashtable();
58 static String _pattern;
59 static int _fileAttrs;
60 private static int _fileIndex;
61 private static FileInfo[] _files;
62 private static bool _isEndOfFiles = true;
65 public static void Reset()
67 ICollection s = _openFilesMap.Keys;
69 int [] iArr = new int[s.Count];
74 public static void FileClose(params int[] fileNumbers)
77 if (fileNumbers == null || fileNumbers.Length == 0)
79 ICollection keySet = FileSystem._openFilesMap.Keys;
80 iArr = new int[keySet.Count];
81 keySet.CopyTo(iArr, 0);
85 iArr = new int[fileNumbers.Length];
86 for (int i = 0; i < fileNumbers.Length; i++)
87 iArr[i] = fileNumbers[i];
92 private static void close(int [] keys)
96 for (int i = 0; i < keys.Length; i++)
98 if (keys[i] < 0 || keys[i] > 255)
100 ExceptionUtils.VbMakeException(
101 VBErrors.IllegalFuncCall);
102 throw new ArgumentOutOfRangeException();
104 obj = _openFilesMap[keys[i]];
108 String message = VBUtils.GetResourceString(52);
109 throw (IOException)VBUtils.VBException(new IOException(message), 52);
111 String fileName = null;
114 ((VBFile)obj).closeFile();
115 fileName = ((VBFile)obj).getFullPath();
119 _openFilesMap.Remove(keys[i]);
120 _fileNameIdMap.Remove(fileName);
124 public static void FileOpen(
128 [Optional, __DefaultParameterValue((int)(-1))] OpenAccess access,
129 [Optional, __DefaultParameterValue((int)(-1))] OpenShare share,
130 [Optional, DefaultParameterValue(-1)] int recordLength)
133 if (!isFileNumberFree(fileNumber))
134 throw ExceptionUtils.VbMakeException(VBErrors.FileAlreadyOpen);
135 if (fileNumber < 0 || fileNumber > 255)
136 throw ExceptionUtils.VbMakeException(VBErrors.BadFileNameOrNumber);
137 if (recordLength != -1 && recordLength <= 0)
138 throw ExceptionUtils.VbMakeException(VBErrors.IllegalFuncCall);
139 if (share != OpenShare.Shared && _fileNameIdMap.ContainsKey(Path.GetFullPath(string.Intern(fileName))))
140 throw ExceptionUtils.VbMakeException(VBErrors.FileAlreadyOpen);
141 if (mode == OpenMode.Input)
143 VBFile vbFile = new InputVBFile(fileName, (FileAccess) access,(recordLength == -1)? 4096:recordLength);
144 _openFilesMap.Add(fileNumber, vbFile);
146 else if (mode == OpenMode.Output || mode == OpenMode.Append)
148 VBFile vbFile = new OutPutVBFile(fileName,mode,access,recordLength);
149 _openFilesMap.Add(fileNumber, vbFile);
151 else if (mode == OpenMode.Random)
153 VBFile vbFile = new RandomVBFile(fileName,mode,access,recordLength);
154 _openFilesMap.Add(fileNumber, vbFile);
156 else if (mode == OpenMode.Binary)
158 VBFile vbFile = new BinaryVBFile(fileName,mode,access,recordLength);
159 _openFilesMap.Add(fileNumber, vbFile);
161 if (share != OpenShare.Shared && !_fileNameIdMap.ContainsKey(Path.GetFullPath(string.Intern(fileName))))
162 _fileNameIdMap.Add(Path.GetFullPath(string.Intern(fileName)),fileNumber);
165 public static void FilePut(int fileNumber,
167 [Optional, DefaultParameterValue((long)-1)] long recordNumber)
169 checkRecordNumber(recordNumber,false);
170 VBFile vbFile = getVBFile(fileNumber);
171 vbFile.put(value,recordNumber);
175 public static void FilePut(int fileNumber,
177 [Optional, DefaultParameterValue((long)-1)] long recordNumber)
179 checkRecordNumber(recordNumber,false);
180 VBFile vbFile = getVBFile(fileNumber);
181 vbFile.put(value,recordNumber);
184 public static void FilePut(int fileNumber,
186 [Optional, DefaultParameterValue((long)-1)] long recordNumber)
188 checkRecordNumber(recordNumber,false);
189 VBFile vbFile = getVBFile(fileNumber);
190 vbFile.put(value,recordNumber);
194 public static void FilePut(int fileNumber,
196 [Optional, DefaultParameterValue((long)-1)] long recordNumber)
198 checkRecordNumber(recordNumber,false);
199 VBFile vbFile = getVBFile(fileNumber);
200 vbFile.put(value,recordNumber);
203 public static void FilePut(int fileNumber,
205 [Optional, DefaultParameterValue((long)-1)] long recordNumber)
207 checkRecordNumber(recordNumber,false);
208 VBFile vbFile = getVBFile(fileNumber);
209 vbFile.put(value,recordNumber);
212 public static void FilePut(int fileNumber,
214 [Optional, DefaultParameterValue((long)-1)] long recordNumber)
216 checkRecordNumber(recordNumber,false);
217 VBFile vbFile = getVBFile(fileNumber);
218 vbFile.put(value,recordNumber);
222 public static void FilePut(int fileNumber,
224 [Optional, DefaultParameterValue((long)-1)] long recordNumber)
226 checkRecordNumber(recordNumber,false);
227 VBFile vbFile = getVBFile(fileNumber);
228 vbFile.put(value,recordNumber);
231 public static void FilePut(int fileNumber,
233 [Optional, DefaultParameterValue((long)-1)] long recordNumber)
235 checkRecordNumber(recordNumber,false);
236 VBFile vbFile = getVBFile(fileNumber);
237 vbFile.put(value,recordNumber);
240 public static void FilePut(int fileNumber,
242 [Optional, DefaultParameterValue((long)-1)] long recordNumber,
243 [Optional, DefaultParameterValue(false)] bool stringIsFixedLength)
245 checkRecordNumber(recordNumber,true);
246 VBFile vbFile = getVBFile(fileNumber);
247 vbFile.put(value,recordNumber,stringIsFixedLength);
250 public static void FilePut(int fileNumber,
252 [Optional, DefaultParameterValue((long)-1)] long recordNumber)
254 checkRecordNumber(recordNumber,false);
255 VBFile vbFile = getVBFile(fileNumber);
256 vbFile.put(value,recordNumber);
259 private static void checkRecordNumber(long recordNumber,bool throwArgExc)
261 if ((recordNumber < 1) && (recordNumber != -1))
264 throw (IOException) ExceptionUtils.VbMakeException(
265 new ArgumentException(
266 Utils.GetResourceString(
267 "Argument_InvalidValue1",
269 VBErrors.BadRecordNum);
272 ExceptionUtils.VbMakeException(VBErrors.BadRecordNum);
273 throw new ArgumentException(
274 Utils.GetResourceString(
275 "Argument_InvalidValue1",
282 private static VBFile getVBFile(int fileNumber)
284 if ((fileNumber < 1) || (fileNumber > 255))
285 throw (IOException) ExceptionUtils.VbMakeException(
286 VBErrors.BadFileNameOrNumber);
287 Object obj = _openFilesMap[fileNumber];
289 throw (IOException) ExceptionUtils.VbMakeException(
290 VBErrors.BadFileNameOrNumber);
294 private static VBFile getVBFile(String pathName)
296 object o = _fileNameIdMap[pathName];
297 int fileNumber = o == null ? 0 : (int) o ;
299 throw (IOException) ExceptionUtils.VbMakeException(
300 VBErrors.BadFileNameOrNumber);
301 Object obj = _openFilesMap[fileNumber];
303 throw (IOException) ExceptionUtils.VbMakeException(
304 VBErrors.BadFileNameOrNumber);
308 public static void FileGet(
311 [Optional, DefaultParameterValue((long)-1)] long recordNumber)
314 checkRecordNumber(recordNumber,false);
315 VBFile vbFile = getVBFile(fileNumber);
316 vbFile.get(out value,recordNumber);
319 public static void FileGet(
322 [Optional, DefaultParameterValue((long)-1)] long recordNumber)
325 checkRecordNumber(recordNumber,false);
326 VBFile vbFile = getVBFile(fileNumber);
327 vbFile.get(out value,recordNumber);
330 public static void FileGet(
333 [Optional, DefaultParameterValue((long)-1)] long recordNumber)
336 checkRecordNumber(recordNumber,false);
337 VBFile vbFile = getVBFile(fileNumber);
338 vbFile.get(out value,recordNumber);
341 public static void FileGet(
344 [Optional, DefaultParameterValue((long)-1)] long recordNumber)
348 checkRecordNumber(recordNumber,false);
349 VBFile vbFile = getVBFile(fileNumber);
350 vbFile.get(out value,recordNumber);
354 public static void FileGet(
357 [Optional, DefaultParameterValue((long)-1)] long recordNumber)
359 checkRecordNumber(recordNumber,false);
360 VBFile vbFile = getVBFile(fileNumber);
361 vbFile.get(out value,recordNumber);
364 public static void FileGet(
367 [Optional, DefaultParameterValue((long)-1)] long recordNumber)
369 checkRecordNumber(recordNumber,false);
370 VBFile vbFile = getVBFile(fileNumber);
371 vbFile.get(out value,recordNumber);
374 public static void FileGet(
377 [Optional, DefaultParameterValue((long)-1)] long recordNumber)
379 checkRecordNumber(recordNumber,false);
380 VBFile vbFile = getVBFile(fileNumber);
381 vbFile.get(out value,recordNumber);
384 public static void FileGet(
387 [Optional, DefaultParameterValue((long)-1)] long recordNumber)
390 checkRecordNumber(recordNumber,false);
391 VBFile vbFile = getVBFile(fileNumber);
392 vbFile.get(out value,recordNumber);
395 public static void FileGet(
398 [Optional, DefaultParameterValue((long)-1)] long recordNumber)
401 checkRecordNumber(recordNumber,false);
402 VBFile vbFile = getVBFile(fileNumber);
403 vbFile.get(out value,recordNumber);
406 public static void FileGet(
409 [Optional, DefaultParameterValue((long)-1)] long recordNumber,
410 [Optional, DefaultParameterValue(false)] bool stringIsFixedLength)
412 checkRecordNumber(recordNumber,true);
413 VBFile vbFile = getVBFile(fileNumber);
414 vbFile.get(ref value,recordNumber,stringIsFixedLength);
417 public static void FileGet(
420 [Optional, DefaultParameterValue((long)-1)] long recordNumber)
423 checkRecordNumber(recordNumber,true);
424 VBFile vbFile = getVBFile(fileNumber);
425 vbFile.get(out value,recordNumber);
429 public static void FileGet(
432 [Optional, DefaultParameterValue((long)-1)] long recordNumber)
434 throw new NotImplementedException();
437 public static void FileGet(
440 [Optional, DefaultParameterValue((long)-1)] long recordNumber,
441 [Optional, DefaultParameterValue(false)] bool arrayIsDynamic,
442 [Optional, DefaultParameterValue(false)] bool stringIsFixedLength)
444 checkRecordNumber(recordNumber,true);
445 VBFile vbFile = getVBFile(fileNumber);
446 vbFile.get(ref value,recordNumber,arrayIsDynamic,stringIsFixedLength);
449 public static long Seek(int fileNumber)
451 VBFile vbFile = getVBFile(fileNumber);
452 return vbFile.seek();
455 public static void Seek(int fileNumber, long position)
457 VBFile vbFile = getVBFile(fileNumber);
458 vbFile.seek(position);
462 public static long Loc(int fileNumber)
464 VBFile vbFile = getVBFile(fileNumber);
465 return vbFile.getPosition();
468 public static long LOF(int fileNumber)
470 VBFile vbFile = getVBFile(fileNumber);
471 return vbFile.getLength();
474 public static void Input(int fileNumber, ref Object value)
476 VBFile vbFile = getVBFile(fileNumber);
481 vbFile.Input(out tmp);
484 else if (value is byte) {
486 vbFile.Input(out tmp);
489 else if (value is char) {
491 vbFile.Input(out tmp);
494 else if (value is double) {
496 vbFile.Input(out tmp);
499 else if (value is short) {
501 vbFile.Input(out tmp);
504 else if (value is int) {
506 vbFile.Input(out tmp);
509 else if (value is long) {
511 vbFile.Input(out tmp);
514 else if (value is float) {
516 vbFile.Input(out tmp);
519 else if (value is Decimal) {
521 vbFile.Input(out tmp);
524 else if (value is string)
525 vbFile.Input((string)value);
526 // Come back to it later
527 // else if (type.get_IsByRef())
528 // vbFile.Input((ObjectRefWrapper)value[i],false);
533 public static String InputString(int fileNumber, int count)
536 throw (ArgumentException)ExceptionUtils.VbMakeException(VBErrors.IllegalFuncCall);
537 VBFile vbFile = getVBFile(fileNumber);
538 if (vbFile.getLength()- vbFile.getPosition() < count)
539 throw (EndOfStreamException)ExceptionUtils.VbMakeException(VBErrors.EndOfFile);
540 return vbFile.InputString(count);
543 public static String LineInput(int fileNumber)
545 VBFile vbFile = getVBFile(fileNumber);
548 throw new EndOfStreamException("Input past end of file.");
550 return vbFile.readLine();
553 public static void Print(int fileNumber, params Object[] output)
555 VBFile vbFile = getVBFile(fileNumber);
556 vbFile.print(output);
559 // Seems not to exist in MS's 1.1 implementation as told by class status pages
560 // public static void PrintLine(int fileNumber)
562 // VBFile vbFile = getVBFile(fileNumber);
563 // vbFile.printLine(null);
566 public static void PrintLine(int fileNumber, params Object[] output)
568 VBFile vbFile = getVBFile(fileNumber);
569 vbFile.printLine(output);
572 public static void Write(int fileNumber, params Object[] output)
574 VBFile vbFile = getVBFile(fileNumber);
575 vbFile.write(output);
578 public static void WriteLine(int fileNumber, params Object[] output)
580 VBFile vbFile = getVBFile(fileNumber);
581 vbFile.writeLine(output);
584 public static void Rename(String oldPath, String newPath)
586 FileInfo file = new FileInfo(newPath);
588 throw (IOException) ExceptionUtils.VbMakeException(
589 VBErrors.FileAlreadyExists);
592 File.Move(oldPath, newPath);
593 }catch (Exception e){
594 throw (ArgumentException)ExceptionUtils.VbMakeException(VBErrors.IllegalFuncCall);
598 public static void FileCopy(String source, String destination)
602 if ((source == null) || (source.Length == 0))
604 ExceptionUtils.VbMakeException(VBErrors.BadFileNameOrNumber);
605 throw new ArgumentException(
606 Utils.GetResourceString("Argument_PathNullOrEmpty"));
610 if ((destination == null) || (destination.Length == 0))
612 ExceptionUtils.VbMakeException(VBErrors.BadFileNameOrNumber);
613 throw new ArgumentException(
614 Utils.GetResourceString("Argument_PathNullOrEmpty"));
617 FileInfo f = new FileInfo(source);
619 throw (FileNotFoundException) ExceptionUtils.VbMakeException(
620 VBErrors.FileNotFound);
621 if (_fileNameIdMap[source] != null)
622 throw (IOException) ExceptionUtils.VbMakeException(
623 VBErrors.FileAlreadyOpen);
624 int lastIndex = destination.LastIndexOf('/');
627 dir = new DirectoryInfo(".");
629 dir = new DirectoryInfo(destination.Substring(0,lastIndex));
633 ExceptionUtils.VbMakeException(VBErrors.FileAlreadyOpen);
634 throw new DirectoryNotFoundException();
637 // the file name length is 0
638 if (destination.Length == lastIndex +1)
640 throw (IOException)ExceptionUtils.VbMakeException(VBErrors.FileAlreadyOpen);
645 f = new FileInfo(destination);
647 throw (IOException) ExceptionUtils.VbMakeException(
648 VBErrors.FileAlreadyExists);
651 File.Copy(source, destination, true);
654 public static void MkDir(String path)
656 if ((path == null) || (path.Length == 0))
658 ExceptionUtils.VbMakeException(VBErrors.BadFileNameOrNumber);
659 throw new ArgumentException(
660 Utils.GetResourceString("Argument_PathNullOrEmpty"));
662 if (Directory.Exists(path))
663 throw (IOException) ExceptionUtils.VbMakeException(
664 VBErrors.PathFileAccess);
666 Directory.CreateDirectory(path);
669 public static void Kill(String pathName)
671 if (_fileNameIdMap[pathName] != null)
672 throw (IOException) ExceptionUtils.VbMakeException(
673 VBErrors.FileAlreadyOpen);
674 if (!File.Exists(pathName))
675 throw (IOException) ExceptionUtils.VbMakeException(
676 VBErrors.FileNotFound);
677 File.Delete(pathName);
680 public static void RmDir(String pathName)
682 if (pathName == null || pathName.Length == 0 )
684 ExceptionUtils.VbMakeException(VBErrors.BadFileNameOrNumber);
685 throw new ArgumentException(pathName);
687 DirectoryInfo dir = new DirectoryInfo(pathName);
690 ExceptionUtils.VbMakeException(VBErrors.PathNotFound);
691 throw new DirectoryNotFoundException();
693 if (dir.GetFiles().Length != 0)
694 throw (IOException) ExceptionUtils.VbMakeException(
695 VBErrors.PathFileAccess);
696 Directory.Delete(pathName);
699 public static FileAttribute GetAttr(String pathName)
701 if (pathName == null || pathName.Length == 0 ||
702 pathName.IndexOf('*') != -1 || pathName.IndexOf('?') != -1)
704 throw (IOException)ExceptionUtils.VbMakeException(VBErrors.BadFileNameOrNumber);
706 // File f = new File(pathName);
708 // throw (FileNotFoundException)
709 // ExceptionUtils.VbMakeException(VBErrors.FileNotFound);
710 return (FileAttribute) File.GetAttributes(pathName);
713 public static void SetAttr(String pathName, FileAttribute fileAttr)
715 if (pathName == null || pathName.Length == 0 ||
716 pathName.IndexOf('*') != -1 || pathName.IndexOf('?') != -1)
718 throw (ArgumentException)ExceptionUtils.VbMakeException(VBErrors.IllegalFuncCall);
720 FileInfo f = new FileInfo(pathName);
721 if (!f.Directory.Exists)
723 ExceptionUtils.VbMakeException(VBErrors.PathNotFound);
724 throw new DirectoryNotFoundException();
728 throw (FileNotFoundException)
729 ExceptionUtils.VbMakeException(VBErrors.FileNotFound);
734 File.SetAttributes(pathName, (FileAttributes)fileAttr);
736 catch (ArgumentException e)
738 throw (ArgumentException) ExceptionUtils.VbMakeException(
739 VBErrors.IllegalFuncCall);
741 catch (DirectoryNotFoundException ex)
743 ExceptionUtils.VbMakeException(VBErrors.PathNotFound);
746 catch (FileNotFoundException ex)
748 throw (FileNotFoundException)
749 ExceptionUtils.VbMakeException(VBErrors.FileNotFound);
755 public static /*synchronized*/ String Dir(String pathName,
756 [Optional, __DefaultParameterValue((int)0)]
757 FileAttribute fileAttribute)
763 _fileAttrs = (int)fileAttribute;
765 if (pathName == null || pathName.Equals(""))
770 if (FileAttribute.Volume == fileAttribute)
774 int lastBabkSlashInx = pathName.LastIndexOf('\\');
775 int lastSlashInx = pathName.LastIndexOf('/');
776 int maxIndex = (lastSlashInx>lastBabkSlashInx)?lastSlashInx:lastBabkSlashInx;
777 String dir = pathName.Substring(0, maxIndex + 1);
778 String fileName = pathName.Substring(1 + maxIndex);
779 if (fileName == null || fileName.Length == 0)
782 // int astricsInx = fileName.indexOf('*');
783 // int questionInx = fileName.indexOf('?');
786 DirectoryInfo directory = new DirectoryInfo(dir);
787 // java.io.File directory = new java.io.File(dir);
788 // java.io.File file;
789 if (!directory.Exists)
791 // path not found - return empty string
795 // if (astricsInx == -1 && questionInx == -1)
797 // pattern = fileName;
801 // pattern = Strings.Replace(fileName, ".", "\\.", 1, -1, CompareMethod.Binary);
802 // pattern = Strings.Replace(pattern, "*", ".*", 1, -1, CompareMethod.Binary);
803 // pattern = Strings.Replace(pattern, "?", ".?", 1, -1, CompareMethod.Binary);
807 // _pattern = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
809 _files = directory.GetFiles(_pattern);
811 if (_files == null || _files.Length == 0)
813 DirectoryInfo[] dirs = directory.GetDirectories(_pattern);
814 if (dirs == null || dirs.Length == 0)
818 answer = dirs[0].Name;
822 answer = _files[0].Name;
826 _isEndOfFiles = false;
833 public static /*synchronized*/ String Dir()
836 if (_files == null || _isEndOfFiles)
837 throw new /*Illegal*/ArgumentException("no path has been initiated");
839 if (_fileIndex < _files.Length)
841 name = _files[_fileIndex].Name;
846 _isEndOfFiles = true;
853 public static DateTime FileDateTime(String pathName)
855 if (pathName == null || pathName.Length == 0 ||
856 pathName.IndexOf('*') != -1 || pathName.IndexOf('?') != -1)
858 ExceptionUtils.VbMakeException(VBErrors.BadFileNameOrNumber);
859 throw new ArgumentException(
860 VBUtils.GetResourceString(
861 "Argument_InvalidValue1",
864 FileInfo f = new FileInfo(pathName);
867 DirectoryInfo d = new DirectoryInfo(pathName);
869 throw (FileNotFoundException)
870 ExceptionUtils.VbMakeException(VBErrors.FileNotFound);
871 return d.LastWriteTime;
873 return f.LastWriteTime;
876 public static long FileLen(String pathName)
878 FileInfo f = new FileInfo(pathName);
880 throw new FileNotFoundException(
881 "file not exists: " + pathName);
887 internal static String SPC(int count)
889 StringBuilder sb = new StringBuilder(count);
890 for (int i = 0; i < count; i++)
893 return sb.ToString();
896 public static int FreeFile()
898 ICollection s = _openFilesMap.Keys;
903 int [] keyArr = new int[s.Count];
909 for (; i < keyArr.Length - 1; i++)
911 if ((keyArr[i]+ 1) < keyArr[i + 1])
915 int retVal = keyArr[i]+ 1;
919 String message = VBUtils.GetResourceString(67);
920 throw (IOException)VBUtils.VBException(
921 new IOException(message),
929 public static bool EOF(int fileNumber)
931 VBFile vbFile = getVBFile(fileNumber);
932 return vbFile.isEndOfFile();
935 // check if a specific number is free
936 private static bool isFileNumberFree(int fileNumber)
938 return !_openFilesMap.ContainsKey(fileNumber);
941 [MonoTODO("If path is another drive, it should change the default folder for that drive, but not switch to it.")]
942 public static void ChDir (string Path)
944 if ((Path=="") || (Path==null))
945 throw new ArgumentException (Utils.GetResourceString ("Argument_PathNullOrEmpty"));
947 Environment.CurrentDirectory = Path;
950 throw new FileNotFoundException (Utils.GetResourceString ("FileSystem_PathNotFound1", Path));
955 public static void ChDrive(char Drive)
958 if (Environment.OSVersion.Platform == PlatformID.Unix)
960 if (Environment.OSVersion.Platform == (PlatformID) 128)
962 throw new InvalidOperationException("ChDrive function is not supported on this platform !");
963 Drive = char.ToUpper(Drive, CultureInfo.InvariantCulture);
964 if ((Drive < 65) || (Drive > 90))
966 throw new ArgumentException(
967 Utils.GetResourceString("Argument_InvalidValue1", "Drive"));
969 Directory.SetCurrentDirectory(String.Concat(StringType.FromChar(Drive), StringType.FromChar(Path.VolumeSeparatorChar)));
972 public static void ChDrive(String Drive)
974 if (Drive != null && Drive.Length != 0)
975 FileSystem.ChDrive(Drive[0]);
978 public static String CurDir()
980 return Environment.CurrentDirectory;
983 public static String CurDir(char Drive)
985 return Directory.GetCurrentDirectory();
988 [MonoTODO("How to deal with Array types?")]
989 public static void FileGetObject(
992 [Optional, DefaultParameterValue((long)-1)] long recordNumber)
994 checkRecordNumber(recordNumber,true);
995 VBFile vbFile = getVBFile(fileNumber);
997 Type type = value.GetType();
999 if (type == null || value is string) {
1001 vbFile.get(ref tmp, recordNumber, false);
1004 else if ( value is bool) {
1006 vbFile.get(out tmp,recordNumber);
1009 else if ( value is char) {
1011 vbFile.get(out tmp,recordNumber);
1014 else if ( value is byte) {
1016 vbFile.get(out tmp,recordNumber);
1019 else if ( value is short) {
1021 vbFile.get(out tmp,recordNumber);
1024 else if ( value is int) {
1026 vbFile.get(out tmp,recordNumber);
1029 else if ( value is long) {
1031 vbFile.get(out tmp,recordNumber);
1034 else if ( value is float) {
1036 vbFile.get(out tmp,recordNumber);
1039 else if ( value is double) {
1041 vbFile.get(out tmp,recordNumber);
1044 else if ( value is Decimal) {
1046 vbFile.get(out tmp,recordNumber);
1049 else if ( value is DateTime) {
1051 vbFile.get(out tmp,recordNumber);
1054 else if (type.IsArray) {
1055 // need to figure out how to convert from Object& to Array&
1056 // vbFile.get(out value, recordNumber,true,false);
1058 throw new NotImplementedException();
1061 throw new NotSupportedException();
1064 public static void FilePutObject(int fileNumber,
1066 [Optional, DefaultParameterValue((long)-1)] long recordNumber)
1069 checkRecordNumber(recordNumber,true);
1070 VBFile vbFile = getVBFile(fileNumber);
1071 Type type = value.GetType();
1072 if(value is string || value == null)
1073 vbFile.put((String)value,recordNumber,false);
1074 else if( value is bool) {
1075 vbFile.put((bool)value, recordNumber);
1077 else if( value is char) {
1078 vbFile.put((char)value,recordNumber);
1080 else if( value is byte) {
1081 vbFile.put((byte)value, recordNumber);
1083 else if( value is short) {
1084 vbFile.put((short)value, recordNumber);
1086 else if( value is int) {
1087 vbFile.put((int)value, recordNumber);
1089 else if( value is long) {
1090 vbFile.put((long)value, recordNumber);
1092 else if( value is float) {
1093 vbFile.put((float)value, recordNumber);
1095 else if( value is double) {
1096 vbFile.put((double)value, recordNumber);
1098 else if( value is Decimal) {
1099 vbFile.put((Decimal)value,recordNumber);
1101 else if( value is DateTime) {
1102 vbFile.put((DateTime)value, recordNumber);
1104 else if(type.IsArray) {
1105 vbFile.put(value,recordNumber,true,false);
1108 throw new NotSupportedException();
1113 private const string obsoleteMsg1 = "Use FilePutObject to write Object types, or";
1114 private const string obsoleteMsg2 = "coerce FileNumber and RecordNumber to Integer for writing non-Object types";
1115 private const string obsoleteMsg = obsoleteMsg1 + obsoleteMsg2;
1117 [System.ObsoleteAttribute(obsoleteMsg, false)]
1118 public static void FilePut(Object FileNumber,
1120 [Optional, DefaultParameterValue(-1)] System.Object RecordNumber)
1122 throw new ArgumentException(Utils.GetResourceString("UseFilePutObject"));
1126 public static void FilePut(int FileNumber,
1128 [Optional, DefaultParameterValue((long)-1)] System.Int64 RecordNumber)
1131 throw new NotImplementedException();
1134 public static void FilePut(int fileNumber,
1136 [Optional, DefaultParameterValue((long)-1)] long recordNumber,
1137 [Optional, DefaultParameterValue(false)] bool arrayIsDynamic,
1138 [Optional, DefaultParameterValue(false)] bool stringIsFixedLength)
1140 checkRecordNumber(recordNumber,true);
1141 VBFile vbFile = getVBFile(fileNumber);
1142 vbFile.put(value,recordNumber,arrayIsDynamic,stringIsFixedLength);
1145 public static void FilePut(int fileNumber,
1147 [Optional, DefaultParameterValue((long)-1)] long recordNumber)
1149 checkRecordNumber(recordNumber,true);
1150 VBFile vbFile = getVBFile(fileNumber);
1151 vbFile.put(value,recordNumber);
1154 public static void Input(int fileNumber, ref bool Value)
1157 VBFile vbFile = getVBFile(fileNumber);
1158 vbFile.Input(out Value);
1161 public static void Input(int fileNumber, ref byte Value)
1163 VBFile vbFile = getVBFile(fileNumber);
1164 vbFile.Input(out Value);
1167 public static void Input(int fileNumber, ref short Value)
1169 VBFile vbFile = getVBFile(fileNumber);
1170 vbFile.Input(out Value);
1173 public static void Input(int fileNumber, ref int Value)
1175 VBFile vbFile = getVBFile(fileNumber);
1176 vbFile.Input(out Value);
1179 public static void Input(int fileNumber, ref long Value)
1181 VBFile vbFile = getVBFile(fileNumber);
1182 vbFile.Input(out Value);
1185 public static void Input(int fileNumber, ref char Value)
1187 VBFile vbFile = getVBFile(fileNumber);
1188 vbFile.Input(out Value);
1191 public static void Input(int fileNumber, ref float Value)
1193 VBFile vbFile = getVBFile(fileNumber);
1194 vbFile.Input(out Value);
1197 public static void Input(int fileNumber, ref double Value)
1199 VBFile vbFile = getVBFile(fileNumber);
1200 vbFile.Input(out Value);
1203 public static void Input(int fileNumber, ref Decimal Value)
1205 VBFile vbFile = getVBFile(fileNumber);
1206 vbFile.Input(out Value);
1209 public static void Input(int fileNumber, ref DateTime Value)
1211 VBFile vbFile = getVBFile(fileNumber);
1212 vbFile.Input(out Value);
1215 public static void Input(int fileNumber, ref string Value)
1217 VBFile vbFile = getVBFile(fileNumber);
1218 vbFile.Input(out Value);
1222 // public static void Input$V$FileSystem$ILSystem_Object$$$(int fileNumber, ObjectRefWrapper Value)
1224 // VBFile vbFile = getVBFile(fileNumber);
1225 // vbFile.Input(Value,false);
1229 public static void Lock(int fileNumber)
1231 throw new NotImplementedException("The method Lock in class FileSystem is not supported");
1235 public static void Lock(int FileNumber, long Record)
1237 throw new NotImplementedException("The method Lock in class FileSystem is not supported");
1241 public static void Lock(int FileNumber, long FromRecord, long ToRecord)
1243 throw new NotImplementedException("The method Lock in class FileSystem is not supported");
1247 public static void Unlock(int FileNumber)
1249 throw new NotImplementedException("The method Unlock in class FileSystem is not supported");
1253 public static void Unlock(int FileNumber, long Record)
1255 throw new NotImplementedException("The method Unlock in class FileSystem is not supported");
1259 public static void Unlock(int FileNumber, long FromRecord, long ToRecord)
1261 throw new NotImplementedException("The method Unlock in class FileSystem is not supported");
1264 public static void FileWidth(int fileNumber, int RecordWidth)
1266 VBFile vbFile = getVBFile(fileNumber);
1267 vbFile.width(fileNumber,RecordWidth);
1270 public static TabInfo TAB()
1272 return new TabInfo((short) - 1);
1275 public static TabInfo TAB(short Column)
1277 return new TabInfo(Column);
1280 public static SpcInfo SPC(short Count)
1282 return new SpcInfo(Count);
1285 public static OpenMode FileAttr(int fileNumber)
1287 VBFile vbFile = getVBFile(fileNumber);
1288 return (OpenMode) vbFile.getMode();
1292 class VBStreamWriter : StreamWriter
1297 public VBStreamWriter(string fileName):base(fileName)
1301 public VBStreamWriter(string fileName, bool append):base(fileName, append)
1306 //TODO: FileFilters from Mainsoft code