Commit for Daniel: many improvements to the FileSystem code.
[mono.git] / mcs / class / Microsoft.VisualBasic / Microsoft.VisualBasic / FileSystem.cs
1 //
2 // FileSystem.cs
3 //
4 // Author:
5 //   
6 // Daniel Campos ( danielcampos@netcourrier.com )
7 // 
8 //
9
10 using System;
11 using System.IO;
12 namespace Microsoft.VisualBasic 
13 {
14         [Microsoft.VisualBasic.CompilerServices.StandardModuleAttribute] 
15         sealed public class FileSystem {
16                 private static System.IO.FileStream[] FHandle=new FileStream[255];
17                 private static Microsoft.VisualBasic.OpenMode[] FMode= new Microsoft.VisualBasic.OpenMode[255];
18                 private static string InitialPath=Environment.CurrentDirectory; 
19                 // Declarations
20                 // Constructors
21                 // Properties
22                 // Methods
23                 [MonoTODO("Needs testing")]
24                 public static void ChDir (System.String Path) 
25                 {
26                         if ( (Path=="") || (Path==null))
27                                 throw new System.ArgumentException("Path is empty"); 
28                         try
29                         {
30                                 Environment.CurrentDirectory=Path;
31                         }
32                         catch ( Exception e){ throw new System.IO.FileNotFoundException ("Invalid drive is specified, or drive is unavailable");}
33                         
34                 }
35                 
36                 [MonoTODO]
37                 public static void ChDrive (System.Char Drive) { throw new NotImplementedException (); }
38                 [MonoTODO]
39                 public static void ChDrive (System.String Drive) { throw new NotImplementedException (); }
40                 
41                 [MonoTODO("Needs testing")]
42                 public static System.String CurDir () 
43                 { 
44                         return Environment.CurrentDirectory;
45                 }
46                 
47                 [MonoTODO("Needs Testing")]
48                 public static System.String CurDir (System.Char Drive)
49                 {
50                         bool MyOK=false;
51                         string MyDrive=(Drive.ToString()).ToLower();
52                         string[] buf=System.IO.Directory.GetLogicalDrives ();
53                         for (int lookfor=0;lookfor<buf.Length;lookfor++)
54                                 if ( buf[lookfor].Substring(0,1).ToLower() == MyDrive ) {MyOK=true; break; }
55                         if (!MyOK)
56                                 throw new System.ArgumentException("Invalid drive is specified.");
57                         if ( Environment.CurrentDirectory.Substring(0,1).ToLower() == MyDrive )
58                                 return Environment.CurrentDirectory  ;
59                         else
60                         {
61                                 if ( InitialPath.Substring(0,1).ToLower() == MyDrive ) 
62                                         return InitialPath;
63                                 else
64                                         return (MyDrive.ToUpper()  + Path.VolumeSeparatorChar + Path.DirectorySeparatorChar  );
65                         }
66                 }
67                 [MonoTODO]
68                 public static System.String Dir () { throw new NotImplementedException (); }
69                 [MonoTODO]
70                 public static System.String Dir (System.String Pathname, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(0)] Microsoft.VisualBasic.FileAttribute Attributes) { throw new NotImplementedException (); }
71                 
72                 [MonoTODO("Needs testing")]
73                 public static void MkDir (System.String Path) 
74                 { 
75                         // if a file called like 'path' does exist
76                         // no exception is generated using .net
77                         // A little extrange?
78                         if (Path==null || Path=="")
79                         {
80                                 throw new System.ArgumentException(); 
81                         }
82                         else
83                         {
84                                 if (System.IO.Directory.Exists (Path))
85                                         throw new System.IO.IOException("Directory already exists");
86                                 else
87                                         System.IO.Directory.CreateDirectory(Path);
88                         }
89                 }
90                 
91                 [MonoTODO("Needs testing")]
92                 public static void RmDir (System.String Path) 
93                 { 
94                         System.IO.Directory.Delete(Path); 
95                 }
96                 
97                 [MonoTODO("Needs testing")]
98                 public static void FileCopy (System.String Source, System.String Destination) 
99                 { 
100                         // using VB, filecopy always overwrites Destination
101                         System.IO.File.Copy(Source,Destination,true); 
102                 }
103                 
104                 [MonoTODO("Needs testing")]
105                 public static System.DateTime FileDateTime (System.String PathName) 
106                 {
107                         // A better exception handling is needed : exceptions
108                         // are not the same as 'GetLastWriteTime'
109                         return System.IO.File.GetLastWriteTime (PathName);
110                 }
111                 
112                 [MonoTODO("Needs Testing")]
113                 public static System.Int64 FileLen(System.String PathName) 
114                 {
115                         FileInfo MyFile=new FileInfo(PathName);
116                         if ( !MyFile.Exists )
117                                 throw new System.ArgumentException(PathName + " does not exists");
118                         return (System.Int64)MyFile.Length;  
119                 }
120                 [MonoTODO]
121                 public static Microsoft.VisualBasic.FileAttribute GetAttr (System.String PathName) { throw new NotImplementedException (); }
122                 
123                 [MonoTODO("Needs testing")]
124                 public static void Kill (System.String PathName) 
125                 {
126                         if (!System.IO.File.Exists(PathName))
127                                 throw new System.IO.FileNotFoundException();
128                         else
129                                 System.IO.File.Delete(PathName);
130                 }
131                 
132                 [MonoTODO]
133                 public static void SetAttr (System.String PathName, Microsoft.VisualBasic.FileAttribute Attributes) { throw new NotImplementedException (); }
134                
135                 [MonoTODO("Needs testing")]
136                 public static void FileOpen (System.Int32 FileNumber, System.String FileName, Microsoft.VisualBasic.OpenMode Mode, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(-1)] Microsoft.VisualBasic.OpenAccess Access, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(-1)] Microsoft.VisualBasic.OpenShare Share, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(-1)] System.Int32 RecordLength)
137                 {  
138                         // at this moment you can open a file
139                         // only for Append, Input or Output
140                         System.IO.FileMode MyMode;
141                         System.IO.FileAccess MyAccess; 
142                         System.IO.FileShare MyShare;
143                         //
144                         // exceptions
145                         //
146                         if ( RecordLength < -1 )
147                                 throw new System.ArgumentException("Record Length is negative (nad not equal to -1)");
148                         if ( RecordLength > 32767)
149                                 throw new System.ArgumentException ("Invalid Record Length");
150                         if (FileNumber <0 || FileNumber > 255)
151                                 throw new System.IO.IOException(FileNumber.ToString() + " is invalid (<-1 or >255)",5); 
152                         if ( (Mode == OpenMode.Output) && ( Access != OpenAccess.Default ) && ( Access != OpenAccess.Write ) )
153                                                 throw new System.ArgumentException("To use Output Mode, you have to use OpenAccess.Write or OpenAccess.Default");
154                                         if ( (Mode == OpenMode.Input) && ( Access != OpenAccess.Default ) && ( Access != OpenAccess.Read ) )
155                                                 throw new System.ArgumentException("To use Input Mode, you have to use OpenAccess.Read or OpenAccess.Default");
156                         //
157                         // implementation
158                         //
159                         FileNumber--;
160                         if (FHandle[FileNumber] != null)
161                                 throw new System.IO.IOException (FileNumber.ToString() + " is in use",5);       
162                         
163                                 switch (Mode)
164                                 {
165                                         case Microsoft.VisualBasic.OpenMode.Append :
166                                                 MyMode=System.IO.FileMode.Append  ;  
167                                                 break;
168                                         case Microsoft.VisualBasic.OpenMode.Binary :
169                                                 throw new NotImplementedException ();
170                                                 
171                                         case Microsoft.VisualBasic.OpenMode.Input  :
172                                                 MyMode=System.IO.FileMode.Open  ;
173                                                 break;
174                                         case Microsoft.VisualBasic.OpenMode.Output  :
175                                                 MyMode=System.IO.FileMode.OpenOrCreate  ;
176                                                 break;
177                                         case Microsoft.VisualBasic.OpenMode.Random  :
178                                                  throw new NotImplementedException ();  
179                                         default:
180                                                 throw new System.ArgumentException("Invalid Share"); 
181                                 }
182                                 switch (Access)
183                                 {
184                                         case Microsoft.VisualBasic.OpenAccess.ReadWrite :  
185                                         case Microsoft.VisualBasic.OpenAccess.Default :
186                                                 MyAccess=System.IO.FileAccess.ReadWrite;
187                                                 break;
188                                         case Microsoft.VisualBasic.OpenAccess.Read :
189                                                 MyAccess=System.IO.FileAccess.Read;
190                                                 break;
191                                         case Microsoft.VisualBasic.OpenAccess.Write :
192                                                 MyAccess=System.IO.FileAccess.Write;
193                                                 break;
194                                         default:
195                                                 throw new System.ArgumentException("Invalid Access");
196                                         
197                                 }
198                                 switch(Share)
199                                 {
200                                         case Microsoft.VisualBasic.OpenShare.Default :
201                                         case Microsoft.VisualBasic.OpenShare.Shared :
202                                                 MyShare=System.IO.FileShare.ReadWrite ;  
203                                                 break;
204                                         case Microsoft.VisualBasic.OpenShare.LockRead  :
205                                                 MyShare=System.IO.FileShare.Write; 
206                                                 break;
207                                         case Microsoft.VisualBasic.OpenShare.LockReadWrite :
208                                                 MyShare=System.IO.FileShare.None ;
209                                                 break;
210                                         case Microsoft.VisualBasic.OpenShare.LockWrite :
211                                                 MyShare=System.IO.FileShare.Read;
212                                                 break;
213                                         default:
214                                                 throw new System.ArgumentException("Invalid Share");
215                                 }
216                                 FHandle[FileNumber]=new System.IO.FileStream (FileName,MyMode,MyAccess,MyShare);
217                                 FMode[FileNumber]=Mode;
218                         
219                                 
220                         
221                 }
222                 [MonoTODO("Needs testing")]
223                 public static void FileClose (params System.Int32[] FileNumbers) 
224                 { 
225                         int bucle=0;
226                         if (FileNumbers.Length  == 0)
227                         {
228                                 Microsoft.VisualBasic.FileSystem.Reset();
229                         }
230                         else
231                         {
232                                 for(bucle=0;bucle<FileNumbers.Length;bucle++)
233                                 {
234                                         if ( FHandle [ FileNumbers[bucle] - 1 ] != null )
235                                         {
236                                                 if (FileNumbers[bucle]>0 && FileNumbers[bucle]<256)
237                                                 {
238                                                         try
239                                                         {
240                                                                 FHandle[ FileNumbers[bucle] - 1].Close();
241                                                                 FHandle[ FileNumbers[bucle] - 1]=null;
242                                                         }
243                                                         catch (Exception e){e.GetType (); FHandle[ FileNumbers[bucle] - 1]= null ;}
244                                                 }
245                                                 else
246                                                         throw new System.IO.IOException (FileNumbers[bucle].ToString() + " Does not exist",52);
247                                         }
248                                         else
249                                                 throw new System.IO.IOException (FileNumbers[bucle].ToString() + " Does not exist",52);
250                                 }
251                         }
252                 }                
253                 [MonoTODO]
254                 public static void FileGetObject (System.Int32 FileNumber, ref System.Object Value, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(-1)] ref System.Int64 RecordNumber) { throw new NotImplementedException (); }
255                 [MonoTODO]
256                 public static void FileGet (System.Int32 FileNumber, ref System.ValueType Value, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(-1)] ref System.Int64 RecordNumber) { throw new NotImplementedException (); }
257                 [MonoTODO]
258                 public static void FileGet (System.Int32 FileNumber, ref System.Array Value, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(-1)] ref System.Int64 RecordNumber, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(false)] ref System.Boolean ArrayIsDynamic, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(false)] ref System.Boolean StringIsFixedLength) { throw new NotImplementedException (); }
259                 [MonoTODO]
260                 public static void FileGet (System.Int32 FileNumber, ref System.Boolean Value, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(-1)] ref System.Int64 RecordNumber) { throw new NotImplementedException (); }
261                 [MonoTODO]
262                 public static void FileGet (System.Int32 FileNumber, ref System.Byte Value, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(-1)] ref System.Int64 RecordNumber) { throw new NotImplementedException (); }
263                 [MonoTODO]
264                 public static void FileGet (System.Int32 FileNumber, ref System.Int16 Value, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(-1)] ref System.Int64 RecordNumber) { throw new NotImplementedException (); }
265                 [MonoTODO]
266                 public static void FileGet (System.Int32 FileNumber, ref System.Int32 Value, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(-1)] ref System.Int64 RecordNumber) { throw new NotImplementedException (); }
267                 [MonoTODO]
268                 public static void FileGet (System.Int32 FileNumber, ref System.Int64 Value, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(-1)] ref System.Int64 RecordNumber) { throw new NotImplementedException (); }
269                 [MonoTODO]
270                 public static void FileGet (System.Int32 FileNumber, ref System.Char Value, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(-1)] ref System.Int64 RecordNumber) { throw new NotImplementedException (); }
271                 [MonoTODO]
272                 public static void FileGet (System.Int32 FileNumber, ref System.Single Value, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(-1)] ref System.Int64 RecordNumber) { throw new NotImplementedException (); }
273                 [MonoTODO]
274                 public static void FileGet (System.Int32 FileNumber, ref System.Double Value, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(-1)] ref System.Int64 RecordNumber) { throw new NotImplementedException (); }
275                 [MonoTODO]
276                 public static void FileGet (System.Int32 FileNumber, ref System.Decimal Value, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(-1)] ref System.Int64 RecordNumber) { throw new NotImplementedException (); }
277                 [MonoTODO]
278                 public static void FileGet (System.Int32 FileNumber, ref System.String Value, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(-1)] ref System.Int64 RecordNumber, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(false)] ref System.Boolean StringIsFixedLength) { throw new NotImplementedException (); }
279                 [MonoTODO]
280                 public static void FileGet (System.Int32 FileNumber, ref System.DateTime Value, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(-1)] ref System.Int64 RecordNumber) { throw new NotImplementedException (); }
281                 [MonoTODO]
282                 public static void FilePutObject (System.Int32 FileNumber, System.Object Value, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(-1)] System.Int64 RecordNumber) { throw new NotImplementedException (); }
283                 [MonoTODO]
284                 [System.ObsoleteAttribute("Use FilePutObject to write Object types, or coerce FileNumber and RecordNumber to Integer for writing non-Object types", false)] 
285                 public static void FilePut (System.Object FileNumber, System.Object Value, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(-1)] System.Object RecordNumber) { throw new NotImplementedException (); }
286                 [MonoTODO]
287                 public static void FilePut (System.Int32 FileNumber, System.ValueType Value, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(-1)] System.Int64 RecordNumber) { throw new NotImplementedException (); }
288                 [MonoTODO]
289                 public static void FilePut (System.Int32 FileNumber, System.Array Value, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(-1)] System.Int64 RecordNumber, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(false)] System.Boolean ArrayIsDynamic, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(false)] System.Boolean StringIsFixedLength) { throw new NotImplementedException (); }
290                 [MonoTODO]
291                 public static void FilePut (System.Int32 FileNumber, System.Boolean Value, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(-1)] System.Int64 RecordNumber) { throw new NotImplementedException (); }
292                 [MonoTODO]
293                 public static void FilePut (System.Int32 FileNumber, System.Byte Value, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(-1)] System.Int64 RecordNumber) { throw new NotImplementedException (); }
294                 [MonoTODO]
295                 public static void FilePut (System.Int32 FileNumber, System.Int16 Value, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(-1)] System.Int64 RecordNumber) { throw new NotImplementedException (); }
296                 [MonoTODO]
297                 public static void FilePut (System.Int32 FileNumber, System.Int32 Value, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(-1)] System.Int64 RecordNumber) { throw new NotImplementedException (); }
298                 [MonoTODO]
299                 public static void FilePut (System.Int32 FileNumber, System.Int64 Value, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(-1)] System.Int64 RecordNumber) { throw new NotImplementedException (); }
300                 [MonoTODO]
301                 public static void FilePut (System.Int32 FileNumber, System.Char Value, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(-1)] System.Int64 RecordNumber) { throw new NotImplementedException (); }
302                 [MonoTODO]
303                 public static void FilePut (System.Int32 FileNumber, System.Single Value, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(-1)] System.Int64 RecordNumber) { throw new NotImplementedException (); }
304                 [MonoTODO]
305                 public static void FilePut (System.Int32 FileNumber, System.Double Value, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(-1)] System.Int64 RecordNumber) { throw new NotImplementedException (); }
306                 [MonoTODO]
307                 public static void FilePut (System.Int32 FileNumber, System.Decimal Value, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(-1)] System.Int64 RecordNumber) { throw new NotImplementedException (); }
308                 [MonoTODO]
309                 public static void FilePut (System.Int32 FileNumber, System.String Value, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(-1)] System.Int64 RecordNumber, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(false)] System.Boolean StringIsFixedLength) { throw new NotImplementedException (); }
310                 [MonoTODO]
311                 public static void FilePut (System.Int32 FileNumber, System.DateTime Value, [System.Runtime.InteropServices.Optional] [System.ComponentModel.DefaultValue(-1)] System.Int64 RecordNumber) { throw new NotImplementedException (); }
312                 [MonoTODO]
313                 public static void Print (System.Int32 FileNumber, params System.Object[] Output) { throw new NotImplementedException (); }
314                 [MonoTODO]
315                 public static void PrintLine (System.Int32 FileNumber, params System.Object[] Output) { throw new NotImplementedException (); }
316                 [MonoTODO]
317                 public static void Input (System.Int32 FileNumber, ref System.Object Value) { throw new NotImplementedException (); }
318                 [MonoTODO("Needs Testing")]
319                 public static void Input (System.Int32 FileNumber, ref System.Boolean Value)
320                 {
321                         string buffer="";
322                         InternalInputExceptions(FileNumber);
323                         buffer=InternalInput(FileNumber,3);
324                         if (buffer=="True")
325                                 Value=true;
326                         else
327                                 Value=false;
328                 }
329                 [MonoTODO("Needs Testing")]
330                 public static void Input (System.Int32 FileNumber, ref System.Byte Value)
331                 {
332                         string buffer="";
333                         InternalInputExceptions(FileNumber);
334                         buffer=InternalInput(FileNumber,1);
335                         if (buffer[0]=='-')
336                                 throw new System.OverflowException();
337                         Value=0;
338                         for (int addnumber=0; addnumber < buffer.Length;addnumber++)
339                         {
340                                 checked {
341                                         Value*=10;
342                                         Value += Byte.Parse(buffer.Substring(addnumber,1));
343
344                                 }
345                         }
346                 }
347                 [MonoTODO("Needs Testing")]
348                 public static void Input (System.Int32 FileNumber, ref System.Int16 Value)
349                 {
350                         string buffer="";
351                         System.Int16 factor=1;
352                         InternalInputExceptions(FileNumber);
353                         buffer=InternalInput(FileNumber,1);
354                         if (buffer[0]=='-')
355                         {
356                                 factor=-1;
357                                 buffer=buffer.Substring(1);
358                         }
359                         Value=0;
360                         for (int addnumber=0; addnumber < buffer.Length;addnumber++)
361                         {
362                                 checked {
363                                         Value*=10;
364                                         Value += Int16.Parse(buffer.Substring(addnumber,1));
365
366                                 }
367                         }
368                         Value*=factor;
369
370                 }
371                 [MonoTODO("Needs Testing")]
372                 public static void Input (System.Int32 FileNumber, ref System.Int32 Value)
373                 {
374                         string buffer="";
375                         int factor=1;
376                         InternalInputExceptions(FileNumber);
377                         buffer=InternalInput(FileNumber,1);
378                         if (buffer[0]=='-')
379                         {
380                                 factor=-1;
381                                 buffer=buffer.Substring(1);
382                         }
383                         Value=0;
384                         for (int addnumber=0; addnumber < buffer.Length;addnumber++)
385                         {
386                                 checked {
387                                         Value*=10;
388                                         Value += Int32.Parse(buffer.Substring(addnumber,1));
389
390                                 }
391                         }
392                         Value*=factor;
393
394                 }
395                 [MonoTODO("Needs Testing")]
396                 public static void Input (System.Int32 FileNumber, ref System.Int64 Value)
397                 {
398                         string buffer="";
399                         int factor=1;
400                         InternalInputExceptions(FileNumber);
401                         buffer=InternalInput(FileNumber,1);
402                         if (buffer[0]=='-')
403                         {
404                                 factor=-1;
405                                 buffer=buffer.Substring(1);
406                         }
407                         Value=0;
408                         for (int addnumber=0; addnumber < buffer.Length;addnumber++)
409                         {
410                                 checked {
411                                         Value*=10;
412                                         Value += Int64.Parse(buffer.Substring(addnumber,1));
413
414                                 }
415                         }
416                         Value*=factor;
417
418                 }
419                 [MonoTODO]
420                 public static void Input (System.Int32 FileNumber, ref System.Char Value) { throw new NotImplementedException (); }
421                 [MonoTODO("Needs Testing")]
422                 public static void Input (System.Int32 FileNumber, ref System.Single Value)
423                 {
424                         System.Single DecimalValue=0;
425                         string buffer="";
426                         int factor=1;
427                         string BufDecimal="";
428                         InternalInputExceptions(FileNumber);
429                         buffer=InternalInput(FileNumber,2);
430                         if (buffer[0]=='-')
431                         {
432                                 factor=-1;
433                                 buffer=buffer.Substring(1);
434                         }
435                         if ( buffer.IndexOf(".")>=0)
436                         {
437                                 if ( buffer.IndexOf(".") < (buffer.Length -1) )
438                                         BufDecimal=buffer.Substring(buffer.IndexOf(".")+1);
439                                 if ( buffer.IndexOf(".") > 0)
440                                         buffer=buffer.Substring(0,buffer.IndexOf("."));
441                                 else
442                                         buffer="";
443
444                         }
445                         Value=0;
446                         if ( BufDecimal.Length > 0)
447                         {
448                                 for (int addnumber=BufDecimal.Length-1; addnumber >=0;addnumber--)
449                                 {
450                                         checked {
451                                                 DecimalValue += System.Single.Parse(BufDecimal.Substring(addnumber,1));
452                                                 DecimalValue /= 10;
453
454                                         }
455                                 }
456                         }
457                         if (buffer.Length >0)
458                         {
459                                 for (int addnumber=0; addnumber < buffer.Length;addnumber++)
460                                 {
461                                         checked {
462                                                 Value*=10;
463                                                 Value += System.Single.Parse(buffer.Substring(addnumber,1));
464
465                                         }
466                                 }
467                         }
468                         Value+=DecimalValue;
469                 }
470                 [MonoTODO("Needs Testing")]
471                 public static void Input (System.Int32 FileNumber, ref System.Double Value)
472                 {
473                         double DecimalValue=0;
474                         string buffer="";
475                         int factor=1;
476                         string BufDecimal="";
477                         InternalInputExceptions(FileNumber);
478                         buffer=InternalInput(FileNumber,2);
479                         if (buffer[0]=='-')
480                         {
481                                 factor=-1;
482                                 buffer=buffer.Substring(1);
483                         }
484                         if ( buffer.IndexOf(".")>=0)
485                         {
486                                 if ( buffer.IndexOf(".") < (buffer.Length -1) )
487                                         BufDecimal=buffer.Substring(buffer.IndexOf(".")+1);
488                                 if ( buffer.IndexOf(".") > 0)
489                                         buffer=buffer.Substring(0,buffer.IndexOf("."));
490                                 else
491                                         buffer="";
492
493                         }
494                         Value=0;
495                         if ( BufDecimal.Length > 0)
496                         {
497                                 for (int addnumber=BufDecimal.Length-1; addnumber >=0;addnumber--)
498                                 {
499                                         checked {
500                                                 DecimalValue += Double.Parse(BufDecimal.Substring(addnumber,1));
501                                                 DecimalValue /= 10;
502
503                                         }
504                                 }
505                         }
506                         if (buffer.Length >0)
507                         {
508                                 for (int addnumber=0; addnumber < buffer.Length;addnumber++)
509                                 {
510                                         checked {
511                                                 Value*=10;
512                                                 Value += Double.Parse(buffer.Substring(addnumber,1));
513
514                                         }
515                                 }
516                         }
517                         Value+=DecimalValue;
518                 }
519                 [MonoTODO]
520                 public static void Input (System.Int32 FileNumber, ref System.Decimal Value) { throw new NotImplementedException (); }
521                 [MonoTODO("Needs Testing")]
522                 public static void Input (System.Int32 FileNumber, ref System.String Value)
523                 {
524                         string buffer="";
525                         InternalInputExceptions(FileNumber);
526                         Value=InternalInput(FileNumber,0);
527                 }
528                 [MonoTODO]
529                 public static void Input (System.Int32 FileNumber, ref System.DateTime Value) { throw new NotImplementedException (); }
530                 private static void InternalInputExceptions(System.Int32 FileNumber)
531                 {
532                         if ( FileNumber < 0 || FileNumber > 255 )
533                                 throw new System.ArgumentException("File Number is not valid");
534                         if ( FHandle[FileNumber - 1] == null)
535                                 throw new System.ArgumentException("File Number is not valid");
536                         if ( FMode[FileNumber - 1] != OpenMode.Input && FMode[FileNumber-1] != OpenMode.Binary )
537                                 throw new System.IO.IOException("File Mode is invalid");
538                         if ( FHandle[FileNumber - 1].Position == FHandle[FileNumber - 1].Length)
539                                 throw new System.IO.EndOfStreamException();
540                 }
541                 private static string InternalInput(System.Int32 FileNumber,int DataType)
542                 {
543
544                         // DataType : an additional filter
545                         // to know if conversion is possible
546                         // 0 --> string
547                         // 1 --> To a numeric (integer) value
548                         // 2 --> To a numeric (not integer) value
549                         // 3 -->  To Boolean
550                         bool found=false;
551                         bool firstzone=true;
552                         bool literal=false;
553                         bool MyOK=true;
554                         bool DecimalFound=false;
555                         bool SignFound=false;
556                         string retval="";
557                         string retval2="";
558                         byte[] BufByte=new byte[1];
559                         while ( !found && ( FHandle[FileNumber-1].Position < FHandle[FileNumber-1].Length ))
560                         {
561                                 FHandle[FileNumber-1].Read (BufByte,0,1);
562                                 switch ((char)BufByte[0])
563                                 {
564                                         case ' ':
565                                                 if (literal)
566                                                         retval+=" ";
567                                                 else {
568                                                         if (!firstzone && (DataType==1 || DataType==2))
569                                                                 found=true;
570                                                         else
571                                                                 retval+=" ";
572                                                 }
573                                                 break;
574                                         case '\t':
575                                                 if (literal) retval+="\t";
576                                                 else if (!firstzone) found=true;
577                                                 break;
578                                         case '"':
579                                                 retval+="\"";
580                                                 if (literal) literal=!literal;
581                                                 else
582                                                 {
583                                                         if (!firstzone) found=true;
584                                                         else                    literal=!literal;
585                                                 }
586                                                 break;
587                                         case ',':
588                                                 if (!literal) found=true;
589                                                 else retval+=",";
590                                                 break;
591                                         case '\x0d':
592                                                 if (!literal)
593                                                 {
594                                                         found=true;
595                                                         if (FHandle[FileNumber - 1].Length > FHandle[FileNumber - 1].Position )
596                                                         {
597                                                                 FHandle[FileNumber - 1].Read (BufByte,0,1);
598                                                                 if (BufByte[0] != 10 )
599                                                                         FHandle[FileNumber - 1].Seek (-1,SeekOrigin.Current );
600                                                         }
601
602                                                 }
603                                                 else {  retval+="\x0d"; }
604                                                 break;
605                                         case '\x0a':
606                                                 if (literal) retval+="\x0a";
607                                                 break;
608                                         default:
609                                                 firstzone=false;
610                                                 retval+=((char)BufByte[0]).ToString();
611                                                 break;
612                                 }
613                         }
614                         switch (DataType)
615                         {
616                                 case 0:
617                                         retval=retval.Trim();
618                                         if (retval.Substring(0,1)=="\"")
619                                         {
620                                                 if (retval.Length > 1)  retval=retval.Substring (1);
621                                                 else retval="";
622                                         }
623                                         if (retval.Length >=1)
624                                         {
625                                                 if (retval.Substring (retval.Length -1 ,1)=="\"")
626                                                 {
627                                                         if (retval.Length > 1)  retval=retval.Substring (0,retval.Length -1);
628                                                         else retval="";
629                                                 }
630                                         }
631                                         retval2=retval;
632                                         break;
633                                 case 1:
634                                 case 2:
635                                         retval=retval.Trim();
636                                         for (int myloop=0; (myloop<retval.Length) && MyOK ;myloop++)
637                                                 switch(retval[myloop])
638                                                 {
639                                                         case '+':
640                                                         case '-':
641                                                                 if (myloop==0 || myloop == (retval.Length -1))
642                                                                 {
643                                                                         if (!SignFound)
644                                                                         {
645                                                                                 retval2=retval[myloop].ToString() + retval2;
646                                                                                 SignFound=true;
647                                                                         }
648                                                                         else
649                                                                                 MyOK=false;
650                                                                 }
651                                                                 else
652                                                                         MyOK=false;
653                                                                 break;
654                                                         case '0': case '1': case '2': case '3': case '4':
655                                                         case '5': case '6': case '7': case '8': case '9':
656                                                                 retval2+=retval.Substring (myloop,1);
657                                                                 break;
658                                                         case '.':
659                                                                 if (DataType==2)
660                                                                 {
661                                                                         if (!DecimalFound)
662                                                                         {
663                                                                                 retval2+="." ;
664                                                                                 DecimalFound=true;
665                                                                         }
666                                                                         else
667                                                                                 MyOK=false;
668                                                                 }
669                                                                 break;
670                                                         default:
671                                                                 MyOK=false;
672                                                                 break;
673                                                 }
674                                         if (MyOK && (retval2.Length >=1 ) )
675                                         {
676                                                 if (retval2[retval2.Length-1]=='.' )
677                                                 {
678                                                         if (retval2.Length >1)
679                                                                 retval2=retval2.Substring (0,retval2.Length -1);
680                                                         else
681                                                                 MyOK=false;
682                                                 }
683                                         }
684                                         else
685                                                 MyOK=false;
686                                         break;
687                                 case 3:
688                                         retval=retval.Trim();
689                                         retval2="False";
690                                         retval=retval.Trim();
691                                         if (retval=="#TRUE#" || retval=="#FALSE#" ||
692                                                 retval.ToUpper() =="TRUE" || retval.ToUpper() =="FALSE" ||
693                                                 retval.ToUpper() == "\"TRUE\"" || retval.ToUpper() == "\"FALSE\"")
694                                         {
695                                                 if (retval=="#TRUE#" || retval.ToUpper() == "TRUE" || retval.ToUpper () == "\"TRUE\"")
696                                                         retval2="True";
697                                         }
698                                         else
699                                         {
700                                                 if (retval.Substring(0,1)=="\"")
701                                                 {
702                                                         if (retval.Length > 1)  retval=retval.Substring (1);
703                                                         else retval="";
704                                                 }
705                                                 if (retval.Length >=1)
706                                                 {
707                                                         if (retval.Substring (retval.Length -1 ,1)=="\"")
708                                                         {
709                                                                 if (retval.Length > 1)  retval=retval.Substring (0,retval.Length -1);
710                                                                 else retval="";
711                                                         }
712                                                 }
713                                                 for (int myloop=0; (myloop<retval.Length) && MyOK ;myloop++)
714                                                         switch(retval[myloop])
715                                                         {
716                                                                 case '0':
717                                                                         break;
718                                                                 case '1': case '2': case '3': case '4': case '5':
719                                                                 case '6': case '7': case '8': case '9':
720                                                                         retval2="True";
721                                                                         break;
722                                                                 case '.': break;
723                                                                 case '-':
724                                                                         if ( (myloop!=0) && (myloop!=retval.Length-1) )
725                                                                                 MyOK=false;
726                                                                         break;
727                                                                 default:
728                                                                         MyOK=false;
729                                                                         break;
730                                                         }
731                                         }
732                                         break;
733                         }
734                         if (MyOK)
735                         {
736                                 return retval2;
737                         }
738                         else // TODO : string explaining cast exception
739                                 throw new System.InvalidCastException();
740                 }
741                 [MonoTODO("Needs Testing")]
742                 public static void Write (System.Int32 FileNumber, params System.Object[] Output) 
743                 {
744                         string MyBuf=null;
745                         byte[] Separator=new byte[1];
746                         byte[] bufout=new Byte[1];
747                         Separator[0]=(byte)',';
748                         if (FileNumber<1 || FileNumber>255)
749                                 throw new System.IO.IOException (FileNumber.ToString() + " does not exists",52);
750                         if (FHandle[FileNumber - 1]==null)
751                                 throw new System.IO.IOException(FileNumber + " does not exists",52);
752                         if ( FMode[ FileNumber - 1] != OpenMode.Output  )
753                                 throw new System.IO.IOException ("FileMode is invalid");
754                         if (Output.Length == 0)
755                         {
756                                 FHandle[FileNumber - 1].Write(Separator,0,1);
757                         }
758                         else
759                         {
760                                 for (int MyArgs=0;MyArgs<Output.Length;MyArgs++)
761                                 {
762                                         MyBuf=WriteAuxiliar(Output[MyArgs]);
763                                         for (int PutsData=0;PutsData<MyBuf.Length;PutsData++)
764                                         {
765                                                 bufout[0]=(byte)MyBuf[PutsData];
766                                                 FHandle[FileNumber-1].Write(bufout,0,1);
767                                         }
768                                         FHandle[FileNumber - 1].Write(Separator,0,1);
769                                 }
770                         }
771                 }
772                 [MonoTODO("Needs Testing")]
773                 public static void WriteLine (System.Int32 FileNumber, params System.Object[] Output) 
774                 {
775                         byte[] Separator=new byte[1];
776                         byte[] bufout=new Byte[1];
777                         byte[] NewLine=new Byte[2];
778                         string MyBuf="";
779                         Separator[0]=(byte)',';
780                         NewLine[0]=(byte)'\x0D';
781                         NewLine[1]=(byte)'\x0A';
782                         if (FileNumber<1 || FileNumber>255)
783                                 throw new System.IO.IOException (FileNumber.ToString() + " does not exists",52);
784                         if (FHandle[FileNumber - 1]==null)
785                                 throw new System.IO.IOException(FileNumber + " does not exists",52);
786                         if ( FMode[ FileNumber - 1] != OpenMode.Output  )
787                                 throw new System.IO.IOException ("FileMode is invalid");
788                         if (Output.Length == 0)
789                         {
790                                 FHandle[FileNumber - 1].Write(NewLine,0,2);
791                         }
792                         else
793                         {
794                                 for (int MyArgs=0;MyArgs<Output.Length;MyArgs++)
795                                 {
796                                         MyBuf=WriteAuxiliar(Output[MyArgs]);
797                                         for (int PutsData=0;PutsData<MyBuf.Length;PutsData++)
798                                         {
799                                                 bufout[0]=(byte)MyBuf[PutsData];
800                                                 FHandle[FileNumber-1].Write(bufout,0,1);
801                                         }
802                                         if (MyArgs < (Output.Length -1))
803                                         {
804                                                 FHandle[FileNumber - 1].Write(Separator,0,1);
805                                         }
806                                         else
807                                         {
808                                                 FHandle[FileNumber - 1].Write(NewLine,0,2);
809                                         }
810                                 }
811                         }
812                 }
813                 private static string WriteAuxiliar(Object Argument)
814                 {
815                         string retval="";
816                         if (Argument==null)
817                                 retval="#NULL#";
818                         else
819                         {
820                                 switch (Argument.GetType().ToString())
821                                 {
822                                 case "System.Boolean": 
823                                         if ( (bool)Argument == true)
824                                                 retval="#TRUE#";
825                                         else
826                                                 retval="#FALSE#";
827                                         break;
828                                 case "System.String":
829                                         retval="\"" + (string)Argument + "\"";
830                                         break;
831                                 case "System.Int64":
832                                 case "System.UInt64":
833                                 case "System.Int32":
834                                 case "System.UInt32":
835                                 case "System.Int16":
836                                 case "System.UInt16":
837                                 case "System.Sbyte":
838                                 case "System.Byte":
839                                         retval = Argument.ToString();
840                                         break;
841                                 case "System.Single":
842                                 case "System.Double":
843                                 case "System.Decimal":
844                                         string buf= (Argument.ToString()) ;
845                                         if ( buf.IndexOf(",")>=0)
846                                                 retval=buf.Substring(0, buf.IndexOf(",")) 
847                                                 + "." + buf.Substring(1+buf.IndexOf(","));
848                                         break;
849                                 case "System.Exception":
850                                         retval=((Exception)Argument).ToString();
851                                         break;
852                                 case "System.Char":
853                                         retval=((char)Argument).ToString();
854                                         break;
855                                 case "System.DateTime":
856                                         retval=((System.DateTime )Argument).ToString("u");
857                                         retval=retval.Substring(0,retval.Length -1);
858                                         if (retval.Substring(11,8)=="00:00:00")
859                                                 retval=retval.Substring(0,10);
860                                         retval= "#" + retval + "#";
861                                         break;
862                                 default :
863                                         throw new NotImplementedException ();
864                                         
865                                 }
866                         }
867                         return retval;
868                 }
869                 [MonoTODO("Needs Testing")]
870                 public static System.String InputString (System.Int32 FileNumber, System.Int32 CharCount) 
871                 {
872                         byte[] Buf;
873                         string retval="";
874                         //
875                         // exceptions
876                         if ( FileNumber<1 || FileNumber>255)
877                                 throw new System.IO.IOException (FileNumber.ToString() + " does not exists",52);
878                         if ( FHandle[FileNumber - 1] == null)
879                                 throw new System.IO.IOException (FileNumber.ToString() + " does not exists",52);
880                         if ( (FMode[FileNumber - 1] != OpenMode.Input) && (FMode[FileNumber - 1] != OpenMode.Binary ))
881                                 throw new System.IO.IOException ("FileMode is invalid");
882                         if (CharCount <0 || CharCount>2e14 )
883                                 throw new System.ArgumentException(); 
884                         if ( (CharCount + FHandle[FileNumber - 1].Position) > FHandle[FileNumber - 1].Length)
885                                 throw new System.IO.EndOfStreamException();     
886                         //
887                         // implementation
888                         Buf=new byte[CharCount];
889                         FHandle[FileNumber - 1].Read(Buf,0,Buf.Length);
890                         for (int myloop=0;myloop<Buf.Length;myloop++)
891                                 retval+=((char)Buf[myloop]).ToString();
892                         return retval;
893                 }
894                 [MonoTODO("Needs testing")]
895                 public static System.String LineInput (System.Int32 FileNumber) 
896                 { 
897                         string retval="";
898                                         int buf='\x00';  
899                         bool found=false;
900                         if ( FileNumber<1 || FileNumber>255)
901                                 throw new System.IO.IOException (FileNumber.ToString() + " does not exists",52);
902                         if ( FHandle[FileNumber - 1] == null)
903                                 throw new System.IO.IOException (FileNumber.ToString() + " does not exists",52);
904                                         
905                         if ( EOF(FileNumber) )
906                                 throw new System.IO.EndOfStreamException();
907                         
908                         while (!found)
909                         {
910                                  
911                                 buf=FHandle[FileNumber - 1].ReadByte();
912                                 if ( (buf == -1) || (buf == '\x0A' ) )
913                                         found=true;
914                                 else
915                                         retval+= ((char)buf).ToString();
916                         }
917                         if ( retval.Length > 0 )
918                                 if ( (buf == '\x0A') && (retval[retval.Length -1 ] == '\x0D') )
919                                         retval=retval.Substring(0,retval.Length -1) ;
920                         return retval;
921                             
922                 }
923                 
924                 [MonoTODO]
925                 public static void Lock (System.Int32 FileNumber) { throw new NotImplementedException (); }
926                 [MonoTODO]
927                 public static void Lock (System.Int32 FileNumber, System.Int64 Record) { throw new NotImplementedException (); }
928                 [MonoTODO]
929                 public static void Lock (System.Int32 FileNumber, System.Int64 FromRecord, System.Int64 ToRecord) { throw new NotImplementedException (); }
930                 [MonoTODO]
931                 public static void Unlock (System.Int32 FileNumber) { throw new NotImplementedException (); }
932                 [MonoTODO]
933                 public static void Unlock (System.Int32 FileNumber, System.Int64 Record) { throw new NotImplementedException (); }
934                 [MonoTODO]
935                 public static void Unlock (System.Int32 FileNumber, System.Int64 FromRecord, System.Int64 ToRecord) { throw new NotImplementedException (); }
936                 [MonoTODO]
937                 public static void FileWidth (System.Int32 FileNumber, System.Int32 RecordWidth) { throw new NotImplementedException (); }
938                 
939                 [MonoTODO("Needs testing")]                
940                 public static System.Int32 FreeFile () 
941                 { 
942                         int bucle=0;
943                         bool found=false;
944                         for (bucle=0;bucle<255;bucle++)
945                                 if (FHandle[bucle]==null)
946                                 {
947                                         found=true;
948                                         break;
949                                 }
950                         if (!found)
951                                 throw new System.IO.IOException ("More than 255 files are in use",67);
952                         else
953                                 return bucle+1;
954                 }
955                 [MonoTODO]
956                 public static void Seek (System.Int32 FileNumber, System.Int64 Position) { throw new NotImplementedException (); }
957                 [MonoTODO]
958                 public static System.Int64 Seek (System.Int32 FileNumber) { throw new NotImplementedException (); }
959                 
960                 [MonoTODO("Needs testing")]
961                 public static System.Boolean EOF ( System.Int32 FileNumber) 
962                 { 
963                         if (FileNumber<1 || FileNumber>255)
964                                 throw new System.IO.IOException (FileNumber.ToString() + " does not exists",52);
965                         if ( FHandle[FileNumber - 1] == null)
966                                 throw new System.IO.IOException (FileNumber.ToString() + " does not exists",52);
967                         if ( FHandle[FileNumber - 1].Length == FHandle[FileNumber - 1].Position)
968                                 return true;
969                         else
970                                 return false;
971                                 
972                 }
973                 
974                 [MonoTODO]
975                 public static System.Int64 Loc (System.Int32 FileNumber) { throw new NotImplementedException (); }
976                 [MonoTODO]
977                 public static System.Int64 LOF (System.Int32 FileNumber) 
978                 {
979                         if (FileNumber<1 || FileNumber>255)
980                                 throw new System.IO.IOException (FileNumber.ToString() + " does not exists",52);
981                         if ( FHandle[FileNumber - 1] == null)
982                                 throw new System.IO.IOException (FileNumber.ToString() + " does not exists",52);        
983                         return (System.Int64)FHandle[FileNumber - 1].Length;
984                 }
985                 [MonoTODO]
986                 public static Microsoft.VisualBasic.TabInfo TAB () { throw new NotImplementedException (); }
987                 [MonoTODO]
988                 public static Microsoft.VisualBasic.TabInfo TAB (System.Int16 Column) { throw new NotImplementedException (); }
989                 [MonoTODO]
990                 public static Microsoft.VisualBasic.SpcInfo SPC (System.Int16 Count) { throw new NotImplementedException (); }
991                 [MonoTODO("Needs Testing")]
992                 public static Microsoft.VisualBasic.OpenMode FileAttr (System.Int32 FileNumber) 
993                 { 
994                         if (FileNumber<1 || FileNumber>255)
995                                 throw new System.IO.IOException (FileNumber.ToString() + " does not exists",52);
996                         if ( FHandle[FileNumber - 1] == null)
997                                 throw new System.IO.IOException (FileNumber.ToString() + " does not exists",52);
998                         return FMode[FileNumber - 1];
999                 }
1000                 [MonoTODO("Needs Testing")]
1001                 public static void Reset ()
1002                 {
1003                         for(int bucle=0;bucle<255;bucle++)
1004                         {
1005                                 if (FHandle[bucle]!=null)
1006                                         try
1007                                         {
1008                                                 FHandle[bucle].Close(); 
1009                                         }
1010                                         catch (Exception e) 
1011                                         { 
1012                                                 FHandle[bucle]=null ;
1013                                         }
1014                         }
1015                 }
1016                 [MonoTODO("Needs Testing")]
1017                 public static void Rename (System.String OldPath, System.String NewPath) 
1018                 {
1019                         if ( !File.Exists (OldPath) && !Directory.Exists( OldPath))
1020                                 throw new System.ArgumentException ( OldPath + " does not exist");
1021                         if ( File.Exists ( NewPath) || Directory.Exists ( NewPath))
1022                                 throw new System.IO.IOException ( NewPath + " already exists");
1023                         if ( File.Exists (OldPath))
1024                                 File.Move (OldPath, NewPath);
1025                         else
1026                                 Directory.Move (OldPath, NewPath); 
1027                 }
1028                 // Events
1029                 
1030         };
1031 }