Computer Science Canada

Unicode Encoding and Byte Removal

Author:  skootles [ Mon Apr 17, 2006 8:28 pm ]
Post subject:  Unicode Encoding and Byte Removal

Well, this is definately something I won't be able to figure out on myself.

So I made a program. It converts Windows Media Player 10 Playlist files ( *.wpl files) into a playlist file that will play on my MP3 Player (A samsung YH-925, and the playlist file is *.plp).

Here's an example of a .wpl playlist file:
code:
<?wpl version="1.0"?>
<smil>
    <head>
        <meta name="Generator" content="Microsoft Windows Media Player -- 10.0.0.4019"/>
        <author/>
        <title>Easy Listening</title>
    </head>
    <body>
        <seq>
            <media src="D:\Limewire\Music\Stained - So Far Away.mp3" tid="{63BCF8C9-6593-4352-8D14-6B169136D475}"/>
            <media src="D:\My Documents\My Music\Simple Plan - Still Not Gettin&apos; Any\06 - Crazy.mp3" tid="{1920B4CA-6927-4719-9CB7-BB713CEC0910}"/>
            <media src="D:\Limewire\Music\Asher Lane - New days.mp3" tid="{33EA83D7-8022-46DF-AD15-956DA3490FEB}"/>
            <media src="D:\Limewire\Music\Disney-Phil Collins - Tarzan - You&apos;ll Be In My Heart.mp3" tid="{F368BBBA-F6F4-4A38-B933-C15ECB1DE5CF}"/>
        </seq>
    </body>
</smil>

