TGC Codebase Backup



playlist format parser by Phaelax

24th Aug 2005 3:34
Summary

loads any playlist format you wish to support without having to change the main function call.



Description



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    rem used to store tokens from calling split()
dim substrings() as string

rem playlist item
TYPE PlaylistItem
   key as string
   value as string
ENDTYPE

rem stores the files keys and values
dim playlist() as PlaylistItem

rem possible keys in a PLS format playlist file
dim keys(3) as string
keys(1) = "File"
keys(2) = "Title"
keys(3) = "Length"




cls

   loadPlaylist("c:\shoutcast-playlist.pls")

   count = array count(playlist())
   p as PlaylistItem
   set cursor 0,0
   for t = 0 to count
      p = playlist(t)
      print p.key," = ",p.value
   next t


suspend for key


`==========================================
`Loads a playlist file
`Returns 1 if file loaded, 0 if failed
`==========================================
function loadPlaylist(filename as string)
   loaded = 0
   if file exist(filename) = 1 AND endsWith(filename,".pls",1)
      loaded = loadPLS(filename)
   endif
endfunction loaded


`==========================================
`Loads a PLS format playlist file
`Returns 1 if file loaded, 0 if failed
`==========================================
function loadPLS(filename as string)
   loaded = 0
   open to read 1, filename
   read string 1, line$
   if lower$(line$) = "[playlist]"
      while file end(1) = 0
         read string 1, line$
         for i = 1 to array count(keys())
            if startsWith(line$, keys(i), 1) = 1
               split(line$, "=")
               key$ = substrings(0)
               value$ = substrings(1)
               addPlaylistItem(key$, value$)
            endif
         next i
      endwhile
      loaded = 1
   endif
endfunction loaded


`==========================================
`Add a playlist element to the array
`==========================================
function addPlaylistItem(key as string, value as string)
   temp as PlaylistItem
   temp.key = key
   temp.value = value
   array insert at bottom playlist()
   c = array count(playlist())
   playlist(c) = temp
endfunction


`==========================================
`Checks to see if string "s" starts with
`string "prefix"
`1 to ignore case, 0 for case-sensitive
`==========================================
function startsWith(s as String, prefix as String, ignoreCase as boolean )
   if ignoreCase
      s = lower$(s)
      prefix = lower$(prefix)
   endif
   pLength = len(prefix)
   sLength = len(s)
   if pLength > sLength then exitfunction 0
   temp as string
   temp = left$(s, pLength)
   if temp = prefix then exitfunction 1
endfunction 0


`==========================================
`Checks to see if string "s" ends with
`string "suffix"
`1 to ignore case, 0 for case-sensitive
`==========================================
function endsWith(s as String, suffix as String, ignoreCase as boolean )
   if ignoreCase
      s = lower$(s)
      suffix = lower$(suffix)
   endif
   xLength = len(suffix)
   sLength = len(s)
   if xLength > sLength then exitfunction 0
   temp as string
   temp = right$(s, xLength)
   if temp = suffix then exitfunction 1
endfunction 0


`==========================================
`Returns a substring from string "s" between
`beginIndex and endIndex, inclusive
`Character positions range from 1 to len(s)
`==========================================
function substring$(s as String, beginIndex as integer, endIndex as integer)
   temp as string
   temp = left$(s, endIndex)
   temp = right$(temp, len(temp)-(beginIndex-1))
endfunction temp


`==========================================
`Splits a string into tokens or seperate
`"pieces", using the specified delimiter.
`Tokens are stored in a string array which
`size ranges from 0 to array count, inclusive
`==========================================
function split(s as String, delim as string)
   empty array substrings()
   dLength = len(delim)
   flag = 1
   while flag = 1
      flag = 0
      sLength = len(s)
      for i = dLength to sLength
         temp$ = substring$(s,(i-dLength)+1,i)
         if temp$ = delim
            appendElement(left$(s,i-dLength))
            s = right$(s,sLength-i)
            flag = 1
            exit
         endif
      next i
      if flag = 0
         appendElement(s)
      endif
   endwhile
endfunction


`==========================================
`Adds string "s" to the end of the array
`used in the function
`==========================================
function appendElement(s as string)
   array insert at bottom substrings()
   c = array count(substrings())
   substrings(c) = s
endfunction


REM +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
REM                 CODE BELOW IS AN EXAMPLE PLS FILE
REM +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

[playlist]
numberofentries=5
File1=http://64.236.34.97:80/stream/1040
Title1=(#1 - 1330/17190) CLUB 977 The 80s Channel
Length1=-1
File2=http://64.236.34.196:80/stream/1040
Title2=(#2 - 1131/14079) CLUB 977 The 80s Channel
Length2=-1
File3=http://64.236.34.4:80/stream/1040
Title3=(#3 - 1386/16845) CLUB 977 The 80s Channel
Length3=-1
File4=http://64.236.34.67:80/stream/1040
Title4=(#4 - 2810/14175) CLUB 977 The 80s Channel
Length4=-1
File5=http://205.188.234.66:8010
Title5=(#5 - 10/10) CLUB 977 The 80s Channel
Length5=-1
Version=2