Friday, June 01, 2007

STL Maps on TurboC++

So, TurboC++ doesn't really have a proper STL implementation, which means you don't have the STL maps, either. So I made my own implementation of the maps, compatible with TurboC++, using a linked list as the container. This was made just for fun, not for being used on a daily basis. Use it at your own risk.

Download it here: http://argent.50webs.org/iMap.h. Note that this requires the latest version of my iString class. Many changes were specifically made to the iString for this class, making it much more useful, but still not as good as the STL string class.

Saturday, April 14, 2007

Encryption

One of my friends and I needed a secure way of communication over scraps. We usually just reversed the text, but it was easily crackable.... So, I came up with my own encryption algorithm that encrypts or decrypts using a passcode. It can be found at http://argent.50webs.org/encrypt.html. It needs a passcode with which it will encrypt and decrypt the code. It does not support limits, so you might get special characters in the encrypted code. Moreover, the code must be copied AS IS. ie. verbatim, without any changes, without touching and stuff... Each passcode generates a unique code. A single passcode will always generate the same code for the same message.

On a side note, blogger now supports input in Hindi. :P

-- अर्जुन अस्थाना

Sunday, April 01, 2007

String container class on Turbo C++

Update: This class has been heavily updated. This now even overloads the global ostream << iString and istream >> iString operators so that direct input/output through cout/cin (eg. cin >> iString and cout << iString) is possible. It has been heavily updated since it's first release. Upgradation is strongly recommended. See download page for more details.

Note: You are strongly advised to use the C++ string class shipped with your compiler if it has STL. If it does not, try using third partly STLs, like STL Port. If THAT doesn't work, then go in for this. Oh, and I've already tried everything for TurboC++, in case you're wondering. It's so old that nothing works on it. This has been made specifically for TurboC++.

The default string container class that ships with TurboC++ doesn't compile. Me being so used to the class, I just couldn't switch to using arrays of characters... So, well, I decided to create something similar myself. Of course, the trouble is, I can't modify cin and cout, so you'll have to use some workarounds when working with I/O. We'll come to that later, anyway.

For those who don't know what a string container class is, let's look at an example. Consider an array of characters. The array size is fixed. You'll have to create and array big enough to hold the largest value that can be inputted by the user. The trouble with this is that if the string is smaller, the rest of the space is wasted. Now, if you choose a smaller array, it can result in a buffer overflow. The solution is using dynamic memory allocation to create a class that does everything automatically, including memory allocation in such a way that the string "just fits". Consider the following piece of code:

char a[100]; strcpy(a,"Hello");

Around 94 bytes are wasted. A much simpler way is using the String container class. Consider the following code:

string a; a="hello";

Here, the memory occupied by a will be exactly 6 bytes. Now, let's say you have two strings and you want to concatenate them. Normally, you would

char a[100]; char b[50]; strcpy(a,"Some text you hope is less than (100-(size of b))"); strcpy(b,"Some more text. skjhgfkjhgsdkhldfk <---- some more text."); strcat(a,b);

If a is not big enough to hold a AND b, it will result in buffer overflow, resulting in what most programmers hope is an instant crash (otherwise, it can mess up the internals and crash a long time later). A much simpler code:

string a,b,c; a="Some text that can be of any size, limited to the max memory you have";
b="Some more text. skjhgfkjhgsdkhldfk <---- some more text.";
c=a+b; // c becomes the value of a followed by value of b
/* OR */ a+=b; // a becomes value of a followed by value of b

Note that here memory is automatically managed. You need not worry about how memory is allocated or buffer overflows or anything.

