04 November 2009

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

15 October 2009

Microsoft C++ != ANSI C++

I am a little bit bitter with Microsoft. I am sure a lot of you out there feel the same way. Our reasons may be different though. My biggest problem with MS is that they take things, any things, and try to make them into their own brand that doesn't like to play well with others. Aside from this, I think that Windows is a great OS. Heck I can't not like it right? Everyone uses it. Well today they might, but in times past Windows was not even an option. Lets take a look back for a moment.

C was developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system. Pretty cool huh. In 1983 the X3J11 committee, formed by the American National Standards Institute (ANSI), started the process of standardizing C.

I know what you are thinking...
"Well Seth, why are you telling me all this bunch of bull crap about the history of C and how you hate MS?"

If you are starting to put the pieces together you already know where this is going. If not just read on and try to imagine what things would be like if there were no standards. I want you to learn about a very bad, nay cursed evil.

Anyhow, while ANSI was creating standards for C, the same sort of thing was happening with C++. You can read all about the history of C++ here. It is nothing less than fascinating. The guys who came up with this, the purest form of C++, are the architects of the modern programming language C++. I kneel before you great oh great lords of code.

Now about this time, Microsoft walks up into the realm of software compilers. So far so good, there is nothing wrong with a little healthy competition when it comes to software.... Or is it...

World: Whats that MS is pulling out of his back pocket? MFC? StdAFX... Windows API? What is this trickery that you are pulling before the our eyes? And what the hell is .NET?
MS: Oh this? This is what the creators and ANSI want C/C++ to REALLY be. Besides we are making it EASIER for you to program.
World: Oh ok... But I thought that ANSI came up with a standard that C and C++ compilers are supposed to use.
MS: Bah hah foolish children, there is no other C++ but Microsoft C++. Look, you can make a dialog box with one single command now!
World: Oh ok... So I can use this to create software.
MS: Yes of course... Windows software. There really is no other OS out there so whatever. Here is a sort of class thing that accesses the windows registry with C++! How cool is that?
World: Oh ok... Cool I guess.
MS: Trust us, you won't even have to learn ANSI C/C++ to use our modified versions. Here's a C++ function to show your icon! Presto!
World: Oh ok... That does sound alot easier.

What I just did there was I told a little story. A story about how the world was tricked by Microsoft. I was tricked, but here's the thing. I didn't even realize I was tricked.

My first compiler was Visual C++ 6.0. Oh yes I shelled out some big bucks for that monster. As a cool bonus I got a membership with Microsoft's Developer Network and recieved free CD's about 4 times a year. I thought I had everything I would ever need in order to learn how to program.

Had I only had a mentor to steer me in the right direction.

As it turns out, I was headed down a road that I wish I never took because I feel like I wasted a couple of years learning the MS bastardized version of C++.

It wasn't until I started to dabble around with Linux that I realized there was something strange. At first it was a small trickle of questions. Whats this POSIX stuff? Curses? WTF? Then the flood gates opened and I knew I had been duped by MS. Those bastards.

Some of you may understand what I am talking about. Others maybe not so much. But I am here to tell you one thing.

Microsoft C/C++ is NOT ANSI C/C++

Do not make this mistake. Do not create an entire set of your own libraries that are based on Microsoft standards. While this is fine if you know for a fact that you will never ever leave the safe and comfy confines of Windows, be aware that once you cross that line into the world of X you are going to either have to gut your libs or write ports of MS functions just to make it compile.

So please if you don't learn anything from my blog, please just take this away. The reason for me telling you this will become more and more clear as I continue to guide you on this journey. For now just tuck it away in your mind... One more time because this is important:

(Microsoft C/C++) != (ANSI C/C++)

What's next? I will be talking about a few terms used within C++ & a few of the more simple things that most documentation won't tell you (but should, but leave it out because they assume you already know these simple things).

MinGW & Code::Blocks


I'm going to talk about MinGW and Code::Blocks. I know what you're thinking... "What did he just call me?" Well relax, I didn't call you any names. Eh yeah... I'll just dive right into it then...

MinGW


What is it?

MinGW is a contraction of "Minimalist GNU for Windows", is a port of the GNU Compiler Collection (GCC), and GNU Binutils, for use in the development of native Microsoft Windows applications. (from the mingw.org website)

What does it do?

This suite of tools will allow you to compile executables for Windows from the following languages:
  • C++
  • C
  • Fortran
  • Java
  • Ada
  • Assembly Language
Why should I get it?

It's free. It's also widely used and robust.

Ok, now that you know what MinGW is, you can forget about it. Unless you are an overachiever, in which case you can learn more here.

All you really need to know about MinGW is that MinGW is a free C/C++ compiler that comes bundled with Code::Blocks. You CAN use other compilers/debuggers with Code::Blocks, if you are some kind of wierd engineer working on DNA sequencing software on some database that resides on some ancient mainframe hardware that requires a certain build of a certain kind of compiler that only compiles a modified fortran dialect (chuckle, what a loser)...

Code::Blocks

What is it?

Code::Blocks is a free C++ IDE built to meet the most demanding needs of its users. It is designed to be very extensible and fully configurable. (from the codeblocks.org website) In short it is a highly advanced super-version of Notepad.exe.

What does it do?

This thing will let you type in the C/C++ script and then click a button and compile and execute it. Very cool. More information can be found here.

Why should I get it?

Ok this is kind of like asking the question would I rather walk to the store which is 5 miles away, or do I get in my car and ride on my butt to the store which is 5 miles away, letting the car do all the hard work. Yes in this case the software is the car. So unless you want to pretend you are a computer scientist from the late 1970's and like dealing with command line prompts, I highly recommend you get this software.

