Advertisement

Ever use C# or another language to do your dirty work?

Started by September 24, 2012 10:47 PM
20 comments, last by Heath 12 years, 4 months ago
I caught myself doing this again today, and figured I'd make a post about it... But I often use C# (and even C and C++) to do "dirty work" for me. For example, formatting a bunch of mixed up text or stripping data out of something I need for a document I'm working on. I'd be willing to bet other programmers do this too! I'm not talking about writing a practical, reusable application...but rather writing a "quick and dirty" application that will do a bunch of work for you in a fraction of a second.

Here is what I just did... I wanted to write a C# extension method that could return the size, in bits, of a DXGI Format (using SlimDX's "Format" type). It would've been easy to do a case/switch style method with a big, monolithic structure. But that's a bad idea from a performance standpoint; especially if some code needs to read the size of any arbitrary format every frame. That's a bunch of wasted time. Instead, the optimal way to do it would be to create an array of sizes which can simply be indexed with the Format value casted to type int. Here's what I mean:

int size = _sizesOfAllFormat[ (int) format ];

That would be FAR faster than a huge, monolithic case/switch operation to run through every possible format.

However, this presented me with a problem. Going through, one by one, all of the DXGI Formats and hand-typing in the sizes. There's like 100 of them (including Format.Unknown), so you can imagine how much of a pain in the *** that would be, as it MUST be done in perfect order for this to work. And there was simply no way I was going to waste the better part of a day hand-typing in all of that crap when I could be working on my engine!

So I decided to let C# do the dirty work for me, once again. A quick Google search yielded a C# file from SharpDX's repository in which all DXGI Formats were listed with their sizes in bits. But they were listed like this:

Format1
Format2
Format3
16

Format4
Format5
32

So I simply used Find+Replace to turn this into one of those monolithic case/switch structures that returned the size in bits of any format passed as a parameter. The method had the following signature:

static int GetSizeInBits(Format format)
{
// The huge switch went here...
}

Now to actually generate the desired code we want: an int[] array containing all of the sizes, in order. Here's the code I wrote to do it:

[source lang="csharp"]

static string sizes = "int[] _formatSizes =\n{\n";

static void Main(string[] args)
{
Format[] allformats = (Format[])Enum.GetValues(typeof(Format));

int item = 0;
foreach (var format in allformats)
{
int size = GetSizeInBits(format);

sizes += string.Format("{0}, ", size.ToString());

if (item == 10)
{
sizes += '\n';
item = 0;
}
}

sizes += "\n};";

System.IO.File.WriteAllText(@"C:\Users\ATC\Desktop\FORMAT_ARRAY.txt", sizes);
}
[/source]

...hit "Debug", the Console flashed up and closed and my text file with my code was spit right out in a fraction of a second! It was formatted a little sloppy, but it was easy to correct within a few seconds. :-)

Just to make sure the code was generated correctly before copy+pasting it into my engine code base I ran the following test:

[source lang="csharp"]static void Main(string[] args)
{
Format[] allformats = (Format[])Enum.GetValues(typeof(Format));

foreach (var format in allformats)
{
int s1 = GetSizeInBits(format);

if (s1 != _formatSizes[(int)format])
Console.WriteLine("God damn it...");
}
}[/source]

I ran the code and the Console did not say "God damn it..." so apparently all went well lol. biggrin.png

Now, I have this handy extension method for my engine which you might even find a use for in your own SlimDX-related projects:

[source lang="csharp"]
static int[] _formatSizes =
{
-1, 128, 128, 128, 128, 96, 96, 96, 96, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 32, 32, 32,
32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
32, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 8, 8, 8, 8, 8, 8, 1, 32, 32, 32, 4, 4, 4, 8,
8, 8, 8, 8, 8, 4, 4, 4, 8, 8, 8, 16, 16, 32, 32,
32, 32, 32, 32, 32, 8, 8, 8, 8, 8, 8
};

public static int GetSize(this Format format)
{
return _formatSizes[(int)format];
}
[/source]

But I was wondering, how many of you often find yourselves using your programming skills to do your dirty work for you, be it homework, text formatting, etc? What's one of the funniest or most clever things you've done with your programming skills?

For me it would have to be using my programming skills to get one of the most beautiful girlfriends a man could ask for (now an ex-girlfriend, thank God, since she was crazy lol). I actually wrote an application with some GDI+ graphics that displayed bits of a "love letter" and eventually asked her out lol. She loved it because she said no guy had ever put that much thought towards asking her out and she thought I was like the smartest guy ever lol...
_______________________________________________________________________________
CEO & Lead Developer at ATCWARE™
"Project X-1"; a 100% managed, platform-agnostic game & simulation engine

Please visit our new forums and help us test them and break the ice!
___________________________________________________________________________________
Not usually, no. That sounds like something I would probably create a macro for however. I use macros all the time for quickly editing similar lines of code and / or long repeated tasks of text editing.
Advertisement

That sounds like something I would probably create a macro for however.



Interesting. Do you think a macro would've been able to solve the problem I had earlier? I don't understand how it could have, as it required several stages of work beyond what I could imagine doing with a keyboard/input macro.

_______________________________________________________________________________
CEO & Lead Developer at ATCWARE™
"Project X-1"; a 100% managed, platform-agnostic game & simulation engine

Please visit our new forums and help us test them and break the ice!
___________________________________________________________________________________
I generally apply the 'ball ache' factor to it aka "can I be arsed to write code I'll only use once or can it lead to better things?"

