How do I incorporate these two routines?
-
I am very new and trying to do something very advanced. Can you open a file with StreamReader, format with StringBuilder, and then pass the results of the StringBuilder to a StringCollection? or am I out of my mind. Here is my StringBuilder 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 } ---------------------------------- And here is my StringCollection Routine; ---------------------------------- StringCollection ReadFileIntoStringCollection() {
-
I am very new and trying to do something very advanced. Can you open a file with StreamReader, format with StringBuilder, and then pass the results of the StringBuilder to a StringCollection? or am I out of my mind. Here is my StringBuilder 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 } ---------------------------------- And here is my StringCollection Routine; ---------------------------------- StringCollection ReadFileIntoStringCollection() {
it seems from your second method you need to parse your string from string builder to lines like what you read from the Stream Am I right? if it is what you need you can do with Split method from string instances like
string s = "hello \r\n how are you? \r\n";
string[] sArr = s.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
StringCollection col = new StringCollection();
col.AddRange(sArr);it was the easiest way with least code change however your methods are not efficient you can add your string to StringCollection instance instead of writing sb.Append("/r/n") hope the post would be useful good luck
-
it seems from your second method you need to parse your string from string builder to lines like what you read from the Stream Am I right? if it is what you need you can do with Split method from string instances like
string s = "hello \r\n how are you? \r\n";
string[] sArr = s.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
StringCollection col = new StringCollection();
col.AddRange(sArr);it was the easiest way with least code change however your methods are not efficient you can add your string to StringCollection instance instead of writing sb.Append("/r/n") hope the post would be useful good luck
You are correct in your assumption. I am not following what you are suggesting. I am not asking for a rewrite of the code, maybe a snipit within the sample I sent as I tend to understand it better from a practical example(what I am working on).
Hessam Jalali wrote:
string s = "hello \r\n how are you? \r\n";string[] sArr = s.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);StringCollection col = new StringCollection();col.AddRange(sArr);
Hessam Jalali wrote:
however your methods are not efficient you can add your string to StringCollection instance instead of writing sb.Append("/r/n")
Thanks, Brian
-
I am very new and trying to do something very advanced. Can you open a file with StreamReader, format with StringBuilder, and then pass the results of the StringBuilder to a StringCollection? or am I out of my mind. Here is my StringBuilder 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 } ---------------------------------- And here is my StringCollection Routine; ---------------------------------- StringCollection ReadFileIntoStringCollection() {
solutionsville wrote:
or am I out of my mind.
Nothing here leads me to that conclusion. :-D If I understand correctly what you mean by incorporating the two routines, you might be able to do something like this.
StringCollection ReadFileIntoStringCollection()
{
const int MaxBytes = 65536;
StreamReader sr = new StreamReader ( chosenFile );
StringCollection result = new StringCollection();
int nBytesRead = 0;
string nextLine;
while ( ( nextLine = sr.ReadLine() ) != null )
{
nBytesRead += nextLine.Length;
if ( nBytesRead > MaxBytes )
break;
// copy and paste the guts of LayoutInput to here
// instead of returning sb.ToString() do
// result.Add ( sb.ToString() ) ;
result.Add ( nextLine );
}
sr.Close();
return result;
}When I say "guts" I mean leave out the first few lines, start with the declaration of sb and go through the end of the while loop. Once you do that there may be some compilation errors or warnings to fix, but I think it should work. Or can be made to work. Hope that's more or less what you had in mind. BDF
-
solutionsville wrote:
or am I out of my mind.
Nothing here leads me to that conclusion. :-D If I understand correctly what you mean by incorporating the two routines, you might be able to do something like this.
StringCollection ReadFileIntoStringCollection()
{
const int MaxBytes = 65536;
StreamReader sr = new StreamReader ( chosenFile );
StringCollection result = new StringCollection();
int nBytesRead = 0;
string nextLine;
while ( ( nextLine = sr.ReadLine() ) != null )
{
nBytesRead += nextLine.Length;
if ( nBytesRead > MaxBytes )
break;
// copy and paste the guts of LayoutInput to here
// instead of returning sb.ToString() do
// result.Add ( sb.ToString() ) ;
result.Add ( nextLine );
}
sr.Close();
return result;
}When I say "guts" I mean leave out the first few lines, start with the declaration of sb and go through the end of the while loop. Once you do that there may be some compilation errors or warnings to fix, but I think it should work. Or can be made to work. Hope that's more or less what you had in mind. BDF
Big Daddy, you are the man! This have been driving me nuts for about a week. This is what it looks like combined and working. -------------- So if I want to add this piece to the same method above, where would I put this? 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(); } Thanks and have a good weekend! -- modified at 15:53 Saturday 25th August, 2007
-
Big Daddy, you are the man! This have been driving me nuts for about a week. This is what it looks like combined and working. -------------- So if I want to add this piece to the same method above, where would I put this? 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(); } Thanks and have a good weekend! -- modified at 15:53 Saturday 25th August, 2007
Hi Brian, I had a good weekend, hope you did too. That's going to be a bit more involved. There are two approaches that I see, no doubt there are others as well. First approach is to get the data from the file into memory using
ReadFileIntoStringCollection()
which now also does the work formerly done inLayoutInput()
. Now the data is in a StringCollection object, from there it needs to go into the RichTextBox control. I don't know the best way to do this, it will take a little bit of experimentation. It might be as simple asrtbDoc.Text = result.ToString();
but I'm not sure that will work. You might have to iterate throughresult
appending eachstring
element ontortbDoc.Lines
or something like that. Once that data is in the RichTextBox control you can just callfindSequenceNumbers()
. This would offer the advantage of still being able toSelect()
a portion of the text and change theSelectionFont
andSelectionColor
. A big disadvantage might be the effort of getting the data into the RichTextBox control and the possible time used byfindSequenceNumbers()
. Second approach involves more hacking up of thefindSequenceNumbers()
function and copying it intoReadFileIntoStringCollection()
. To do this, instead of searching for your targets inlines[lineNum]
you'll search in an element ofresult
. Probably you can still use the [] array notation, seems to work with most Collection objects. But now you can't just set theSelectionFont
andSelectionColor
properties because we have just a plain old string, not the contents of a RichTextBox control. Instead you'll need to insert the raw rtf formatting sequences. Also you'll need to put some other rtf stuff into the beginning elements ofresult
as well as some ending stuff once the file reading is done. If you use the second approach, we find ourselves back to the problem of getting the StringCollection object into the RichTextBox control. Except now we're hoping thatrtbDoc.Rtf = result.ToString();
is the silver bullet. You might consider putting in a temporary statement likestring temp = result.ToString();
in the while loop when reading the file. Just so you can see in the debugger what is going to returned by callingToString()
on a StringCollection object. Another thing -
Hi Brian, I had a good weekend, hope you did too. That's going to be a bit more involved. There are two approaches that I see, no doubt there are others as well. First approach is to get the data from the file into memory using
ReadFileIntoStringCollection()
which now also does the work formerly done inLayoutInput()
. Now the data is in a StringCollection object, from there it needs to go into the RichTextBox control. I don't know the best way to do this, it will take a little bit of experimentation. It might be as simple asrtbDoc.Text = result.ToString();
but I'm not sure that will work. You might have to iterate throughresult
appending eachstring
element ontortbDoc.Lines
or something like that. Once that data is in the RichTextBox control you can just callfindSequenceNumbers()
. This would offer the advantage of still being able toSelect()
a portion of the text and change theSelectionFont
andSelectionColor
. A big disadvantage might be the effort of getting the data into the RichTextBox control and the possible time used byfindSequenceNumbers()
. Second approach involves more hacking up of thefindSequenceNumbers()
function and copying it intoReadFileIntoStringCollection()
. To do this, instead of searching for your targets inlines[lineNum]
you'll search in an element ofresult
. Probably you can still use the [] array notation, seems to work with most Collection objects. But now you can't just set theSelectionFont
andSelectionColor
properties because we have just a plain old string, not the contents of a RichTextBox control. Instead you'll need to insert the raw rtf formatting sequences. Also you'll need to put some other rtf stuff into the beginning elements ofresult
as well as some ending stuff once the file reading is done. If you use the second approach, we find ourselves back to the problem of getting the StringCollection object into the RichTextBox control. Except now we're hoping thatrtbDoc.Rtf = result.ToString();
is the silver bullet. You might consider putting in a temporary statement likestring temp = result.ToString();
in the while loop when reading the file. Just so you can see in the debugger what is going to returned by callingToString()
on a StringCollection object. Another thingHey BDF, yes I had a great weekend, time on the Harley, etc... I had the same thought you did, and tried it. I didn't get any more speed performance from the routine doing it either way. What did cut the time down on the load to RTB was to disable the RTB control until the routine is complete. It went from 3 1/2 minutes to about 20 seconds for a 8MB file. I am gonna move on. You have provided me great insight as to what I was missing. Thank you very much for you help! V/R Brian
-
Hey BDF, yes I had a great weekend, time on the Harley, etc... I had the same thought you did, and tried it. I didn't get any more speed performance from the routine doing it either way. What did cut the time down on the load to RTB was to disable the RTB control until the routine is complete. It went from 3 1/2 minutes to about 20 seconds for a 8MB file. I am gonna move on. You have provided me great insight as to what I was missing. Thank you very much for you help! V/R Brian
solutionsville wrote:
disable the RTB control until the routine is complete
Good idea. Wish I'd thought of that! :doh: I suppose the code was chugging along putting in lots of the rtf commands and each time the control had to redisplay itself. Only having to do that one time would account for the big time difference. Good luck with your project! BDF