Esempio n. 1
0
        public void OpenFile(string fileName, Encoding enc)
        {
            Clear();

            if (fs != null)
            {
                fs.Dispose();
            }

            SaveEOL = Environment.NewLine;

            //read lines of file
            fs = new FileStream(fileName, FileMode.Open);
            var length = fs.Length;

            //read signature
            enc = DefineEncoding(enc, fs);
            int shift = DefineShift(enc);

            //first line
            sourceFileLinePositions.Add((int)fs.Position);
            base.lines.Add(null);
            //other lines
            sourceFileLinePositions.Capacity = (int)(length / 7 + 1000);

            //int prev = 0;
            //while(fs.Position < length)
            //{
            //    var b = fs.ReadByte();

            //    if (b == 10)// \n
            //    {
            //        sourceFileLinePositions.Add((int)(fs.Position) + shift);
            //        base.lines.Add(null);
            //    }else
            //    if (prev == 13)// \r (Mac format)
            //    {
            //        sourceFileLinePositions.Add((int)(fs.Position - 1) + shift);
            //        base.lines.Add(null);
            //        SaveEOL = "\r";
            //    }

            //    prev = b;
            //}

            //if (prev == 13)
            //{
            //    sourceFileLinePositions.Add((int)(fs.Position) + shift);
            //    base.lines.Add(null);
            //}

            int          prev    = 0;
            int          prevPos = 0;
            BinaryReader br      = new BinaryReader(fs, enc);

            while (fs.Position < length)
            {
                prevPos = (int)fs.Position;
                var b = br.ReadChar();

                if (b == 10)// \n
                {
                    sourceFileLinePositions.Add((int)fs.Position);
                    base.lines.Add(null);
                }
                else
                if (prev == 13)// \r (Mac format)
                {
                    sourceFileLinePositions.Add((int)prevPos);
                    base.lines.Add(null);
                    SaveEOL = "\r";
                }

                prev = b;
            }

            if (prev == 13)
            {
                sourceFileLinePositions.Add((int)prevPos);
                base.lines.Add(null);
            }

            if (length > 2000000)
            {
                GC.Collect();
            }

            Line[] temp = new Line[100];

            var c = base.lines.Count;

            base.lines.AddRange(temp);
            base.lines.TrimExcess();
            base.lines.RemoveRange(c, temp.Length);


            int[] temp2 = new int[100];
            c = base.lines.Count;
            sourceFileLinePositions.AddRange(temp2);
            sourceFileLinePositions.TrimExcess();
            sourceFileLinePositions.RemoveRange(c, temp.Length);


            fileEncoding = enc;

            OnLineInserted(0, Count);
            //load first lines for calc width of the text
            var linesCount = Math.Min(lines.Count, CurrentTB.ClientRectangle.Height / CurrentTB.CharHeight);

            for (int i = 0; i < linesCount; i++)
            {
                LoadLineFromSourceFile(i);
            }
            //
            NeedRecalc(new TextChangedEventArgs(0, linesCount - 1));
            if (CurrentTB.WordWrap)
            {
                OnRecalcWordWrap(new TextChangedEventArgs(0, linesCount - 1));
            }
        }
Esempio n. 2
0
 public override void InsertLine(int index, Line line)
 {
     sourceFileLinePositions.Insert(index, -1);
     base.InsertLine(index, line);
 }