(The indentation won't be shown because of the forum, but whatever)

and here's an example of this file converted into a .plp playlist file:
code:
PLP PLAYLIST
VERSION 1.20

HDD, System\MUSIC\Stained - So Far Away.mp3
HDD, System\MUSIC\06 - Crazy.mp3
HDD, System\MUSIC\Asher Lane - New days.mp3
HDD, System\MUSIC\Disney-Phil Collins - Tarzan - You'll Be In My Heart.mp3


So what the program has to do is seek out the file names, then put a "HDD, System\MUSIC\" infront of them. It also has to change all occurences of "&apos;" into apostrophes, as they should be.

So, after this conversion, it won't play in my mp3 player. To make it work properly, I have to open it into notepad, and then re-save it, but change the encoding from ANSI to unicode. And then, I have to open the file in a hex editor, and remove the first two bytes, which have the hex values FF and FE, respectively. Then it will work on my mp3 player.

So I'm wondering, is there a way in turing for me to be able to encode the file using unicode, and then remove the first two bytes?

Thanks Smile

Oh, and here is a copy of my program. I know it may be a little inefficient in spots, but it's been half a year since I've had comp. sci., so it's all going off of memory.

code:
var fn, fn2 : int
var textfile, character, playlist, yplay : string := ""

var location : int

View.Set ("graphics:800;600,position:middle;center")

put "Please enter the name of the Windows Media Player playlist"
put "(including the extension).", skip
put "NOTE: It must be in the same directory as this executable. ", skip
get playlist :*

put skip, "Please enter the name of the YH-925 Playlist (without the extension)", skip
get yplay :*

yplay += ".plp"

open : fn, playlist, get, seek
open : fn2, yplay, put, seek

put skip, "-------------   A copy of the output file:   -------------", skip

put "PLP PLAYLIST"              % Prints a copy of the playlist header to the screen
put "VERSION 1.20"
put ""

put : fn2, "PLP PLAYLIST"       % Prints a copy of the playlist header to the playlist file
put : fn2, "VERSION 1.20"
put : fn2, ""




loop
    location := 0
    exit when eof (fn)
    if character = chr (92) then
        tell : fn, location
       
        loop                                    % -----------   START "\" removal loop   -----------
            get : fn, character : 1
            if character = chr (92) then        % Description: The file is searched for any occurences of the "\" character. When it
                tell : fn, location             % is detected, a reference point is made, and the search continues for others. When there
            end if                              % are no more on the line, the reference point is seeked, and then moves on to the next loop.
            exit when character = chr (34)
        end loop                                % -----------   END "\" removal loop   -----------
       
        seek : fn, location

        loop                                                    % -----------   START &apos; removal and replacement loop   -----------
            get : fn, character : 1
            if character = "&" then
                tell : fn, location
                get : fn, character : 1
                if character = "a" then
                    get : fn, character : 1
                    if character = "p" then
                        get : fn, character : 1
                        if character = "o" then
                            get : fn, character : 1
                            if character = "s" then
                                get : fn, character : 1
                                if character = ";" then
                                    get : fn, character : 1
                                    textfile += "'"
                                else
                                    seek : fn, location - 1
                                end if
                            else
                                seek : fn, location - 1
                            end if
                        else
                            seek : fn, location - 1
                        end if
                    else
                        seek : fn, location - 1
                    end if
                else
                    seek : fn, location - 1
                end if
            end if
            exit when character = chr (34)                         % Exit when a quotation mark is detected
            textfile += character
        end loop                                                 % -----------   END &apos; removal and replacement loop   -----------

        put "HDD, System", chr (92), "MUSIC", chr (92) ..
        put : fn2, "HDD, System", chr (92), "MUSIC", chr (92) ..
        put textfile ..
        put : fn2, textfile ..
        put ""
        put : fn2, ""
        textfile := ""
    end if
    get : fn, character : 1
end loop


close : fn
close : fn2

Author:  Delos [ Tue Apr 18, 2006 1:35 pm ]
Post subject: 

Use read and write instead of put and get. That's the first big step to take, since it will then write the file in binary format (i.e., free of ASCII).
This should work...if not, well try this first and then post up again.

Author:  skootles [ Tue Apr 18, 2006 2:38 pm ]
Post subject: 

When I replaced put with write, for example:
code:
out : fn2, "PLP PLAYLIST"       
put : fn2, "VERSION 1.20"
put : fn2, ""

to
code:
write : fn2, "PLP PLAYLIST"       
write : fn2, "VERSION 1.20"
write : fn2, ""

I get multiple "Argument to 'read' or 'write' statement is not a variable" errors. So can I only use variables for write?

Author:  Delos [ Tue Apr 18, 2006 4:52 pm ]
Post subject: 

Apparently. That's not too much of a problem is it? Unless you're regularly dealing with strings longer than 256 chars...

Author:  skootles [ Tue Apr 18, 2006 7:01 pm ]
Post subject: 

New code:
code:
var fn, fn2 : int
var textfile, character, playlist, yplay : string := ""
var location : int

View.Set ("graphics:800;600,position:middle;center")



put "Please enter the name of the Windows Media Player playlist"
put "(including the extension).", skip
put "NOTE: It must be in the same directory as this executable. ", skip
get playlist :*


put skip, "Please enter the name of the YH-925 Playlist", skip
get yplay :*


yplay += ".plp"                         

open : fn, playlist, read, seek         % The Windows Media Player playlist file
open : fn2, yplay, write, seek          % The MP3 Player playlist file

put skip, "-------------   A copy of the outwrite file:   -------------", skip

var v1 : string := "PLP PLAYLIST"             
var v2 : string := "VERSION 1.20"
var v3 : string := ""

put v1
put v2
put v3

write : fn2, v1       % Prints a copy of the playlist header to the playlist file
write : fn2, v2
write : fn2, v3




loop
    location := 0
    exit when eof (fn)
    if character = chr (92) then
        tell : fn, location
       
        loop                                    % -----------   START "\" removal loop   -----------
            read : fn, character : 1
            if character = chr (92) then        % Description: The file is searched for any occurences of the "\" character. When it
                tell : fn, location             % is detected, a reference point is made, and the search continues for others. When there
            end if                              % are no more on the line, the reference point is seeked, and then moves on to the next loop.
            exit when character = chr (34)
        end loop                                % -----------   END "\" removal loop   -----------
       
        seek : fn, location

        loop                                                    % -----------   START &apos; removal and replacement loop   -----------
            read : fn, character : 1
            if character = "&" then
                tell : fn, location
                read : fn, character : 1
                if character = "a" then
                    read : fn, character : 1
                    if character = "p" then
                        read : fn, character : 1
                        if character = "o" then
                            read : fn, character : 1
                            if character = "s" then
                                read : fn, character : 1
                                if character = ";" then
                                    read : fn, character : 1
                                    textfile += "'"
                                else
                                    seek : fn, location - 1
                                end if
                            else
                                seek : fn, location - 1
                            end if
                        else
                            seek : fn, location - 1
                        end if
                    else
                        seek : fn, location - 1
                    end if
                else
                    seek : fn, location - 1
                end if
            end if
            exit when character = chr (34)                         % Exit when a quotation mark is detected. (Quotes signal the end of the filename)
            textfile += character
        end loop                                                 % -----------   END &apos; removal and replacement loop   -----------

       
        var test : string := "HDD, System"
        test += chr(92)
        test += "MUSIC"
        test += chr(92)
        test += textfile
       
        put test
        write : fn2, test
        %put textfile
        %write : fn2, textfile
        write : fn2, v3
        textfile := ""
        test := ""
    end if
    read : fn, character : 1
end loop


close : fn
close : fn2


and everything comes out horribly spaced:

Posted Image, might have been reduced in size. Click Image to view fullscreen.

Plus I don't know what those weird 3 characters are Crying or Very sad

Author:  Delos [ Tue Apr 18, 2006 11:13 pm ]
Post subject: 

Using write outputs it in binary format - this is not something that Notepad can recognize very well. However, your file headers should be less of a pain to work with.
Have you tested the new playlists with your MP3 player? If they still don't work, we can work on some actual Hex editting stuff. It's really quite a bit of fun, playing with obscure values that make no sense to the average eye. Of course, we'll have made code efficient enough that we'll never have to deal with said values...

Author:  skootles [ Wed Apr 19, 2006 2:14 pm ]
Post subject: 

No, the playlist files still don't work. So, I guess let's get into Hex Editing Razz

I showed the problem to my old comp sci teacher, and he said he'd take a look at it, so I may get some help from him.

Author:  Delos [ Wed Apr 19, 2006 3:39 pm ]
Post subject: 

Right right, no problem. Just post a copy of the same playlist in both formats, and we'll move on from there. Also, what Hex editor are you using?

Author:  skootles [ Wed Apr 19, 2006 4:26 pm ]
Post subject: 

Alrighty. Here's the windows media player file (*.wpl):

code:
<?wpl version="1.0"?>
<smil>
    <head>
        <meta name="Generator" content="Microsoft Windows Media Player -- 10.0.0.4019"/>
        <author/>
        <title>Easy Listening</title>
    </head>
    <body>
        <seq>
            <media src="D:\Limewire\Music\Stained - So Far Away.mp3" tid="{63BCF8C9-6593-4352-8D14-6B169136D475}"/>
            <media src="D:\My Documents\My Music\Simple Plan - Still Not Gettin&apos; Any\06 - Crazy.mp3" tid="{1920B4CA-6927-4719-9CB7-BB713CEC0910}"/>
            <media src="D:\Limewire\Music\Asher Lane - New days.mp3" tid="{33EA83D7-8022-46DF-AD15-956DA3490FEB}"/>
            <media src="D:\Limewire\Music\Disney-Phil Collins - Tarzan - You&apos;ll Be In My Heart.mp3" tid="{F368BBBA-F6F4-4A38-B933-C15ECB1DE5CF}"/>
        </seq>
    </body>
</smil>


and Here's the MP3 Player playlist file (*.plp):

code:
PLP PLAYLIST
VERSION 1.20

HDD, System\MUSIC\Stained - So Far Away.mp3
HDD, System\MUSIC\06 - Crazy.mp3
HDD, System\MUSIC\Asher Lane - New days.mp3
HDD, System\MUSIC\Disney-Phil Collins - Tarzan - You'll Be In My Heart.mp3


Now if you copy the .plp file from above into notepad, you still have to save it with unicode encoding, then open it up in a hex editor (I'm using one that I found on my computer, translhextion) and remove the first two bytes.

Author:  Delos [ Wed Apr 19, 2006 8:10 pm ]
Post subject: 

Delos wrote:
Right right, no problem. Just post a copy of the same playlist in both formats, and we'll move on from there. Also, what Hex editor are you using?


Mayhaps I ought have spake that a little differently. No damage:
Please attach a copy of the same playlist in both formats...

I'm not sure if you have the capability to save in .plp - considering your dilemma. But if you can, awsome. If not, then just post one of your editted files.

Author:  skootles [ Wed Apr 19, 2006 9:17 pm ]
Post subject: 

Haha, okay.

So I've included 4 files in the zip. 1 is the original Windows Media Player Playlist file (Easy Listening.wpl), and then there are 3 MP3 Player Playlist files:

The first, which has "NOT WORKING" is the raw conversion. It's the output from the program, so it doesn't work in the MP3 Player.

The second which has "ALMOST WORKING" is the first one, except I've opened it up in notepad, and re-saved it with Unicode encoding.

The third which has "WORKING FULLY" is the second one, but I've opened it in Translhextion and removed the first two bytes. Now it works perfectly in the MP3 Player.

Now just note that when you open up the second one in notepad, you can't see the first two bytes that need to be removed. My teacher got confused with this, so just clarifing Smile

Author:  Delos [ Thu Apr 20, 2006 12:40 pm ]
Post subject: 

Right, this is better now. I'll probably be able to work on this a little tonight - not right now since studying is taking complete precedence...well, mostly at least Very Happy.

For the time being, make sure you know how to convert between decimal and hexadecimal (and vice versa); and simple ASCII manipulations between decimal and their character equivalents.

For instance, if I were to throw the number 2E at you, I'd expect you to be able to tell me what the character equivalent was (it's ".", FYI).
Work on that for the time being, and we'll get to the rest soon enough. (Don't worry if some of your conversion output as blank spaces - a lot of characters simply have no renderable equivalent.

Author:  Delos [ Fri Apr 21, 2006 7:48 am ]
Post subject: 

I'm going to guess that by this time you have figured out what I set out above. On to the next step.
Now that conversions between decimal and hex are no problem, you're going to have to do a bit of logical analysis of the hex dumps. The usefulness here is that you'll be able to create a specific type of file that goes beyond the mere suffix that is attached to it.
For instance, Turing is sneaky when it comes to saving pictures. They claim that it can save in .jpg; when in fact the resultant pictures have none of the compression of JPEGs and are indeed bitmaps with different suffixes. The hex headers remain the same.
That being said, your task to to do the following:
- determine the header for .plp files, according to what you have. Post up the hex dump of it. Don't worry about the text version, that is mostly meaningless.
- determine the format that will allow the MP3 player to recognise the following file:
"Hello - World.mp3"
Again, post up the hex for it. You should find that the entire path for this inclues a little more than the name of the song...something about a system folder...

I'd be a little more instructive - just I don't have the time right now! I'm sure you can handle this much though. If you get stuck, say so and I'll give you a few more hints.

Author:  skootles [ Fri Apr 21, 2006 3:02 pm ]
Post subject: 

I understand, exams are inportant. Razz

But I'm not quite sure what the header is, or where I'd find it. Methinks at the top, but there's nothing really there. To save some time, I just got a screenie of the hex editor when I've got the file open. Note the first two bytes - these are the ones that need to be removed before the playlist will play in the player.

Posted Image, might have been reduced in size. Click Image to view fullscreen.

And here is the same file when opened in notepad:

code:
PLP PLAYLIST
VERSION 1.20

HDD, System\MUSIC\Hello - World.mp3


The "System\MUSIC" part is obviously the path to the file, where all music that is synced to the device is put.

Author:  Delos [ Fri Apr 21, 2006 6:55 pm ]
Post subject: 

You're off to a good start. Essentially, a header is a little bit of hex at the top of the file that gives some details about its contents. These can be quite complex (hell, even bitmap headers are pretty long) to barely nonexistent (the .wpl don't really have headers).
When you're creating your playlists, you'll need to have a default header to be added to the beginning of each file. You'll then fill the body with the data for each song, and finally...well...where there's a header...

