Exemplo n.º 1
0
        public static Encoding TryDetectEncoding(string dbfFileName)
        {
            var cpgFile = Shapefile.GetCpgFileName(dbfFileName);

            if (!System.IO.File.Exists(cpgFile))
            {
                return(null);
            }

            var encodingText = System.IO.File.ReadAllText(cpgFile);

            if (encodingText?.ToUpper()?.Trim() == "UTF-8" || encodingText?.ToUpper()?.Trim() == "UTF8")
            {
                return(Encoding.UTF8);
            }
            else if (encodingText?.Contains("1256") == true)
            {
                //return Dbf.DbfFile._arabicEncoding;
                return(DbfFile._arabicEncoding);
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 2
0
        public static void Write <T>(string dbfFileName,
                                     IEnumerable <T> values,
                                     List <ObjectToDbfTypeMap <T> > mapping,
                                     Encoding encoding,
                                     bool overwrite = false)
        {
            var columns = mapping.Select(m => m.FieldType).ToList();

            int control = 0;

            try
            {
                //if (columns.Count != mapping.Count)
                //{
                //    throw new NotImplementedException();
                //}

                var mode = Shapefile.GetMode(dbfFileName, overwrite);

                System.IO.Stream stream = new System.IO.FileStream(dbfFileName, mode);

                System.IO.BinaryWriter writer = new System.IO.BinaryWriter(stream);

                DbfHeader header = new DbfHeader(values.Count(), mapping.Count, GetRecordLength(columns), encoding);

                writer.Write(IRI.Msh.Common.Helpers.StreamHelper.StructureToByteArray(header));

                foreach (var item in columns)
                {
                    writer.Write(IRI.Msh.Common.Helpers.StreamHelper.StructureToByteArray(item));
                }

                //Terminator
                writer.Write(byte.Parse("0D", System.Globalization.NumberStyles.HexNumber));

                for (int i = 0; i < values.Count(); i++)
                {
                    control = i;
                    // All dbf field records begin with a deleted flag field. Deleted - 0x2A (asterisk) else 0x20 (space)
                    writer.Write(byte.Parse("20", System.Globalization.NumberStyles.HexNumber));

                    for (int j = 0; j < mapping.Count; j++)
                    {
                        // 1400.02.03-comment
                        //byte[] temp = new byte[columns[j].Length];

                        object value = mapping[j].MapFunction(values.ElementAt(i));

                        var temp = DbfFieldMappings.Encode(value, columns[j].Length, encoding);

                        // 1400.02.03-comment
                        //if (value is DateTime dt)
                        //{
                        //    value = dt.ToString("yyyyMMdd");
                        //}

                        //if (value != null)
                        //{
                        //    //encoding.GetBytes(value.ToString(), 0, value.ToString().Length, temp, 0);
                        //    temp = GetBytes(value.ToString(), temp, encoding);
                        //}

                        ////string tt = encoding.GetString(temp);
                        ////var le = tt.Length;
                        writer.Write(temp);
                    }
                }

                //End of file
                writer.Write(byte.Parse("1A", System.Globalization.NumberStyles.HexNumber));

                writer.Close();

                stream.Close();

                System.IO.File.WriteAllText(Shapefile.GetCpgFileName(dbfFileName), encoding.BodyName);
            }
            catch (Exception ex)
            {
                string message = ex.Message;

                string m2 = message + " " + control.ToString();
            }
        }
Exemplo n.º 3
0
        internal static void Write <T>(string fileName,
                                       List <T> values,
                                       List <Func <T, object> > mapping,
                                       List <DbfFieldDescriptor> columns,
                                       Encoding encoding,
                                       bool overwrite = false)
        {
            int control = 0;

            try
            {
                if (columns.Count != mapping.Count)
                {
                    throw new NotImplementedException();
                }

                //var mode = overwrite ? System.IO.FileMode.Create : System.IO.FileMode.CreateNew;
                var mode = Shapefile.GetMode(fileName, overwrite);

                System.IO.Stream stream = new System.IO.FileStream(fileName, mode);

                System.IO.BinaryWriter writer = new System.IO.BinaryWriter(stream);

                DbfHeader header = new DbfHeader(values.Count, mapping.Count, GetRecordLength(columns), encoding);

                writer.Write(IRI.Ket.Common.Helpers.StreamHelper.StructureToByteArray(header));

                foreach (var item in columns)
                {
                    writer.Write(IRI.Ket.Common.Helpers.StreamHelper.StructureToByteArray(item));
                }

                //Terminator
                writer.Write(byte.Parse("0D", System.Globalization.NumberStyles.HexNumber));

                for (int i = 0; i < values.Count; i++)
                {
                    control = i;
                    // All dbf field records begin with a deleted flag field. Deleted - 0x2A (asterisk) else 0x20 (space)
                    writer.Write(byte.Parse("20", System.Globalization.NumberStyles.HexNumber));

                    for (int j = 0; j < mapping.Count; j++)
                    {
                        byte[] temp = new byte[columns[j].Length];

                        object value = mapping[j](values[i]);

                        if (value != null)
                        {
                            //encoding.GetBytes(value.ToString(), 0, value.ToString().Length, temp, 0);
                            temp = GetBytes(value.ToString(), temp, encoding);
                        }

                        //string tt = encoding.GetString(temp);
                        //var le = tt.Length;
                        writer.Write(temp);
                    }
                }

                //End of file
                writer.Write(byte.Parse("1A", System.Globalization.NumberStyles.HexNumber));

                writer.Close();

                stream.Close();

                System.IO.File.WriteAllText(Shapefile.GetCpgFileName(fileName), encoding.BodyName);
            }
            catch (Exception ex)
            {
                string message = ex.Message;

                string m2 = message + " " + control.ToString();
            }
        }