Showing posts with label source code. Show all posts
Showing posts with label source code. Show all posts

02 November 2009

Timers

Ok well I decided to take this in a little different direction. I am going to start putting small bits of code up here and piece them together into different things. That way you can learn and I can experiment. So if you are as excited as I am about programming then whip out your compiler and lets get started.
The first thing I will go over is how to create a simple timer which you can use to trigger things. What things? Anything you want really. In my example it will put a line out to the console after a certain amount of milliseconds.
By the way, 1000 milliseconds is equal to 1 second. So 10000 milliseconds would be equal to 10 seconds. It seems complicated but lets take a look.
One thing you should also consider is that time is relative when dealing with different operating systems. A tick is the smallest resolution of time that the CPU your dealing with can process. (I generally think of a tick as 1 millisecond) In my example I am using a long variable named sc_timer. It will capture the current time by using getticks(); function. To get the timer to trigger, you subtract sc_timer from getticks(). The result is how much time has passed since you last updated the sc_timer variable. Pretty dang cool. You can use an if statement to compare if the elapsed time is greater than a certain amount of milliseconds. In my example I used 10000 milliseconds (10 seconds).

I created a console application in Code::Blocks and modified it... main.cpp:

#include <iostream>
#include "sc_getticks.h"
using namespace std;
long sc_timer;
int main()
{
sc_timer=getticks();
while(1)
{
if( (getticks() - sc_timer) > 10000)
{
sc_timer=getticks();
cout << sc_timer << "\n";
}
}
return 0;
}



Next I created a new file and called it sc_getticks.cpp

#ifdef _WIN32
#include <windows.h>
#endif
#include "sc_getticks.h"
long getticks(void)
{
#ifdef _WIN32
return GetTickCount();
#else
long curtime; struct timeval tp; struct timezone tzp; static long secbase;
gettimeofday(&tp,&tzp); if(!secbase) { secbase = tp.tv_sec; return tp.tv_usec/1000; }
curtime = (tp.tv_sec - secbase)*1000 + tp.tv_usec/1000; return curtime;
#endif
}
You can see that I put in some preprocesser directives. Don't worry about that for now. All you want to concentrate on is that this function will return the current tick count. (Note: if you are just using Windows systems you can replace getticks(); function and file and just use GetTickCount(); )

One more file... (its a small one) sc_getticks.h
extern "C" long getticks(void);
Now when you compile all this together you should get a console application that spits out the current tick count once every 10 seconds.

There you have it, a relatively painless timer. What you do with it is up to you. I will build on this example so stay tuned!

Download files for this project

14 October 2009

Thumbs.DB Annihilator


Here is a computer program that I created that I call Thumbs DB Annihilator!

This program will search your hard drives for all those Thumbs.db files that Windows thinks it has to create and it will DELETE every single one of them. WARNING: This WILL DELETE all THUMBS.DB files on ALL YOUR DRIVES. (Turn off the Windows System feature that creates thumbs.db files)

This program is specifically written for Windows systems and I don't think it will compile on anything else. In this instance it does not matter because all I care about is deleting the thumbs.db files that Windows creates. I will get into creating code that will compile on more than one OS a little later.

Here is the link to the zip file from my Google site:

Thumbsdb.zip

This file has the executable in it and also has the Visual C++ workspace to build it if you have that. The following files should be in the zip file:

c_thumbsdb.cpp contains class CThumbsdbCleaner
c_thumbsdb.h contains definition for class CThumbsdbCleaner
thumbsdb.cpp contains int main(int argc, char* argv[])

If you are using MinGW (GNU C) to compile from command line type these lines into the command prompt to compile it:

g++ -c thumbsdb.cpp
g++ -c c_thumbsdb.cpp
g++ *.o -o thumbsdb.exe

You'll probably notice that it is kind of overly complex code for what it is, but there is a reason for it and I will build on it in the future. I also left in alot of commented out code that you can toy around with to make it delete other files. Be extremely careful with this or you may delete some things you don't want it to. For now, take some time and just look around into the C++ class and structure.

Even with the extra class file and header file, it is a very simple program. It just reads the disks folders and looks through them for the thumbs.db files and if it finds one it zaps it. It adds the total number of files it deletes and the size of the files in case you are curious.

The best way to use this program is from a command prompt, but you can just double click on it from windows and it will still work. The only downfall to executing it by double click is that you won't be able to see the final results because it will close the console window immediately after the program exits.

I will be posting more of my little projects as time passes.

I will talk about Code::Blocks and MinGW coming up...