You haven't quite completed the assigned tasks just yet, but keep plugging away at them. Once you're past those - it's really quite a simple transition to create the playlist itself.

Author:  skootles [ Fri Apr 21, 2006 11:52 pm ]
Post subject: 

Okay, I think I've got it..
Header:
code:
00000000  50 00 4c 00 50 00 20 00 50 00 4c 00 41 00 59 00 4c 00 49 00  P.L.P. .P.L.A.Y.L.I.
00000014  53 00 54 00 0d 00 0a 00 56 00 45 00 52 00 53 00 49 00 4f 00  S.T.....V.E.R.S.I.O.
00000028  4e 00 20 00 31 00 2e 00 32 00 30 00 0d 00 0a 00 0d 00 0a 00  N. .1...2.0.........


and format:

code:
0000003c  48 00 44 00 44 00 2c 00 20 00 53 00 79 00 73 00 74 00 65 00  H.D.D.,. .S.y.s.t.e.
00000050  6d 00 5c 00 4d 00 55 00 53 00 49 00 43 00 5c 00 48 00 65 00  m.\.M.U.S.I.C.\.H.e.
00000064  6c 00 6c 00 6f 00 20 00 2d 00 20 00 57 00 6f 00 72 00 6c 00  l.l.o. .-. .W.o.r.l.
00000078  64 00 2e 00 6d 00 70 00 33 00 0d 00 0a 00                    d...m.p.3.....     


