**** Merged from MCS ****
[mono-project.git] / mcs / class / Microsoft.VisualBasic / Microsoft.VisualBasic / InputVBFile.cs
blob593d4875be8efdaadac4a0d0422904f955e0202f
1 /*
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.
24 using System;
25 using System.IO;
26 using System.Text;
27 using Microsoft.VisualBasic;
28 using Microsoft.VisualBasic.CompilerServices;
31 public class InputVBFile : BaseVBFile
33 private StreamReader _streamReader;
35 public InputVBFile (string fileName, FileAccess access, int recordLength)
36 : base(fileName, OpenMode.Input, (access == (FileAccess)OpenAccess.Default) ? OpenAccess.Read : (OpenAccess) access, recordLength)
38 if ((access != (FileAccess)OpenAccess.Read) && (access != (FileAccess)OpenAccess.Default))
39 throw new ArgumentException(Utils.GetResourceString("FileSystem_IllegalOutputAccess"));
41 this._streamReader = new StreamReader(_fileStream);
44 public override void closeFile()
46 if (_streamReader != null)
48 _streamReader.Close();
49 _streamReader = null;
51 base.closeFile();
54 private string readString()
56 StringBuilder sb = new StringBuilder("");
57 int cInt = _streamReader.Read();
58 char c = (char)cInt;
60 bool inStr = false;
62 while (cInt != -1 && !(c == ',' && !inStr) && c != '\r')
64 if (c == '\"' && inStr)
66 sb.Append(c);
67 if ((char)_streamReader.Peek() == ',')
68 _streamReader.Read();
69 break;
71 else if (c == '\"')
72 inStr = true;
73 sb.Append(c);
74 cInt = _streamReader.Read();
75 c = (char)cInt;
77 if (c == '\r')
78 _streamReader.Read();
79 if (sb.Length > 1 && sb[0]=='\"' && sb[sb.Length-1] =='\"')
81 sb.Remove(0, 1);
82 sb.Remove(sb.Length-1, 1);
85 return sb.ToString();
88 private string readNumber()
91 StringBuilder sb = new StringBuilder();
92 int cInt = _streamReader.Read();
93 char c = (char)cInt;
95 while (cInt != -1 && c != ',' && c != '\r' && c != 32) //spacebar
97 sb.Append(c);
98 cInt = _streamReader.Read();
99 c = (char)cInt;
102 return sb.ToString();
106 public override void Input(out bool Value)
108 string str = readString().Trim();
110 if (str[0] == '#' && str.Length != 1)
111 str = str.Substring(1, str.Length - 1);
112 Object obj = str;
114 // TODO
115 // Value = BooleanType.FromObject(obj) ? 1 : 0);
117 Value = BooleanType.FromObject(obj);
120 public override void Input(out byte Value)
122 string str = readNumber().Trim();
123 Value = byte.Parse(str);
126 public override void Input(out short Value)
128 string str = readNumber().Trim();
129 Value = short.Parse(str);
132 public override void Input(out int Value)
134 string str = readNumber().Trim();
135 Value = int.Parse(str);
138 public override void Input(out long Value)
140 string str = readNumber().Trim();
141 Value = long.Parse(str);
144 public override void Input(out char Value)
146 string str = readString().Trim();
147 Value = char.Parse(str);
150 public override void Input(out float Value)
152 string str = readNumber().Trim();
153 Value = float.Parse(str);
156 public override void Input(out double Value)
158 string str = readNumber().Trim();
159 Value = double.Parse(str);
162 public override void Input(out Decimal Value)
164 string str = readNumber().Trim();
165 Value = Decimal.Parse(str);
168 public override string Input(string val)
170 return readString().Trim();
173 public override string InputString(int count)
175 int i = 0;
176 StringBuilder sb = new StringBuilder("");
177 int current = 0;
178 while (i < count)
180 current = this._streamReader.Peek();
181 _fileStream.Position = _fileStream.Position+1;
182 if (current == -1 || current == 26)
183 throw (EndOfStreamException) ExceptionUtils.VbMakeException(VBErrors.EndOfFile);
184 sb.Append((char)current);
185 i++;
187 return sb.ToString();
190 public override void Input(out DateTime Value)
192 string str = readString().Trim();
193 if (str[0] == '#' && str.Length != 1)
194 str = str.Substring(1, str.Length - 1);
195 Value = DateTime.Parse(str);
198 public override string readLine()
200 return this._streamReader.ReadLine();
203 public override bool canRead()
205 return true;
208 public override long seek()
210 return getPosition();
213 public override void seek(long position)
215 long nPos = position - 1;
216 if (_fileStream.Length <= nPos)
217 _fileStream.SetLength(nPos);
218 _fileStream.Position = nPos;
221 public override bool isEndOfFile()
223 return (this._streamReader.Peek() == -1);