Re: Trim a multiple line message to a single line

Tech-Archive recommends: Fix windows errors by optimizing your registry



Jesse, Here is what I did to get it working. At first I didn't realize that I
needed to declare the int. Because of the position of the TX in some lines I
had to move the format out to the 86th pos.

Thanks a bunch!

using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace DatReader
{
class Streamer
{
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.Length < 29) { throw new
InvalidOperationException("invalid input"); }
if (line[29] != ' ')
{
int txPos;
int rxPos = -1;
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;
else
charactersTillPoint = rxPos;
string part0 = line.Substring(0, charactersTillPoint);
string part1 = line.Substring(charactersTillPoint);
sb.Append(part0.PadRight(86));
sb.Append(part1);
}
else
sb.Append(line);
}
else
{
sb.Append(line.Substring(30));
}
}
return sb.ToString();
}
}
}

"Jesse Houwing" wrote:

Hello Brian,

Jesse, I appreciate all your help so far, and have now come up with
something I am once again unsure how to fix.;

In the Example.txt file I sent you, when the lines are combined into
one they are a little jumbled. I would like to format the line so that
the RX and TX portion all start at the 78th position from the left.
Everything to the left of that would never go past the 75th position.

I know that I can perform a StringBuilder.Insert, just not sure how or
where to add it into this code.

Here is the code for what we made work.

Basically you have two options.
1) Find the location and insert as many spaces as needed.
2) Find the location, split the string. Now use stringBuilder.AppendFormat
function
if you use the following format: "{0, -78}{1}" and the two parts of the string
you have collected using substring it will padd the string to 78 positions
if needed:

/* might need more specific searching for RX/TX */
if (line.InfexOf("TX") > 0 || line.InfexOf("RX") > 0)
{
int charactersTillPoint = /* Find out the position */
string part0 = line.SubString(0, charactersTillPoint);
string part1 = line.SubString(charactersTillPoint);
sb.AppendFormat("{0, -78}{1}", part0, part1);
}


I've inserted where to mark it with #####

---begin code---
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace DatReader
{
class Streamer
{
public static string LayoutInput(string input)
{
StreamReader sr = File.OpenText(input);
StringBuilder sb = new StringBuilder(input.Length);
string line;
while ((line = sr.ReadLine()) != null)
{
if (line.Length < 29) { throw new
InvalidOperationException("invalid input"); }
if (line[29] != ' ')
{
sb.Append("\r\n");


#### This is where to insert

sb.Append(line);
}
else
{
sb.Append(line.Substring(30));
}
}
return sb.ToString();
}
}
}
---end code---

Thanks,

"Jesse Houwing" wrote:

Hello Brian,

I have no clue how to give text a different colour. I am sure though
that you would need to use a RichTextBox. I'd guess you'd have to
insert some RTF control characters. Where to insert them would be
easy to find out if it's always the 34th Hex pair.

