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 Microsoft.VisualBasic.CompilerServices;
35 * @limit TAB(int) - not supported.
36 * @limit not all file attributes are supported. supported are: Hidden, Directory, ReadOnly
37 * @limit ChDir(..) - not supported.
38 * @limit ChDrive - not supported.
39 * @limit CurDir(Char) - ignores the parameter.
42 namespace Microsoft.VisualBasic
44 [StandardModuleAttribute]
45 sealed public class FileSystem
48 private static Hashtable _fileNameIdMap = new Hashtable();
49 private static Hashtable _openFilesMap = new Hashtable();
50 private static Hashtable _randomRecordLength = new Hashtable();
52 static String _pattern;
53 static int _fileAttrs;
54 private static int _fileIndex;
55 private static FileInfo[] _files;
56 private static bool _isEndOfFiles = true;
59 public static void Reset()
63 ICollection s = _openFilesMap.Keys;
65 int [] iArr = new int[s.Count];
70 public static void FileClose(int[] fileNumbers)
73 if (fileNumbers.Length == 0)
75 ICollection keySet = FileSystem._openFilesMap.Keys;
76 iArr = new int[keySet.Count];
77 keySet.CopyTo(iArr, 0);
81 iArr = new int[fileNumbers.Length];
82 for (int i = 0; i < fileNumbers.Length; i++)
83 iArr[i] = fileNumbers[i];
88 private static void close(int [] keys)
92 for (int i = 0; i < keys.Length; i++)
94 if (keys[i] < 0 || keys[i] > 255)
96 ExceptionUtils.VbMakeException(
97 VBErrors.IllegalFuncCall);
98 throw new ArgumentOutOfRangeException();
100 obj = _openFilesMap[keys[i]];
104 String message = VBUtils.GetResourceString(52);
105 throw (IOException)VBUtils.VBException(new IOException(message), 52);
107 String fileName = null;
110 ((VBFile)obj).closeFile();
111 fileName = ((VBFile)obj).getFullPath();
115 _openFilesMap.Remove(keys[i]);
116 _fileNameIdMap.Remove(fileName);
120 public static void FileOpen(
124 [System.Runtime.InteropServices.Optional]
125 [System.ComponentModel.DefaultValue(-1)] OpenAccess access,
126 [System.Runtime.InteropServices.Optional]
127 [System.ComponentModel.DefaultValue(-1)] OpenShare share,
128 [System.Runtime.InteropServices.Optional]
129 [System.ComponentModel.DefaultValue(-1)] int recordLength)
132 if (!isFileNumberFree(fileNumber))
133 throw ExceptionUtils.VbMakeException(VBErrors.FileAlreadyOpen);
134 if (fileNumber < 0 || fileNumber > 255)
135 throw ExceptionUtils.VbMakeException(VBErrors.BadFileNameOrNumber);
136 if (recordLength != -1 && recordLength <= 0)
137 throw ExceptionUtils.VbMakeException(VBErrors.IllegalFuncCall);
138 if (share != OpenShare.Shared && _fileNameIdMap.ContainsKey(Path.GetFullPath(string.Intern(fileName))))
139 throw ExceptionUtils.VbMakeException(VBErrors.FileAlreadyOpen);
140 if (mode == OpenMode.Input)
142 VBFile vbFile = new InputVBFile(fileName, (FileAccess) access,(recordLength == -1)? 4096:recordLength);
143 _openFilesMap.Add(fileNumber, vbFile);
145 else if (mode == OpenMode.Output || mode == OpenMode.Append)
147 VBFile vbFile = new OutPutVBFile(fileName,mode,access,recordLength);
148 _openFilesMap.Add(fileNumber, vbFile);
150 else if (mode == OpenMode.Random)
152 VBFile vbFile = new RandomVBFile(fileName,mode,access,recordLength);
153 _openFilesMap.Add(fileNumber, vbFile);
155 else if (mode == OpenMode.Binary)
157 VBFile vbFile = new BinaryVBFile(fileName,mode,access,recordLength);
158 _openFilesMap.Add(fileNumber, vbFile);
160 _fileNameIdMap.Add(Path.GetFullPath(string.Intern(fileName)),fileNumber);
163 public static void FilePut(int fileNumber,
165 [System.Runtime.InteropServices.Optional]
166 [System.ComponentModel.DefaultValue(-1)] long recordNumber)
168 checkRecordNumber(recordNumber,false);
169 VBFile vbFile = getVBFile(fileNumber);
170 vbFile.put(value,recordNumber);
174 public static void FilePut(int fileNumber,
176 [System.Runtime.InteropServices.Optional]
177 [System.ComponentModel.DefaultValue(-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 [System.Runtime.InteropServices.Optional]
187 [System.ComponentModel.DefaultValue(-1)] long recordNumber)
189 checkRecordNumber(recordNumber,false);
190 VBFile vbFile = getVBFile(fileNumber);
191 vbFile.put(value,recordNumber);
195 public static void FilePut(int fileNumber,
197 [System.Runtime.InteropServices.Optional]
198 [System.ComponentModel.DefaultValue(-1)] long recordNumber)
200 checkRecordNumber(recordNumber,false);
201 VBFile vbFile = getVBFile(fileNumber);
202 vbFile.put(value,recordNumber);
205 public static void FilePut(int fileNumber,
207 [System.Runtime.InteropServices.Optional]
208 [System.ComponentModel.DefaultValue(-1)] long recordNumber)
210 checkRecordNumber(recordNumber,false);
211 VBFile vbFile = getVBFile(fileNumber);
212 vbFile.put(value,recordNumber);
215 public static void FilePut(int fileNumber,
217 [System.Runtime.InteropServices.Optional]
218 [System.ComponentModel.DefaultValue(-1)] long recordNumber)
220 checkRecordNumber(recordNumber,false);
221 VBFile vbFile = getVBFile(fileNumber);
222 vbFile.put(value,recordNumber);
226 public static void FilePut(int fileNumber,
228 [System.Runtime.InteropServices.Optional]
229 [System.ComponentModel.DefaultValue(-1)] long recordNumber)
231 checkRecordNumber(recordNumber,false);
232 VBFile vbFile = getVBFile(fileNumber);
233 vbFile.put(value,recordNumber);
236 public static void FilePut(int fileNumber,
238 [System.Runtime.InteropServices.Optional]
239 [System.ComponentModel.DefaultValue(-1)] long recordNumber)
241 checkRecordNumber(recordNumber,false);
242 VBFile vbFile = getVBFile(fileNumber);
243 vbFile.put(value,recordNumber);
246 public static void FilePut(int fileNumber,
248 [System.Runtime.InteropServices.Optional]
249 [System.ComponentModel.DefaultValue(-1)] long recordNumber,
250 [System.Runtime.InteropServices.Optional]
251 [System.ComponentModel.DefaultValue(false)] bool stringIsFixedLength)
253 checkRecordNumber(recordNumber,true);
254 VBFile vbFile = getVBFile(fileNumber);
255 vbFile.put(value,recordNumber,stringIsFixedLength);
258 public static void FilePut(int fileNumber,
260 [System.Runtime.InteropServices.Optional]
261 [System.ComponentModel.DefaultValue(-1)] long recordNumber)
263 checkRecordNumber(recordNumber,false);
264 VBFile vbFile = getVBFile(fileNumber);
265 vbFile.put(value,recordNumber);
268 public static void FileGet(int fileNumber,
270 [System.Runtime.InteropServices.Optional]
271 [System.ComponentModel.DefaultValue(-1)] long recordNumber)
274 checkRecordNumber(recordNumber,false);
275 VBFile vbFile = getVBFile(fileNumber);
276 vbFile.get(out value,recordNumber);
279 private static void checkRecordNumber(long recordNumber,bool throwArgExc)
281 if ((recordNumber < 1) && (recordNumber != -1))
284 throw (IOException) ExceptionUtils.VbMakeException(
285 new ArgumentException(
286 Utils.GetResourceString(
287 "Argument_InvalidValue1",
289 VBErrors.BadRecordNum);
292 ExceptionUtils.VbMakeException(VBErrors.BadRecordNum);
293 throw new ArgumentException(
294 Utils.GetResourceString(
295 "Argument_InvalidValue1",
302 private static VBFile getVBFile(int fileNumber)
304 if ((fileNumber < 1) || (fileNumber > 255))
305 throw (IOException) ExceptionUtils.VbMakeException(
306 VBErrors.BadFileNameOrNumber);
307 Object obj = _openFilesMap[fileNumber];
309 throw (IOException) ExceptionUtils.VbMakeException(
310 VBErrors.BadFileNameOrNumber);
314 private static VBFile getVBFile(String pathName)
316 object o = _fileNameIdMap[pathName];
317 int fileNumber = o == null ? 0 : (int) o ;
319 throw (IOException) ExceptionUtils.VbMakeException(
320 VBErrors.BadFileNameOrNumber);
321 Object obj = _openFilesMap[fileNumber];
323 throw (IOException) ExceptionUtils.VbMakeException(
324 VBErrors.BadFileNameOrNumber);
328 public static void FileGet(
331 [System.Runtime.InteropServices.Optional]
332 [System.ComponentModel.DefaultValue(-1)] long recordNumber)
335 checkRecordNumber(recordNumber,false);
336 VBFile vbFile = getVBFile(fileNumber);
337 vbFile.get(out value,recordNumber);
340 public static void FileGet(
343 [System.Runtime.InteropServices.Optional]
344 [System.ComponentModel.DefaultValue(-1)] long recordNumber)
347 checkRecordNumber(recordNumber,false);
348 VBFile vbFile = getVBFile(fileNumber);
349 vbFile.get(out value,recordNumber);
352 public static void FileGet(
355 [System.Runtime.InteropServices.Optional]
356 [System.ComponentModel.DefaultValue(-1)] long recordNumber)
360 checkRecordNumber(recordNumber,false);
361 VBFile vbFile = getVBFile(fileNumber);
362 vbFile.get(out value,recordNumber);
366 public static void FileGet(int fileNumber,
368 [System.Runtime.InteropServices.Optional]
369 [System.ComponentModel.DefaultValue(-1)] long recordNumber)
371 checkRecordNumber(recordNumber,false);
372 VBFile vbFile = getVBFile(fileNumber);
373 vbFile.get(out value,recordNumber);
376 public static void FileGet(int fileNumber,
378 [System.Runtime.InteropServices.Optional]
379 [System.ComponentModel.DefaultValue(-1)] long recordNumber)
381 checkRecordNumber(recordNumber,false);
382 VBFile vbFile = getVBFile(fileNumber);
383 vbFile.get(out value,recordNumber);
386 public static void FileGet(int fileNumber,
388 [System.Runtime.InteropServices.Optional]
389 [System.ComponentModel.DefaultValue(-1)] long recordNumber)
391 checkRecordNumber(recordNumber,false);
392 VBFile vbFile = getVBFile(fileNumber);
393 vbFile.get(out value,recordNumber);
396 public static void FileGet(int fileNumber,
398 [System.Runtime.InteropServices.Optional]
399 [System.ComponentModel.DefaultValue(-1)] long recordNumber)
402 checkRecordNumber(recordNumber,false);
403 VBFile vbFile = getVBFile(fileNumber);
404 vbFile.get(out value,recordNumber);
407 public static void FileGet(int fileNumber,
409 [System.Runtime.InteropServices.Optional]
410 [System.ComponentModel.DefaultValue(-1)] long recordNumber)
413 checkRecordNumber(recordNumber,false);
414 VBFile vbFile = getVBFile(fileNumber);
415 vbFile.get(out value,recordNumber);
418 public static void FileGet(int fileNumber,
420 [System.Runtime.InteropServices.Optional]
421 [System.ComponentModel.DefaultValue(-1)] long recordNumber,
422 [System.Runtime.InteropServices.Optional]
423 [System.ComponentModel.DefaultValue(false)] bool bIgnored)
425 checkRecordNumber(recordNumber,true);
426 VBFile vbFile = getVBFile(fileNumber);
427 vbFile.get(ref value,recordNumber,bIgnored);
430 public static void FileGet(int fileNumber,
432 [System.Runtime.InteropServices.Optional]
433 [System.ComponentModel.DefaultValue(-1)] long recordNumber)
435 checkRecordNumber(recordNumber,false);
436 VBFile vbFile = getVBFile(fileNumber);
437 vbFile.get(ref value,recordNumber);
440 public static long Seek(int fileNumber)
442 VBFile vbFile = getVBFile(fileNumber);
443 return vbFile.seek();
446 public static void Seek(int fileNumber, long position)
448 VBFile vbFile = getVBFile(fileNumber);
449 vbFile.seek(position);
453 public static long Loc(int fileNumber)
455 VBFile vbFile = getVBFile(fileNumber);
456 return vbFile.getPosition();
459 public static long LOF(int fileNumber)
461 VBFile vbFile = getVBFile(fileNumber);
462 return vbFile.getLength();
465 public static void Input(int fileNumber, ref Object value)
467 VBFile vbFile = getVBFile(fileNumber);
474 vbFile.Input(out tmp);
477 else if (value is byte) {
479 vbFile.Input(out tmp);
482 else if (value is char) {
484 vbFile.Input(out tmp);
487 else if (value is double) {
489 vbFile.Input(out tmp);
492 else if (value is short) {
494 vbFile.Input(out tmp);
497 else if (value is int) {
499 vbFile.Input(out tmp);
502 else if (value is long) {
504 vbFile.Input(out tmp);
507 else if (value is float) {
509 vbFile.Input(out tmp);
512 else if (value is Decimal) {
514 vbFile.Input(out tmp);
517 else if (value is string)
518 vbFile.Input((string)value);
519 // Come back to it later
520 // else if (type.get_IsByRef())
521 // vbFile.Input((ObjectRefWrapper)value[i],false);
526 public static String InputString(int fileNumber, int count)
529 throw (ArgumentException)ExceptionUtils.VbMakeException(VBErrors.IllegalFuncCall);
530 VBFile vbFile = getVBFile(fileNumber);
531 if (vbFile.getLength()- vbFile.getPosition() < count)
532 throw (EndOfStreamException)ExceptionUtils.VbMakeException(VBErrors.EndOfFile);
533 return vbFile.InputString(count);
536 public static String LineInput(int fileNumber)
538 VBFile vbFile = getVBFile(fileNumber);
541 throw new EndOfStreamException("Input past end of file.");
543 return vbFile.readLine();
546 public static void Print(int fileNumber, Object[] output)
548 VBFile vbFile = getVBFile(fileNumber);
549 vbFile.print(output);
552 public static void PrintLine(int fileNumber)
554 VBFile vbFile = getVBFile(fileNumber);
555 vbFile.printLine(null);
558 public static void PrintLine(int fileNumber, Object[] output)
560 VBFile vbFile = getVBFile(fileNumber);
561 vbFile.printLine(output);
564 public static void Write(int fileNumber, Object[] output)
566 VBFile vbFile = getVBFile(fileNumber);
567 vbFile.write(output);
570 public static void WriteLine(int fileNumber, Object[] output)
572 VBFile vbFile = getVBFile(fileNumber);
573 vbFile.writeLine(output);
576 public static void Rename(String oldPath, String newPath)
578 FileInfo file = new FileInfo(newPath);
580 throw (IOException) ExceptionUtils.VbMakeException(
581 VBErrors.FileAlreadyExists);
584 Directory.Move(oldPath, newPath);
585 }catch (DirectoryNotFoundException e){
586 throw (ArgumentException)ExceptionUtils.VbMakeException(VBErrors.IllegalFuncCall);
590 public static void FileCopy(String source, String destination)
594 if ((source == null) || (source.Length == 0))
596 ExceptionUtils.VbMakeException(VBErrors.BadFileNameOrNumber);
597 throw new ArgumentException(
598 Utils.GetResourceString("Argument_PathNullOrEmpty"));
601 if ((destination == null) || (destination.Length == 0))
603 ExceptionUtils.VbMakeException(VBErrors.BadFileNameOrNumber);
604 throw new ArgumentException(
605 Utils.GetResourceString("Argument_PathNullOrEmpty"));
608 FileInfo f = new FileInfo(source);
610 throw (FileNotFoundException) ExceptionUtils.VbMakeException(
611 VBErrors.FileNotFound);
612 if (_fileNameIdMap[source] != null)
613 throw (IOException) ExceptionUtils.VbMakeException(
614 VBErrors.FileAlreadyOpen);
615 int lastIndex = destination.LastIndexOf('/');
618 dir = new DirectoryInfo(".");
620 dir = new DirectoryInfo(destination.Substring(0,lastIndex));
624 ExceptionUtils.VbMakeException(VBErrors.FileAlreadyOpen);
625 throw new DirectoryNotFoundException();
628 // the file name length is 0
629 if (destination.Length == lastIndex +1)
631 throw (IOException)ExceptionUtils.VbMakeException(VBErrors.FileAlreadyOpen);
635 f = new FileInfo(destination);
637 throw (IOException) ExceptionUtils.VbMakeException(
638 VBErrors.FileAlreadyExists);
639 File.Copy(source, destination);
642 public static void MkDir(String path)
644 if ((path == null) || (path.Length == 0))
646 ExceptionUtils.VbMakeException(VBErrors.BadFileNameOrNumber);
647 throw new ArgumentException(
648 Utils.GetResourceString("Argument_PathNullOrEmpty"));
650 if (Directory.Exists(path))
651 throw (IOException) ExceptionUtils.VbMakeException(
652 VBErrors.PathFileAccess);
654 Directory.CreateDirectory(path);
657 public static void Kill(String pathName)
659 if (_fileNameIdMap[pathName] != null)
660 throw (IOException) ExceptionUtils.VbMakeException(
661 VBErrors.FileAlreadyOpen);
662 if (!File.Exists(pathName))
663 throw (IOException) ExceptionUtils.VbMakeException(
664 VBErrors.FileNotFound);
665 File.Delete(pathName);
668 public static void RmDir(String pathName)
670 if (pathName == null || pathName.Length == 0 )
672 ExceptionUtils.VbMakeException(VBErrors.BadFileNameOrNumber);
673 throw new ArgumentException(pathName);
675 DirectoryInfo dir = new DirectoryInfo(pathName);
678 ExceptionUtils.VbMakeException(VBErrors.PathNotFound);
679 throw new DirectoryNotFoundException();
681 if (dir.GetFiles().Length != 0)
682 throw (IOException) ExceptionUtils.VbMakeException(
683 VBErrors.PathFileAccess);
684 Directory.Delete(pathName);
687 public static FileAttribute GetAttr(String pathName)
689 if (pathName == null || pathName.Length == 0 ||
690 pathName.IndexOf('*') != -1 || pathName.IndexOf('?') != -1)
692 throw (IOException)ExceptionUtils.VbMakeException(VBErrors.BadFileNameOrNumber);
694 // File f = new File(pathName);
696 // throw (FileNotFoundException)
697 // ExceptionUtils.VbMakeException(VBErrors.FileNotFound);
698 return (FileAttribute) File.GetAttributes(pathName);
701 public static void SetAttr(String pathName, FileAttribute fileAttr)
703 if (pathName == null || pathName.Length == 0 ||
704 pathName.IndexOf('*') != -1 || pathName.IndexOf('?') != -1)
706 throw (ArgumentException)ExceptionUtils.VbMakeException(VBErrors.IllegalFuncCall);
708 FileInfo f = new FileInfo(pathName);
709 if (!f.Directory.Exists)
711 ExceptionUtils.VbMakeException(VBErrors.PathNotFound);
712 throw new DirectoryNotFoundException();
716 throw (FileNotFoundException)
717 ExceptionUtils.VbMakeException(VBErrors.FileNotFound);
722 File.SetAttributes(pathName, (FileAttributes)fileAttr);
724 catch (ArgumentException e)
726 throw (ArgumentException) ExceptionUtils.VbMakeException(
727 VBErrors.IllegalFuncCall);
729 catch (DirectoryNotFoundException ex)
731 ExceptionUtils.VbMakeException(VBErrors.PathNotFound);
734 catch (FileNotFoundException ex)
736 throw (FileNotFoundException)
737 ExceptionUtils.VbMakeException(VBErrors.FileNotFound);
743 public static /*synchronized*/ String Dir(String pathName)
745 return Dir(pathName, 0);
748 public static /*synchronized*/ String Dir(String pathName,
749 [System.Runtime.InteropServices.Optional]
750 [System.ComponentModel.DefaultValue(0)]
751 FileAttribute fileAttribute)
757 _fileAttrs = (int)fileAttribute;
759 if (pathName == null || pathName.Equals(""))
764 if (FileAttribute.Volume == fileAttribute)
768 int lastBabkSlashInx = pathName.LastIndexOf('\\');
769 int lastSlashInx = pathName.LastIndexOf('/');
770 int maxIndex = (lastSlashInx>lastBabkSlashInx)?lastSlashInx:lastBabkSlashInx;
771 String dir = pathName.Substring(0, maxIndex + 1);
772 String fileName = pathName.Substring(1 + maxIndex);
773 if (fileName == null || fileName.Length == 0)
776 // int astricsInx = fileName.indexOf('*');
777 // int questionInx = fileName.indexOf('?');
780 DirectoryInfo directory = new DirectoryInfo(dir);
781 // java.io.File directory = new java.io.File(dir);
782 // java.io.File file;
783 if (!directory.Exists)
785 // path not found - return empty string
789 // if (astricsInx == -1 && questionInx == -1)
791 // pattern = fileName;
795 // pattern = Strings.Replace(fileName, ".", "\\.", 1, -1, CompareMethod.Binary);
796 // pattern = Strings.Replace(pattern, "*", ".*", 1, -1, CompareMethod.Binary);
797 // pattern = Strings.Replace(pattern, "?", ".?", 1, -1, CompareMethod.Binary);
801 // _pattern = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
803 _files = directory.GetFiles(_pattern);
805 if (_files == null || _files.Length == 0)
807 DirectoryInfo[] dirs = directory.GetDirectories(_pattern);
808 if (dirs == null || dirs.Length == 0)
812 answer = dirs[0].Name;
816 answer = _files[0].Name;
820 _isEndOfFiles = false;
827 public static /*synchronized*/ String Dir()
830 if (_files == null || _isEndOfFiles)
831 throw new /*Illegal*/ArgumentException("no path has been initiated");
833 if (_fileIndex < _files.Length)
835 name = _files[_fileIndex].Name;
840 _isEndOfFiles = true;
847 public static DateTime FileDateTime(String pathName)
849 if (pathName == null || pathName.Length == 0 ||
850 pathName.IndexOf('*') != -1 || pathName.IndexOf('?') != -1)
852 ExceptionUtils.VbMakeException(VBErrors.BadFileNameOrNumber);
853 throw new ArgumentException(
854 VBUtils.GetResourceString(
855 "Argument_InvalidValue1",
858 FileInfo f = new FileInfo(pathName);
861 DirectoryInfo d = new DirectoryInfo(pathName);
863 throw (FileNotFoundException)
864 ExceptionUtils.VbMakeException(VBErrors.FileNotFound);
865 return d.LastWriteTime;
867 return f.LastWriteTime;
870 public static long FileLen(String pathName)
872 FileInfo f = new FileInfo(pathName);
874 throw new FileNotFoundException(
875 "file not exists: " + pathName);
881 internal static String SPC(int count)
883 StringBuilder sb = new StringBuilder(count);
884 for (int i = 0; i < count; i++)
887 return sb.ToString();
890 public static int FreeFile()
892 ICollection s = _openFilesMap.Keys;
897 int [] keyArr = new int[s.Count];
903 for (; i < keyArr.Length - 1; i++)
905 if ((keyArr[i]+ 1) < keyArr[i + 1])
909 int retVal = keyArr[i]+ 1;
913 String message = VBUtils.GetResourceString(67);
914 throw (IOException)VBUtils.VBException(
915 new IOException(message),
923 public static bool EOF(int fileNumber)
926 VBFile vbFile = getVBFile(fileNumber);
927 return vbFile.isEndOfFile();
930 // check if a specific number is free
931 private static bool isFileNumberFree(int fileNumber)
933 return !_openFilesMap.ContainsKey(fileNumber);
936 [MonoTODO("If path is another drive, it should change the default folder for that drive, but not switch to it.")]
937 public static void ChDir (string Path)
939 if ((Path=="") || (Path==null))
940 throw new ArgumentException (Utils.GetResourceString ("Argument_PathNullOrEmpty"));
942 Environment.CurrentDirectory = Path;
945 throw new FileNotFoundException (Utils.GetResourceString ("FileSystem_PathNotFound1", Path));
950 public static void ChDrive(char Drive)
952 Drive = char.ToUpper(Drive, CultureInfo.InvariantCulture);
953 if ((Drive < 65) || (Drive > 90))
955 throw new ArgumentException(
956 Utils.GetResourceString("Argument_InvalidValue1", "Drive"));
958 Directory.SetCurrentDirectory(String.Concat(StringType.FromChar(Drive), StringType.FromChar(Path.VolumeSeparatorChar)));
961 public static void ChDrive(String Drive)
963 if (Drive != null && Drive.Length != 0)
964 FileSystem.ChDrive(Drive[0]);
967 public static String CurDir()
969 return Environment.CurrentDirectory;
972 public static String CurDir(char Drive)
974 return Directory.GetCurrentDirectory();
977 public static void FileGetObject(int fileNumber,
979 [System.Runtime.InteropServices.Optional]
980 [System.ComponentModel.DefaultValue(-1)] long recordNumber)
984 checkRecordNumber(recordNumber,true);
985 VBFile vbFile = getVBFile(fileNumber);
987 Type type = value.GetType();
989 if (type == null || value is string) {
991 vbFile.get(ref tmp, recordNumber, false);
994 else if ( value is bool) {
996 vbFile.get(out tmp,recordNumber);
999 else if ( value is char) {
1001 vbFile.get(out tmp,recordNumber);
1004 else if ( value is byte) {
1006 vbFile.get(out tmp,recordNumber);
1009 else if ( value is short) {
1011 vbFile.get(out tmp,recordNumber);
1014 else if ( value is int) {
1016 vbFile.get(out tmp,recordNumber);
1019 else if ( value is long) {
1021 vbFile.get(out tmp,recordNumber);
1024 else if ( value is float) {
1026 vbFile.get(out tmp,recordNumber);
1029 else if ( value is double) {
1031 vbFile.get(out tmp,recordNumber);
1034 else if ( value is Decimal) {
1036 vbFile.get(out tmp,recordNumber);
1039 else if ( value is DateTime) {
1041 vbFile.get(out tmp,recordNumber);
1044 else if (type.IsArray) {
1045 // need to figure out how to convert from Object& to Array&
1046 // vbFile.get(out value, recordNumber,true,false);
1048 throw new NotImplementedException();
1051 throw new NotSupportedException();
1054 public static void FileGet(int fileNumber,
1056 [System.Runtime.InteropServices.Optional]
1057 [System.ComponentModel.DefaultValue(-1)] long recordNumber)
1060 checkRecordNumber(recordNumber,true);
1061 VBFile vbFile = getVBFile(fileNumber);
1062 vbFile.get(out value,recordNumber);
1066 public static void FileGet(int fileNumber, out ValueType value, long recordNumber)
1068 throw new NotImplementedException();
1071 public static void FileGet(int fileNumber,
1073 [System.Runtime.InteropServices.Optional]
1074 [System.ComponentModel.DefaultValue(-1)] long recordNumber,
1075 [System.Runtime.InteropServices.Optional]
1076 [System.ComponentModel.DefaultValue(false)] bool arrayIsDynamic,
1077 [System.Runtime.InteropServices.Optional]
1078 [System.ComponentModel.DefaultValue(false)] bool stringIsFixedLength)
1082 checkRecordNumber(recordNumber,true);
1083 VBFile vbFile = getVBFile(fileNumber);
1084 vbFile.get(ref value,recordNumber,arrayIsDynamic,stringIsFixedLength);
1087 public static void FilePutObject(int fileNumber,
1089 [System.Runtime.InteropServices.Optional]
1090 [System.ComponentModel.DefaultValue(-1)] long recordNumber)
1093 checkRecordNumber(recordNumber,true);
1094 VBFile vbFile = getVBFile(fileNumber);
1095 Type type = value.GetType();
1096 if(value is string || value == null)
1097 vbFile.put((String)value,recordNumber,false);
1098 else if( value is bool) {
1099 vbFile.put((bool)value, recordNumber);
1101 else if( value is char) {
1102 vbFile.put((char)value,recordNumber);
1104 else if( value is byte) {
1105 vbFile.put((byte)value, recordNumber);
1107 else if( value is short) {
1108 vbFile.put((short)value, recordNumber);
1110 else if( value is int) {
1111 vbFile.put((int)value, recordNumber);
1113 else if( value is long) {
1114 vbFile.put((long)value, recordNumber);
1116 else if( value is float) {
1117 vbFile.put((float)value, recordNumber);
1119 else if( value is double) {
1120 vbFile.put((double)value, recordNumber);
1122 else if( value is Decimal) {
1123 vbFile.put((Decimal)value,recordNumber);
1125 else if( value is DateTime) {
1126 vbFile.put((DateTime)value, recordNumber);
1128 else if(type.IsArray) {
1129 vbFile.put(value,recordNumber,true,false);
1132 throw new NotSupportedException();
1137 private const string obsoleteMsg1 = "Use FilePutObject to write Object types, or";
1138 private const string obsoleteMsg2 = "coerce FileNumber and RecordNumber to Integer for writing non-Object types";
1139 private const string obsoleteMsg = obsoleteMsg1 + obsoleteMsg2;
1141 [System.ObsoleteAttribute(obsoleteMsg, false)]
1142 public static void FilePut(Object FileNumber,
1144 [System.Runtime.InteropServices.Optional]
1145 [System.ComponentModel.DefaultValue(-1)] System.Object RecordNumber)
1147 throw new ArgumentException(Utils.GetResourceString("UseFilePutObject"));
1151 public static void FilePut(int FileNumber,
1153 [System.Runtime.InteropServices.Optional]
1154 [System.ComponentModel.DefaultValue(-1)] System.Int64 RecordNumber)
1157 throw new NotImplementedException();
1160 public static void FilePut(int fileNumber,
1162 [System.Runtime.InteropServices.Optional]
1163 [System.ComponentModel.DefaultValue(-1)] long recordNumber,
1164 [System.Runtime.InteropServices.Optional]
1165 [System.ComponentModel.DefaultValue(false)] bool arrayIsDynamic,
1166 [System.Runtime.InteropServices.Optional]
1167 [System.ComponentModel.DefaultValue(false)] bool stringIsFixedLength)
1169 checkRecordNumber(recordNumber,true);
1170 VBFile vbFile = getVBFile(fileNumber);
1171 vbFile.put(value,recordNumber,arrayIsDynamic,stringIsFixedLength);
1174 public static void FilePut(int fileNumber,
1176 [System.Runtime.InteropServices.Optional]
1177 [System.ComponentModel.DefaultValue(-1)] long recordNumber)
1180 checkRecordNumber(recordNumber,true);
1181 VBFile vbFile = getVBFile(fileNumber);
1182 vbFile.put(value,recordNumber);
1185 public static void Input(int fileNumber, ref bool Value)
1188 VBFile vbFile = getVBFile(fileNumber);
1189 vbFile.Input(out Value);
1192 public static void Input(int fileNumber, ref byte Value)
1194 VBFile vbFile = getVBFile(fileNumber);
1195 vbFile.Input(out Value);
1198 public static void Input(int fileNumber, ref short Value)
1200 VBFile vbFile = getVBFile(fileNumber);
1201 vbFile.Input(out Value);
1204 public static void Input(int fileNumber, ref int Value)
1206 VBFile vbFile = getVBFile(fileNumber);
1207 vbFile.Input(out Value);
1210 public static void Input(int fileNumber, ref long Value)
1212 VBFile vbFile = getVBFile(fileNumber);
1213 vbFile.Input(out Value);
1216 public static void Input(int fileNumber, ref char Value)
1218 VBFile vbFile = getVBFile(fileNumber);
1219 vbFile.Input(out Value);
1222 public static void Input(int fileNumber, ref float Value)
1224 VBFile vbFile = getVBFile(fileNumber);
1225 vbFile.Input(out Value);
1228 public static void Input(int fileNumber, ref double Value)
1230 VBFile vbFile = getVBFile(fileNumber);
1231 vbFile.Input(out Value);
1234 public static void Input(int fileNumber, ref Decimal Value)
1236 VBFile vbFile = getVBFile(fileNumber);
1237 vbFile.Input(out Value);
1240 public static void Input(int fileNumber, ref DateTime Value)
1242 VBFile vbFile = getVBFile(fileNumber);
1243 vbFile.Input(out Value);
1246 public static void Input(int fileNumber, ref string Value)
1248 VBFile vbFile = getVBFile(fileNumber);
1249 vbFile.Input(out Value);
1253 // public static void Input$V$FileSystem$ILSystem_Object$$$(int fileNumber, ObjectRefWrapper Value)
1255 // VBFile vbFile = getVBFile(fileNumber);
1256 // vbFile.Input(Value,false);
1260 public static void Lock(int fileNumber)
1262 throw new NotImplementedException("The method Lock in class FileSystem is not supported");
1266 public static void Lock(int FileNumber, long Record)
1268 throw new NotImplementedException("The method Lock in class FileSystem is not supported");
1272 public static void Lock(int FileNumber, long FromRecord, long ToRecord)
1274 throw new NotImplementedException("The method Lock in class FileSystem is not supported");
1278 public static void Unlock(int FileNumber)
1280 throw new NotImplementedException("The method Unlock in class FileSystem is not supported");
1284 public static void Unlock(int FileNumber, long Record)
1286 throw new NotImplementedException("The method Unlock in class FileSystem is not supported");
1290 public static void Unlock(int FileNumber, long FromRecord, long ToRecord)
1292 throw new NotImplementedException("The method Unlock in class FileSystem is not supported");
1295 public static void FileWidth(int fileNumber, int RecordWidth)
1297 VBFile vbFile = getVBFile(fileNumber);
1298 vbFile.width(fileNumber,RecordWidth);
1301 public static TabInfo TAB()
1303 return new TabInfo((short) - 1);
1306 public static TabInfo TAB(short Column)
1308 return new TabInfo(Column);
1311 public static SpcInfo SPC(short Count)
1313 return new SpcInfo(Count);
1316 public static OpenMode FileAttr(int fileNumber)
1318 VBFile vbFile = getVBFile(fileNumber);
1319 return (OpenMode) vbFile.getMode();
1323 class VBStreamWriter : StreamWriter
1328 public VBStreamWriter(string fileName):base(fileName)
1332 public VBStreamWriter(string fileName, bool append):base(fileName, append)
1337 //TODO: FileFilters from Mainsoft code