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
47 private FileSystem () {}
49 private static Hashtable _fileNameIdMap = new Hashtable();
50 private static Hashtable _openFilesMap = new Hashtable();
51 private static Hashtable _randomRecordLength = new Hashtable();
53 static String _pattern;
54 static int _fileAttrs;
55 private static int _fileIndex;
56 private static FileInfo[] _files;
57 private static bool _isEndOfFiles = true;
60 public static void Reset()
64 ICollection s = _openFilesMap.Keys;
66 int [] iArr = new int[s.Count];
71 public static void FileClose(int[] fileNumbers)
74 if (fileNumbers.Length == 0)
76 ICollection keySet = FileSystem._openFilesMap.Keys;
77 iArr = new int[keySet.Count];
78 keySet.CopyTo(iArr, 0);
82 iArr = new int[fileNumbers.Length];
83 for (int i = 0; i < fileNumbers.Length; i++)
84 iArr[i] = fileNumbers[i];
89 private static void close(int [] keys)
93 for (int i = 0; i < keys.Length; i++)
95 if (keys[i] < 0 || keys[i] > 255)
97 ExceptionUtils.VbMakeException(
98 VBErrors.IllegalFuncCall);
99 throw new ArgumentOutOfRangeException();
101 obj = _openFilesMap[keys[i]];
105 String message = VBUtils.GetResourceString(52);
106 throw (IOException)VBUtils.VBException(new IOException(message), 52);
108 String fileName = null;
111 ((VBFile)obj).closeFile();
112 fileName = ((VBFile)obj).getFullPath();
116 _openFilesMap.Remove(keys[i]);
117 _fileNameIdMap.Remove(fileName);
121 public static void FileOpen(
125 [System.Runtime.InteropServices.Optional]
126 [System.ComponentModel.DefaultValue(-1)] OpenAccess access,
127 [System.Runtime.InteropServices.Optional]
128 [System.ComponentModel.DefaultValue(-1)] OpenShare share,
129 [System.Runtime.InteropServices.Optional]
130 [System.ComponentModel.DefaultValue(-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 _fileNameIdMap.Add(Path.GetFullPath(string.Intern(fileName)),fileNumber);
164 public static void FilePut(int fileNumber,
166 [System.Runtime.InteropServices.Optional]
167 [System.ComponentModel.DefaultValue(-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 [System.Runtime.InteropServices.Optional]
178 [System.ComponentModel.DefaultValue(-1)] long recordNumber)
180 checkRecordNumber(recordNumber,false);
181 VBFile vbFile = getVBFile(fileNumber);
182 vbFile.put(value,recordNumber);
185 public static void FilePut(int fileNumber,
187 [System.Runtime.InteropServices.Optional]
188 [System.ComponentModel.DefaultValue(-1)] long recordNumber)
190 checkRecordNumber(recordNumber,false);
191 VBFile vbFile = getVBFile(fileNumber);
192 vbFile.put(value,recordNumber);
196 public static void FilePut(int fileNumber,
198 [System.Runtime.InteropServices.Optional]
199 [System.ComponentModel.DefaultValue(-1)] long recordNumber)
201 checkRecordNumber(recordNumber,false);
202 VBFile vbFile = getVBFile(fileNumber);
203 vbFile.put(value,recordNumber);
206 public static void FilePut(int fileNumber,
208 [System.Runtime.InteropServices.Optional]
209 [System.ComponentModel.DefaultValue(-1)] long recordNumber)
211 checkRecordNumber(recordNumber,false);
212 VBFile vbFile = getVBFile(fileNumber);
213 vbFile.put(value,recordNumber);
216 public static void FilePut(int fileNumber,
218 [System.Runtime.InteropServices.Optional]
219 [System.ComponentModel.DefaultValue(-1)] long recordNumber)
221 checkRecordNumber(recordNumber,false);
222 VBFile vbFile = getVBFile(fileNumber);
223 vbFile.put(value,recordNumber);
227 public static void FilePut(int fileNumber,
229 [System.Runtime.InteropServices.Optional]
230 [System.ComponentModel.DefaultValue(-1)] long recordNumber)
232 checkRecordNumber(recordNumber,false);
233 VBFile vbFile = getVBFile(fileNumber);
234 vbFile.put(value,recordNumber);
237 public static void FilePut(int fileNumber,
239 [System.Runtime.InteropServices.Optional]
240 [System.ComponentModel.DefaultValue(-1)] long recordNumber)
242 checkRecordNumber(recordNumber,false);
243 VBFile vbFile = getVBFile(fileNumber);
244 vbFile.put(value,recordNumber);
247 public static void FilePut(int fileNumber,
249 [System.Runtime.InteropServices.Optional]
250 [System.ComponentModel.DefaultValue(-1)] long recordNumber,
251 [System.Runtime.InteropServices.Optional]
252 [System.ComponentModel.DefaultValue(false)] bool stringIsFixedLength)
254 checkRecordNumber(recordNumber,true);
255 VBFile vbFile = getVBFile(fileNumber);
256 vbFile.put(value,recordNumber,stringIsFixedLength);
259 public static void FilePut(int fileNumber,
261 [System.Runtime.InteropServices.Optional]
262 [System.ComponentModel.DefaultValue(-1)] long recordNumber)
264 checkRecordNumber(recordNumber,false);
265 VBFile vbFile = getVBFile(fileNumber);
266 vbFile.put(value,recordNumber);
269 public static void FileGet(int fileNumber,
271 [System.Runtime.InteropServices.Optional]
272 [System.ComponentModel.DefaultValue(-1)] long recordNumber)
275 checkRecordNumber(recordNumber,false);
276 VBFile vbFile = getVBFile(fileNumber);
277 vbFile.get(out value,recordNumber);
280 private static void checkRecordNumber(long recordNumber,bool throwArgExc)
282 if ((recordNumber < 1) && (recordNumber != -1))
285 throw (IOException) ExceptionUtils.VbMakeException(
286 new ArgumentException(
287 Utils.GetResourceString(
288 "Argument_InvalidValue1",
290 VBErrors.BadRecordNum);
293 ExceptionUtils.VbMakeException(VBErrors.BadRecordNum);
294 throw new ArgumentException(
295 Utils.GetResourceString(
296 "Argument_InvalidValue1",
303 private static VBFile getVBFile(int fileNumber)
305 if ((fileNumber < 1) || (fileNumber > 255))
306 throw (IOException) ExceptionUtils.VbMakeException(
307 VBErrors.BadFileNameOrNumber);
308 Object obj = _openFilesMap[fileNumber];
310 throw (IOException) ExceptionUtils.VbMakeException(
311 VBErrors.BadFileNameOrNumber);
315 private static VBFile getVBFile(String pathName)
317 object o = _fileNameIdMap[pathName];
318 int fileNumber = o == null ? 0 : (int) o ;
320 throw (IOException) ExceptionUtils.VbMakeException(
321 VBErrors.BadFileNameOrNumber);
322 Object obj = _openFilesMap[fileNumber];
324 throw (IOException) ExceptionUtils.VbMakeException(
325 VBErrors.BadFileNameOrNumber);
329 public static void FileGet(
332 [System.Runtime.InteropServices.Optional]
333 [System.ComponentModel.DefaultValue(-1)] long recordNumber)
336 checkRecordNumber(recordNumber,false);
337 VBFile vbFile = getVBFile(fileNumber);
338 vbFile.get(out value,recordNumber);
341 public static void FileGet(
344 [System.Runtime.InteropServices.Optional]
345 [System.ComponentModel.DefaultValue(-1)] long recordNumber)
348 checkRecordNumber(recordNumber,false);
349 VBFile vbFile = getVBFile(fileNumber);
350 vbFile.get(out value,recordNumber);
353 public static void FileGet(
356 [System.Runtime.InteropServices.Optional]
357 [System.ComponentModel.DefaultValue(-1)] long recordNumber)
361 checkRecordNumber(recordNumber,false);
362 VBFile vbFile = getVBFile(fileNumber);
363 vbFile.get(out value,recordNumber);
367 public static void FileGet(int fileNumber,
369 [System.Runtime.InteropServices.Optional]
370 [System.ComponentModel.DefaultValue(-1)] long recordNumber)
372 checkRecordNumber(recordNumber,false);
373 VBFile vbFile = getVBFile(fileNumber);
374 vbFile.get(out value,recordNumber);
377 public static void FileGet(int fileNumber,
379 [System.Runtime.InteropServices.Optional]
380 [System.ComponentModel.DefaultValue(-1)] long recordNumber)
382 checkRecordNumber(recordNumber,false);
383 VBFile vbFile = getVBFile(fileNumber);
384 vbFile.get(out value,recordNumber);
387 public static void FileGet(int fileNumber,
389 [System.Runtime.InteropServices.Optional]
390 [System.ComponentModel.DefaultValue(-1)] long recordNumber)
392 checkRecordNumber(recordNumber,false);
393 VBFile vbFile = getVBFile(fileNumber);
394 vbFile.get(out value,recordNumber);
397 public static void FileGet(int fileNumber,
399 [System.Runtime.InteropServices.Optional]
400 [System.ComponentModel.DefaultValue(-1)] long recordNumber)
403 checkRecordNumber(recordNumber,false);
404 VBFile vbFile = getVBFile(fileNumber);
405 vbFile.get(out value,recordNumber);
408 public static void FileGet(int fileNumber,
410 [System.Runtime.InteropServices.Optional]
411 [System.ComponentModel.DefaultValue(-1)] long recordNumber)
414 checkRecordNumber(recordNumber,false);
415 VBFile vbFile = getVBFile(fileNumber);
416 vbFile.get(out value,recordNumber);
419 public static void FileGet(int fileNumber,
421 [System.Runtime.InteropServices.Optional]
422 [System.ComponentModel.DefaultValue(-1)] long recordNumber,
423 [System.Runtime.InteropServices.Optional]
424 [System.ComponentModel.DefaultValue(false)] bool bIgnored)
426 checkRecordNumber(recordNumber,true);
427 VBFile vbFile = getVBFile(fileNumber);
428 vbFile.get(ref value,recordNumber,bIgnored);
431 public static void FileGet(int fileNumber,
433 [System.Runtime.InteropServices.Optional]
434 [System.ComponentModel.DefaultValue(-1)] long recordNumber)
436 checkRecordNumber(recordNumber,false);
437 VBFile vbFile = getVBFile(fileNumber);
438 vbFile.get(ref value,recordNumber);
441 public static long Seek(int fileNumber)
443 VBFile vbFile = getVBFile(fileNumber);
444 return vbFile.seek();
447 public static void Seek(int fileNumber, long position)
449 VBFile vbFile = getVBFile(fileNumber);
450 vbFile.seek(position);
454 public static long Loc(int fileNumber)
456 VBFile vbFile = getVBFile(fileNumber);
457 return vbFile.getPosition();
460 public static long LOF(int fileNumber)
462 VBFile vbFile = getVBFile(fileNumber);
463 return vbFile.getLength();
466 public static void Input(int fileNumber, ref Object value)
468 VBFile vbFile = getVBFile(fileNumber);
475 vbFile.Input(out tmp);
478 else if (value is byte) {
480 vbFile.Input(out tmp);
483 else if (value is char) {
485 vbFile.Input(out tmp);
488 else if (value is double) {
490 vbFile.Input(out tmp);
493 else if (value is short) {
495 vbFile.Input(out tmp);
498 else if (value is int) {
500 vbFile.Input(out tmp);
503 else if (value is long) {
505 vbFile.Input(out tmp);
508 else if (value is float) {
510 vbFile.Input(out tmp);
513 else if (value is Decimal) {
515 vbFile.Input(out tmp);
518 else if (value is string)
519 vbFile.Input((string)value);
520 // Come back to it later
521 // else if (type.get_IsByRef())
522 // vbFile.Input((ObjectRefWrapper)value[i],false);
527 public static String InputString(int fileNumber, int count)
530 throw (ArgumentException)ExceptionUtils.VbMakeException(VBErrors.IllegalFuncCall);
531 VBFile vbFile = getVBFile(fileNumber);
532 if (vbFile.getLength()- vbFile.getPosition() < count)
533 throw (EndOfStreamException)ExceptionUtils.VbMakeException(VBErrors.EndOfFile);
534 return vbFile.InputString(count);
537 public static String LineInput(int fileNumber)
539 VBFile vbFile = getVBFile(fileNumber);
542 throw new EndOfStreamException("Input past end of file.");
544 return vbFile.readLine();
547 public static void Print(int fileNumber, Object[] output)
549 VBFile vbFile = getVBFile(fileNumber);
550 vbFile.print(output);
553 public static void PrintLine(int fileNumber)
555 VBFile vbFile = getVBFile(fileNumber);
556 vbFile.printLine(null);
559 public static void PrintLine(int fileNumber, Object[] output)
561 VBFile vbFile = getVBFile(fileNumber);
562 vbFile.printLine(output);
565 public static void Write(int fileNumber, Object[] output)
567 VBFile vbFile = getVBFile(fileNumber);
568 vbFile.write(output);
571 public static void WriteLine(int fileNumber, Object[] output)
573 VBFile vbFile = getVBFile(fileNumber);
574 vbFile.writeLine(output);
577 public static void Rename(String oldPath, String newPath)
579 FileInfo file = new FileInfo(newPath);
581 throw (IOException) ExceptionUtils.VbMakeException(
582 VBErrors.FileAlreadyExists);
585 Directory.Move(oldPath, newPath);
586 }catch (DirectoryNotFoundException e){
587 throw (ArgumentException)ExceptionUtils.VbMakeException(VBErrors.IllegalFuncCall);
591 public static void FileCopy(String source, String destination)
595 if ((source == null) || (source.Length == 0))
597 ExceptionUtils.VbMakeException(VBErrors.BadFileNameOrNumber);
598 throw new ArgumentException(
599 Utils.GetResourceString("Argument_PathNullOrEmpty"));
602 if ((destination == null) || (destination.Length == 0))
604 ExceptionUtils.VbMakeException(VBErrors.BadFileNameOrNumber);
605 throw new ArgumentException(
606 Utils.GetResourceString("Argument_PathNullOrEmpty"));
609 FileInfo f = new FileInfo(source);
611 throw (FileNotFoundException) ExceptionUtils.VbMakeException(
612 VBErrors.FileNotFound);
613 if (_fileNameIdMap[source] != null)
614 throw (IOException) ExceptionUtils.VbMakeException(
615 VBErrors.FileAlreadyOpen);
616 int lastIndex = destination.LastIndexOf('/');
619 dir = new DirectoryInfo(".");
621 dir = new DirectoryInfo(destination.Substring(0,lastIndex));
625 ExceptionUtils.VbMakeException(VBErrors.FileAlreadyOpen);
626 throw new DirectoryNotFoundException();
629 // the file name length is 0
630 if (destination.Length == lastIndex +1)
632 throw (IOException)ExceptionUtils.VbMakeException(VBErrors.FileAlreadyOpen);
636 f = new FileInfo(destination);
638 throw (IOException) ExceptionUtils.VbMakeException(
639 VBErrors.FileAlreadyExists);
640 File.Copy(source, destination);
643 public static void MkDir(String path)
645 if ((path == null) || (path.Length == 0))
647 ExceptionUtils.VbMakeException(VBErrors.BadFileNameOrNumber);
648 throw new ArgumentException(
649 Utils.GetResourceString("Argument_PathNullOrEmpty"));
651 if (Directory.Exists(path))
652 throw (IOException) ExceptionUtils.VbMakeException(
653 VBErrors.PathFileAccess);
655 Directory.CreateDirectory(path);
658 public static void Kill(String pathName)
660 if (_fileNameIdMap[pathName] != null)
661 throw (IOException) ExceptionUtils.VbMakeException(
662 VBErrors.FileAlreadyOpen);
663 if (!File.Exists(pathName))
664 throw (IOException) ExceptionUtils.VbMakeException(
665 VBErrors.FileNotFound);
666 File.Delete(pathName);
669 public static void RmDir(String pathName)
671 if (pathName == null || pathName.Length == 0 )
673 ExceptionUtils.VbMakeException(VBErrors.BadFileNameOrNumber);
674 throw new ArgumentException(pathName);
676 DirectoryInfo dir = new DirectoryInfo(pathName);
679 ExceptionUtils.VbMakeException(VBErrors.PathNotFound);
680 throw new DirectoryNotFoundException();
682 if (dir.GetFiles().Length != 0)
683 throw (IOException) ExceptionUtils.VbMakeException(
684 VBErrors.PathFileAccess);
685 Directory.Delete(pathName);
688 public static FileAttribute GetAttr(String pathName)
690 if (pathName == null || pathName.Length == 0 ||
691 pathName.IndexOf('*') != -1 || pathName.IndexOf('?') != -1)
693 throw (IOException)ExceptionUtils.VbMakeException(VBErrors.BadFileNameOrNumber);
695 // File f = new File(pathName);
697 // throw (FileNotFoundException)
698 // ExceptionUtils.VbMakeException(VBErrors.FileNotFound);
699 return (FileAttribute) File.GetAttributes(pathName);
702 public static void SetAttr(String pathName, FileAttribute fileAttr)
704 if (pathName == null || pathName.Length == 0 ||
705 pathName.IndexOf('*') != -1 || pathName.IndexOf('?') != -1)
707 throw (ArgumentException)ExceptionUtils.VbMakeException(VBErrors.IllegalFuncCall);
709 FileInfo f = new FileInfo(pathName);
710 if (!f.Directory.Exists)
712 ExceptionUtils.VbMakeException(VBErrors.PathNotFound);
713 throw new DirectoryNotFoundException();
717 throw (FileNotFoundException)
718 ExceptionUtils.VbMakeException(VBErrors.FileNotFound);
723 File.SetAttributes(pathName, (FileAttributes)fileAttr);
725 catch (ArgumentException e)
727 throw (ArgumentException) ExceptionUtils.VbMakeException(
728 VBErrors.IllegalFuncCall);
730 catch (DirectoryNotFoundException ex)
732 ExceptionUtils.VbMakeException(VBErrors.PathNotFound);
735 catch (FileNotFoundException ex)
737 throw (FileNotFoundException)
738 ExceptionUtils.VbMakeException(VBErrors.FileNotFound);
744 public static /*synchronized*/ String Dir(String pathName)
746 return Dir(pathName, 0);
749 public static /*synchronized*/ String Dir(String pathName,
750 [System.Runtime.InteropServices.Optional]
751 [System.ComponentModel.DefaultValue(0)]
752 FileAttribute fileAttribute)
758 _fileAttrs = (int)fileAttribute;
760 if (pathName == null || pathName.Equals(""))
765 if (FileAttribute.Volume == fileAttribute)
769 int lastBabkSlashInx = pathName.LastIndexOf('\\');
770 int lastSlashInx = pathName.LastIndexOf('/');
771 int maxIndex = (lastSlashInx>lastBabkSlashInx)?lastSlashInx:lastBabkSlashInx;
772 String dir = pathName.Substring(0, maxIndex + 1);
773 String fileName = pathName.Substring(1 + maxIndex);
774 if (fileName == null || fileName.Length == 0)
777 // int astricsInx = fileName.indexOf('*');
778 // int questionInx = fileName.indexOf('?');
781 DirectoryInfo directory = new DirectoryInfo(dir);
782 // java.io.File directory = new java.io.File(dir);
783 // java.io.File file;
784 if (!directory.Exists)
786 // path not found - return empty string
790 // if (astricsInx == -1 && questionInx == -1)
792 // pattern = fileName;
796 // pattern = Strings.Replace(fileName, ".", "\\.", 1, -1, CompareMethod.Binary);
797 // pattern = Strings.Replace(pattern, "*", ".*", 1, -1, CompareMethod.Binary);
798 // pattern = Strings.Replace(pattern, "?", ".?", 1, -1, CompareMethod.Binary);
802 // _pattern = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
804 _files = directory.GetFiles(_pattern);
806 if (_files == null || _files.Length == 0)
808 DirectoryInfo[] dirs = directory.GetDirectories(_pattern);
809 if (dirs == null || dirs.Length == 0)
813 answer = dirs[0].Name;
817 answer = _files[0].Name;
821 _isEndOfFiles = false;
828 public static /*synchronized*/ String Dir()
831 if (_files == null || _isEndOfFiles)
832 throw new /*Illegal*/ArgumentException("no path has been initiated");
834 if (_fileIndex < _files.Length)
836 name = _files[_fileIndex].Name;
841 _isEndOfFiles = true;
848 public static DateTime FileDateTime(String pathName)
850 if (pathName == null || pathName.Length == 0 ||
851 pathName.IndexOf('*') != -1 || pathName.IndexOf('?') != -1)
853 ExceptionUtils.VbMakeException(VBErrors.BadFileNameOrNumber);
854 throw new ArgumentException(
855 VBUtils.GetResourceString(
856 "Argument_InvalidValue1",
859 FileInfo f = new FileInfo(pathName);
862 DirectoryInfo d = new DirectoryInfo(pathName);
864 throw (FileNotFoundException)
865 ExceptionUtils.VbMakeException(VBErrors.FileNotFound);
866 return d.LastWriteTime;
868 return f.LastWriteTime;
871 public static long FileLen(String pathName)
873 FileInfo f = new FileInfo(pathName);
875 throw new FileNotFoundException(
876 "file not exists: " + pathName);
882 internal static String SPC(int count)
884 StringBuilder sb = new StringBuilder(count);
885 for (int i = 0; i < count; i++)
888 return sb.ToString();
891 public static int FreeFile()
893 ICollection s = _openFilesMap.Keys;
898 int [] keyArr = new int[s.Count];
904 for (; i < keyArr.Length - 1; i++)
906 if ((keyArr[i]+ 1) < keyArr[i + 1])
910 int retVal = keyArr[i]+ 1;
914 String message = VBUtils.GetResourceString(67);
915 throw (IOException)VBUtils.VBException(
916 new IOException(message),
924 public static bool EOF(int fileNumber)
927 VBFile vbFile = getVBFile(fileNumber);
928 return vbFile.isEndOfFile();
931 // check if a specific number is free
932 private static bool isFileNumberFree(int fileNumber)
934 return !_openFilesMap.ContainsKey(fileNumber);
937 [MonoTODO("If path is another drive, it should change the default folder for that drive, but not switch to it.")]
938 public static void ChDir (string Path)
940 if ((Path=="") || (Path==null))
941 throw new ArgumentException (Utils.GetResourceString ("Argument_PathNullOrEmpty"));
943 Environment.CurrentDirectory = Path;
946 throw new FileNotFoundException (Utils.GetResourceString ("FileSystem_PathNotFound1", Path));
951 public static void ChDrive(char Drive)
953 Drive = char.ToUpper(Drive, CultureInfo.InvariantCulture);
954 if ((Drive < 65) || (Drive > 90))
956 throw new ArgumentException(
957 Utils.GetResourceString("Argument_InvalidValue1", "Drive"));
959 Directory.SetCurrentDirectory(String.Concat(StringType.FromChar(Drive), StringType.FromChar(Path.VolumeSeparatorChar)));
962 public static void ChDrive(String Drive)
964 if (Drive != null && Drive.Length != 0)
965 FileSystem.ChDrive(Drive[0]);
968 public static String CurDir()
970 return Environment.CurrentDirectory;
973 public static String CurDir(char Drive)
975 return Directory.GetCurrentDirectory();
978 public static void FileGetObject(int fileNumber,
980 [System.Runtime.InteropServices.Optional]
981 [System.ComponentModel.DefaultValue(-1)] long recordNumber)
985 checkRecordNumber(recordNumber,true);
986 VBFile vbFile = getVBFile(fileNumber);
988 Type type = value.GetType();
990 if (type == null || value is string) {
992 vbFile.get(ref tmp, recordNumber, false);
995 else if ( value is bool) {
997 vbFile.get(out tmp,recordNumber);
1000 else if ( value is char) {
1002 vbFile.get(out tmp,recordNumber);
1005 else if ( value is byte) {
1007 vbFile.get(out tmp,recordNumber);
1010 else if ( value is short) {
1012 vbFile.get(out tmp,recordNumber);
1015 else if ( value is int) {
1017 vbFile.get(out tmp,recordNumber);
1020 else if ( value is long) {
1022 vbFile.get(out tmp,recordNumber);
1025 else if ( value is float) {
1027 vbFile.get(out tmp,recordNumber);
1030 else if ( value is double) {
1032 vbFile.get(out tmp,recordNumber);
1035 else if ( value is Decimal) {
1037 vbFile.get(out tmp,recordNumber);
1040 else if ( value is DateTime) {
1042 vbFile.get(out tmp,recordNumber);
1045 else if (type.IsArray) {
1046 // need to figure out how to convert from Object& to Array&
1047 // vbFile.get(out value, recordNumber,true,false);
1049 throw new NotImplementedException();
1052 throw new NotSupportedException();
1055 public static void FileGet(int fileNumber,
1057 [System.Runtime.InteropServices.Optional]
1058 [System.ComponentModel.DefaultValue(-1)] long recordNumber)
1061 checkRecordNumber(recordNumber,true);
1062 VBFile vbFile = getVBFile(fileNumber);
1063 vbFile.get(out value,recordNumber);
1067 public static void FileGet(int fileNumber, out ValueType value, long recordNumber)
1069 throw new NotImplementedException();
1072 public static void FileGet(int fileNumber,
1074 [System.Runtime.InteropServices.Optional]
1075 [System.ComponentModel.DefaultValue(-1)] long recordNumber,
1076 [System.Runtime.InteropServices.Optional]
1077 [System.ComponentModel.DefaultValue(false)] bool arrayIsDynamic,
1078 [System.Runtime.InteropServices.Optional]
1079 [System.ComponentModel.DefaultValue(false)] bool stringIsFixedLength)
1083 checkRecordNumber(recordNumber,true);
1084 VBFile vbFile = getVBFile(fileNumber);
1085 vbFile.get(ref value,recordNumber,arrayIsDynamic,stringIsFixedLength);
1088 public static void FilePutObject(int fileNumber,
1090 [System.Runtime.InteropServices.Optional]
1091 [System.ComponentModel.DefaultValue(-1)] long recordNumber)
1094 checkRecordNumber(recordNumber,true);
1095 VBFile vbFile = getVBFile(fileNumber);
1096 Type type = value.GetType();
1097 if(value is string || value == null)
1098 vbFile.put((String)value,recordNumber,false);
1099 else if( value is bool) {
1100 vbFile.put((bool)value, recordNumber);
1102 else if( value is char) {
1103 vbFile.put((char)value,recordNumber);
1105 else if( value is byte) {
1106 vbFile.put((byte)value, recordNumber);
1108 else if( value is short) {
1109 vbFile.put((short)value, recordNumber);
1111 else if( value is int) {
1112 vbFile.put((int)value, recordNumber);
1114 else if( value is long) {
1115 vbFile.put((long)value, recordNumber);
1117 else if( value is float) {
1118 vbFile.put((float)value, recordNumber);
1120 else if( value is double) {
1121 vbFile.put((double)value, recordNumber);
1123 else if( value is Decimal) {
1124 vbFile.put((Decimal)value,recordNumber);
1126 else if( value is DateTime) {
1127 vbFile.put((DateTime)value, recordNumber);
1129 else if(type.IsArray) {
1130 vbFile.put(value,recordNumber,true,false);
1133 throw new NotSupportedException();
1138 private const string obsoleteMsg1 = "Use FilePutObject to write Object types, or";
1139 private const string obsoleteMsg2 = "coerce FileNumber and RecordNumber to Integer for writing non-Object types";
1140 private const string obsoleteMsg = obsoleteMsg1 + obsoleteMsg2;
1142 [System.ObsoleteAttribute(obsoleteMsg, false)]
1143 public static void FilePut(Object FileNumber,
1145 [System.Runtime.InteropServices.Optional]
1146 [System.ComponentModel.DefaultValue(-1)] System.Object RecordNumber)
1148 throw new ArgumentException(Utils.GetResourceString("UseFilePutObject"));
1152 public static void FilePut(int FileNumber,
1154 [System.Runtime.InteropServices.Optional]
1155 [System.ComponentModel.DefaultValue(-1)] System.Int64 RecordNumber)
1158 throw new NotImplementedException();
1161 public static void FilePut(int fileNumber,
1163 [System.Runtime.InteropServices.Optional]
1164 [System.ComponentModel.DefaultValue(-1)] long recordNumber,
1165 [System.Runtime.InteropServices.Optional]
1166 [System.ComponentModel.DefaultValue(false)] bool arrayIsDynamic,
1167 [System.Runtime.InteropServices.Optional]
1168 [System.ComponentModel.DefaultValue(false)] bool stringIsFixedLength)
1170 checkRecordNumber(recordNumber,true);
1171 VBFile vbFile = getVBFile(fileNumber);
1172 vbFile.put(value,recordNumber,arrayIsDynamic,stringIsFixedLength);
1175 public static void FilePut(int fileNumber,
1177 [System.Runtime.InteropServices.Optional]
1178 [System.ComponentModel.DefaultValue(-1)] long recordNumber)
1181 checkRecordNumber(recordNumber,true);
1182 VBFile vbFile = getVBFile(fileNumber);
1183 vbFile.put(value,recordNumber);
1186 public static void Input(int fileNumber, ref bool Value)
1189 VBFile vbFile = getVBFile(fileNumber);
1190 vbFile.Input(out Value);
1193 public static void Input(int fileNumber, ref byte Value)
1195 VBFile vbFile = getVBFile(fileNumber);
1196 vbFile.Input(out Value);
1199 public static void Input(int fileNumber, ref short Value)
1201 VBFile vbFile = getVBFile(fileNumber);
1202 vbFile.Input(out Value);
1205 public static void Input(int fileNumber, ref int Value)
1207 VBFile vbFile = getVBFile(fileNumber);
1208 vbFile.Input(out Value);
1211 public static void Input(int fileNumber, ref long Value)
1213 VBFile vbFile = getVBFile(fileNumber);
1214 vbFile.Input(out Value);
1217 public static void Input(int fileNumber, ref char Value)
1219 VBFile vbFile = getVBFile(fileNumber);
1220 vbFile.Input(out Value);
1223 public static void Input(int fileNumber, ref float Value)
1225 VBFile vbFile = getVBFile(fileNumber);
1226 vbFile.Input(out Value);
1229 public static void Input(int fileNumber, ref double Value)
1231 VBFile vbFile = getVBFile(fileNumber);
1232 vbFile.Input(out Value);
1235 public static void Input(int fileNumber, ref Decimal Value)
1237 VBFile vbFile = getVBFile(fileNumber);
1238 vbFile.Input(out Value);
1241 public static void Input(int fileNumber, ref DateTime Value)
1243 VBFile vbFile = getVBFile(fileNumber);
1244 vbFile.Input(out Value);
1247 public static void Input(int fileNumber, ref string Value)
1249 VBFile vbFile = getVBFile(fileNumber);
1250 vbFile.Input(out Value);
1254 // public static void Input$V$FileSystem$ILSystem_Object$$$(int fileNumber, ObjectRefWrapper Value)
1256 // VBFile vbFile = getVBFile(fileNumber);
1257 // vbFile.Input(Value,false);
1261 public static void Lock(int fileNumber)
1263 throw new NotImplementedException("The method Lock in class FileSystem is not supported");
1267 public static void Lock(int FileNumber, long Record)
1269 throw new NotImplementedException("The method Lock in class FileSystem is not supported");
1273 public static void Lock(int FileNumber, long FromRecord, long ToRecord)
1275 throw new NotImplementedException("The method Lock in class FileSystem is not supported");
1279 public static void Unlock(int FileNumber)
1281 throw new NotImplementedException("The method Unlock in class FileSystem is not supported");
1285 public static void Unlock(int FileNumber, long Record)
1287 throw new NotImplementedException("The method Unlock in class FileSystem is not supported");
1291 public static void Unlock(int FileNumber, long FromRecord, long ToRecord)
1293 throw new NotImplementedException("The method Unlock in class FileSystem is not supported");
1296 public static void FileWidth(int fileNumber, int RecordWidth)
1298 VBFile vbFile = getVBFile(fileNumber);
1299 vbFile.width(fileNumber,RecordWidth);
1302 public static TabInfo TAB()
1304 return new TabInfo((short) - 1);
1307 public static TabInfo TAB(short Column)
1309 return new TabInfo(Column);
1312 public static SpcInfo SPC(short Count)
1314 return new SpcInfo(Count);
1317 public static OpenMode FileAttr(int fileNumber)
1319 VBFile vbFile = getVBFile(fileNumber);
1320 return (OpenMode) vbFile.getMode();
1324 class VBStreamWriter : StreamWriter
1329 public VBStreamWriter(string fileName):base(fileName)
1333 public VBStreamWriter(string fileName, bool append):base(fileName, append)
1338 //TODO: FileFilters from Mainsoft code