and I've attatched them as text files too Smile

Author:  Delos [ Sat Apr 22, 2006 11:21 pm ]
Post subject: 

Good work. You should be all set to create your .plp files now. To summarize:

- the file will start with the header. This will be default to every file.
- file names being with the "HDD" path and end with the "0A 00" delimiter.
- to write the file in Binary format, use write instead of put.

Be careful when you're writing your files - you may have to play around a bit with it to get just the right catenation of your strings. I've often encountered problems where I've written a linebreak where there oughtn't be one - resulting in extra characters, resulting in non-working files.
You may have a little bit of a challenge creating the file names, but now that you know the format it shouldn't be too bad. Notice the characters ("00") inserted between each letter - make sure you follow that as well.

See what you can do. I can forsee only one possible problem to do with the way Turing writes files...but that souldn't be too much of a problem. Post up your results, working or otherwise, and if need be we'll delve deeper into the intricacies of writing binary files.

Author:  skootles [ Sun Apr 23, 2006 9:47 pm ]
Post subject: 

Okay, so I've changed the gets to reads, puts to writes, and discovered a few inefficiencies in my code Wink But the output playlist file didn't work, and I've attatched the hexdump of it.

I wasn't sure how I could ge the spacing right (with the "00" 's between each character, and I've no clue how there got to be so many of them). There's a copy of my code also attatched.

