Exemplo n.º 1
0
 public bool LoadFromFile(string filename)
 {
     using (StreamReader sr = new StreamReader(filename, EncodingExtensions.GetEncoding(filename)))
     {
         return(LoadFromText(sr.ReadToEnd()));
     }
 }
        private string ReadContent(Site site, HttpWebResponse response)
        {
            MemoryStream memoryStream = new MemoryStream(0x1000);

            using (Stream responseStream = response.GetResponseStream())
            {
                if (responseStream == null)
                {
                    return(string.Empty);
                }
                else
                {
                    byte[] buffer = new byte[0x1000];
                    int    bytes;
                    while ((bytes = responseStream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        memoryStream.Write(buffer, 0, bytes);
                    }
                }
            }

            byte[] contentBytes = memoryStream.StreamToBytes();
            contentBytes = PreventCutOff(contentBytes);
            if (string.IsNullOrEmpty(site.EncodingName))
            {
                var      charSet     = response.CharacterSet;
                Encoding htmlCharset = EncodingExtensions.GetEncoding(charSet, contentBytes);
                return(htmlCharset.GetString(contentBytes, 0, contentBytes.Length));
            }
            else
            {
                return(site.Encoding.GetString(contentBytes, 0, contentBytes.Length));
            }
        }
Exemplo n.º 3
0
        public string Chardet(byte[] bytes, int index, int count)
        {
            var buffer = new byte[count];

            Array.Copy(bytes, index, buffer, 0, count);

            var encoding = EncodingExtensions.GetEncoding(buffer);

            return(encoding.GetString(buffer));
        }
Exemplo n.º 4
0
        private string GenerateMarkdownHtml(string path)
        {
            var bytes    = File.ReadAllBytes(path);
            var encoding = EncodingExtensions.GetEncoding(path, bytes.Length);

            var md = encoding.GetString(bytes);

            md = WebUtility.HtmlEncode(md);

            var html = Resources.md2html.Replace("{{content}}", md);

            return(html);
        }
Exemplo n.º 5
0
        protected virtual string ReadContent(Site site, HttpResponseMessage response)
        {
            byte[] contentBytes = response.Content.ReadAsByteArrayAsync().Result;
            contentBytes = PreventCutOff(contentBytes);
            if (string.IsNullOrWhiteSpace(site.EncodingName))
            {
                var      charSet     = response.Content.Headers.ContentType?.CharSet;
                Encoding htmlCharset = EncodingExtensions.GetEncoding(charSet, contentBytes);
                return(htmlCharset.GetString(contentBytes, 0, contentBytes.Length));
            }

            return(site.Encoding.GetString(contentBytes, 0, contentBytes.Length));
        }
Exemplo n.º 6
0
 private string ReadContent(HttpResponseMessage response, HttpRequestTask requestTask)
 {
     byte[] contentBytes = response.Content.ReadAsByteArrayAsync().Result;
     contentBytes = PreventCutOff(contentBytes);
     if (requestTask.Encoding == null)
     {
         var charSet     = response.Content.Headers.ContentType?.CharSet;
         var htmlCharset = EncodingExtensions.GetEncoding(charSet, contentBytes);
         return(htmlCharset.GetString(contentBytes, 0, contentBytes.Length));
     }
     else
     {
         return(requestTask.Encoding.GetString(contentBytes, 0, contentBytes.Length));
     }
 }
 private string ReadContent(Site site, HttpResponseMessage response)
 {
     byte[] contentBytes = response.Content.ReadAsByteArrayAsync().Result;
     contentBytes = PreventCutOff(contentBytes);
     if (string.IsNullOrEmpty(site.EncodingName))
     {
         var      charSet     = response.Content.Headers.ContentType == null ? null : response.Content.Headers.ContentType.CharSet;
         Encoding htmlCharset = EncodingExtensions.GetEncoding(charSet, contentBytes);
         return(htmlCharset.GetString(contentBytes, 0, contentBytes.Length));
     }
     else
     {
         return(site.Encoding.GetString(contentBytes, 0, contentBytes.Length));
     }
 }
Exemplo n.º 8
0
        public void LoadFile(string path)
        {
            const int limit  = 10000;
            var       binded = false;

            using (var sr = new StreamReader(new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), EncodingExtensions.GetEncoding(path)))
            {
                //edit by gh
                //var conf = new CsvHelper.Configuration.Configuration() {MissingFieldFound = null, BadDataFound = null};
                CsvConfiguration conf = new CsvConfiguration(CultureInfo.CurrentCulture);
                using (var csv = new CsvReader(sr, conf))
                {
                    var i = 0;
                    while (csv.Read())
                    {
                        List <string> result = new List <string>();
                        int           k      = 0;
                        for (k = 0; csv.TryGetField <string>(k, out string value); k++)
                        {
                            result.Add(value);
                        }
                        if (!binded)
                        {
                            SetupColumnBinding(result.Count + 1);
                            binded = true;
                        }
                        else
                        {
                            //补位
                            for (; k < dataGrid.Columns.Count; k++)
                            {
                                result.Add("");
                            }
                        }
                        var row = Concat(new[] { $"{i++ + 1}".PadLeft(6) }, result.ToArray());
                        if (i > limit)
                        {
                            Rows.Add(Enumerable.Repeat("...", row.Length).ToArray());
                            break;
                        }

                        Rows.Add(row);
                    }
                }

                /*
                 * using (var parser = new CsvParser(sr, conf))
                 * {
                 *  var i = 0;
                 *  while (true)
                 *  {
                 *      var row = parser.Read();
                 *      if (row == null)
                 *          break;
                 *      row = Concat(new[] { $"{i++ + 1}".PadLeft(6) }, row);
                 *
                 *      if (!binded)
                 *      {
                 *          SetupColumnBinding(row.Length);
                 *          binded = true;
                 *      }
                 *
                 *      if (i > limit)
                 *      {
                 *          Rows.Add(Enumerable.Repeat("...", row.Length).ToArray());
                 *          break;
                 *      }
                 *
                 *      Rows.Add(row);
                 *  }
                 * }
                 */
                //-------------------//
            }
        }
