public RString(byte[] buffer, CharEncoding encoding) { Encoding enc; switch (encoding) { case CharEncoding.Native: enc = Encoding.GetEncoding("us-ascii"); break; case CharEncoding.Utf8: enc = Encoding.GetEncoding("utf-8"); break; case CharEncoding.Latin1: enc = Encoding.GetEncoding("iso-8859-1"); break; case CharEncoding.Bytes: enc = Encoding.GetEncoding("utf-8"); break; default: throw new ArgumentOutOfRangeException(); } _text = enc.GetString(buffer, 0, buffer.Length); }
private object DecodeBytes(byte[] data, int fieldIndex) { var field = Fields[fieldIndex]; try { var type = field.DbfType; if (type == DbfType.Double || type == DbfType.Numeric) { return(DecodeFloatPoint(data, field, type)); } if (type == DbfType.Date) { return(DecodeDate(data)); } if (type == DbfType.Logical) { return(DecodeLogical(data)); } if (type == DbfType.Memo) { return(DecodeMemo(data, fieldIndex)); } return(CharEncoding.GetString(data, 0, field.FieldLength).TrimEnd()); } catch (Exception e) { var msg = String.Format("Unable to decode field [{0}], value <{1}>", field.Name, CharEncoding.GetString(data, 0, field.FieldLength)); throw new DbfReaderException(msg, e); } }
public StreamInImpl(Stream stream, CharEncoding encoding, int tabsize) { this.stream = stream; pushed = false; c = (int)'\x0000'; tabs = 0; this.tabsize = tabsize; curline = 1; curcol = 1; this.encoding = encoding; state = FSM_ASCII; endOfStream = false; }
public StreamInImpl(Stream stream, CharEncoding encoding, int tabsize) { Stream = stream; Pushed = false; Character = '\x0000'; Tabs = 0; TabSize = tabsize; CursorLine = 1; CursorColumn = 1; Encoding = encoding; State = FSM_ASCII; EndOfStream = false; }
public StreamInImpl(Stream stream, CharEncoding encoding, int tabsize) { this.stream = stream; pushed = false; c = (int) '\x0000'; tabs = 0; this.tabsize = tabsize; curline = 1; curcol = 1; this.encoding = encoding; state = FSM_ASCII; endOfStream = false; }
private void LoadControllers() { const string EncoderRegexFmt = @"\p{Is{0}}"; Array encodings = Enum.GetValues(typeof(CharEncoding)); _detectors = new Dictionary <CharEncoding, Regex>(encodings.Length); foreach (object encoding in encodings) { CharEncoding encd = (CharEncoding)encoding; if (encd != CharEncoding.Unknown) { _detectors.Add(encd, new Regex(string.Format(EncoderRegexFmt, encd.ToString()))); } } }
private object DecodeDate(byte[] data) { var strYear = CharEncoding.GetString(data, 0, 4); var strMonth = CharEncoding.GetString(data, 4, 2); var strDay = CharEncoding.GetString(data, 6, 2); object ret = null; int year, month, day; if (Int32.TryParse(strYear, out year) && Int32.TryParse(strMonth, out month) && Int32.TryParse(strDay, out day)) { ret = new DateTime(year, month, day); } return(ret); }
/// <summary> /// The ToChars. /// </summary> /// <param name="ba">The ba<see cref="Byte[]"/>.</param> /// <param name="tf">The tf<see cref="CharEncoding"/>.</param> /// <returns>The <see cref="Char[]"/>.</returns> public static Char[] ToChars(this Byte[] ba, CharEncoding tf = CharEncoding.ASCII) { switch (tf) { case CharEncoding.ASCII: return(Encoding.ASCII.GetChars(ba)); case CharEncoding.UTF8: return(Encoding.UTF8.GetChars(ba)); case CharEncoding.Unicode: return(Encoding.Unicode.GetChars(ba)); default: return(Encoding.ASCII.GetChars(ba)); } }
/// <summary> /// The ToBytes. /// </summary> /// <param name="ca">The ca<see cref="String"/>.</param> /// <param name="tf">The tf<see cref="CharEncoding"/>.</param> /// <returns>The <see cref="Byte[]"/>.</returns> public static Byte[] ToBytes(this String ca, CharEncoding tf = CharEncoding.ASCII) { switch (tf) { case CharEncoding.ASCII: return(Encoding.ASCII.GetBytes(ca)); case CharEncoding.UTF8: return(Encoding.UTF8.GetBytes(ca)); case CharEncoding.Unicode: return(Encoding.Unicode.GetBytes(ca)); default: return(Encoding.ASCII.GetBytes(ca)); } }
private object DecodeFloatPoint(byte[] data, DbfField field, DbfType type) { object ret = null; var str = CharEncoding.GetString(data, 0, field.FieldLength); if (str.Length != 0) { var last = str[str.Length - 1]; if (last != ' ' && last != '?') { var culture = CultureInfo.InvariantCulture; str = str.Replace(',', '.'); ret = type == DbfType.Double ? (object)Double.Parse(str, Styles, culture) : Decimal.Parse(str, Styles, culture); } } return(ret); }
private object DecodeMemo(byte[] data, int fieldIndex) { if (DbtReader == null) { throw new DbfReaderException("Unable to read memo file"); } var str = CharEncoding.GetString(data, 0, Fields[fieldIndex].FieldLength); if (string.IsNullOrWhiteSpace(str)) { return(null); } long index; if (!long.TryParse(str.Trim(), out index)) { return(null); } // reading memo file DbtReader.BaseStream.Seek((index + 1) << 9, SeekOrigin.Begin); var ms = new MemoryStream(); var xCnt = 0; // read until [0x1a, 0x1a] in file while (xCnt < 2) { var bt = DbtReader.ReadByte(); ms.WriteByte(bt); xCnt = bt == 0x1a ? xCnt + 1 : 0; } var memo = ms.ToArray(); var retString = CharEncoding.GetString(new byte[] { 0x8d, 0x0a }); return(CharEncoding.GetString(memo, 0, memo.Length - 2).Replace(retString, string.Empty)); }
internal (Object[], long) NextRecord(IEnumerable <int> selectIndexes, IList <int> sortedIndexes) { if (_IsClosed) { throw new DBFException("Source is not open"); } var position = _BaseStream.Position; var tOrderdSelectIndexes = sortedIndexes; var recordObjects = new Object[_Header.FieldArray.Length]; try { var isDeleted = false; do { if (isDeleted) { position += _BinaryRead.ReadBytes(_Header.RecordLength - 1).Length; } int t_byte = _BinaryRead.ReadByte(); if (t_byte == DBFFieldType.EndOfData) { return(null, position); } position++; isDeleted = (t_byte == '*'); } while (isDeleted); position = position - 1; var j = 0; var k = -1; for (var i = 0; i < _Header.FieldArray.Length; i++) { if (tOrderdSelectIndexes.Count == j && j != 0 || (tOrderdSelectIndexes.Count > j && tOrderdSelectIndexes[j] > i && tOrderdSelectIndexes[j] != k)) { _BaseStream.Seek(_Header.FieldArray[i].FieldLength, SeekOrigin.Current); continue; } if (tOrderdSelectIndexes.Count > j) { k = tOrderdSelectIndexes[j]; } j++; switch (_Header.FieldArray[i].DataType) { case NativeDbType.UnicodeChar: var b_arrayU = new byte[ _Header.FieldArray[i].FieldLength ]; _BinaryRead.Read(b_arrayU, 0, b_arrayU.Length); recordObjects[i] = _UCharEncoding.GetString(b_arrayU).TrimEnd('\0', ' '); break; case NativeDbType.Char: var b_array = new byte[ _Header.FieldArray[i].FieldLength ]; _BinaryRead.Read(b_array, 0, b_array.Length); recordObjects[i] = CharEncoding.GetString(b_array).TrimEnd(); break; case NativeDbType.Date: var t_byte_year = new byte[4]; _BinaryRead.Read(t_byte_year, 0, t_byte_year.Length); var t_byte_month = new byte[2]; _BinaryRead.Read(t_byte_month, 0, t_byte_month.Length); var t_byte_day = new byte[2]; _BinaryRead.Read(t_byte_day, 0, t_byte_day.Length); try { var tYear = CharEncoding.GetString(t_byte_year); var tMonth = CharEncoding.GetString(t_byte_month); var tDay = CharEncoding.GetString(t_byte_day); int tIntYear, tIntMonth, tIntDay; if (int.TryParse(tYear, out tIntYear) && int.TryParse(tMonth, out tIntMonth) && int.TryParse(tDay, out tIntDay)) { recordObjects[i] = new DateTime( tIntYear, tIntMonth, tIntDay); } else { recordObjects[i] = null; } } catch (ArgumentOutOfRangeException) { /* this field may be empty or may have improper value set */ recordObjects[i] = null; } break; case NativeDbType.Float: try { var t_float = new byte[ _Header.FieldArray[i].FieldLength ]; _BinaryRead.Read(t_float, 0, t_float.Length); var tParsed = CharEncoding.GetString(t_float); var tLast = tParsed.Substring(tParsed.Length - 1); if (tParsed.Length > 0 && tLast != " " && tLast != NullSymbol) { recordObjects[i] = double.Parse(tParsed, NumberStyles.Float | NumberStyles.AllowLeadingWhite, NumberFormatInfo.InvariantInfo); } else { recordObjects[i] = null; } } catch (FormatException e) { throw new DBFException("Failed to parse Float", e); } break; case NativeDbType.Numeric: try { var t_numeric = new byte[ _Header.FieldArray[i].FieldLength ]; _BinaryRead.Read(t_numeric, 0, t_numeric.Length); var tParsed = CharEncoding.GetString(t_numeric); var tLast = tParsed.Substring(tParsed.Length - 1); if (tParsed.Length > 0 && tLast != " " && tLast != NullSymbol) { recordObjects[i] = Decimal.Parse(tParsed, NumberStyles.Float | NumberStyles.AllowLeadingWhite, NumberFormatInfo.InvariantInfo); } else { recordObjects[i] = null; } } catch (FormatException e) { throw new DBFException( "Failed to parse Number", e); } break; case NativeDbType.Logical: var t_logical = _BinaryRead.ReadByte(); //todo find out whats really valid if (t_logical == 'Y' || t_logical == 't' || t_logical == 'T' || t_logical == 't') { recordObjects[i] = true; } else if (t_logical == DBFFieldType.UnknownByte) { recordObjects[i] = DBNull.Value; } else { recordObjects[i] = false; } break; case NativeDbType.Memo: //if (String.IsNullOrEmpty(streamMemoLoc) && streamMemo == null) if (_StreamMemo == null) { throw new Exception("Memo Location Not Set"); } var tRawMemoPointer = _BinaryRead.ReadBytes(_Header.FieldArray[i].FieldLength); var tMemoPoiner = CharEncoding.GetString(tRawMemoPointer); if (string.IsNullOrEmpty(tMemoPoiner)) { recordObjects[i] = DBNull.Value; break; } long tBlock; if (!long.TryParse(tMemoPoiner, out tBlock)) { //Because Memo files can vary and are often the least importat data, //we will return null when it doesn't match our format. recordObjects[i] = DBNull.Value; break; } //recordObjects[i] = new MemoValue(tBlock, this, streamMemoLoc, GetLazyStreamFromLocation()); recordObjects[i] = new MemoValue(tBlock, this, _StreamMemo); break; default: _BinaryRead.ReadBytes(_Header.FieldArray[i].FieldLength); recordObjects[i] = DBNull.Value; break; } } } catch (EndOfStreamException) { return(null, position); } catch (IOException e) { throw new DBFException("Problem Reading File", e); } return(selectIndexes.Any() ? selectIndexes.Select(it => recordObjects[it]).ToArray() : recordObjects, position); }
/// <summary> /// Decodes a single Unicode character from the buffer and advances the buffer's position. /// </summary> /// <param name="buffer">The buffer to read.</param> /// <returns>A Unicode character.</returns> public static char?DecodeChar(ByteBuffer buffer) { return(CharEncoding.Decode(buffer, 0)); }
/// <summary> /// Encodes a single Unicode character and appends the bytes to the buffer. /// </summary> /// <param name="data">The Unicode character.</param> /// <param name="buffer">The destination buffer.</param> public static void EncodeChar(char?data, ByteBuffer buffer) { CharEncoding.Encode(data, buffer); }
/// <summary> /// Gets the encode size of a single Unicode character. /// </summary> /// <param name="value">The Unicode character.</param> /// <returns>Encode size in bytes of the Unicode character.</returns> public static int GetCharEncodeSize(char?value) { return(CharEncoding.GetEncodeSize(value)); }
internal Object[] NextRecord(IEnumerable <int> selectIndexes, IList <int> sortedIndexes) { if (isClosed) { throw new DBFException("Source is not open"); } IList <int> tOrderdSelectIndexes = sortedIndexes; var recordObjects = new Object[_header.FieldArray.Length]; try { bool isDeleted = false; do { if (isDeleted) { _dataInputStream.ReadBytes(_header.RecordLength - 1); } int t_byte = _dataInputStream.ReadByte(); if (t_byte == DBFFieldType.EndOfData) { return(null); } isDeleted = (t_byte == '*'); } while (isDeleted); int j = 0; int k = -1; for (int i = 0; i < _header.FieldArray.Length; i++) { if (tOrderdSelectIndexes.Count == j && j != 0 || (tOrderdSelectIndexes.Count > j && tOrderdSelectIndexes[j] > i && tOrderdSelectIndexes[j] != k)) { _dataInputStream.BaseStream.Seek(_header.FieldArray[i].FieldLength, SeekOrigin.Current); continue; } if (tOrderdSelectIndexes.Count > j) { k = tOrderdSelectIndexes[j]; } j++; switch (_header.FieldArray[i].DataType) { case NativeDbType.Char: var b_array = new byte[ _header.FieldArray[i].FieldLength ]; _dataInputStream.Read(b_array, 0, b_array.Length); //StringBuilder sb = new StringBuilder(); //for (int c = 0; c < b_array.Length && b_array[c] != 0; c++) //{ // sb.Append((char)b_array[c]); //} //recordObjects[i] = sb.ToString().TrimEnd(); recordObjects[i] = CharEncoding.GetString(b_array).TrimEnd(); //recordObjects[i] = Encoding.GetEncoding("ISO-8859-1").GetString(b_array).TrimEnd(); break; case NativeDbType.Date: byte[] t_byte_year = new byte[4]; _dataInputStream.Read(t_byte_year, 0, t_byte_year.Length); byte[] t_byte_month = new byte[2]; _dataInputStream.Read(t_byte_month, 0, t_byte_month.Length); byte[] t_byte_day = new byte[2]; _dataInputStream.Read(t_byte_day, 0, t_byte_day.Length); try { var tYear = CharEncoding.GetString(t_byte_year); var tMonth = CharEncoding.GetString(t_byte_month); var tDay = CharEncoding.GetString(t_byte_day); int tIntYear, tIntMonth, tIntDay; if (Int32.TryParse(tYear, out tIntYear) && Int32.TryParse(tMonth, out tIntMonth) && Int32.TryParse(tDay, out tIntDay)) { recordObjects[i] = new DateTime( tIntYear, tIntMonth, tIntDay); } else { recordObjects[i] = null; } } catch (ArgumentOutOfRangeException) { /* this field may be empty or may have improper value set */ recordObjects[i] = null; } break; case NativeDbType.Float: try { byte[] t_float = new byte[ _header.FieldArray[i].FieldLength ]; _dataInputStream.Read(t_float, 0, t_float.Length); String tParsed = CharEncoding.GetString(t_float); var tLast = tParsed.Substring(tParsed.Length - 1); if (tParsed.Length > 0 && tLast != " " && tLast != DBFFieldType.Unknown) { //recordObjects[i] = Double.Parse(tParsed, NumberStyles.Float | NumberStyles.AllowLeadingWhite); recordObjects[i] = GAPPSF.Utils.Conversion.StringToDouble(tParsed); } else { recordObjects[i] = null; } } catch (FormatException e) { throw new DBFException("Failed to parse Float", e); } break; case NativeDbType.Numeric: try { byte[] t_numeric = new byte[ _header.FieldArray[i].FieldLength ]; _dataInputStream.Read(t_numeric, 0, t_numeric.Length); string tParsed = CharEncoding.GetString(t_numeric); var tLast = tParsed.Substring(tParsed.Length - 1); if (tParsed.Length > 0 && tLast != " " && tLast != DBFFieldType.Unknown) { //recordObjects[i] = Decimal.Parse(tParsed, NumberStyles.Float | NumberStyles.AllowLeadingWhite); recordObjects[i] = GAPPSF.Utils.Conversion.StringToDouble(tParsed); } else { recordObjects[i] = null; } } catch (FormatException e) { throw new DBFException( "Failed to parse Number", e); } break; case NativeDbType.Logical: byte t_logical = _dataInputStream.ReadByte(); //todo find out whats really valid if (t_logical == 'Y' || t_logical == 't' || t_logical == 'T' || t_logical == 't') { recordObjects[i] = true; } else if (t_logical == DBFFieldType.UnknownByte) { recordObjects[i] = DBNull.Value; } else { recordObjects[i] = false; } break; case NativeDbType.Memo: if (string.IsNullOrEmpty(_dataMemoLoc)) { throw new Exception("Memo Location Not Set"); } var tRawMemoPointer = _dataInputStream.ReadBytes(_header.FieldArray[i].FieldLength); var tMemoPoiner = CharEncoding.GetString(tRawMemoPointer); if (string.IsNullOrEmpty(tMemoPoiner)) { recordObjects[i] = DBNull.Value; break; } long tBlock; if (!long.TryParse(tMemoPoiner, out tBlock)) { //Because Memo files can vary and are often the least importat data, //we will return null when it doesn't match our format. recordObjects[i] = DBNull.Value; break; } recordObjects[i] = new MemoValue(tBlock, this, _dataMemoLoc); break; default: _dataInputStream.ReadBytes(_header.FieldArray[i].FieldLength); recordObjects[i] = DBNull.Value; break; } } } catch (EndOfStreamException) { return(null); } catch (IOException e) { throw new DBFException("Problem Reading File", e); } return(selectIndexes.Any() ? selectIndexes.Select(it => recordObjects[it]).ToArray() : recordObjects); }
private void WriteRecord(BinaryWriter dataOutput, DBFRecord record) { if (!record.IsDirty()) { return; } //var originalPosition = _BaseStream.Position; _BaseStream.Seek(record.Position, SeekOrigin.Begin); dataOutput.Write((byte)' '); for (var j = 0; j < _Header.FieldArray.Length; j++) { /* iterate throught fields */ if (!record.IsDirty(j)) { _BaseStream.Seek(_Header.FieldArray[j].FieldLength, SeekOrigin.Current); continue; } var objectArray = record.ValueArray; switch (_Header.FieldArray[j].DataType) { case NativeDbType.UnicodeChar: var str_valueU = (objectArray[j] != null && objectArray[j] != DBNull.Value) ? objectArray[j].ToString() : ""; var bufferU = Utils.textPadding(str_valueU, _UCharEncoding, _Header.FieldArray[j].FieldLength, Utils.ALIGN_LEFT, (byte)0x00 ); dataOutput.Write(bufferU); break; case NativeDbType.Char: var str_value = (objectArray[j] != null && objectArray[j] != DBNull.Value) ? objectArray[j].ToString() : ""; var buffer = Utils.textPadding(str_value, CharEncoding, _Header.FieldArray[j].FieldLength ); dataOutput.Write(buffer); break; case NativeDbType.Date: if (objectArray[j] != null && objectArray[j] != DBNull.Value) { var tDate = (DateTime)objectArray[j]; dataOutput.Write( CharEncoding.GetBytes(tDate.ToString("yyyyMMdd"))); } else { dataOutput.Write( Utils.FillArray(new byte[8], DBFFieldType.Space)); } break; case NativeDbType.Float: if (objectArray[j] != null && objectArray[j] != DBNull.Value) { var tDouble = Convert.ToDouble(objectArray[j]); dataOutput.Write( Utils.NumericFormating( tDouble, CharEncoding, _Header.FieldArray[j].FieldLength, _Header.FieldArray[j].DecimalCount ) ); } else { dataOutput.Write( Utils.textPadding( NullSymbol, CharEncoding, _Header.FieldArray[j].FieldLength, Utils.ALIGN_RIGHT ) ); } break; case NativeDbType.Numeric: if (objectArray[j] != null && objectArray[j] != DBNull.Value) { var tDecimal = Convert.ToDecimal(objectArray[j]); dataOutput.Write( Utils.NumericFormating( tDecimal, CharEncoding, _Header.FieldArray[j].FieldLength, _Header.FieldArray[j].DecimalCount ) ); } else { dataOutput.Write( Utils.textPadding( NullSymbol, CharEncoding, _Header.FieldArray[j].FieldLength, Utils.ALIGN_RIGHT ) ); } break; case NativeDbType.Logical: if (objectArray[j] != null && objectArray[j] != DBNull.Value) { if ((bool)objectArray[j]) { dataOutput.Write(DBFFieldType.True); } else { dataOutput.Write(DBFFieldType.False); } } else { dataOutput.Write(DBFFieldType.UnknownByte); } break; case NativeDbType.Memo: if (objectArray[j] != null && objectArray[j] != DBNull.Value) { var tMemoValue = ((MemoValue)objectArray[j]); tMemoValue.Write(this); dataOutput.Write(Utils.NumericFormating(tMemoValue.Block, CharEncoding, 10, 0)); } else { dataOutput.Write( Utils.textPadding("", CharEncoding, 10 ) ); } break; default: throw new DBFException("Unknown field type " + _Header.FieldArray[j].DataType); } } /* iterating through the fields */ _BinaryWriter.Flush(); //_BaseStream.Seek(originalPosition, SeekOrigin.Begin); }
private void WriteRecord(BinaryWriter dataOutput, Object[] objectArray) { dataOutput.Write((byte)' '); for (int j = 0; j < header.FieldArray.Length; j++) { /* iterate throught fields */ switch (header.FieldArray[j].DataType) { case NativeDbType.Char: if (objectArray[j] != null && objectArray[j] != DBNull.Value) { String str_value = objectArray[j].ToString(); dataOutput.Write( Utils.textPadding(str_value, CharEncoding, header.FieldArray[j]. FieldLength ) ); } else { dataOutput.Write( Utils.textPadding("", CharEncoding, header.FieldArray[j]. FieldLength ) ); } break; case NativeDbType.Date: if (objectArray[j] != null && objectArray[j] != DBNull.Value) { DateTime tDate = (DateTime)objectArray[j]; dataOutput.Write( CharEncoding.GetBytes(tDate.ToString("yyyyMMdd"))); } else { dataOutput.Write( Utils.FillArray(new byte[8], DBFFieldType.Space)); } break; case NativeDbType.Float: if (objectArray[j] != null && objectArray[j] != DBNull.Value) { Double tDouble = Convert.ToDouble(objectArray[j]); dataOutput.Write( Utils.NumericFormating( tDouble, CharEncoding, header.FieldArray[j].FieldLength, header.FieldArray[j].DecimalCount ) ); } else { dataOutput.Write( Utils.textPadding( DBFFieldType.Unknown, CharEncoding, header.FieldArray[j].FieldLength, Utils.ALIGN_RIGHT ) ); } break; case NativeDbType.Numeric: if (objectArray[j] != null && objectArray[j] != DBNull.Value) { Decimal tDecimal = Convert.ToDecimal(objectArray[j]); dataOutput.Write( Utils.NumericFormating( tDecimal, CharEncoding, header.FieldArray[j].FieldLength, header.FieldArray[j].DecimalCount ) ); } else { dataOutput.Write( Utils.textPadding( DBFFieldType.Unknown, CharEncoding, header.FieldArray[j].FieldLength, Utils.ALIGN_RIGHT ) ); } break; case NativeDbType.Logical: if (objectArray[j] != null && objectArray[j] != DBNull.Value) { if ((bool)objectArray[j]) { dataOutput.Write(DBFFieldType.True); } else { dataOutput.Write(DBFFieldType.False); } } else { dataOutput.Write(DBFFieldType.UnknownByte); } break; case NativeDbType.Memo: if (objectArray[j] != null && objectArray[j] != DBNull.Value) { var tMemoValue = ((MemoValue)objectArray[j]); tMemoValue.Write(this); dataOutput.Write(Utils.NumericFormating(tMemoValue.Block, CharEncoding, 10, 0)); } else { dataOutput.Write( Utils.textPadding("", CharEncoding, 10 ) ); } break; default: throw new DBFException("Unknown field type " + header.FieldArray[j].DataType); } } /* iterating through the fields */ }