Sign in to follow this  
  • entries
  • comments
  • views

I Am A Terrible Programmer, Part II

Sign in to follow this  


Now that (I'd like to think) I have a working knowledge of the C++ language, I'm trying to branch out a bit. As such, I signed up to take a C# class this summer.

It's weird learning a new language. Picking up on the basics of the language and getting things working is easy enough; it's usually just a matter of hunting through intellisense for a correct-sounding function and asking the occasional question. But, its a far cry from getting something working to actually utilizing the language. I know just about enough C# to realize that I am just writing C++ code with C# syntax, and that I'm not doing anything in a particularly clever, C#-friendly way.

Take my file parser for instance. I needed to save out and load in collision and waypoint info for the map editor. Now, I should be doing all this in XML, but I don't really know how to use the built-in XML parser in .NET, so I'm just using plain text and white-space separated values. This leads to one of the dirtiest, most unsafe file parsers I've ever written (and I've written some pretty terrible ones). Sure, it works (if the text file is not malformed), but this is some dailyWTF quality code, if you ask me.

Of course, I couldn't say that without showing you my code. That just wouldn't be fair, now would it? Hope you have a good laugh, at least:

private void ReadMapFromFile(string filename)
string delim = " ";
char[] delims = delim.ToCharArray();
System.IO.StreamReader reader = new System.IO.StreamReader(filename);
string s = reader.ReadLine();
System.Diagnostics.Debug.Assert(s == "MAP");
s = reader.ReadLine();
System.Diagnostics.Debug.Assert(s == m_FileName);
s = reader.ReadLine();
System.Diagnostics.Debug.Assert(s == "RESOLUTION");
s = reader.ReadLine();
string[] words = s.Split(delims);
int X = Convert.ToInt32(words[0]);
int Y = Convert.ToInt32(words[1]);
s = reader.ReadLine();
System.Diagnostics.Debug.Assert(s == "LINES");
while ((s = reader.ReadLine()) != "CIRCLES")
words = s.Split(delims);
Point begin = new Point(Convert.ToInt32(words[0]), Convert.ToInt32(words[1]));
Point end = new Point(Convert.ToInt32(words[2]), Convert.ToInt32(words[3]));
System.Collections.ArrayList al = new System.Collections.ArrayList();
while ((s = reader.ReadLine()) != "RECTANGLES")
words = s.Split(delims);
Circle c = new Circle(new Point(Convert.ToInt32(words[0]), Convert.ToInt32(words[1])), Convert.ToInt32(words[2]));
while ((s = reader.ReadLine()) != "WAYPOINTS")
words = s.Split(delims);
Rectangle r = new Rectangle(Convert.ToInt32(words[0]), Convert.ToInt32(words[1]), Convert.ToInt32(words[2]), Convert.ToInt32(words[3]));
while ((s = reader.ReadLine()) != null)
words = s.Split(delims);
Waypoint wp = new Waypoint(new Point(Convert.ToInt32(words[0]), Convert.ToInt32(words[1])));
for (int i = 2; i < words.Length; ++i)
catch (System.IO.IOException)

And here is a sample text file it would read in:

1024 768
281 322 490 214
490 214 362 520
362 520 173 501
173 501 281 322
710 395 141
99 98 45
160 709 715 50
864 102 -282 -48
57 572 3 1
312 123 0 2
602 188 3 1
449 609 2 0

At this point, class is over in 2 weeks so I'm just crunching to get everything done. Clean, safe code be damned [razz].
Sign in to follow this  

1 Comment

Recommended Comments

The XML stuff is pretty easy to pick up (I used the XPath interfaces to great effect in my Collada converter). Alternatively you can use tools like ANTLR to produce C# code for parsers for you text data. They'll be more error-tolerate than your hand-rolled one.

Share this comment

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now