Author:  Delos [ Tue Apr 25, 2006 12:31 pm ]
Post subject: 

Quick note: will look at code soon...final stretch is almost over!

Author:  skootles [ Tue Apr 25, 2006 2:41 pm ]
Post subject: 

Haha, okay. Good luck!

Author:  Delos [ Fri Apr 28, 2006 11:25 am ]
Post subject: 

This morning I decided to try this without using hex...just because it was turning out to be quite the complicated deal using hex. This is the result. Load it up into your player and tell me if it works.
If so, great, I'll work you through recreating it. If not...well, it's back to hex.
There's only one song on the playlist.

Author:  skootles [ Sat Apr 29, 2006 12:31 pm ]
Post subject: 

Sorry, but it didn't work. It just gave me the standard error, so I guess it's time for Hex.

Author:  Delos [ Sun Apr 30, 2006 12:10 am ]
Post subject: 

Well...wracked my brain a little over this, but try it out!
This one is based off a slightly modified "Easy Listening.wpl", saved as "Easy Listening 2.wpl" and included in the zip. I had to modify the directories a little, and change the name of one of the songs to match the "Fully Working" plp that you'd provided.

Tell me if it works and we'll go from there.

Author:  skootles [ Sun Apr 30, 2006 10:05 pm ]
Post subject: 

Well, my hat goes off to you. It works!
Now it's time to share your secret >=D
Haha Very Happy

Author:  Delos [ Tue May 02, 2006 11:06 am ]
Post subject: 

Thank you, thank you. Always good to be appreciated. And now on to the good stuff...

What I'll do is give you an outline of what to create. I'll leave quite a few holes into which I'll try and make you stumble. This will be a learning experience.
And since I don't get to do this too often, I'll indulge a bit and introduce you into some more interesting methods of programming - a bit closer to OOP but not quite there. I'm guessing that you don't know too much about modules and the like, so we'll structure this around that level of programming.

To start things off, make sure you give the Modules tutorial a read. It's just a coincidence that I wrote that...I'd've referred to it anyway...Very Happy.
I've opted to go the Modules route as opposed to the Classes route since I don't think I'm qualified to instruct you on the intricacies that lie therein. Modules I know well and have used for a long time. Classes, though ultimately more useful, I have less experience in and plus, Cervantes' great 3-part Classes tut is the place for you to go if you want to learn about them. Oh, and self-teaching too.

To wit, the aim of this programme will be to read in a WPL file, parse out the names of the songs within, and then write a PLP file using a bit of hex manipulation to ensure that it writes correctly.
Ergo:
code:

unit
module WPL_PLP
    export convert
   
    proc readWPLFile
    end readWPLFile
   
    proc parseSongNames
    end parseSongNames
   
    proc writePLPFile
    end writePLPFile

    proc convert
    end convert
