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.

It helped me (even though it took 20 mins to write).


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 = ".jpeg";
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 22, 2009

Copy 64 bytes to a new location - 6502 Assembly

Since 6502 allows different ways to index, copying 64 bytes (any number of bytes but less than 256)

      .ORG $1000
      LDX #$0
LOOP LDA $1280,X ;Starting address (Copy from)
      STA $2A00,X ;New Location (Copy to)
      INX
      CPX #$40 ;Compare ind X with memory ($20 -> 32 in DEC)
      BNE LOOP
      BRK

      .ORG $1280
      .DB "Trying to get more than 32 bytes so I could"
      .DB " prove that program only copy 32 bytes."

Yes! It's that simple. If you want to run the code, try copying and pasting it. If that doesn't work then try typing it up by looking at this code because I used HTML to color this code so, it might poses some trouble when assembling it.

6502 Assembly

For one of the lab for Computer Oganization II course, we had to do 6502 assembly programming.



Here is the summary of the lab:

Write a 6502, assembly code, program which will do the following:

When the user press

- '0' on the keyboard, it output a pre-defined firstname (or a string.)

- '1' outputs the lastname

- 'a' for addition

- 'b' for subtraction



For addition and subtraction, user enter a hex-digit and that has to be converted to a byte. Addition or subtraction is performed on the byte, then result is outputted as a hexdigit.



For e.g.

If user enter 02 as arg1 and 08 as arg2, arg1 will be stored in the memory as the ascii-hex value. In our case, 02 is stored as 0x30 and 0x32 (check an ascii table), respectively. Similarly, 08 would be stored as 0x30 and 0x38, respectively. Now, from 0x30 and 0x32, we need to convert this to the byte 0x02. Same process apply for arg2. Now, we have 0x02 as arg1 and 0x38 as arg2. We perform the add/subtract operation. In our case, result is 10. We conver the result to 0x0a (=10). Then we output the result.





My code is here:

http://cid-3e83bfd71e4816b4.skydrive.live.com/browse.aspx/Public/6502hexAddition



The PRINT subroutine is not mine, professor provided that.



NOTE: I used a 6502 simulator (http://home.pacbell.net/michal_k/). A Very good program!

To run the code:

1) Make sure you installed the 6502 simulator (from the link I provided above.)

2) Open the 6502 simulator and create a new window.

3) Copy and paste the code into a new window.

4) Press F7 to assemble.

5) Press F6 to go into debugger mode.

6) Press F5 to run.

7) If you don't already see a In/Out window, press Alt + 6.



Here is a sample output:

(Since the In/Out window was not resizable, I had to do some prtscrn and paint works. But, I didn't modify the output using paint program!)

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.