示例#1
0
        public DBFFile(string filename)
        {
            try
            {
                FileInfo fi = new FileInfo(filename);
                if (!fi.Exists)
                {
                    return;
                }
                _filename = filename;

                StreamReader sr = new StreamReader(filename);
                BinaryReader br = new BinaryReader(sr.BaseStream);

                _header = new DBFFileHader(br);
                _fields = new List <FieldDescriptor>();
                for (int i = 0; i < _header.FieldsCount; i++)
                {
                    FieldDescriptor field = new FieldDescriptor(br);
                    _fields.Add(field);
                }
                sr.Close();

                int    c           = 1;
                string idFieldName = _idField;
                while (HasField(idFieldName))
                {
                    idFieldName = _idField + "_" + c++;
                }
                _idField = idFieldName;

                _encoder = null;
                try
                {
                    switch ((CodePage)_header.LanguageDriver)
                    {
                    case CodePage.DOS_USA:
                        _encoder = EncodingFromCodePage(437);
                        break;

                    case CodePage.DOS_Multilingual:
                        _encoder = EncodingFromCodePage(850);
                        break;

                    case CodePage.Windows_ANSI:
                        _encoder = EncodingFromCodePage(1252);
                        break;

                    case CodePage.EE_MS_DOS:
                        _encoder = EncodingFromCodePage(852);
                        break;

                    case CodePage.Nordic_MS_DOS:
                        _encoder = EncodingFromCodePage(865);
                        break;

                    case CodePage.Russian_MS_DOS:
                        _encoder = EncodingFromCodePage(866);
                        break;

                    case CodePage.Windows_EE:
                        _encoder = EncodingFromCodePage(1250);
                        break;

                    case CodePage.UTF_7:
                        _encoder = new UTF7Encoding();
                        break;
                    }
                }
                catch { }
                if (_encoder == null)
                {
                    FileInfo encFi = new FileInfo(fi.Directory.FullName + @"\dbf_default_encoding.txt");
                    if (encFi.Exists)
                    {
                        using (StreamReader encSr = new StreamReader(encFi.FullName))
                        {
                            switch (encSr.ReadLine().ToLower())
                            {
                            case "utf8":
                            case "utf-8":
                                _encoder = new UTF8Encoding();
                                break;

                            case "unicode":
                                _encoder = new UnicodeEncoding();
                                break;

                            case "ascii":
                                _encoder = new ASCIIEncoding();
                                break;
                            }
                        }
                    }

                    if (_encoder == null)
                    {
                        _encoder = new UTF7Encoding();
                    }
                }
                //Record(0);
                //Record(1);
            }
            catch
            {
            }
        }
示例#2
0
        public static bool Write(BinaryWriter bw, Fields fields)
        {
            if (bw == null || fields == null)
            {
                return(false);
            }

            int c = 0, rl = 1;  // deleted Flag

            foreach (IField field in fields.ToEnumerable())
            {
                switch (field.type)
                {
                case FieldType.biginteger:
                    c++; rl += 18;
                    break;

                case FieldType.boolean:
                    c++; rl += 1;
                    break;

                case FieldType.character:
                    c++; rl += 1;
                    break;

                case FieldType.Date:
                    c++; rl += 8;
                    break;

                case FieldType.Double:
                    c++; rl += 31;
                    break;

                case FieldType.Float:
                    c++; rl += 11;
                    break;

                case FieldType.ID:
                    c++; rl += 9;
                    break;

                case FieldType.integer:
                    c++; rl += 9;
                    break;

                case FieldType.Shape:
                    break;

                case FieldType.smallinteger:
                    c++; rl += 6;
                    break;

                case FieldType.String:
                    c++; rl += (field.size > 255) ? 255 : field.size;
                    break;

                default:
                    c++; rl += (field.size <= 0) ? 255 : field.size;
                    break;
                }
            }

            short hLength = (short)(32 * c + 33);

            bw.Write((byte)3);                         // Version
            bw.Write((byte)106);                       // YY
            bw.Write((byte)6);                         // MM
            bw.Write((byte)12);                        // DD
            bw.Write((int)0);                          // recordCount
            bw.Write(hLength);                         // headerLength
            bw.Write((short)rl);                       // Length of each record
            bw.Write((short)0);                        // Reserved1
            bw.Write((byte)0);                         // IncompleteTransac
            bw.Write((byte)0);                         // EncryptionFlag
            bw.Write((int)0);                          // FreeRecordThread
            bw.Write((int)0);                          // Reserved2
            bw.Write((int)0);                          // Reserved3
            bw.Write((byte)0);                         // MDX
            bw.Write((byte)CodePage.DOS_Multilingual); // LanguageDriver  Codepage 850
            bw.Write((short)0);                        // Reserved4

            foreach (IField field in fields.ToEnumerable())
            {
                FieldDescriptor.Write(bw, field);
            }

            bw.Write((byte)13); // Terminator 0x0D
            return(true);
        }