Fixed API: added missing attribute System.Serializable
[mono.git] / mcs / class / Microsoft.VisualBasic / Microsoft.VisualBasic / BinaryVBFile.cs
1 /*
2  * Copyright (c) 2002-2003 Mainsoft Corporation.
3  * Copyright (C) 2004 Novell, Inc (http://www.novell.com)
4  *
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:
11  * 
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  * 
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.
22  */
23
24
25 using System;
26 using System.IO;
27 using System.Text;
28
29 using Microsoft.VisualBasic;
30 using Microsoft.VisualBasic.CompilerServices;
31
32 public class BinaryVBFile : RandomVBFile
33 {
34         public BinaryVBFile(String fileName, OpenMode mode, OpenAccess access, int recordLength) : 
35                 base(fileName,mode,access,recordLength)
36         {
37                 
38         }   
39   
40         public override bool canWrite()
41         {
42                 return true;
43         }
44     
45         public override bool canRead()
46         {
47                 return true;  
48         }
49     
50         private String readString()
51         {
52                 StringBuilder sb = new StringBuilder("");
53                 int cInt = _binaryReader.Read();
54                 char c = (char) cInt;
55                 bool inStr = false;
56                 while (cInt != -1 && !(c == ',' && !inStr) && c != '\r') //spacebar
57                 {
58                         if (c == '\"' && inStr)
59                         {
60                                 sb.Append(c);
61                                 if ((char) _binaryReader.PeekChar() == ',')
62                                         _binaryReader.Read();
63                                 break;
64                         }
65                         else if (c == '\"')
66                                 inStr = true;
67                         sb.Append(c);
68                         cInt = _binaryReader.Read();
69                         c = (char) cInt;
70                 }
71                 if (c == '\r')
72                         _binaryReader.Read();
73                 if (sb.Length > 1
74                     && sb[0] == '\"'
75                     && sb[sb.Length - 1] == '\"')
76                 {
77                         sb.Remove(0, 1);
78                         sb.Remove(sb.Length - 1, 1);
79                 }
80                 return sb.ToString();
81         }
82     
83         private String readNumber()
84         {
85                 StringBuilder sb = new StringBuilder();
86                 int cInt = _binaryReader.Read();
87                 char c = (char) cInt;
88                 while (cInt != -1 && c != ',' && c != '\r' && c != 32) //spacebar
89                 {
90                         sb.Append(c);
91                         cInt = _binaryReader.Read();
92                         c = (char) cInt;
93                 }
94                 return sb.ToString();
95         }
96     
97         public override void Input(out bool Value)
98         {
99                 String str = readString().Trim();
100
101                 if (str[0] == '#' && str.Length != 1)
102                         str = str.Substring(1, str.Length - 1);
103                 Object obj = str;
104                 // Temporary
105                 Value = BooleanType.FromObject(obj);
106         }
107
108         public override void Input(out byte Value)
109         {
110                 char val = this._binaryReader.ReadChar();
111                 while (val == 10 || val == 32)
112                         val = this._binaryReader.ReadChar();
113                 String str = readString().Trim(); 
114                 Value = ByteType.FromString(val+str);
115         }
116
117         public override void Input(out short Value)
118         {
119                 char val = this._binaryReader.ReadChar();
120                 while (val == 10 || val == 32)
121                         val = this._binaryReader.ReadChar();
122                 String str = readString().Trim(); 
123                 Value = ShortType.FromString(val+str); 
124         }
125
126         public override void Input(out int Value)
127         {
128                 char val = this._binaryReader.ReadChar();
129                 while (val == 10 || val == 32)
130                         val = this._binaryReader.ReadChar();
131                 String str = readString().Trim(); 
132                 Value = IntegerType.FromString(val+str);
133         }
134
135         public override void Input(out long Value)
136         {
137                 char val = this._binaryReader.ReadChar();
138                 while (val == 10 || val == 32)
139                         val = this._binaryReader.ReadChar();
140                 String str = readString().Trim(); 
141                 Value = LongType.FromString(val+str);
142         }
143
144         public override void Input(out char Value)
145         {
146                 char val = this._binaryReader.ReadChar();
147                 while (val == 10 || val == 32)
148                         val = this._binaryReader.ReadChar();
149                 String str = readString().Trim(); 
150                 Value = CharType.FromString(val+str);
151         }
152
153         public override void Input(out float Value)
154         {
155                 char val = this._binaryReader.ReadChar();
156                 while (val == 10 || val == 32)
157                         val = this._binaryReader.ReadChar();
158                 String str = readString().Trim(); 
159                 Value = SingleType.FromString(val+str);
160         }
161
162         public override void Input(out double Value)
163         {
164                 char val = this._binaryReader.ReadChar();
165                 while (val == 10 || val == 32)
166                         val = this._binaryReader.ReadChar();
167                 String str = readString().Trim(); 
168                 Value = DoubleType.FromString(val+str);
169         }
170
171         public override void Input(out Decimal Value)
172         {
173                 char val = (char)this._binaryReader.PeekChar();
174                 while (val == 10 || val == 32)
175                 {
176                         this._binaryReader.ReadChar();
177                         val = (char)this._binaryReader.PeekChar();
178                 }
179                 String str = readString().Trim(); 
180                 Value = DecimalType.FromString(str);        
181         }
182
183         public override string Input(string val)
184         {
185                 return readString()/*.Trim()*/;
186         }
187
188         public DateTime Input( DateTime Value)
189         {
190                 String str = readString().Trim();
191                 if (str[0] == '#' && str.Length != 1)
192                         str = str.Substring(1, str.Length - 1);
193                 return DateTime.Parse(str);
194         }
195     
196         public  override string InputString(int count)
197         {
198                 int i = 0;
199                 StringBuilder sb = new StringBuilder("");
200                 int current = 0;
201                 while (i < count)
202                 {
203                         current = this._binaryReader.Read();
204                         if (current == -1 || current == 26)
205                                 throw (EndOfStreamException) ExceptionUtils.VbMakeException(VBErrors.EndOfFile);
206                         sb.Append((char)current);
207                         i++;            
208                 }
209                 return sb.ToString();
210         }
211
212     
213         public override long seek()
214         {
215                 return getPosition() + 1;
216         }
217     
218         public override void seek(long position)
219         {
220                 if (position <= 0)
221                         throw (IOException) ExceptionUtils.VbMakeException(
222                                                                            VBErrors.BadRecordNum);
223                 setPosition(position - 1);
224         }
225     
226         public override string readLine()
227         {
228                 return readString();
229         }
230 }