Exemplo n.º 9
0
        private void LoadFileAsync(string path)
        {
            Task.Run(() =>
            {
                const int maxLength             = 5 * 1024 * 1024;
                const int maxHighlightingLength = (int)(0.5 * 1024 * 1024);
                var buffer = new MemoryStream();
                bool fileTooLong;
                using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    fileTooLong = fs.Length > maxLength;
                    while (fs.Position < fs.Length && buffer.Length < maxLength)
                    {
                        if (_disposed)
                        {
                            break;
                        }
                        var lb  = new byte[8192];
                        int len = fs.Read(lb, 0, lb.Length);
                        buffer.Write(lb, 0, len);
                    }
                }
                if (_disposed)
                {
                    return;
                }

                if (fileTooLong)
                {
                    _context.Title += " (0 ~ 5MB)";
                }

                var bufferCopy = buffer.ToArray();
                buffer.Dispose();

                //edit by gh -
                //使用NChardet解决大部分编码识别问题
                var encoding = EncodingExtensions.GetEncoding(path, bufferCopy.Length);
                //var encoding = EncodingExtensions.GetEncoding_utf(bufferCopy);
                //-----------

                var doc = new TextDocument(encoding.GetString(bufferCopy));
                doc.SetOwnerThread(Dispatcher.Thread);

                if (_disposed)
                {
                    return;
                }

                Dispatcher.BeginInvoke(new Action(() =>
                {
                    Encoding           = encoding;
                    SyntaxHighlighting = bufferCopy.Length > maxHighlightingLength
                        ? null
                        : HighlightingManager.Instance.GetDefinitionByExtension(Path.GetExtension(path));
                    Document = doc;

                    _context.IsBusy = false;
                }), DispatcherPriority.Render);
            });
        }