Mike’s Dump

May 8, 2005

WTF

Filed under: Code — mikesdump @ 2:03 pm

I check The Daily WTF
blog pretty
much every day. I have seem some pretty “interesting” ways to solve
some problems that sometimes didn’t need to be solved but most of the
time
it gives me a good laugh. It is kind of like laughing at someone that
gets hurt, it is funny as long as it isn’t you. Well this past weekend
I had to put in some overtime and found this “fine” piece of code that
I had to “refactor”. What the code is trying to do is take the data
received from a UDP packet and prefix or suffix a the message with IP
the message was received from (if configured).



If String.Compare(protocolTaipNema, "TAIP", True, New System.Globalization.CultureInfo(4105)) = 0 Then
'Get rid of  38 108 116 59 and  38 108 116 59
If e.GetData(0) = 38 Then
Array.Copy(e.GetData, 3, e.GetData, 0, e.GetData.Length - 3)
End If

If String.Compare(noneSuffixPrefix, "PREFIX", True, New System.Globalization.CultureInfo(4105)) = 0 Then
addressLength = addressValue.Length
length = e.GetData.Length + addressLength  'e.Data.Length() + addressLength
ReDim address(length)
Array.Copy(e.GetData, 0, address, (addressLength), e.GetData.Length)
For i = 0 To addressLength - 1
address(i) = Convert.ToByte(addressValue.Chars(i))
Next
packetInfoValue.Body = System.Text.Encoding.ASCII.GetString(address)
End If

If String.Compare(noneSuffixPrefix, "SUFFIX", True, New System.Globalization.CultureInfo(4105)) = 0 Then
addressLength = addressValue.Length
length = e.GetData.Length + addressLength
ReDim address(length)
Array.Copy(e.GetData, 0, address, 0, e.GetData.Length)

For i = 0 To length
If address(i) = 62 Then
i += 1 'next position
Exit For
End If
Next
Dim count As Integer
For count = 0 To addressLength - 1
address(i + count) = Convert.ToByte(addressValue.Chars(count))
Next
packetInfoValue.Body = System.Text.Encoding.ASCII.GetString(address)
End If

If String.Compare(noneSuffixPrefix, "NONE", True, New System.Globalization.CultureInfo(4105)) = 0 Then
addressLength = addressValue.Length
length = e.GetData.Length + addressLength
ReDim address(length)
Array.Copy(e.GetData, 0, address, 0, e.GetData.Length)
packetInfoValue.Body = System.Text.Encoding.ASCII.GetString(address)
End If
End If

Dim o As New Object
o = CType(packetInfoValue, Object)
InterProcessCommunicationValue.Send(o)

… it isn’t as funny when you have to fix it.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: