11/06/2011

Make Your Own Hacker Typer 1

Today our first follower on Twitter, @aleksandar6630, asked me to visit an interesting application, Hacker Typer by Simone Masiero, to click 'Hack!' and to type. You can try it now by yourself. Pretty awesome?! It is open source, so if you're into JavaScript, you can check out Hacker Typer on github. But, Aleksandar gave me an idea for my first code here - let's see how we can write our own Hacker Typer in C!

I assume you have already installed IDE etc. If you haven't, and you don't know what you should do, read the Install tips first :) So, let's do some coding.

Assuming that you already know some basic stuff (you know how functions work, how to read files, etc.), I'll write it as short and understandable as possible. See if you can understand, if you can't - I'll put some tutorials on functions, variables, files and so on here.

So, we will need a function to read the characters as we type, and to read some awesome source code from a file and write it on the screen. We will use getch() for our keyboard input without echo, getc() to read chars from the file and putchar() to write them out on the screen. That's for the first version, later on we can add some more options, like choosing the input file, number of chars to write out when we press a key and so on. You should
#include <stdio.h>
in the beginning of the code, because these functions are defined in it.

We will write everything as a separate function which we will call from the main() function, so we can use it later in other programs ("Where the heck will we use it?" I don't know, but it's always better that way).

So, assume that we have an opened file for reading. For now, that will be our only argument for the function. Here's what it should look like:
int hackertyper(FILE *fin)
/* the return type is int so we can return
   error code if there is an error; we will
   skip that for now

   arguments:
     FILE *fin - pointer to the opened file
*/
{
  char key, fch;
  /* key - key read from the keyboard
  fch - char read from the file */
  while ((key=getch())!=27) // ascii for ESC key
  {
    fch = getc(fin);
    putchar(fch);
  }
  return 0; // zero exit code - no error
}
So now we have a function which reads keys from the keyboard, and writes out output from the file. Note that we don't check for the end of the file, and eventually we will run out of buffer. What then? We can reset the file and start from the beginning, or we can just exit the function (which is not so cool if we have short file, and plenty of "work" to do, you know what I mean). So, to reset the file when we reach the end, before the
fch = getc(fin);
line we can add the check:
if (feof(fin)) rewind(fin);
That should position file pointer to the beginning of the file, once we have reached the end.

That's it. Now all you need is a source code file (you can also use this one :D). Call the function from main(). The main function should look something like this:
int main()
{
  FILE *dat = fopen("hcktp1.c", "r");
  // instead of hcktp1.c you can use whatever
  // file name you have; if you don't write
  // the full path, the file should be in the
  // same directory like the executable
  hackertyper(dat);
  return 0;
}
Warning: getch() is not a standard C function. It comes with conio.h, and is made for MS-DOS, so it's not available in Linux. It's also deprecated, recommended variant for new systems is _getch() (locking the thread) or _getch_nolock() (not locking the thread), for Windows. Now, for Linux users, there is a method to emulate getch() function via changing terminal status; you can find the code here.

And that's it. Compile and run. Now, in the part 2, you will see how to add some extra options which the original Hacker Typer supports. Until then, you can experiment, and tell me if you find out something new. Cheers!

The complete hcktp1.c (works on Windows and Linux, just read the comment in the beginning).

2 comments:

  1. Idea: Perhaps just "Thank you!" is not useful.

    ReplyDelete
  2. Maybe not really, but I wish I knew when someone finds these topics useful, so it is useful in a way :)

    ReplyDelete

If you have anything useful to say (ideas, suggestions, questions, business proposals, or just "Thank you!"), please feel free to comment! Also, you can use my e-mail and/or find me on Twitter!