This class has been defined in ANSI C++, but is not properly implemented in TurboC++. That's why I created my own class. It can be downloaded from here. There's a huge difference between the standard string container class and this one, though:

  1. It is called iString instead of string. This is because I don't want to confuse string with my own implementation.

  2. Many of the standard C++ string functions haven't been declared. This is because I didn't have enough time. However, it can use the standard C string functions (like strcpy and stuff) provided it is passed to the functions as -stringName instead of stringName. Note the preceding hyphen '-'. We'll come back to it later.

  3. Since I can't modify cin and cout, you cant directly I/O these strings. However, there's a workaround. For outputting, use -stringName (note the hyphen '-'). For inputting, declare a temporary array of characters to store the string and then set the string as the array. For example:

    char *buffer; // declare buffer as a pointer buffer=new char[128]; // buffer is an array of 128 characters // The above two lines are almost equivalent to "char buffer[128];" iString myString; // myString is an object of the class iString cin.getline(buffer, 127); // get upto newline or 127 characters, whichever comes first myString=buffer; // set myString as the value of buffer, only upto what is inputted // ie. get upto first '\0' and store it in myString. rest of the buffer is not read delete [] buffer; // we dont need the buffer anymore, so free the memory cout << -myString; // note the hyphen '-'. that's the only difference here.

    Update: Created a function to directly input this string. The function requires getchar(), for which it automatically includes stdio.h if it has not been included. See download page for more details.

Now, the reason why you have to use these workarounds. The thing is, cin and cout are predefined classes. They input and output predefined datatypes, which includes integers, floating point numbers, characters, arrays of characters and strings (the container class). They do a different thing for int, a different thing for float, a different thing for char and so on. Now, since iString wasn't defined in cin and cout, you'll have to convert them to something more recognisable by them. The - (hyphen) before the object signifies the value of the string, as an array of characters, instead of an iString. While inputting, you write the data into a temporary array of characters, and then copy it from the array into the string.

Similarly, wherever you have to use the string as an array of characters, just put a hyphen '-' before it. Like strcmp(-string1, -string2) or printf("%s", -string). However, note that you wont be able to modify the string using the hyphen approach. The only way to modify the string is using = (equal to), += (append) and * (read input) operators and their respective methods. Oh, and you needn't use the hyphen when accessing a single element at a posititon. You can directly do string[position] instead of -string[position].

Saturday, March 31, 2007

TurboC++ on Linux

Ever tried running TurboC++ on Linux? Hate CBSE for still using such a sucky compiler even though it has gone out of use after 13 years? DosEMU giving nightmares and not working right? DosBOX has come to your rescue.

Seriously, after trying years to make TurboC++ work on DosEMU using various configurations, I finally gave up and tried to use DosBox, something that was originally meant for playing DOS games, but works like a charm for TurboC++.

Anyway, to get TurboC++ working on Linux:

  1. Get Linux: Like, DUH!
  2. Get TurboC++: Get it here
  3. Get DosBOX: Get it here. I've also compiled RPMs for DosBOX on FC5:
    1. SRPM
    2. RPM
    3. debuginfo RPM
  4. Install DosBOX See DosBox documentation on its site
  5. Integrate TurboC++ with DosBOX:
    1. Unzip the TurboC++ zipfile into ~/tc. It will create a directory called tc inside ~/tc. Rename this directory to something like INSTALL or something.
    2. Run DosBOX and the enter the following code:
      mount c ~/tc
      c:
      cd install
      install
      It will show an installation screen. In the first screen, ensure that the source media is C, not A. On the other screen, The Default Works Just Fine™.
    3. When the installation is done, issue the following commands:
      cd \tc\bin
      tc
      This will launch TurboC++. Go to Options->Directories. In Includes, put ";C:\TC\CLASSLIB\INCLUDE" ath the end (without the quotes). In Libraries, put ";C:\TC\CLASSLIB\LIB" at the end (without the quotes). This isn't required, but this allows you to use the predefined classes, like vectors, queues, stacks, lists, string etc., offered by C++. Exit TurboC++
  6. (Optional) Make DosBOX autorun TurboC++:
    Open ~/.dosboxrc in your favourite editor and put the following in it:
    @echo off
    echo Setting default sound mixers
    SET BLASTER=A220 I7 D1 H5 T6
    SET ULTRASND=240,3,3,5,5
    SET ULTRADIR=C:\ULTRASND
    echo Mounting TurboC++ directory as C:
    mount c ~/dosbox
    echo Setting required environment variables
    set FILES=20
    c:
    set PATH=c:/tc/bin;c:/bin;z:/
    echo Running TurboC++
    tc
    @echo Exiting...
    exit