end WPL_PLP


Since we'll be playing around with the file while creating it, comment out the 'unit' for now. We have our stubs, and a general idea of what we'll be doing next.
For now, your job is to do the following:
- get both readWPLFile and parseSongNames working
- this means reading in the WPL file in its entirety, then searching through it and extracting anything that is defined as a file name
- you will then need to modify those file names to account for apostraphes and other possible character conversions used in the WPL format

Some hints:
- you will not know before hand how many file names are in a given file, so you'll want some flexibility in the way you parse the data
- WPL files follow a very defined structure. That is to say, a lot of things are constant about it.
- consider this: when parsing the file name, are you going to parse the entire path or just the song title? Which do you need, which don't you need? How can you distinguish between different paths?

I want to see some clean coding coming from you. No extraneous code whatsoever. That means that everything must be compartmentalized. If I were to run the code without calling any of the procs from within your module, I would want nothing to occur. (As a comparision, if we look at the final product from the Modules tutorial, running that file (sans 'unit') as it is will do nothing more than load the procs into memory. That's what you're aiming for.)
Make use of functions as much as possible. As Cervantes would say (and I believe he's quoting wtd), follow the DRY principle: Don't Repeat Yourself. If you see the same code twice, replace it with a function. Procedures are alright, but try to push yourself towards fcns more.
Also the stubs I've lain out are just a guideline. Don't let them restrict you. The current task is quite simple, but be creative...well, try to at least.

I'll be expecting the finished task within 36 hours...give or take a few. You don't have much else to do do you? Unless of course you have some random tests coming up...then those come first... Wink
It is now high noon. Go!

Author:  skootles [ Tue May 02, 2006 4:19 pm ]
Post subject: 

Whoops! I forgot that WMP changed the ampersand (sp?) (the & symbol) into &amp;
No worries. All fixed now Smile


code:
var fn, fn2 : int                % Variables for the files to be opened

var currentsong : string := ""    % A temporary var for when a variable is being assigned to a song name.
var character : string := ""      % Used for the open file
var playlist : string := ""       % The Windows Media Player Playlist
var yplay : string := ""          % The Samsung YH-925 Playlist

var location : int := 0           % A location variable for the open file

var count : int := 0              % Used for knowing which var of an array to write a song name too.

var numofsongs : int := 0         % The number of songs in the WMP Playlist


View.Set ("graphics:800;600,position:middle;center")





open : fn2, yplay, put, seek



proc GetSongNames

    var songs : array 1 .. numofsongs of string

    seek : fn, 0
    loop
        location := 0
        exit when eof (fn)
        if character = chr (92) then
            tell : fn, location

            loop
                get : fn, character : 1
                if character = chr (92) then
                    tell : fn, location
                end if
                exit when character = chr (34)
            end loop

            seek : fn, location

            loop
                get : fn, character : 1                                             % The ' symbol is replaced with  &apos;
                if character = "&" then
                    tell : fn, location
                    get : fn, character : 1
                    if character = "a" then
                        get : fn, character : 1
                        if character = "p" then
                            get : fn, character : 1
                            if character = "o" then
                                get : fn, character : 1
                                if character = "s" then
                                    get : fn, character : 1
                                    if character = ";" then
                                        get : fn, character : 1
                                        currentsong += "'"
                                    else
                                        seek : fn, location - 1
                                    end if
                                else
                                    seek : fn, location - 1
                                end if
                            else
                                seek : fn, location - 1
                            end if
                        elsif character = "m" then
                       
                            get : fn, character : 1                                 %   The & symbol is replaced with &amp;
                            if character = "p" then
                                get : fn, character : 1
                                if character = ";" then
                                    get : fn, character : 1
                                    currentsong += "&"
                                else
                                    seek : fn, location - 1
                                end if
                            else
                                seek : fn, location - 1
                            end if
                           
                        else
                            seek : fn, location - 1
                        end if
                    else
                        seek : fn, location - 1
                    end if
                end if

                exit when character = chr (34)                     % Exit when a quotation mark is detected
                currentsong += character
            end loop

            count += 1
            songs (count) := currentsong
            currentsong := ""

        end if

        get : fn, character : 1

    end loop

    %  ---------  For testing purposes   (lists song names)  -----------

    put skip

    for i : 1 .. numofsongs
        put songs (i)
    end for


end GetSongNames




proc readWPLFile

    open : fn, playlist, get, seek
    loop
        exit when eof (fn)

        get : fn, character : 1
        if character = "<" then
            tell : fn, location
            get : fn, character : 1
            if character = "m" then
                get : fn, character : 1
                if character = "e" then
                    get : fn, character : 1
                    if character = "d" then
                        get : fn, character : 1
                        if character = "i" then
                            get : fn, character : 1
                            if character = "a" then
                                get : fn, character : 1
                                numofsongs += 1
                            end if
                        end if
                    end if
                end if
            end if
        end if
    end loop

end readWPLFile



/*
 proc writePLPFile
 end writePLPFile

 proc convert
 end convert */


put "Please enter the name of the Windows Media Player playlist"
put "(including the extension).", skip
put "NOTE: It must be in the same directory as this executable. ", skip
get playlist : *


%put skip, "Please enter the name of the YH-925 Playlist (without the extension)", skip
%get yplay : *

%yplay += ".plp"



readWPLFile
GetSongNames


I feel just like I used to when submitting assignments to my teacher Razz

I've commented out some things, for now, and renamed other things.. but it's all there.

Author:  Delos [ Tue May 02, 2006 10:25 pm ]
Post subject: 

Wow...if I were your teacher, well let's just say you wouldn't be getting a stellar mark for that. What happened to the modules? And the lack of extraneous code? Yes, no? Mayhaps?
You need a major REWRITE. (Don't worry about the caps there, inside joke). I want you to go check out the tut on flexible arrays. That would be why I had the word 'flexiblity' bolded in my last post.... Rolling Eyes .
You'll incorporate that into your programme, instead of using a dynamic array. (What's the difference? A flexy array can have its bounds dynamically changed during the programme. A dynamic array simply has an unspecified upper bound that will be set at initialization).
I'm not completely adverse to the way you've read your file in. It works, is damn lengthy, but works. Plus, you avoid having to use an extra array or two...but the downside is that it's messy, messy code - and there is far less that you can do with it.
The idea with the flexy array will be to read in the entire file, then extract the necassary bits elsewhere.
Also, here's be big one, I want to see parameters! You have none so far. I could count half a dozen spots where they'd be extremly prudent, and that's without digging too deep.

So, in conclusion, you fail it. There, I said it. Now go clean this code up. You still have about 24 hours or so Very Happy. Good luck.

Author:  skootles [ Thu May 04, 2006 7:53 pm ]
Post subject: 

Late..
I probably still fail..
But at least I added the Flexible Array.. Razz

Author:  Delos [ Thu May 04, 2006 9:26 pm ]
Post subject: 

And Domo-kun says "Eh?"

Ok, so we're getting somewhere - slowly. You're right about still failing it though Wink.
I now have two options: since your code does work, we could press forward and worry about reprecussions later. The major ones I would foresee involve lengthy coding and inefficiencies due to minor paramter use.
The other option would be to linger on these two sections until you get them up to par, then push forward...

In cases like these, we have to ask ourselves: WWwtdD? What would wtd do? Most likely, he'd slap us both - you for not using parameters, and me for not having learnt Ruby yet.
After that, he'd point us to one of his more rarely looked at tuts: Premature Optimization. While what we're doing here is not so much optimization as heavy-duty cleaning, it still falls in the same category. The code as it stands holds a lot of meaning to you - both nostalgically and technically. Hence, to push forward cautiously is the better of the two vices.
This comes with a price, other than the aforementioned...you have to be in agreement that once your proggie works, you won't just run off and spend the afternoon giddily creating playlists. No, that would teach you only a little (about Hex writing and such). Far more important will be the use of paramaters, compartmentalization, and the joys of arrays that you'll encounter once we're done...
The idea being that once the programme works, we'll optimize and reorganize the hell out of it. Sounds good?

Your next assignment, therefore, is quite simple. Create a proc, write_header (or whatever other name you prefer), that does just that.
Look back at the header of the file in hex that you figured out a few posts ago. Your task will be to take those values, and write them to a file.
To make it a litle more clear:
- take the literal string values that will form the PLP header ("PLP Playlist Version...")
- convert each character to its hex equivalent, so as to match the values you see in that output you attached
- insert the necassary buffer characters as seen in the output
- convert these values back into text and write them to the file

Did I say that was simple? Uh...maybe not. I'll leave you with the warning that I've intentionally left some gaping pitfalls in there - intentional, of course. I won't tell you where, since that'd take the fun out of it. And by fun I mean learning, of course...>_> <_<.

Author:  skootles [ Fri May 05, 2006 2:21 pm ]
Post subject: 

Definately not simple.. Razz

I cna't seem to find my CompSci binder, or anything on the forum for converting ASCII to Hex.. so I'm stuck about there right now. But I'm still confused about what I'm doing.. why bother convertiong it all to Hex if I'm just going to convert it right back?

>.<

Author:  Delos [ Fri May 05, 2006 3:24 pm ]
Post subject: 

Conversion to Hex can be done in either a simple or complex method. The complex one is the route most people take, since it involves using formlae they've learnt in math/comp sci class (i.e., how to convert a decimal (base 10) number to hexadecimal (base 16)).

As for why we're doing this...that's an interesting question. It is by no means necassary for writing this proggie. It does, however, allow us to verify the contents of the file against the hex dump of the hex editor to ensure that they are exactly the same. Viewing pure ASCII output is good, but there are a lot of characters that won't render in ASCII and are simply displayed as a blank space...which means that if we see a space, it could be one of a dozen or so characters.
My own solution does not incorporate hex any longer since I've already done all the checks needed. I'd advise you to keep it in for now at least, since it will make your debugging a little easier. Just a little!

BTW, I'm fully aware that I gave you no more direction as to how to convert to hex in this post than in any of my others posts. I wonder if that could possibly mean something...Wink

Author:  skootles [ Fri May 12, 2006 2:11 pm ]
Post subject: 

Sorry about this week-long silence. Between school, with all the summative assignments I have (4 and counting), and work, I haven't gotten much computer time, let alone Turing time. I'll try to get that done this weekend, but it's going to be a hectic weekend, so maybe not until monday or tuesday, who knows. Sad

Author:  Delos [ Sat May 13, 2006 1:21 pm ]
Post subject: 

Heh, take your time. I've started work which means I won't have access to Turing to check anything until the weekends anyhow. But keep plugging away at it anyhow, you'll get there soon enough.

Author:  skootles [ Sun May 14, 2006 6:28 pm ]
Post subject: 

Yeah.. I've got 4 summative projects. One that I can only work on at school (construction class), and the other 3 are tough. Plus I've started work as well, so it'll be even worse.. Sad

Author:  skootles [ Mon May 22, 2006 7:57 pm ]
Post subject: 

Hmm

Could we perhaps skip this part? That is, if it's not really necessary.. Confused

Author:  Delos [ Mon May 22, 2006 8:44 pm ]
Post subject: 

Wow, it's been a long time since I last looked at this topic ^_^. No worries, I'm mostly up to speed. (Woah, fireworks going off all over the neighbourhood).
Let's get to it then. You want to skip the Hex part? Sure. As I said previously though, it will make your debugging a little easier. Give a bit, take a bit, ya' know.

I've forgotten how far you'd gotten before (and looking back through the thread isn't going to help me too much either!). And if that is the case, chances are you've forgotten why you wrote most of the code that you did previously.
So, we'll back-track a little, but this time we'll pounce forward with far greater speed since we know that you've done most of this already and that things work well.

So as a recap:
- create a Module
If I don't see a module structure in your next code post, I'm going to be most upset - and might start leading you down completely needless paths in spite! (Seriously though, it's a constructive way to learn. Force yourself to adapt to using Modules, you'll find you like them in no time at all).
- Procedures that you'll need:
+ reading in of the WPL file
+ extracting the names of the songs from the extract
+ writing those files to a new PLP file

IIRC, you had done either or both of the first two +'s there. However, I don't suspect I was satisfied with how tight your code has been. As a basic challenge, I'd like you to have your read_WPL_file procedure to be at most 20 lines (excluding the procedure headings, of course.
A good challenge? I'm sure you're up to it.
Look into Flexible arrays for this purpose. I've probably mentioned it before, and I don't recall if you did or not. I'll mention it again anyway, since flexy arrays are so awsome Laughing!

Name extraction shouldn't be difficult at all. I will suggest using functions here that will accept the raw data as their input (paramters), and spew out the parsed Song title as their output (result).
While you're at it, check out Cervantes new Fcnal Programming Tut, though the ideas mightn't be directly related, he's got some fantastic discussion of what functions are and how to use them. You'll find it really useful!

These assigned tasks sound terribly familiar. Something tells me we've spent a while on these already - not worries. We have time yet.


: