Re: Read text file to Temp file and apply formating and color chan



Sure thing Peter, Here is the code. Do you need me to email you a sample file?

Yes I meant Ghz.

I call this routine from the OpenFileDialog;

This reads and formats the text into the single line format;
----- Begin Code -----
using System;
using System.Drawing;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace TCEditor
{
class Streamer
{

#region File Stream and Format Routine
public static string LayoutInput(string input)
{
StreamReader sr = File.OpenText(input);
StringBuilder sb = new StringBuilder(input.Length);
bool firstLine = true;
string line;
while ((line = sr.ReadLine()) != null)
{
if (line.Trim() == "")
continue;
if (line.Length < 29) { throw new InvalidOperationException("invalid
input"); }
if (line[29] != ' ')
{
int txPos;
int rxPos = -1;
int len = 0;
if (firstLine)
firstLine = false;
else
sb.Append("\r\n");
if (((txPos = line.IndexOf("TX")) > -1) || ((rxPos = line.IndexOf("RX")) > 0))
{
int charactersTillPoint;
if (txPos > -1)
{
charactersTillPoint = txPos;
len = line.Substring(txPos).Length;
}
else
{
charactersTillPoint = rxPos;
len = line.Substring(rxPos).Length;
}
string part0 = line.Substring(0, charactersTillPoint);
string part1 = line.Substring(charactersTillPoint);
sb.Append(part0.PadRight(86));
sb.Append(part1);
}
else
sb.Append(line);
sb.Append(' ');
if (len == 12)
sb.Append(' ');
}
else
{
sb.Append(line.Substring(31));
}
}
return sb.ToString();
}
#endregion
}
}
-----End Code-----
This is the routine that adds the color to the specified locations;
I call this as the next routine in the OpenFileDialog
-----Begin Code-----
private void findSequenceNumbers()
{
toolStripStatusLabel.Visible = toolStripProgressBar.Visible = true;
toolStripProgressBar.Value = 0;
int lineNum = 0;
bool startingNewLine = true;
FontStyle style = FontStyle.Bold;
string[] lines = rtbDoc.Lines;
string text = rtbDoc.Text;
toolStripProgressBar.Maximum = text.Length;
for (int i = 0; i < text.Length; i++)
{
if (startingNewLine)
{
if ((lines[lineNum].Contains("ARES_EINDICATION")) ||
(lines[lineNum].Contains("ARES_INDICATION")))
{
i += 169;

rtbDoc.Select(i, 2);
rtbDoc.SelectionFont = new Font(rtbDoc.SelectionFont,
rtbDoc.SelectionFont.Style ^ style);
rtbDoc.SelectionColor = Color.DarkBlue;
}
else if (lines[lineNum].Contains("]CODELINE_INDICATION_MSG"))
{
i += 160;
rtbDoc.Select(i, 2);
rtbDoc.SelectionFont = new Font(rtbDoc.SelectionFont,
rtbDoc.SelectionFont.Style ^ style);
rtbDoc.SelectionColor = Color.DarkBlue;
}
else
{
i += lines[lineNum].Length - 1;
}
startingNewLine = false;
Application.DoEvents();
}
if (text[i] == '\n')
{
startingNewLine = true;
lineNum++;
}
toolStripProgressBar.Value = i;
}
toolStripStatusLabel.Visible = toolStripProgressBar.Visible = false;
rtbDoc.Select(0, 0);
rtbDoc.ScrollToCaret();
}
-----End Code-----



"Peter Duniho" wrote:

Brian Cook wrote:
It takes 3Min30Sec for a 7MB file.

I am runing a P4 3.4Mhz PC with 2GB of RAM.

I assume that's "3.4Ghz", you mean. :)

Maybe you could post a concise-but-complete example of code that
reliably demonstrates your scenario. I am not seeing nearly so awful a
performance result.

I created a short application that simply puts 7MB worth of dummy text
into a RichTextBox. Actually, it's technically 14MB...I took the
conservative view that your "7MB file" is regular ASCII, and so I
actually generate 7 * 1024 * 1024 characters. Since .NET uses Unicode,
this is actually 14MB, not 7MB.

I generate the dummy text randomly one character at a time, one fake
sentence at a time, appending each new sentence to a StringBuilder that
has been preinitialized to the 7 million (or so :) ) characters. Then I
use the ToString() method and assign the result to the Text property
of the RichTextBox.

I ran this on my Core 2 Duo 2.33Ghz computer with 2GB of RAM.

The initialization of the data takes roughly 1 second, and roughly
another 3 seconds to copy it all to the RichTextBox.

So yes, there's a fair amount of overhead in the control, relatively
speaking (copying the text takes three times as long as generating it in
the first place). But three and a half minutes is WAY too slow. I
don't know what you're code is doing, but it seems likely to me that you
have some basic algorithmic issues going on, and your problem will be
better solved addressing those rather than looking for ways to make the
control itself faster. My experiment suggests that the control itself
has acceptable performance, when used efficiently.

IMHO, the best sample code would include something that autogenerates
the data equivalent to your 7MB file, and which also includes whatever
initialization code you are using, along with a RichTextBox contained by
the form for the program. In other words, the program itself should be
as small as possible (no downloading 7MB files :) ), but should still
reproduce the same performance results you're seeing, by doing the exact
same initialization you're doing.

Pete

.



Relevant Pages