There you have it. Open Source software which will allow you to create your own software. How great is that? In this post I am only going to talk about what MinGW and Code::Blocks are and I have provided you with the links to the websites. I'll go over creating some simple programs next. So go ahead and download and install, and get ready for the ride of your LIFE! WOooooOOoO!

Code::Blocks / MinGW Bundle Download from Sourceforge

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...

Seth... Coder


Well it's been a while since I last posted anything on here, I could not really think of anything to post. Go figure.
What I have been doing is thinking about what I want this blog to become. I want to give my knowledge back to the world and to help aspiring coders. I want to share some of the pitfalls that I fell into while learning so that others do not waste thier time.
I am going to start posting some stuff about C++, PhP, and other programming languages and/or scripting tools. I've been dabbling with this sort of thing ever since my loving mother bought me a Commodore 64 when I was only 12 years old. Back then, Microsoft BASIC 2.0 was the operating system. I tore the machine apart and learned assembly language using the Commodore's 6510 (6502 based CPU). For whatever reason, I became obsessed with learning how it worked and how to make it do whatever I wanted it to do. Over the years I have upgraded my machines. C128, Amiga, 386, 486, Pentium, AMD, now um... Linux, FreeBSD, etc. All the while I have studied in great detail how to program the machines in various languages. I was using BBSs with 300 baud modems to connect to the FIDOnet in 1986.
By far, my favorite language is C/C++ so that is where I am going to primarily steer on this adventure. If I get off course with some wierd language like Ruby please someone talk some sense into me. If you know me please hit me in the face. (Yes that was a dig at Ruby)
So anyhow, I'll get started here.

27 February 2009

Computer Viruses Suck

I really like tinkering around with computers. It has always intrigued me to learn about how they work and what they can do. I am still constantly amazed by the new technologies that come out each day. One thing I do not like though is malicious software. Any kind of this software such as viruses, trojan horses, worms, adware, etc. has no place in today's world of computing. Yet all of it exists and it is becoming more prevalent every day.
I just did a thorough scan of my computer and it found 235 infected files. This is just ridiculous. I remember installing Windows XP once and I had the internet connection plugged in. In between the time that I realized it and the network hardware activated (about 20 seconds) 4 worms penetrated onto the system. Luckily they weren't very harmful ones and I was able to manually patch up to SP2 with a CD that I burned it to beforehand. But seriously should I have to do this?
I liken this to to anarchy. Even though there is software out there to prevent and remove viruses and malicious software, people are still creating them. Computers don't create them.
Come on people seriously, stop it. I have had to reload my windows like 4 times this year just because people wrote something malicious and it screwed up my computer. I am getting tired of it.
Well I guess I will keep my windows disks close and use Linux as much as I can. I don't see this situation getting any better any time soon. In fact it probably will get much worse.

17 February 2009

GIMP

I want to share a secret with everyone that I have known about for a long time. Don’t worry it isn’t top secret. Well, it probably isn't that much of a secret to everyone. There is free software called GIMP, which is short for GNU Image Manipulation Program. I’ve been using it for a while now. I discovered it back when I was first starting to tinker around with Linux. During that time, GIMP was just being developed, and was somewhat basic. Since those days the software has become more robust, more feature rich, and cross platform. It rivals the latest offering from Adobe Photoshop. Best of all it is free. There is a version of GIMP for use with portable pen drives that you can take with you and use from any computer. If you have ever found yourself wondering what graphic program to use to make a cool graphic for your blog or website, then this software is for you. It won’t make you a professional graphic artist overnight, but you will find that there are a lot of great tools you will have at your disposal to easily create graphics. I am not going to go into any details or tutorials about how to use the software. You will find tons of information like this already exists on the webs just Google search it.
I have created a couple of images to give you an example of what you can do with GIMP. It should also demonstrate my strange mind at work. Now on with the show!

Sarah my lovely wife. This is a frame from a movie she was in in 1972 called 'Grelsh'.

Cleveland the dancing monkey! I drew him with paint brush tool then added a couple of filters.

This is my sister. I wonder what she is thinking about.


If you want to get the software it is found here:
GIMP home page: http://www.gimp.org/
GIMP Portable: http://portableapps.com/apps/graphics_pictures/gimp_portable

Good luck and happy gimping!

15 February 2009

Totally Secure Radio


This is something I found while I was out working the other day. It is a AM/FM cassette/CD player and it is attached to a telecommunications frame with a link chain and secure lock. The sad part is that the chain and lock probably cost more than the radio.

14 February 2009

First Post

Well here it is, the first post of my new blog. I am somewhat new to blogging, but I have had my hands in other projects and websites for a while now. So what do I do with this blog? Lets see... Maybe I could take a picture of my dog's butt and put it up on here, but I dunno if that is going to be in violation of the TOS of blogspot. So being that what it is, I will refrain and save you the 5 seconds of your life that you will never get back.
WHAT DO YOU PEOPLE WANT FROM ME?!??!?? I pour my heart and soul out on this site and yet nothing! I get no followers! zOMG WHY!?!
Wait... I just set this site up like 5 minutes ago and this is my first post. How could I possibly believe that people are lining up to read my lovely blogs? Ok... I'm ok. Seriously. Deep breaths. I watched a youtube video on how to deal with this kind of stuff. Or was it how to mix mentos and diet coke? Dang, I always get those confused.
Thank you for visiting. Come back soon and we can have great fun in the wayubs. Yes those great enter wayubs. loller!