Page 1 of 1

Converting PB 4.41 Program to PB 6

Posted: Thu Sep 28, 2023 10:12 pm
by matalog
I found a tool online, and it happened to have PB source code with it, the original complied program supplied from 2010 works, but when I compile the software it does not. Has much changed in this code since then, that could be stopping this from working correctly, but not throwing up any errors as it runs:

Code: Select all

;/ TZX2TAP Converter
; (c) 2010 By LCD
; Written using PureBasic 4.41
; This Software is Open Source
; No WinAPI Calls used, so multiplatform-compatible
version$="0.21"
; Added: Column witdhs now saved in Preference file
directory$=GetCurrentDirectory()

Global *Buffer=AllocateMemory(65536)
Procedure.s GetFileName(Filename$); Without suffixes
  len=Len(Filename$):Pos=len
  For a=1 To len
    If Mid(Filename$,a,1)="."
      Pos=a
    EndIf
  Next a
  ProcedureReturn Left(Filename$,Pos-1)
EndProcedure
Procedure TZX2TAP(Filename$)
  Shared Report
  Out$=GetFileName(Filename$)+".tap"
  error=0
  fileIn=ReadFile(#PB_Any,Filename$)
  If fileIn And FileSize(Out$)<1
    fileout=CreateFile(#PB_Any,Out$)
    If fileout
      ReadData(fileIn,*Buffer,10)
      ID$=PeekS(*Buffer,7)
      If ID$="ZXTape!" And PeekB(*Buffer+7)&$FF=$1A
        Repeat
          id=ReadByte(fileIn)&255
          Select id
            Case $10
              pause=ReadWord(fileIn)&$FFFF
              length=ReadWord(fileIn)&$FFFF
              ReadData(fileIn,*Buffer,length)
              WriteWord(fileout,length)
              WriteData(fileout,*Buffer,length)
            Case $11
              Pilot=ReadWord(fileIn)&$FFFF
              Sync0=ReadWord(fileIn)&$FFFF
              Sync1=ReadWord(fileIn)&$FFFF
              Pulse0=ReadWord(fileIn)&$FFFF
              Pulse1=ReadWord(fileIn)&$FFFF
              Tone=ReadWord(fileIn)&$FFFF
              lastbits=ReadByte(fileIn)&255
              pause=ReadWord(fileIn)&$FFFF
              length=ReadWord(fileIn)&$FFFF+ReadByte(fileIn)<<16
              If length>65535
                length=65535
                AddGadgetItem(Report,-1,Out$+": size over 65535 bytes cannot be saved in TAP")
              EndIf
              ReadData(fileIn,*Buffer,length)
              WriteWord(fileout,length)
              WriteData(fileout,*Buffer,length)
            Case $12 To $13,$15 To $1F
              error=1
              error$="Unsupported!"
              AddGadgetItem(Report,-1,Out$+": Unsupported block found ($"+Hex(id)+")")
            Case $14
              Pulse0=ReadWord(fileIn)&$FFFF
              Pulse1=ReadWord(fileIn)&$FFFF
              lastbits=ReadByte(fileIn)&255
              pause=ReadWord(fileIn)&$FFFF
              length=ReadWord(fileIn)&$FFFF+ReadByte(fileIn)<<16
              If length>65535
                length=65535
                AddGadgetItem(Report,-1,Out$+": size over 65535 bytes cannot be saved in TAP")
              EndIf
              ReadData(fileIn,*Buffer,length)
              WriteWord(fileout,length)
              WriteData(fileout,*Buffer,length)
            Case $20
              ReadWord(fileIn)
            Case $21,$30
              length=ReadByte(fileIn)&255
              ReadData(fileIn,*Buffer,length)
              text$=PeekS(*Buffer,length)
            Case $22 To $29
            Case $2A
              ReadLong(fileIn)
            Case $31
              pause=ReadByte(fileIn)&255
              length=ReadByte(fileIn)&255
              ReadData(fileIn,*Buffer,length)
              text$=PeekS(*Buffer,length)
            Case $32
              length=ReadWord(fileIn)&$FFFF
              ReadData(fileIn,*Buffer,length)
              text$=PeekS(*Buffer,length)
            Case $33
              Infos=ReadByte(fileIn)&$FF
              ReadData(fileIn,*Buffer,Infos*3)
            Case $35
              ReadData(fileIn,*Buffer,10)
              length=ReadWord(fileIn)&$FFFF
              ReadData(fileIn,*Buffer,length)
            Case $5A
              ReadData(fileIn,*Buffer,9)
          EndSelect
        Until Eof(fileIn) Or error
        CloseFile(fileout)
      EndIf
    EndIf
    CloseFile(fileIn)
  EndIf
EndProcedure
CompilerSelect #PB_Compiler_OS ;{
  CompilerCase #PB_OS_Linux
    sys$="Tux_"
  CompilerCase #PB_OS_MacOS
    sys$="Mac_"
  CompilerCase #PB_OS_Windows
    sys$="Win_"
CompilerEndSelect ;}


a=OpenPreferences("tzx2tap.ini") ;{ Load Preferences from ini
ExaminePreferenceGroups()
PreferenceGroup("001") ;{
win_posx=ReadPreferenceLong("WinPosX",0)
win_posy=ReadPreferenceLong("WinPosY",0)
win_sizx=ReadPreferenceLong("WinSizX",1021)
win_sizy=ReadPreferenceLong("WinSizY",700)
splitterA=ReadPreferenceLong("Splitter1",200)
splitterB=ReadPreferenceLong("Splitter2",500)
directory$=ReadPreferenceString("Currentdir",directory$)
column0=ReadPreferenceLong("column0",300)
column1=ReadPreferenceLong("column1",100)
column2=ReadPreferenceLong("column2",100)
column3=ReadPreferenceLong("column3",100)
ClosePreferences() ;}
;}

window=OpenWindow(#PB_Any,win_posx,win_posy,win_sizx,win_sizy,sys$+"TZX2TAP v. "+version$+" Written in 2010 by LCD",#PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_SizeGadget)
CompilerIf #PB_Compiler_OS=#PB_OS_Windows ;{
  SmartWindowRefresh(window,1)
CompilerEndIf ;}

TZXDirs=ExplorerTreeGadget(#PB_Any,0,0,200,500,directory$,#PB_Explorer_AlwaysShowSelection|#PB_Explorer_NoFiles|#PB_Explorer_NoDriveRequester|#PB_Explorer_AutoSort)
TZXFiles=ExplorerListGadget(#PB_Any,200,0,700,500,directory$+"*.tzx",#PB_Explorer_AlwaysShowSelection|#PB_Explorer_MultiSelect|#PB_Explorer_GridLines|#PB_Explorer_FullRowSelect|#PB_Explorer_NoFolders|#PB_Explorer_NoParentFolder|#PB_Explorer_NoDirectoryChange|#PB_Explorer_NoDriveRequester|#PB_Explorer_AutoSort)

SetGadgetItemAttribute(TZXFiles,0,#PB_Explorer_ColumnWidth,column0,0)
SetGadgetItemAttribute(TZXFiles,0,#PB_Explorer_ColumnWidth,column1,1)
SetGadgetItemAttribute(TZXFiles,0,#PB_Explorer_ColumnWidth,column2,2)
SetGadgetItemAttribute(TZXFiles,0,#PB_Explorer_ColumnWidth,column3,3)


Report=EditorGadget(#PB_Any,0,500,920,200,#PB_Editor_ReadOnly)
splitter1=SplitterGadget(#PB_Any,0,0,920,500,TZXDirs,TZXFiles,#PB_Splitter_Vertical|#PB_Splitter_Separator)
splitter2=SplitterGadget(#PB_Any,0,0,920,700,splitter1,Report,#PB_Splitter_Separator)
ConvertButton=ButtonGadget(#PB_Any,900,0,120,29,"Convert to TAP")
Gosub resizing
SetGadgetState(splitter1,splitterA)
SetGadgetState(splitter2,splitterB)
Repeat
wwe=WaitWindowEvent()
If wwe=#PB_Event_Gadget;{
  Select EventGadget()
    Case TZXDirs
      SetGadgetText(TZXFiles,GetGadgetText(TZXDirs))
    Case ConvertButton
      Gosub conversion
  EndSelect ;}
ElseIf wwe=#PB_Event_SizeWindow ;{
  Gosub resizing
EndIf ;}
Until wwe=#PB_Event_CloseWindow

a=CreatePreferences("tzx2tap.ini") ;{ Save Preferences
PreferenceGroup("001") ;{
WritePreferenceLong("WinPosX",WindowX(window))
WritePreferenceLong("WinPosY",WindowY(window))
WritePreferenceLong("WinSizX",WindowWidth(window))
WritePreferenceLong("WinSizY",WindowHeight(window))
WritePreferenceLong("Splitter1",GetGadgetState(splitter1))
WritePreferenceLong("Splitter2",GetGadgetState(splitter2))
WritePreferenceString("Currentdir",GetGadgetText(TZXDirs))
WritePreferenceLong("column0",GetGadgetItemAttribute(TZXFiles,0,#PB_Explorer_ColumnWidth,0))
WritePreferenceLong("column1",GetGadgetItemAttribute(TZXFiles,0,#PB_Explorer_ColumnWidth,1))
WritePreferenceLong("column2",GetGadgetItemAttribute(TZXFiles,0,#PB_Explorer_ColumnWidth,2))
WritePreferenceLong("column3",GetGadgetItemAttribute(TZXFiles,0,#PB_Explorer_ColumnWidth,3))

ClosePreferences() ;}
;}
CloseWindow(window)
End

resizing: ;{
sizex=WindowWidth(window)
sizey=WindowHeight(window)
ResizeGadget(splitter2,#PB_Ignore,#PB_Ignore,sizex-121,sizey)
ResizeGadget(ConvertButton,sizex-120,#PB_Ignore,#PB_Ignore,#PB_Ignore)
;}
conversion: ;{
ClearGadgetItems(Report)
HideGadget(TZXFiles,1)
For a=0 To CountGadgetItems(TZXFiles)-1
  dir$=GetGadgetText(TZXFiles)
  If GetGadgetItemState(TZXFiles,a)&#PB_Explorer_Selected
    Filename$=GetGadgetItemText(TZXFiles,a,0)
    ext$=LCase(GetExtensionPart(Filename$))
    If ext$="tzx"
      fullpath$=dir$+Filename$
      b=GetFileAttributes(fullpath$)
      TZX2TAP(fullpath$)
      AddGadgetItem(Report,-1,Filename$)
    EndIf
  EndIf
Next a
HideGadget(TZXFiles,0)
Return ;}

Re: Converting PB 4.41 Program to PB 6

Posted: Thu Sep 28, 2023 10:45 pm
by normeus
ascii to unicode is the problem

Code: Select all

ID$=PeekS(*Buffer,7,#PB_Ascii); this line
:
:
:
text$=PeekS(*Buffer,length,#PB_Ascii); these other lines
Norm.
PS
Typed while visual studio is doing an update ;)

Re: Converting PB 4.41 Program to PB 6

Posted: Fri Sep 29, 2023 12:51 am
by matalog
That's great, thanks. It works again.

One interesting thing is that the program is now 120KB, whereas when it was compiled with PB 4.1, it was only 30KB.

Re: Converting PB 4.41 Program to PB 6

Posted: Fri Sep 29, 2023 8:54 am
by BarryG
matalog wrote: Fri Sep 29, 2023 12:51 amthe program is now 120KB, whereas when it was compiled with PB 4.1, it was only 30KB.
Totally normal for the age difference of versions. This also happens with other languages, too.

Re: Converting PB 4.41 Program to PB 6

Posted: Sat Apr 13, 2024 12:44 pm
by CDXbow
I was trying out some even older code, from PB 3.x, written by By Lance Jepsen. 21 year old code and it still runs. There is one problem, the result appears in Chinese characters. I'm guessing this is a unicode problem but I haven't been able to fix it. Original thread here https://www.purebasic.fr/english/viewtopic.php?t=6958

Code: Select all

;Download .html code from web
;By Lance Jepsen, 7/19/2003
;Based on code by Pille, 14.07.2003
;Adapted from Jost Schwider's VB OpenUrl
;Greetings Pille! Mirabile dictu!


Enumeration
  #Window 
  #Editor
  #Url 
  #cmdOpenUrl
  #View
EndEnumeration

defaultUrl.s="http://www.google.com";Put URL you want to grab data from in here

Procedure.s OpenURL(URL.s, OpenType.b)
  
  isLoop.b=1
  INET_RELOAD.l=$80000000
  hInet.l=0: hURL.l=0: Bytes.l=0
  Buffer.s=Space(2048)
  res.s=""
  
  hInet = InternetOpen_("PB@INET", OpenType, #Null, #Null, 0)
  hURL = InternetOpenUrl_(hInet, URL, #Null, 0, INET_RELOAD, 0)
  
  Repeat
    InternetReadFile_(hURL, @Buffer, Len(Buffer), @Bytes)
    If Bytes = 0
      isLoop=0
    Else
      res = res + Left(Buffer, Bytes)
    EndIf
  Until isLoop=0
  
  Debug res
  
  InternetCloseHandle_(hURL)
  InternetCloseHandle_(hInet)
  
  ProcedureReturn res
EndProcedure

If OpenWindow(#Window, 0, 0, 500, 300, "Download", #PB_Window_SystemMenu | #PB_Window_TitleBar | #PB_Window_ScreenCentered)
  
  EditorGadget(#Url, 5, 5, 410, 20)
  EditorGadget(#Editor, 5, 30, 490, 260)
  ;HideGadget(#Url,1)
  HideGadget(#Editor,1)
  ButtonGadget(#cmdOpenUrl, 420, 5, 75, 20, "Download")
  
  SetGadgetText(#Url, defaultUrl)
  
  Repeat
    EventID.l = WaitWindowEvent()
    If EventID = #PB_Event_Gadget
      Select EventGadget()
          
        Case #cmdOpenUrl
          
          result = MessageRequester("Download","Are you connected to the Internet?",#PB_MessageRequester_YesNo)
          If result = #PB_MessageRequester_Yes
            ProgressBarGadget(0, 80, 80,250, 25, 0,100)
            SetGadgetState   (0, 0)
            SetGadgetText(#Editor, OpenUrl(GetGadgetText(#Url),1))
            SetGadgetState   (0, 20)
            result$ = OpenUrl(GetGadgetText(#Url),1)
            SetGadgetState   (0, 80)
            If CreateFile(0,"test.html") ; Now we save the data we grabbed to a html file. Open up this file with a web browser to see the data you grabbed.
              SetGadgetState   (0, 90)
              WriteString(0, result$)
              SetGadgetState   (0, 100)
              CloseFile(0)
            EndIf
            Delay(500)
            HideGadget(0,1)
            SetGadgetText(#Editor,"Downloading complete. File TEST.HTML created.")
            HideGadget(#Editor,0)
            DisableGadget(#cmdOpenUrl,1)           
          EndIf
          
      EndSelect
    EndIf       
  Until EventID = #PB_Event_CloseWindow
  
EndIf
End

Re: Converting PB 4.41 Program to PB 6

Posted: Sat Apr 13, 2024 4:35 pm
by Bisonte
@ CDXbow:

You dont need API calls anymore for doing this... ReceiveHTTPFile(), ReceiveHTTPMemory() ! Look at the Help - HTTP Lib ;)

Re: Converting PB 4.41 Program to PB 6

Posted: Sun Apr 14, 2024 1:19 am
by CDXbow
You dont need API calls anymore for doing this... ReceiveHTTPFile(), ReceiveHTTPMemory() ! Look at the Help - HTTP Lib ;)
Thanks for the prompt reply. I will give it a go, Fingers crossed.