In this case I'd just do the copy and paste dance once I'd had a look over it, seen the pattern and then rolled with it.
OR
I'd only do what I needed at that moment and return to it later to care about other things aka 'I only need format 1 & 3 right now.. screw it, switch statement it is...'.

Of course if I need to do deeper analysis then out comes the Programmer UI in Winforms to let me visualise data but even that generally isn't fancy.

Example; I needed to get a feel for how many types of mesh were in a 9 cell area of a map which was 27x31 in size. Result: Winform's app which had a tiny bit of GDI to visualise the 3x3 grid and some list boxes to let me select the target centre cell. As I required more info I just bolted more boxes onto the form. Nothing fancy, but did the job of giving me the data I needed and getting the data by hand would have been a ball ache...






Interesting. Do you think a macro would've been able to solve the problem I had earlier? I don't understand how it could have, as it required several stages of work beyond what I could imagine doing with a keyboard/input macro.


I would need to see the exact starting text, but I use it all the time to expand lines of text into functions, or to convert xml to json. Things like that.
I used to load up QBasic.exe to do certain text processing things.

The hardest part of learning C for me, after years of Basic, was the bullshit string handling in C's standard library. In Basic I dealt with text strings in a straight forward manner. In C it was suddenly about memory management and other issues.
Advertisement
Isn't that what much of programming is for? Using code to solve problems, constructive laziness and all that?

I've used C# to help generate a few spreadsheets I didn't want to fill out for somebody, yes. Nothing fancy at all, I just printed all the text entries (which were randomly generated) into a text file with indentation to format it, and then I copied that into Excel or whatever. I really didn't want to do that spreadsheet.

I would need to see the exact starting text, but I use it all the time to expand lines of text into functions, or to convert xml to json. Things like that.


Now that I've thought about it a bit and read up a little on using macros, no, I don't think it would've been possible. The starting text was totally out of order, for one. It was much more than a formatting issue. Unfortunately I dumped all the text files in my recycle bin earlier today and emptied it, so they are as good as gone. I'm not going to try to do a memory recovery just to get them back lol.

C#, however, made it very easy. I took an operation that would've cost me numerous hours to do by hand and knocked it out in the matter of minutes it took to write the code and the fraction of a second the program took to execute. But you have taught me something. I can indeed start using macros for certain things. Thanks for the suggestion/info!

It's something I do quite a bit: using C# to do tedious/busy little jobs I don't want to do by hand. I also find myself using mini C# applications to test ideas and figure things out quite a bit. Often times when I'm working on a complicated C or C++ project I will open up a new instance of VS and start little console apps to test architectural concepts before trying to do a full implementation. I even do this with other C# projects to test algorithms and bits of code when I'm not ready to do a full build of my project.

I'm surprised no one laughed at me for the last part of my original post though, lol. It's a true story, and a lot of my friends still bring it up and laugh about it time to time haha. XD
_______________________________________________________________________________
CEO & Lead Developer at ATCWARE™
"Project X-1"; a 100% managed, platform-agnostic game & simulation engine

Please visit our new forums and help us test them and break the ice!
___________________________________________________________________________________

For me it would have to be using my programming skills to get one of the most beautiful girlfriends a man could ask for (now an ex-girlfriend, thank God, since she was crazy lol). I actually wrote an application with some GDI+ graphics that displayed bits of a "love letter" and eventually asked her out lol. She loved it because she said no guy had ever put that much thought towards asking her out and she thought I was like the smartest guy ever lol...

I'm torn, so I'll just give you two responses.

Response 1: Man, we men sure do some stupid things for chicks. Yeah, like that one guy who proposed at that Sacramento Kings game a few years back? It was all over Failblog, the friggin mascot gave him a beer if I recall. Pssht, she crazy.

Response 2: Man, we men can complain, but we sure have it easy. All we have to do is say "Pssht, she crazy" and that's it. Doesn't matter if she was "one of the most beautiful girlfriends a man could ask for", and good enough to go through all this effort and write an app for her. rolleyes.gif


I have in that past, but now that I know better, C#, C, and C++ would be pretty far down the list of tools I'd use to do text formatting work --

Depending on how the data is formatted, macros in Word or Excel can be used if those are tools you're familiar with.

Vim's commands can do damn-near anything, and is sometimes nice if you have to "explore" a bit because you don't know the exact commands necessary, because you can see the result of each step quickly and back it up if you bork things.

sed and awk are tools from the *nix world (also available on Windows or any other OS) that do exactly that with text, using regular expressions. sed performs fairly simple substitutions based on a regex match. awk has more fully-featured programmability that occurs based on the same style of regex match.

If you need something even more programmable than awk, Perl was designed for extracting text and producing reports from it, so it's very good at this type of work.


These options are still "programming" in a fashion, but in general are far more tractable and easy than general-purpose programming languages, once you know the tool. If you're dealing with binary data, the first thing I'd do is see if there's a command-line tool that can do what I need on one file, then I'd create a script to automate it over multiple files (the general pattern with binary data processing), possibly in combination with other command-line tools. This is actually "the Unix philosophy", where typical programs do one thing really well, and you pipe intermediate data between programs to massage the output into the form you want. For example, grep, sort, and the more program are commonly used together.

throw table_exception("(? ???)? ? ???");

This topic is closed to new replies.

Advertisement