Tuesday, July 28, 2009

Java - The Renamer!

When a disaster striked my computer, I lost all my music files. :( If its any other portable music player, I would've just drag the songs from it to my computer. I don't think Apple made that easier for IPOD TOUCH users. So, I downloaded SharePod to copy the songs to my computer. But songs were renamed to the following format.
TrackNo - Title.mp3
But my track number was 00, for all of the songs. So I had all the songs with the following format.
00 - Beat It.mp3
I didn't like that format, I just wanted the song name. I knew it would take atleast a day for me to rename all the songs manually. Only thing came to my mind was, SHELL SCRIPT. I am not very expert in shellscripting but with Window's Vista, all I could use is DOS. Did few google search and wasn't successfull with the google search, I decided to do my own multiple files renamer. Since, Java provides most of the functionality I needed to do this program, I went with Java.
Here is the code below. Just run the program like this (don't change any code, uncomment anything) and see if the output of your renamed files are correct. If its correct then you could uncomment the bolded 2 lines of code which will actually rename the original files.

import java.awt.FlowLayout;
import java.io.File;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
public class FilesRenamer
{
    public static void main(String[] args) throws Exception
    {
        JTextArea text = new JTextArea(15, 30);
        text.setText("Original Files:\nRenamed Files:\n\n");

        File selected[];

        JScrollPane scroll = new JScrollPane(text);

        JFileChooser chooser = new JFileChooser("C:\\Users\\Owner\\Music");
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        chooser.setMultiSelectionEnabled(true);

        JFrame f = new JFrame("Multiple Files Renamer");
        f.setLayout(new FlowLayout());
        f.add(scroll);
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        f.setSize(500, 600);
        f.setVisible(true);

        int returnVal = chooser.showOpenDialog(f);
        if (returnVal == JFileChooser.APPROVE_OPTION)
        {
            selected = chooser.getSelectedFiles();
            if (selected.length == 1 && !selected[0].exists())
                JOptionPane.showMessageDialog(null,
                  "File does not exists!", "Alert!",
                  JOptionPane.ERROR_MESSAGE); 
            else
            {
                for (int i=0; i<selected.length; i++)
                {
                    text.append(selected[i].toString() + "\n");
                    String temp = newname(selected[i]);
                    if (temp != null)
                    {
                        text.append(temp + "\n\n");
                    }
                }
            }
        }
        chooser.setSelectedFile(null);
    }
    public static String newname(File fullpath)
    {     
        String newfilename = fullpath.getName();
        newfilename = newfilename.substring(5);
        File filedir = new File (fullpath.getParentFile() + "\\" 
            + newfilename);
        //if (full.renameTo(filedir))
        return filedir.toString();
        //return null;
    }
}
Let's say if you want to play with extension, such as to make jpg to jpeg or change from mp3 to wma then add the following function into the code.

public static String extension(String filename)
{
    String justFileName = filename.substring(0, filename.lastIndexOf('.'));
    String ext = filename.substring(filename.lastIndexOf('.'));
    ext = ".mp3";
    String newfilename = justFileName+""+ext;
    return(newfilename);     
}
and change the newname function to the following.
public static String newname(File fullpath)
{     
    String newfilename = fullpath.getName();
    //newfilename = newfilename.substring(5);
    newfilename = extension(newfilename);
    File filedir = new File (fullpath.getParentFile() + "\\" + newfilename);
    //if (full.renameTo(filedir))
    return filedir.toString();
    //return null;
}

Wednesday, April 15, 2009

C - Client/Server on a localhost.

This is post is about a client/server assignment I did for the Intro to OS course. The client and server has to be running on a localhost and server should be executed before client. Otherwise, it doesn't make sense. You could try running the client first, but no meaningful output will be seen. So, run the server first. (Ofcourse, I provided the source code so you need to compile first.)


This the basic idea of the program:

The server starts and creates a well-known pipe and waits for a client. Whenever a client connects to the server, server waits for client to write sumthing to the well-known pipe. Client will write it's pid, name (a person's name to be looked up in a database) and a char to identify address or phonenumber of the person that client is requesting (a= address or p=phonenumber). Server, then, read this info from well-known pipe and then break down the string into appropriate words(I could've used a string tokenizer, built-in function, but I realized that later. I think, I created a my own string tokenizer.) Using the child's pid, the server creates a private pipe for communicating with the client. Then, the server try to execute the command, on unix system, it formed from the passed-in info from client. If it's successful, then server writes appropriate messages to the client using the private pipe. Then client does some clean up (deletes the private pipe, closes the pipes) and then exits.


NOTE: Server could only handle one client at a time. To handle more than one client, server should have a queue in which client is added. Then, client is popped one at a time and server would then do a fork function call. That newly created child (of the server) would deal with the request from the client. Server would create another child (fork() again) and deal with the second client and so on.)

Here is a sample output (I have blurred some of the things that reveals any information about school's server.)

Here is how I ran it:
Running server:
1) Started a SSH window for the server.
2) I executed the 'ps' command to show that no other process are running
(other than bash and ps itself)
3) I started the server.
4) Server now waits for a connection from client...

Running Client:
1) Opened another SSH window for the client.
2) I executed the 'ps' command to show that no other process are running
(other than bash and ps itself)
3) I executed the 'ls' command to list the files in that current directory.
4) I executed the 'cat' cmd with a2.db as the input.
(a2.db is the database file. Data for each record is seperated with a delimiter ':')
5) Client was started.
6) A name was entered; in this case, Mark, since that name is in the database.
7) 'a' was entered to see Mark's address from the database.
8) Client output the results.
(Client exited - automatically.)
9) Client was started again.
10) Will Smith was entered as the name.
11) 'p' was entered to see Will Smith's phonenumber.
12) Client out put the results and exited.
13) I ran the 'dir' command to show that all the pipes have been removed.
(if 'dir' don't work use 'ls')
14) Server was killed by pressing "CTRL + C"



If the above paragraph is confusing, then try reading the code, which is lot easier to understand.
Code:
http://cid-3e83bfd71e4816b4.skydrive.live.com/browse.aspx/Public/cProgrammingServerClient

Saturday, April 4, 2009

Java - Pie Chart Editor/Creator





This is one of the assignment I did for JAVA-2 Course.














Here is the files : http://cid-3e83bfd71e4816b4.skydrive.live.com/browse.aspx/Public/JAVAPieChart
I think just reading the comments will help a lot. I don't think I need to explain the codes in there.