private string LayoutInput(string input)
{
StringReader sr = new StringReader(input);
StringBuilder sb = new StringBuilder(input.Length);
string line;
while ((line = sr.ReadLine())!= null)
{
if (line.Length < 29) { throw new InvalidOperationException("invalid
input"); }
if (line[29] != ' ')
{
// This is the line that holds the type
if (line.SubString(29).StartsWith("ARES_"))
{
// find the position of the 34th pair. That would
be right behind the 34th space following ARES_
// Alternatively, you could hardcode this position,
this is more flexible
int pos = 29;
for (int i = 0; i < 34 && pos < line.Length; i++)
{
pos = line.Substring(pos).IndexOf(' ', pos);
}
// If a pair was found 34 pairs out
if (pos > 29)
{
sb.Append(line.Substring(0, pos)); // Add everything
up to the place we need to insert the control characters
sb.Append(/*control characters to change the color*/);
sb.Append(line.Substring(pos, 2)); // Add the
HEX pair
sb.Append(/*control characters to reset the color*/);
sb.Append(line.Substring(pos + 2)); // Add the
remainder of the string
continue;
}
// Otherwise just fall throught o the default behaviour...
}
sb.Append(line);
}
else
{
sb.Append(line.Substring(30));
}
}
}
It's all in one function now, but I'd place the extra checks in a
subfunction should they grow any further.

I tried to find a string function that automatically gives the xth
position of a substring (IndexOf(what, from, count, number) but I
couldn't find it... too bad ;)).

Again you could use a regex for this...

Regex.Replace("(?<base>ARES_(\S+\s){34})(?<hexpair>[A-F0-9]{2})",
"$1" + "control character for colour" + "$2" + "control character for
normal colour");

It's actually easier for me to read than the function above, but that
is partly due to the fact that I'm presenting courses in .NET Regular
Expressions as part of my job.

Jesse

I wnt with the Stringbuilder solution. It seemed easier to follow to
me than the Regex one.

"Jesse Houwing" wrote:

Hello Brian,

Did you go for the regex or the stringbuilder solution?

Jesse

Ok, Jessee, I have a new Wrinkle for you.

Lets say I want a specific hex pair highlighted in Blue on every
line that starts out with ARES_INDICATION or ARES_EINDICATION.

It would be the 34 item from the left.

If you have the Example.txt I emailed you, the last line in the
how I want it to look the field would the C7 that follows the 02
02 hex.

If not I can send it to you again.

"Jesse Houwing" wrote:

Hello Brian,

Thank you Jesse, that will help a bunch. I have C# classes
scheduled Aug/Sep/Oct so that I can try to avoid more questions
in the future.

You're very welcome and don't hesitate to ask questions that is
what these newsgroups are for after all. Just make sure you've
tried ;)

Jesse

V/R

Brian

"Jesse Houwing" wrote:

Hello Brian,

You can do this quite easily with either a regex or a simple
function I'll try to demonstrate both:

basically you need a regex that matches the
\r\ndate+time+[number] that is followed by two spaces and
replace it with a space

so the regex looks like this (I simplified it a bit, but given
the fact that all the lines are very well layed out I don't
thing you have to make it more differcult :).

\r\n[^\]]+\][ ]{2}

now replace this by " " and you're all set.

So:
private static Regex _rx = new Regex(@"\r\n[^\]]+\][ ]{2}",
RegexOptions.Compiled);
private string LayoutInput(string input)
{
return _rx.Replace(input, " ");
}
Or you could use a StringReader/StringBuilder combination:
private string LayoutInput(string input)
{
StringReader sr = new StringReader(input);
StringBuilder sb = new StringBuilder(input.Length);
string line;
while ((line = sr.ReadLine())!= null)
{
if (line.Length < 29) { throw new
InvalidOperationException("invalid
input"); /* or simply continue; */ }
if (line[29] != ' ')
{
if (sb.Length > 0)
{
sb.Append("\r\n");
}
sb.Append(line);
}
else
{
sb.Append(line.Substring(30));
}
}
return sb.ToString();
}
I haven't used much error checking to make sure the format is
correct. But I think you can go on from here.
Jesse
Sent you an email with a sampling.

"Jesse Houwing" wrote:

Hello Brian,

Could you send a sample file with two of these data blocks f
what they should look like and how you receive them directly
to my email? I'll reply back to this thread with a solution
when I have time to go over it. My newsreader keeps messing
up your samples, so it would be easier if I had two text
files.

Jesse

Jesse the data inside of the [] is static six digits for
each line.

The total number of lines may vary anywhere from one to as
much as 8 lines of data.

The line should look like this when pieced together;

2007/07/27 11:00:03 [153006]ARES_INDICATION 010.050.016.002
404.2.01
(6511)
RX 74 bytes 65 11 26 02 BC 6C AA 20 76 93 51 53 50 76 13 48
52
00
52
02 02
C7 83 D7 07 07 1B 0B 00 00 00 00 28 0A 06 06 06 06 06 06 06
06
06
06
06 0A 06
06 06 0A 06 06 06 0A 06 06 06 06 06 06 06 06 06 06 06 06
06
06
06
06
06 06 06 40
All in one big line without word wrap.
The next entry would be a different header (ARES_INDICATION
for
example).
"Jesse Houwing" wrote:
Hello Brian,

2007/07/27 11:00:03 [153006]

My guess is that these aren't the same length for every
line. If they were it would be easy:
.



Relevant Pages

  • Re: Read text file to Temp file and apply formating and color chan
    ... I call this routine from the OpenFileDialog; ... public static string LayoutInput ... int txPos; ... int charactersTillPoint; ...
    (microsoft.public.dotnet.languages.csharp)
  • Change the color of text at a specific location on each text line
    ... public static string LayoutInput ... int txPos; ... int charactersTillPoint; ... if (txPos> -1) ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Trim a multiple line message to a single line
    ... "Jesse Houwing" wrote: ... Find the location, split the string. ... private string LayoutInput ... Again you could use a regex for this... ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Reading from file into an array
    ... Jesse Houwing ... string row = textIn.ReadLine; ... using (StreamReader textIn = new StreamReader(new FileStream(path, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: String format
    ... "Jesse Houwing" wrote: ... How is this possible?This string has many dates like above in the ... The MatchEvaluator is used to parse the dates (I used the exact ... format used here to make sure it parses correctly). ...
    (microsoft.public.dotnet.framework.aspnet)