Sunday, 2 June 2013

Came Across The R Project, Installing RTextTools

While I was searching Text Classification Machine Learning library for C++ I came across The R project. It is basically a library for statistics related programming which Machine Learning is all about.

So I found a library called RTextTools which uses R Project. I haven't yet used RTextTools so cannot comment about that. So I had to install RTextTools.

To install RTextTools, I will first have to install The R. Here I found all the instruction on how to install The R Project on Ubuntu. So the very first step given is to add the repository in your sources.list file
You will have to add the following line to your sources.list file which is present in /etc/apt/sources.list

deb http://<my.favorite.cran.mirror>/bin/linux/ubuntu quantal/

Now replace <my.favorite.cran.mirror> according to your location. I live in India so I choose this.

http://ftp.iitm.ac.in/cran/ 

Take care of the duplicate http://. Now before updating your apt packages cache you will have to add the key of the repository which can be added using the following command which is also mentioned in the file I have mentioned above. 

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E084DAB9

Now you can issue the following command

$ sudo apt-get update

and then 

$sudo apt-get install r-base

After installing the above package, you can install the rtexttools by issuing the following commands

$ sudo R

The above command will take you to R console 

Now here you can issue the following command to install it.

install.packages("RTextTools")

Now this will install RTextTools. I will soon be posting on how to use RTextTools and its various function which Currently I also don't know. :)

Saturday, 1 June 2013

Save dialog coming up while executing main.cgi

Every time I re-install my operating system I have to setup every damn thing again. Anyways! I am writing here the problem that I faced while setting up my cgi-bin folder. After installing apache server, CGI will work fine in /usr/lib/cgi-bin folder. But I was setting up the cgi-bin folder in my home directory as I had to install a software named BAKAPLAN made by one of my friends.

If you check the BAKAPLAN github page that I have mentioned above you will find the basic instruction on how to setup cgi-bin folder in public_html folder. I followed all the instruction written in there. Still when I tried to execute my own CGIPhotoAlbum, instead of executing it, It showed me dialog box to save the file. The executable file that I was trying to run had name "main.cgi". I don't know the reason behind this. May be because I was using an extension .cgi. Maybe I had not given the appropriate permissions to the files in the cgi-bin folder.

So the first thing I did was to give permission the folder. I issued the following command.

chmod -R 755 public_html/

Execute this command when you are in your home directory. "$ cd ~"

So next thing that I thought of was to tell the apache server that .cgi is also a cgi script. This reason kindof not making any sense but just for sake my some part of brain add the following line in /etc/apache2/sites-available/default or edit if it is already there

<Directory "/home/*/public_html/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                SetHandler cgi-script
                AddHandler cgi-script .cgi
                Order allow,deny
                Allow from all
        </Directory>

Notice the AddHandler line. That is what I added. This solved my problem. Hope it does yours too. :)

Tuesday, 29 January 2013

CGI C++ Photo Uploader

This project was given to me as an entrance test for GD. So I started off preparing for it from the very time when it was given to me. I faced many problems and I got to learn lots of things.

The very first problem was I was not familiar with CGI and I had never used CGI with C++.
So the first thing was to learn about CGI So I searched for CGI and collected some information about it. After this was to the turn to configure apache to support CGI. So I searched How to do it and started doing it Everything was already configured and CGI was working already.

Now I could start working with the project. CGI files are placed in /usr/lib/cgi-bin/ But you can change this directory and use your own directory by changing apache configuration in /etc/apache2/site-available//default file. Now I can start working on the project.

The very first problem that how to parse the POST, GET data and how to handle File Uploads with CGI. So After researching a little I found a library RUDECGI which allowed me to do that. It is a very simple library with lots of usefull function.

Installation procedure is pretty simple
./configure
make
sudo make install

How to use RUDECGI

Include file: rude/cgi.h

and use -lrudecgi while compiling to link the libraries. like this

g++ -o main main.cpp -lrudecgi

Initializing CGI object syntax: CGI cgi
There are many methods of the object but I have used only the following method in the program
cgi.value("")  This function to get value of get or post data.
cgi.isFile() Check if file was uploaded then
cgi.filename to get the name of the uploaded file

Now was the turn to learn about database. I was only familiar with MySQL So I thought of using some other DBMS this time. I had heard about SQLite and that it is very small and takes very small space and is even used by many mobile operating systems. So I thought about using it for my project

Sqlite header files can be installed by by executing the following commands in terminal

sudo apt-get install libsqlite3-dev sqlite3

to create a database you can use the following command

sqlite3 <database-name>

and then start entering your sql comands

or you can use sqlitebrowser

sudo apt-get install sqlitebrowser

After creating database, Now was the time to create connection to database through c++

sqlite3_open to open connection to the database
sqlite3_exec to execute sql commands which do not return anything such as INSERT, DELETE

To execute sql commands which return something we will use series of commands
sqlite3_prepare_v2() to prepare the sql statement if this return SQLITE_OK then you can proceed
sqlite3_step() to process all the rows

This was all about the database, the next problem that I faced took a lot of my time. Everything that you will put in the cgi-bin folder apache will consider it a cgi script and execute it. So you cannot store any of your javascript files,css files, and in my case photos. So I searched alot and people suggested to keep these files in document root folder for eg in /var/www as it is outside of cgi-bin folder so cgi will not execute it.

The other error I got was of C++ such as illegal conversion from const char* to char* etc which can be solved by using sstream.

All went well in the end and My task was complete and I got to learn lots of things in this week. :)) Happy Coding..