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.
27 using Microsoft.VisualBasic;
28 using Microsoft.VisualBasic.CompilerServices;
34 public class RandomVBFile : BaseVBFile
36 protected BinaryWriter _binaryWrite;
37 protected BinaryReader _binaryReader;
38 protected Encoding _encoding;
41 public RandomVBFile(String fileName, OpenMode mode, OpenAccess access,int recordLength)
42 : base(fileName, mode,
43 (access == OpenAccess.Default) ? OpenAccess.ReadWrite : (OpenAccess) access, recordLength)
46 if (access != OpenAccess.Read)
47 _binaryWrite = new BinaryWriter(_fileStream);
48 if (access != OpenAccess.Write)
49 _binaryReader = new BinaryReader(_fileStream);
50 _encoding = new UTF8Encoding(false, true);
53 protected bool getBoolean(long recordNumber,bool isSetRecord)
56 setRecord(recordNumber);
57 bool tmp = _binaryReader.ReadBoolean();
61 * @see Microsoft.VisualBasic.VBFile#getChar(long)
63 protected char getChar(long recordNumber,bool isSetRecord)
66 setRecord(recordNumber);
67 char tmp = _binaryReader.ReadChar();
71 * @see Microsoft.VisualBasic.VBFile#getByte(long)
73 protected byte getByte(long recordNumber,bool isSetRecord)
76 setRecord(recordNumber);
77 int tmp = _binaryReader.ReadByte();
81 * @see Microsoft.VisualBasic.VBFile#getShort(long)
83 protected short getShort(long recordNumber,bool isSetRecord)
86 setRecord(recordNumber);
87 short tmp = _binaryReader.ReadInt16();
91 * @see Microsoft.VisualBasic.VBFile#getInt(long)
93 protected int getInt(long recordNumber,bool isSetRecord)
96 setRecord(recordNumber);
97 int tmp = _binaryReader.ReadInt32();
101 * @see Microsoft.VisualBasic.VBFile#getLong(long)
103 protected long getLong(long recordNumber,bool isSetRecord)
106 setRecord(recordNumber);
107 long tmp = _binaryReader.ReadInt64();
111 * @see Microsoft.VisualBasic.VBFile#getFloat(long)
113 protected float getFloat(long recordNumber,bool isSetRecord)
116 setRecord(recordNumber);
117 float tmp = _binaryReader.ReadSingle();
121 * @see Microsoft.VisualBasic.VBFile#getDouble(long)
123 protected double getDouble(long recordNumber,bool isSetRecord)
126 setRecord(recordNumber);
127 double tmp = _binaryReader.ReadDouble();
131 protected Decimal getDecimal(long recordNumber,bool isSetRecord)
134 setRecord(recordNumber);
135 Decimal tmp = _binaryReader.ReadDecimal();
139 protected DateTime getDateTime(long recordNumber,bool isSetRecord)
142 setRecord(recordNumber);
144 double tmp = _binaryReader.ReadDouble();
145 return DateTime.FromOADate(tmp);
148 public override void get(out bool value,long recordNumber)
150 checkReadPermision();
151 value = getBoolean(recordNumber,true)? true : false;
154 public override void get(out byte value,long recordNumber)
156 checkReadPermision();
157 value = getByte(recordNumber,true);
160 public override void get(out short value,long recordNumber)
162 checkReadPermision();
163 value = getShort(recordNumber,true);
166 public override void get(out char value,long recordNumber)
168 checkReadPermision();
169 value = getChar(recordNumber,true);
172 public override void get(out int value, long recordNumber)
174 checkReadPermision();
175 value = getInt(recordNumber,true);
178 public override void get(out long value, long recordNumber)
180 checkReadPermision();
181 value = getLong(recordNumber,true);
184 public override void get(out float value,long recordNumber)
186 checkReadPermision();
187 value = getFloat(recordNumber,true);
190 public override void get(out double value,long recordNumber)
192 checkReadPermision();
193 value = getDouble(recordNumber,true);
196 public override void get(out DateTime value,long recordNumber)
198 checkReadPermision();
199 value = getDateTime(recordNumber,true);
202 public override void get(out Decimal value,long recordNumber)
204 checkReadPermision();
205 Decimal tmp = getDecimal(recordNumber,true);
209 public override void get(ref string str, long recordNumber, bool bIgnored)
211 checkReadPermision();
213 if (str == null || str.Length == 0)
214 strLen = _binaryReader.ReadInt16();
217 str = new String(_binaryReader.ReadChars(strLen));
220 public override void get(ref object value,long recordNumber)
222 checkReadPermision();
223 if (value.GetType().IsArray) {
224 // need to figure out how to convert from Object& to Array &
225 // get(value,recordNumber,false,false);
226 throw new NotImplementedException();
230 setRecord(recordNumber);
231 value = _binaryReader.ReadString();
235 public override void get(ref Array value, long recordNum, bool arrIsDynamic, bool strIsFixedLen)
237 checkReadPermision();
240 Type type = arr.GetType().GetElementType();
241 int rank = (arr as Array).Rank;
242 if (rank == 0 || rank > 2)
243 throw new ArgumentException(Utils.GetResourceString("Argument_UnsupportedArrayDimensions"));
245 if (getPosition() >= getLength())
249 base.setRecord(recordNum);
252 if (strIsFixedLen && (type == typeof(string)))
255 obj = (arr as Array).GetValue(0);
257 obj = (arr as Array).GetValue(0, 0);
259 strLen = ((String) obj).Length;
261 throw new ArgumentException(
262 Utils.GetResourceString(
263 "Argument_InvalidFixedLengthString"));
265 int len1 = (arr as Array).GetLength(0);
266 int len2 = (rank == 2) ? (arr as Array).GetLength(1):0;
271 for (int i = 0 ; i < len1 ; i++)
274 val = new String(_binaryReader.ReadChars(strLen));
277 strLen = _binaryReader.ReadInt16();
278 val = new String(_binaryReader.ReadChars(strLen));
280 (arr as Array).SetValue(val,i);
285 for (int i = 0 ; i < len1 ; i++)
287 for (int j = 0 ; j < len2 ; j++)
289 val = this._binaryReader.ReadString();
290 if (strIsFixedLen && val.Length != strLen)
291 throw new ArgumentException(
292 Utils.GetResourceString(
293 "Argument_InvalidFixedLengthString"));
294 (arr as Array).SetValue(val,i,j);
301 public override void put(bool val, long recordNumber)
303 checkWritePermision();
304 putBoolean(val,recordNumber,true);
307 * @see Microsoft.VisualBasic.VBFile#putChar(char, long)
309 public override void put(byte value, long recordNumber)
311 checkWritePermision();
312 putByte(value,recordNumber,true);
315 public override void put(short value, long recordNumber)
317 checkWritePermision();
318 putShort(value,recordNumber,true);
322 public override void put( char value, long recordNumber)
324 checkWritePermision();
325 putChar(value,recordNumber,true);
329 public override void put(int value, long recordNumber)
331 checkWritePermision();
332 putInt(value,recordNumber,true);
335 public override void put(long value, long recordNumber)
337 checkWritePermision();
338 putLong(value,recordNumber,true);
341 public override void put(float value, long recordNumber)
343 checkWritePermision();
344 putSingle(value,recordNumber,true);
347 public override void put(double value, long recordNumber)
349 checkWritePermision();
350 putDouble(value,recordNumber,true);
353 public override void put(Decimal value, long recordNumber)
355 checkWritePermision();
356 putDecimal(value,recordNumber,true);
359 public override void put(String value,long recordNumber,bool stringIsFixedLength)
361 checkWritePermision();
362 if (value.IndexOf('\"') == -1)
363 putString(value,recordNumber,stringIsFixedLength,true);
366 for (int i = 0 ; i < value.Length;i++)
367 _binaryWrite.Write(value[i]);
372 public override void put(Object arr,long recordNum,
373 bool arrIsDynamic,bool strIsFixedLen)
375 checkWritePermision();
378 throw new ArgumentException(
379 Utils.GetResourceString("Argument_ArrayNotInitialized"));
381 Type type = arr.GetType().GetElementType();
382 int rank = (arr as Array).Rank;
383 if (rank == 0 || rank > 2)
384 throw new ArgumentException(
385 Utils.GetResourceString("Argument_UnsupportedArrayDimensions"));
386 base.setRecord(recordNum);
389 if (strIsFixedLen && (type == typeof(string)))
392 obj = (arr as Array).GetValue(0);
394 obj = (arr as Array).GetValue(0, 0);
396 strLen = ((String) obj).Length;
398 throw new ArgumentException(
399 Utils.GetResourceString(
400 "Argument_InvalidFixedLengthString"));
402 int len1 = (arr as Array).GetLength(0);
403 int len2 = (rank == 2) ? (arr as Array).GetLength(1) : 0;
406 for (int i = 0 ; i < len1 ; i++)
408 putObject((arr as Array).GetValue(i), recordNum , type,strIsFixedLen);
413 for (int i = 0 ; i < len1 ; i++)
415 for (int j = 0 ; j < len2 ; j++)
417 putObject((arr as Array).GetValue(i), recordNum , type,strIsFixedLen);
423 private void putObject(Object obj ,long recordNumber ,Type type,bool strIsFixedLen)
426 putString((string)obj,recordNumber,strIsFixedLen,false);
427 else if (obj is bool)
428 putBoolean((bool)obj, recordNumber,false);
429 else if (obj is char)
430 putChar((char)obj, recordNumber,false);
431 else if (obj is byte)
432 putByte((byte)obj, recordNumber,false);
433 else if (obj is short)
434 putShort((short)obj,recordNumber,false);
436 putInt((int)obj, recordNumber,false);
437 else if (obj is long)
438 putLong((long)obj, recordNumber,false);
439 else if (obj is double)
440 putDouble((double)obj,recordNumber,false);
441 else if (obj is float)
442 putSingle((float)obj, recordNumber,false);
443 else if (obj is DateTime)
444 putDatetime((DateTime)obj,recordNumber,false);
448 public override void put(DateTime value,long recordNumber)
450 checkWritePermision();
451 putDatetime(value,recordNumber,true);
454 protected void putBoolean(bool val, long recordNumber,bool isSetRecord)
457 setRecord(recordNumber);
460 _binaryWrite.Write(val);
462 _binaryWrite.Write(val);
463 // setPosition(getPosition()+2);
466 * @see Microsoft.VisualBasic.VBFile#putChar(char, long)
468 protected void putChar(char val, long recordNumber,bool isSetRecord)
471 setRecord(recordNumber);
473 _binaryWrite.Write(val);
474 // setPosition(getPosition()+2);
478 * @see Microsoft.VisualBasic.VBFile#putShort(short, long)
480 protected void putShort(short val, long recordNumber,bool isSetRecord)
483 setRecord(recordNumber);
485 _binaryWrite.Write(val);
486 // setPosition(getPosition()+2);
489 protected void putByte(byte val, long recordNumber,bool isSetRecord)
492 setRecord(recordNumber);
494 _binaryWrite.Write(val);
495 // setPosition(getPosition()+2);
498 * @see Microsoft.VisualBasic.VBFile#putInt(int, long)
500 protected void putInt(int val, long recordNumber,bool isSetRecord)
503 setRecord(recordNumber);
505 _binaryWrite.Write(val);
506 // setPosition(getPosition()+4);
509 * @see Microsoft.VisualBasic.VBFile#putLong(long, long)
511 protected void putLong(long val, long recordNumber,bool isSetRecord)
514 setRecord(recordNumber);
516 _binaryWrite.Write(val);
517 // setPosition(getPosition()+8);
520 * @see Microsoft.VisualBasic.VBFile#putFloat(float, long)
522 protected void putSingle(float val, long recordNumber,bool isSetRecord)
525 setRecord(recordNumber);
527 _binaryWrite.Write(val);
528 // setPosition(getPosition()+4);
531 * @see Microsoft.VisualBasic.VBFile#putDouble(double, long)
533 protected void putDouble(double val, long recordNumber,bool isSetRecord)
536 setRecord(recordNumber);
538 _binaryWrite.Write(val);
539 // setPosition(getPosition()+8);
542 protected void putDecimal(Decimal val, long recordNumber,bool isSetRecord)
545 setRecord(recordNumber);
547 _binaryWrite.Write(val);
548 // setPosition(getPosition()+8);
551 protected void putDatetime(DateTime val, long recordNumber,bool isSetRecord)
554 setRecord(recordNumber);
556 _binaryWrite.Write(val.ToOADate());
557 // setPosition(getPosition()+8);
560 public void putString(
563 bool stringIsFixedLength,bool isSetRecord)
567 int byteCount = _encoding.GetByteCount(value);
569 setRecord(recordNumber);
570 if (stringIsFixedLength)
571 checkLength(byteCount);
574 checkLength(byteCount+2);
575 _binaryWrite.Write((short)byteCount);
578 for (int i = 0 ; i < byteCount ; i++)
579 _binaryWrite.Write(value[i]);
580 // setPosition(getPosition()+((stringIsFixedLength) ? byteCount :byteCount+2));
583 public override long seek()
586 throw ExceptionUtils.VbMakeException(VBErrors.InternalError);
587 return ((getPosition() + _recordLen - 1) / _recordLen);
590 public override void seek(long position)
592 _fileStream.Position = position;
595 public override bool isEndOfFile()
597 return (this._fileStream.Length == this._fileStream.Position);
600 public override void width(int fileNumber, int RecordWidth)
602 if (RecordWidth < 0 || RecordWidth > 255)
603 throw (ArgumentException) ExceptionUtils.VbMakeException(
604 VBErrors.IllegalFuncCall);
605 _width = RecordWidth;