Example #1
0
        private static Dbffile ReadFields(BinaryReader recbr)
        {
            var    listFieldNameBytes = new List <byte>();
            string fieldName          = "";

            //byte[] fieldNameArray = new byte[11];
            //for (int k = 10; k >= 0; k--)
            //{
            //    fieldNameArray[k] = recbr.ReadByte();
            //}

            for (int i = 0; i < 11; i++)
            {
                byte fieldNameChar = recbr.ReadByte();
                //byte fieldNameChar = fieldNameArray[i];
                if (fieldNameChar == 0)
                {
                    SkipReadByte(10 - i, recbr);
                    break;
                }
                else
                {
                    listFieldNameBytes.Add(fieldNameChar);
                }
            }

            int fieldNameSize = listFieldNameBytes.Count;

            byte[] fieldNameBytes = new byte[fieldNameSize];
            //int m = 0;
            for (int j = 0; j < fieldNameSize; j++)
            //for (int j = fieldNameSize - 1; j >= 0; j--)
            {
                fieldNameBytes[j] = listFieldNameBytes[j];
                //m++;
            }
            // US-ASCII
            //fieldName = System.Text.Encoding.GetEncoding(20127).GetString(fieldNameBytes);
            fieldName = System.Text.Encoding.ASCII.GetString(fieldNameBytes);

            byte[] fieldTypeByte = { 0 };
            string fieldType     = "";

            fieldTypeByte[0] = recbr.ReadByte();
            // US-ASCII
            //fieldType = System.Text.Encoding.GetEncoding(20127).GetString(fieldTypeByte);
            fieldType = System.Text.Encoding.ASCII.GetString(fieldTypeByte);

            SkipReadByte(4, recbr);

            //byte[] fieldLengthByte = { 0 };
            //int fieldLength = 0;
            //fieldLengthByte[0] = recbr.ReadByte();
            //fieldLength = Byte2Int(fieldLengthByte);
            int fieldLength = recbr.ReadByte();

            //byte[] fieldDecimalpartLengthByte = { 0 };
            //int fieldDecimalpartLength = 0;
            //fieldDecimalpartLengthByte[0] = recbr.ReadByte();
            //fieldDecimalpartLength = Byte2Int(fieldDecimalpartLengthByte);
            int fieldDecimalpartLength = recbr.ReadByte();

            Dbffile dbfFields = new Dbffile(new List <string>())
            {
                FieldName              = fieldName,
                FieldType              = fieldType,
                FieldLength            = fieldLength,
                FieldDecimalpartLength = fieldDecimalpartLength
            };

            SkipReadByte(14, recbr);

            return(dbfFields);
        }
Example #2
0
        private static Dbffile[] ReadDbf(string recdbf)
        {
            //Dbffileを開く
            FileStream   fs = new FileStream(recdbf, FileMode.Open);
            BinaryReader br = new BinaryReader(fs);

            //---メイン・ファイル・ヘッダ---
            byte fileInfo = br.ReadByte();
            //ファイル長の値はワード単位(16 ビットを1ワードとする)で、ヘッダの 50 ワードを含む全ファイルの長さです。
            byte  fileLastUpdate1 = br.ReadByte();
            byte  fileLastUpdate2 = br.ReadByte();
            byte  fileLastUpdate3 = br.ReadByte();
            int   fileRecordNum   = br.ReadInt32();;
            short fileHeaderBytes = br.ReadInt16();
            short fileRecordBytes = br.ReadInt16();

            SkipReadByte(20, br);

            int fieldNum = (fileHeaderBytes - 33) / 32;

            //int fieldNum = fileRecordNum / 32;
            Dbffile[] dbfFields = new Dbffile[fieldNum];
            for (int i = 0; i < fieldNum; i++)
            {
                dbfFields[i] = ReadFields(br);
            }

            //フィールドの終わりを示す符号(0DH)
            SkipReadByte(1, br);

            for (int j = 0; j < fileRecordNum; j++)
            {
                /*
                 * データ・レコードの先頭の1バイトが
                 * 半角空白(20H)のとき、このレコードは削除されていないことをあらわす。アスタリスク(2AH)の
                 * とき、 このレコードは削除されていることをあらわす。
                 * */
                SkipReadByte(1, br);
                for (int k = 0; k < fieldNum; k++)
                {
                    int    fieldLength = dbfFields[k].FieldLength;
                    byte[] valueByte   = new byte[fieldLength];
                    for (int l = 0; l < fieldLength; l++)
                    {
                        valueByte[l] = br.ReadByte();
                    }
                    string strValue = "";
                    if (dbfFields[k].FieldType.Equals("N"))
                    {
                        strValue = System.Text.Encoding.ASCII.GetString(valueByte);// ASCII
                    }
                    else if (dbfFields[k].FieldType.Equals("C"))
                    {
                        strValue = System.Text.Encoding.GetEncoding(932).GetString(valueByte);// SJIS
                    }
                    //List<string> listValues = dbfFields[k].ListValues;
                    //listValues.Add(strValue.Trim());
                    dbfFields[k].ListValues.Add(strValue.Trim());
                }
            }

            return(dbfFields);
        }