Now, everytime you run DosBOX, it will automatically open up TurboC++. You can create a shortcut or something on the desktop for running DosBOX. Though, as the great thinkers of the 21st century have said, Your Mileage May Vary™.

Saturday, March 03, 2007

Lunar Eclipse tonight

Tonight is the Lunar Eclipse. Here's a time table picked up directly from the US Navy Lunar Eclipse Computer (time has been offset to +0530GMT, ie. Indian Standard Time)):

                  Total Eclipse of the Moon

                            NEW DELHI
                            o  '    o  '
                         E077 12, N28 42

                 Zone:  5.50h East of Greenwich

                                                      Moon's
                                                Azimuth   Altitude
                                     h  m            o        o
Moonrise               2007 Mar 03  17:55          79.2     ----
Moon enters penumbra   2007 Mar 04  01:46.4       224.8     61.7
Moon enters umbra      2007 Mar 04  03:00.0       246.1     48.3
Moon enters totality   2007 Mar 04  04:13.8       258.7     33.0
Middle of eclipse      2007 Mar 04  04:50.9       263.6     25.0
Moon leaves totality   2007 Mar 04  05:28.0       268.0     17.0
Moon leaves umbra      2007 Mar 04  06:41.7       276.3      1.5
Moonset                2007 Mar 04  06:51         277.3     ----

Wednesday, February 28, 2007

unpacker

Made an unpacker following a discussion on irc://irc.freenode.net/#fedora (or was it #kde?) that there's no good automatic unpacking tool for the CLI. Sure, you have ark etc. for the GUI, but you dont have an all-in-one utility for the CLI. So, here's the code for the thing. Just put it in your .bashrc and run it as

$ unpack file1.tbz2 file2.tZ file3.zip ...
unpack() {
  if shopt nocasematch | grep off >/dev/null 2>&1; then prevcasematch=0; fi
  shopt -s nocasematch
  while [ $# -gt 0 ]; do
    if ! [ -f $1 ]; then echo "File \"$i\" does not exist"; shift; continue; fi
    case $1 in
      *.tar.gz | *.tgz )            echo "Extracting $1 (gzipped tar archive):"; tar -xzvf $1 ;;
      *.tar.bz2 | *.tbz2 )          echo "Extracting $1 (bzipped tar archive):"; tar -xjvf $1 ;;
      *.tar.z | *.tz )              echo "Extracting $1 (compressed tar archive):"; tar -xZvf $1 ;;
      *.zip ) a=${1%%.[zZ][iI][pP]} echo "Extracting $1 (zip archive):"; mkdir $a; cd $a; unzip ../$1; cd - ;;
      *.rar ) a=${1%%.[rR][aA][rR]} echo "Extracting $1 (rar archive):"; mkdir $a; cd $a; unrar x ../$1; cd - ;;
    esac
    shift
  done
  if [ x$prevcasematch = x0 ]; then shopt -u nocasematch; fi
  }

Update: Just checked, it was neither #kde nor #fedora, it was #bash >.>

Saturday, February 17, 2007

ampsig submission script for amaroK

These days you will find dynamic signatures in almost every forum. There are many sites which allow you to generate dynamic sigs. One of them is ampsig. The difference between ampsig and others is that it can display live statistics about the song you're listening to. For example, here's a sample of mine (there are many different themes available, too. one of them has been randomly selected for you):

I've made a script which submits data to the ampsig server. It's avaialble for XMMS2, Noatun and amaroK. Noatun and XMMS2 scripts are a bit too buggy because I was half asleep while working on them (I personally wouldn't have used them had I not made them), but amaroK script is much better. Supports colour, verbose mode, GUI among other stuff. Read more about it here. Get it here