home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 109 / EnigmaAmiga109CD.iso / software / musica / getcddb / source / ge2df9~1.bb2 next >
Text File  |  2000-01-07  |  6KB  |  226 lines

  1. ;GetCDDB_ParseFuncs.bb2
  2.  
  3. ; parse CDDB files
  4.  
  5. XINCLUDE "GetCDDB_MiscFuncs.bb2"
  6. XINCLUDE "GetCDDB_Consts.bb2"
  7.  
  8. ;----------------------------------------------------------------------
  9. ; This function relies on the CDDB file returned being in an exact format
  10. ; - bit of a kludge
  11.  
  12. Function.w GetCDDBResult{cddbfile$}
  13.  
  14. If OpenFile(0,cddbfile$)
  15.   FileInput 0
  16.   Repeat
  17.     inputstr$=Edit$(80)
  18.   Until Instr(inputstr$,"Content-Type:")
  19.  
  20.   inputstr$=Edit$(80) ; this is a blank line
  21.  
  22.   inputstr$=Edit$(80) ; inputstr now contains the result code
  23.  
  24.   CloseFile 0
  25.   Function Return Value{Left$(inputstr$,3)}
  26. Else
  27.   Function Return 0
  28. EndIf
  29. End Function
  30.  
  31. ;----------------------------------------------------------------------
  32. ; Process result number - return whether ok or not 
  33. ; Result was obtained from GetCDDBResult
  34.  
  35. Function.b ResolveResult{cddbresult.w}
  36.  
  37.   If cddbresult=0
  38.     Function Return 0
  39.   EndIf
  40.  
  41.   result$=Left$(Str$(cddbresult),2) ;get first two digits of error code
  42.                                     ; for generically addressing errors
  43.  
  44.   Select result$
  45.  
  46.     Case "20"
  47.       ;cmd ok, no action needed
  48.       Function Return -1
  49.  
  50.     Case "21"
  51.       ;cmd ok, prepare to read data from server
  52.       Function Return -1
  53.  
  54.     Case "22"
  55.       ;cmd ok, prewpare to give server data - if unexpected, treat as 21
  56.       Function Return -1
  57.  
  58.     Case "23"
  59.       ;cmd ok, connection closing at client's request
  60.       Function Return -1
  61.  
  62.     Case "40"
  63.       If cddbresult=401
  64.         ; no entry
  65.         Function Return #_NOENTRY_ERROR
  66.       Else
  67.         ;cmd failed due to server error - try different server
  68.         ErrorReq{"Command failed due to server error|Reconnect to different server"}
  69.         Function Return #_SERVER_ERROR
  70.       EndIf
  71.  
  72.     Case "41"
  73.       ;cmd failed as 40, information follows reconnect as 40
  74.       ErrorReq{"Command failed due to server error|Reconnect to different server"}
  75.       Function Return #_SERVER_ERROR
  76.  
  77.     Case "43"
  78.       ;cmd failed as 40, connection dropped by server reconnect as 40
  79.       ErrorReq{"Command failed due to server error|Reconnect to different server"}
  80.       Function Return #_SERVER_ERROR
  81.  
  82.     Case "50"
  83.       ;cmd failed due to client error - don't reconnect
  84.       ErrorReq{"CDDB server returned 'Command syntax error'|Please contact author about this error"}
  85.       Function Return #_CLIENT_ERROR
  86.  
  87.     Case "51"
  88.       ;cmd failed as 50, info follows
  89.       ErrorReq{"CDDB server returned 'Command syntax error'|Please contact author about this error"}
  90.       Function Return #_CLIENT_ERROR
  91.  
  92.     Case "53"
  93.       ; cmd failed, connection dropped, retry connect
  94.       ErrorReq{"CDDB server returned 'Command syntax error'|Please contact author about this error"}
  95.       Function Return #_CLIENT_ERROR
  96.  
  97.     Default
  98.       ErrorReq{"Unexpected return code from server"}
  99.       Function Return #_UNEXPECTED_ERROR
  100.  
  101.   End Select
  102. End Function
  103.  
  104. ;----------------------------------------------------------------------
  105.  
  106.  
  107. Function.b ConvertCDDBFile{cdplayer$, cddbfile$, cddafile$}
  108.  
  109.   SHARED overwrite.b
  110.  
  111.   If Exists(cddafile$) AND overwrite.b=0 ;ie don't overwrite
  112.     Function Return -1
  113.   EndIf
  114.  
  115.   GTSetString 0,#_STATUS_TEXT,"Converting file..." ; this possibly should be  
  116.                                                    ; SetStatus("Converting file.."
  117.  
  118.   result.w=ResolveResult{GetCDDBResult{cddbfile$}}
  119.   If result.w<>-1
  120.     Function Return result
  121.   EndIf
  122.  
  123.   If NOT ReadFile(3,cddbfile$)
  124.     ErrorReq{"Unable to read temporary CDDB file"}
  125.     Function Return #_READ_ERROR
  126.   EndIf
  127.  
  128.   FileInput 3
  129.  
  130.   CaseSense 0
  131.  
  132.   Dim track$(100)
  133.   author$=""
  134.   title$=""
  135.  
  136.   Repeat
  137.     inputstr$=Edit$(80)
  138.   Until Instr(inputstr$,"HTTP") OR Eof(3)
  139.  
  140.   If Eof(3)
  141.     ErrorReq{"Unexpected page loading error"}
  142.     Function Return #_UNEXPECTED_ERROR
  143.    EndIf
  144.  
  145.  
  146.   ;inputstr now has the line with the page error code on it
  147.   ; eg "HTTP\1.1 200 OK"
  148.   inputstr$=UnRight$(inputstr$,9)
  149.  
  150.   errorcode.w=Value{Left$(inputstr$,3)}
  151.  
  152.   If errorcode.w<>200
  153.     ErrorReq{"An error has occurred in the loading on this page. The server returned error code "+Str$(errorcode.w)}
  154.     Function Return #_HTTP_ERROR
  155.   EndIf
  156.  
  157.   ;suppose we now know that we have a disc file
  158.   i.b=0
  159.   Repeat
  160.     inputstr$=Edit$(80)
  161.     If Instr(inputstr$,"DTITLE=")
  162.       Select LCase$(cdplayer$)
  163.         Case "optycdplayer"
  164.           temp$=UnRight$(inputstr$,7)
  165.           author$=Left$(temp$,Instr(temp$," /")) ; botchy?
  166.           title$=UnRight$(temp$,Instr(temp$," /")+2)
  167.           If Len(title$)=0 Then title$=author$
  168.  
  169. ;        Case "splayer"  ;splayer now uses the same format as Optycdplayer
  170. ;          title$=Replace$(UnRight$(inputstr$,7),"/",":")
  171.  
  172.         Default
  173.           cdplayer$="optycdplayer"
  174.           temp$=UnRight$(inputstr$,7)
  175.           author$=UnLeft$(temp$,Instr(temp$," /")+4) ; botchy?
  176.           title$=UnRight$(temp$,Instr(temp$," /")+2)
  177.  
  178.       End Select
  179.     EndIf
  180.  
  181.     If Instr(inputstr$,"TTITLE")
  182.       inputstr$=Replace$(inputstr$,"TTITLE","")
  183.       i.b=Value{Left$(inputstr$,Instr(inputstr$,"="))}
  184.       track$(i)=Right$(inputstr$,Len(inputstr$)-Instr(inputstr$,"="))
  185. ;      NPrint track$(i)
  186.     EndIf
  187.  
  188.     tot_tracks.b=i
  189.  
  190.   Until Eof(3)
  191.   CloseFile 3
  192.  
  193.   If Len(title$)=0 OR Len(track$(0))=0 ; invalid cddb file
  194.     ErrorReq{"This CD is not in the CDDB database. Try a different genre"}
  195.     DeleteFile_ cddbfile$
  196.     Function Return #_NOENTRY_ERROR
  197.   EndIf
  198.  
  199.   If WriteFile(4,cddafile$)
  200.     FileOutput 4
  201.     If LCase$(cdplayer$)="optycdplayer"
  202.       NPrint author$
  203.       NPrint title$
  204.     Else
  205.       If LCase$(cdplayer$)="splayer"
  206.         NPrint title$
  207.       EndIf
  208.     EndIf
  209.  
  210.     For i=0 To tot_tracks
  211.       NPrint track$(i)
  212.     Next
  213.  
  214.     CloseFile 4
  215.   Else
  216.     ErrorReq{"Unable to open temporary output file for writing"}
  217.     Function Return #_WRITE_ERROR
  218.   EndIf
  219.  
  220.   WindowOutput 0
  221.   Function Return -1
  222.  
  223. End Function
  224.  
  225.  
  226.