; ASCIItoKW3 v1.00
; Public Domain
; 95.03.26 - 95.03.29

; This program takes an ascii text file and converts it to
; Kindwords3 format. Kindwords3 can load ascii files - but not
; very well. It links all lines of text in one large chunk,
; ignoring paragraphs and joining some words together. ASCIItoKW3
; hopefully improves on this. However, YOU USE THIS PROGRAM AT
; YOUR OWN RISK! It's a hack - and may not produce a perfectly
; correct file. I take no responsibility for any loss of data
; or anything else bad that may happen to anyone who uses this
; program.

; Carl Read
; CyberCraft
; PO Box 4032
; Mayfair
; Hastings 4201
; NEW ZEALAND

.Initialize:
  WBStartup

  ;This function when used with colours 1 or 2 produces the right
  ;shading for windows and gadgets etc. regardless of what
  ;Kickstart the program is running on. The exception is menu
  ;colours and window colours. Using shade{} on them will screw
  ;up the look of the menus.
  Function Shade{sh}
    If ExecVersion<36
      If Int(sh+.49)=1
        sh=2
      Else
        If Int(sh+.49)=2 Then sh=1
      EndIf
    EndIf
    Function Return sh
  End Function

  quitFlag=0
  ev.l=0
  ldPath$=""
  ldFi$=""
  paras=0
  fiSize.l=0
  long.l=0
  Dim text$(32000)

  Screen 0,0,0,640,DispHeight,2,$8000,"ASCIItoKW3 v1.00.",Shade{2},Shade{1}
  RGB 0,9,10,10:RGB Shade{1},0,0,0
  RGB Shade{2},15,15,15:RGB 3,15,14,12

.Menu:
  MenuColour 2
  MenuTitle 0,0,"  Project  "
  MenuItem 0,0,0,0,"Open","O"
  MenuItem 0,0,0,1,"Save As     ","A"
  MenuItem 0,0,0,2,"Help","H"
  MenuItem 0,0,0,3,"About","?"
  MenuItem 0,0,0,4,"Quit","Q"
  MenuTitle 0,1,"  Options  "
  MenuItem 0,3,1,0,"  Join Lines"
  MenuItem 0,1,1,1,"  ASCII Output"
  MenuState 0,0,1,Off

.Gads:
  GadgetPens shade{1},0
  Borders 2,1
  BorderPens shade{2},shade{1}
  TextGadget 0,132,-15,4,1," OK "
  TextGadget 1,138,135,0,1," OK "
  TextGadget 2,238,135,0,1," OK "

.Wind0:
  Window 0,0,10,640,DispHeight-10,$0100|$800|$1000,"",2,1
  SetMenu 0

.Start:
  Gosub MainLoop
End

.MainLoop:
  Gosub AsciiOpen
  While quitFlag=0
    Gosub CheckEvents
  Wend
Return

.CheckEvents:
  ev=WaitEvent
  If ev<>0
    winNo=EventWindow
    If (ev AND $100)=$100 Then Gosub MenResponse
    If (ev AND $200)=$200 Then Gosub CloseResponse
    If (ev AND $40)=$40 Then Gosub GadResponse
    If (ev AND $4)=$4 Then Gosub TidyWindow
    If (ev AND $400)=$400 Then Gosub KeyResponse
  EndIf
Return

.ChEv:
  ev=Event
  If ev<>0
    winNo=EventWindow
    If (ev AND $100)=$100 Then Gosub MenResponse
    If (ev AND $200)=$200 Then Gosub CloseResponse
    If (ev AND $40)=$40 Then Gosub GadResponse
    If (ev AND $4)=$4 Then Gosub TidyWindow
    If (ev AND $400)=$400 Then Gosub KeyResponse
  EndIf
Return

.MenResponse:
  men=MenuHit
  it=ItemHit
  Select men+1
    Case 1
      Select it+1
        Case 1 ;Open
          Gosub AsciiOpen
        Case 2 ;Save As
          If paras>0
            Gosub KW3Save
          EndIf
        Case 3 ;Help
          Gosub DoHelp
        Case 4 ;About
          SizeLimits 100,30,344,170
          Window 2,142,12,344,170,15|$10|$20|$400|$1000,"About ASCIItoKW3.",2,1,1
          SetMenu 0
          Gosub AboutDraw
        Case 5 ;Quit
          quitFlag=1
      End Select
    Case 2
      Select it+1
        Case 1 ;Join Lines
      End Select
  End Select
Return

.KeyResponse:
  WindowInput winNo
  If Inkey$=Chr$(139) Then Gosub DoHelp
Return

.TidyWindow:
  Select winNo
    Case 1 ;
    Case 2 ;
      Gosub AboutDraw
    Case 3 ;
      Gosub HelpDraw
  End Select
Return

.AboutDraw:
  Use Window 2
  WLocate 0,2:WColour shade{2},0
  NPrint String$(" ",12),"ASCIItoKW3 v1.00."
  NPrint "":WColour shade{1}
  NPrint String$(" ",14),"By Carl Read."
  NPrint String$(" ",8),"Released 29th March 1995."
  NPrint "":WColour shade{2}
  NPrint " ASCIItoKW3 is  non-copyrighted  public"
  NPrint " domain software. Spread it, modify it,"
  NPrint " (it's written in Blitz Basic 2 and the"
  NPrint " code is PD too),  and  generally  feel"
  NPrint " free to do whatever you want with it."
  NPrint "":WColour shade{1}
  NPrint String$(" ",15),"Carl Read"
  NPrint String$(" ",15),"CyberCraft"
  NPrint String$(" ",10),"PO Box 4032 Mayfair"
  NPrint String$(" ",13),"Hastings 4201"
  NPrint String$(" ",14),"NEW ZEALAND"
Return

.DoHelp:
  SizeLimits 100,30,542,170
  Window 3,49,24,542,170,15|$10|$20|$400|$1000,"ASCIItoKW3 Help.",2,1,2
  SetMenu 0
  Gosub HelpDraw
Return

.HelpDraw:
  Use Window 3
  WLocate 0,2:WColour shade{2},0
  NPrint "          ASCIItoKW3 v1.00.  By Carl Read - CyberCraft."
  NPrint "":WColour shade{1},0
  NPrint "     This program takes an ascii text file and  converts  it  to"
  NPrint " Kindwords3 format. Kindwords3 can load ascii files  -  but  not"
  NPrint " very well. It links all lines  of  text  in  one  large  chunk,"
  NPrint " ignoring paragraphs and joining some words together. ASCIItoKW3"
  NPrint " hopefully improves on this. However, YOU USE  THIS  PROGRAM  AT"
  NPrint " YOUR OWN RISK! It's a hack - and may not  produce  a  perfectly"
  NPrint " correct file. I take no responsibility for  anything  bad  that"
  NPrint " that may happen to anyone  who  uses  this program."
  NPrint "     With the menu option `Join Lines'  unselected,  no  attempt"
  NPrint " will be made to join lines into paragraphs. With `ASCII Output'"
  NPrint " selected, files are saved in ascii format and not KW3 format."
  NPrint "     Future: Better docs for a  start!  Also,  better  paragraph"
  NPrint " creation is needed."
  NPrint "     See `About' in the menus if you wish to get in touch."
Return

.AsciiOpen:
  prompt$="Load ASCII file."
  path$=ldPath$:fi$=ldFi$:noFiCheck=0:Gosub FiRequest
  If ldError=0
    ldPath$=path$:ldFi$=fi$
    Gosub AsciiLoad
  Else
    Gosub SelMen
  EndIf
Return

.AsciiLoad:
  a$=ldFi$
  If ldPath$<>""
    If Right$(ldPath$,1)<>":" Then a$="/"+a$
  EndIf
  a$=ldPath$+a$
  errFlag=0:pError=0
  SetErr
    errFlag=1
  End SetErr
  fiSize=Exists(a$)
  If errFlag=0 AND fiSize>0
    If ReadFile(0,a$)
      ;Clear out previous strings.
      For n=0 To paras:text$(n)="":Next
      ;Load file.
      paras=0:po=0:c$="":WColour shade{2},0
      Flag10=0:Flag13=0
      For long=1 To fiSize
        Gosub ChEv
        FileInput 0
        b$=Inkey$
        If b$=Chr$(10) OR b$=Chr$(13)
          If b$=Chr$(10) AND Flag13=1 Then Flag13=2
          If b$=Chr$(13) AND Flag10=1 Then Flag13=2
          If Flag13<>2
            If b$=Chr$(10)Then Flag10=1
            If b$=Chr$(13)Then Flag13=1
            text$(paras)=c$:po$=c$:Gosub PrOut
            c$="":paras+1
            If paras=32000
              long=fiSize:pError=1
              errMes1$="Too many lines of text!"
              errMes2$="No more than 32000 allowed."
              Gosub ErrMessage
              Gosub SelMen
            EndIf
          EndIf
          If Flag13=2 Then Flag13=0:Flag10=0
        Else
          c$+b$
        EndIf
      Next
      If c$<>"" AND pError=0
        text$(paras)=c$:paras+1:po$=c$:Gosub PrOut
      EndIf
      ;Remove leading spaces if all lines have some.
      Use Window 0:paraJoin=MenuChecked(0,1,0)
      If paraJoin=-1 AND pError=0
        WColour shade{1},0
        a=-1
        For n=0 To paras-1
          Gosub ChEv
          ;text$(n)=Replace$(text$(n),Chr$(13),"")
          po$=text$(n):Gosub PrOut
          If text$(n)<>""
            If Left$(text$(n),1)=" " AND (a=-1 OR a>1)
              For m=1 To Len(text$(n))
                If Mid$(text$(n),m,1)<>" "
                  b=m-1:m=Len(text$(n))
                EndIf
              Next
              If a=-1
                a=b
              Else
                If b<a Then a=b
              EndIf
            Else
              a=0
            EndIf
          EndIf
        Next
        WColour 3,0
        If a>0
          For n=0 To paras-1
            Gosub ChEv
            text$(n)=UnRight$(text$(n),a):po$=text$(n):Gosub PrOut
          Next
        EndIf
      EndIf
      ;Process text.
      WColour shade{2},0
      If paras>1 AND paraJoin=-1 AND pError=0
        a=0:b=0:Use Window 0
        While a<paras
          Gosub ChEv
          If text$(a)=""
            text$(b)="":a+1:b+1:po$="":Gosub PrOut
          Else
            text$(b)=text$(a):a+1
            If a<paras
              c=0
              While c=0
                Gosub ChEv
                If text$(a)="" OR Left$(text$(a),1)=" "
                  c=1:po$=text$(b):Gosub PrOut:b+1
                Else
                  If Right$(text$(b),1)<>" "
                    text$(b)+" "
                  EndIf
                  text$(b)+text$(a):a+1
                  If a=paras
                    c=1:po$=text$(b):Gosub PrOut:b+1
                  EndIf
                EndIf
              Wend
            Else
              po$=text$(b):Gosub PrOut:b+1
            EndIf
          EndIf
        Wend
        If b<paras
          For n=b To paras-1:text$(n)="":Next:paras=b
        EndIf
      EndIf
      ;Compute file size.
      WColour shade{1},0
      If pError=0
        fiSize=paras*8+558
        For n=0 To paras-1
          Gosub ChEv
          po$=text$(n):Gosub PrOut
          fiSize+Len(text$(n))+(Len(text$(n))AND 1)
        Next
        svFi$=ldFi$
        If Len(svFi$)>4
          If Right$(svFi$,4)=".asc"
            svFi$=UnLeft$(svFi$,4)
          Else
            svFi$=Left$(svFi$,21)
          EndIf
        EndIf
        svFi$+".KW3"
        If svPath$="" Then svPath$=ldPath$
        MenuState 0,0,1,On
        Gosub KW3Save
      EndIf
    Else
      Gosub LdingProblems
    EndIf
    CloseFile 0
  Else
    Gosub LdingProblems
  EndIf
  ClrErr
  Use Window 0
Return

.LdingProblems:
  errMes1$="Problems loading file!"
  errMes2$="Processing canceled."
  Gosub ErrMessage
  Gosub SelMen
Return

.SvingProblems:
  errMes1$="Problems saving file!"
  errMes2$="Saving canceled."
  Gosub ErrMessage
Return

.PrOut:
  Use Window 0
  WLocate 0,po*8+2
  NPrint Left$(po$+String$(" ",79),79)
  NPrint String$(" ",79)
  po$="":po+1:If po=20 Then po=0
Return

.KW3Save:
  prompt$="Saving as a KW3 file."
  asciiFlag=MenuChecked(0,1,1)
  path$=svPath$:fi$=svFi$
  If asciiFlag=-1 Then fi$=".asc":prompt$="Saving as an ASCII file."
  noFiCheck=1:Gosub FiRequest
  If ldError=0
    svPath$=path$:svFi$=fi$
    Gosub KSave
  EndIf
  Gosub SelMen
Return

.SelMen:
  Use Window 0:WColour shade{2},0
  po$=String$(" ",29)+"Select a menu option.":Gosub PrOut
Return

.KSave:
  a$=svFi$
  If svPath$<>""
    If Right$(svPath$,1)<>":" Then a$="/"+a$
  EndIf
  a$=svPath$+a$
  If paras>0
    errFlag=0
    SetErr
      errFlag=1
    End SetErr
    If errFlag=0
      If WriteFile(0,a$)
        FileOutput 0
        If asciiFlag=0
          Restore KW3Dta
          For n=1 To 4:Read a:Print Chr$(a):Next
          Print Mkl$(fiSize)
          For n=1 To 426:Read a:Print Chr$(a):Next
          For n=0 To paras-1
            Print Chr$(87)+Chr$(84)+Chr$(88)+Chr$(84)
            long=Len(text$(n)):Print Mkl$(long)
            Print text$(n)
            If (Len(text$(n))AND 1)=1 Then Print Chr$(0)
          Next
          For n=1 To 132:Read a:Print Chr$(a):Next
        Else
          For n=0 To paras-1
            NPrint text$(n)
          Next
        EndIf
      Else
        Gosub SvingProblems
      EndIf
      CloseFile 0
    Else
      Gosub SvingProblems
    EndIf
    ClrErr
  Else
    errMes1$="Nothing to save!"
    errMes2$=""
    Gosub ErrMessage
  EndIf
  Use Window 0:WColour shade{2},0
Return

.CloseResponse:
  Select winNo
    Case 1
    Case 2
      Free Window 2
    Case 3
      Free Window 3
  End Select
Return

.GadResponse:
  gad=GadgetHit
  Select winNo
     Case 1
     Case 2
       Free Window 2
     Case 3
       Free Window 3
  End Select
Return

.FiRequest:
  Use Screen 0: ShowScreen 0
  MaxLen a$=250:MaxLen b$=192
  a$=path$:b$=fi$:If a$+b$="" Then a$="RAM:"
  ldError=0
  SetErr
    ldError=1
  End SetErr
  If ldError=1 Then ClrErr:Return
  If ExecVersion<36
    pathFi$=FileRequest$(prompt$,a$,b$)
  Else
    pathFi$=ASLFileRequest$(prompt$,a$,b$,"",164,13,312,DispHeight-16)
  EndIf
  If pathFi$="" Then ldError=1:ClrErr:Return
  If Exists(pathFi$)=0 AND noFiCheck=0
    errMes1$="That file doesn't exist"
    errMes2$="or is empty."
    Gosub ErrMessage
    ldError=1
  Else
    ldError=0:a$=pathFi$:Gosub StripFile
    If b$+c$=""
      path$="":fi$=a$
    Else
      path$=b$:fi$=c$
    EndIf
  EndIf
  ClrErr
Return

StripFile:
  b$="":c$=""
  For n=Len(a$) To 1 Step-1
    If Mid$(a$,n,1)="/"
      b$=Left$(a$,n-1):c$=Mid$(a$,n+1):n=1
    Else
      If Mid$(a$,n,1)=":"
        b$=Left$(a$,n):c$=Mid$(a$,n+1):n=1
      EndIf
    EndIf
  Next
Return

.ErrMessage:
  Window 1,20,24,312,70,14|$10|$20|$400|$1000,"Error Message.",2,1,0
  SetMenu 0:WColour 3,0
  NPrint""
  NPrint Left$(String$(" ",18),18-Len(errMes1$)/2)+errMes1$
  NPrint""
  NPrint Left$(String$(" ",18),18-Len(errMes2$)/2)+errMes2$
  FlushEvents
  aa=0
  While aa=0
    ev=Event
    If ev>0
      If EventWindow=1
        gad=GadgetHit
        If (ev AND $20)=$20 AND gad=1 Then aa=1
        If (ev AND $200)=$200 Then aa=1
      EndIf
    EndIf
  Wend
  Free Window 1:FlushEvents:ev=0
Return

.KW3Dta:
  ;First block of data.
  Data 70,79,82,77
  ;Four byte total file length (minus 8) inserted here.
  ;Second block of data.
  Data 87,79
  Data 87,79,87,86,82,78,0,0,0,8
  Data 0,0,0,1,0,0,0,0,87,70
  Data 78,84,0,0,0,10,1,0,0,8
  Data 116,111,112,97,122,0,87,73,78,70
  Data 0,0,0,184,0,0,0,0,0,0
  Data 0,0,0,0,0,0,0,0,0,0
  Data 0,0,0,0,0,0,0,0,0,0
  Data 0,0,0,0,0,0,0,0,0,0
  Data 0,0,0,0,0,0,0,0,0,0
  Data 0,0,0,0,0,0,0,0,0,0
  Data 0,0,0,0,0,0,0,0,0,0
  Data 0,0,0,0,0,0,0,0,0,0
  Data 0,0,0,0,0,0,0,0,0,0
  Data 0,0,0,0,0,0,0,0,0,0
  Data 0,0,0,0,0,0,0,0,0,0
  Data 0,0,0,0,0,0,0,0,0,0
  Data 0,0,0,0,47,117,147,236,0,0
  Data 0,0,0,0,0,0,0,0,0,0
  Data 0,0,0,0,0,0,0,0,0,0
  Data 0,0,0,0,0,0,0,0,0,0
  Data 0,0,0,0,0,0,0,0,0,0
  Data 0,1,0,0,0,0,0,0,0,193
  Data 0,0,0,0,0,0,0,0,87,68
  Data 79,67,0,0,0,54,0,1,0,13
  Data 0,12,21,192,0,12,21,192,0,0
  Data 84,96,0,0,0,0,0,0,140,160
  Data 0,0,56,64,0,0,56,64,0,0
  Data 0,0,1,0,0,0,0,0,2,0
  Data 0,1,0,0,0,46,0,0,0,0
  Data 87,83,77,68,0,0,0,54,0,2
  Data 0,0,10,170,0,0,15,255,5,156
  Data 15,0,12,151,0,13,15,112,0,207
  Data 15,15,15,208,1,196,9,142,7,102
  Data 6,8,13,203,0,60,0,72,1,1
  Data 1,0,1,0,0,0,0,0,0,0
  Data 0,0,87,80,70,48,0,0,0,20
  Data 0,15,0,0,0,0,0,0,0,0
  Data 0,0,0,0,0,0,0,0,0,0
  Data 87,80,65,82,0,0,0,28,0,0
  Data 84,96,0,0,84,96,0,0,0,0
  Data 0,0,140,160,0,2,0,0,0,1
  Data 0,0,1,2,0,124,87,84,65,66
  Data 0,0,0,0
  ;Paragraphs are to be inserted here.
  ;  Each paragraph consists of four bytes (87,84,88,84) followed
  ;  by four bytes giving the paragraph length and then the
  ;  paragraph itself. If the paragraph length is an uneven
  ;  number then a zero byte is added also.
  ;Last block of data.
  Data 87,72,69,68,0,0,0,6
  Data 3,1,0,0,0,0,87,80,65,82
  Data 0,0,0,28,0,0,84,96,0,0
  Data 84,96,0,0,0,0,0,0,140,160
  Data 0,2,0,0,0,1,0,0,1,2
  Data 0,124,87,84,65,66,0,0,0,0
  Data 87,84,88,84,0,0,0,0,87,70
  Data 79,84,0,0,0,6,3,1,0,0
  Data 0,0,87,80,65,82,0,0,0,28
  Data 0,0,84,96,0,0,84,96,0,0
  Data 0,0,0,0,140,160,0,2,0,0
  Data 0,1,0,0,1,2,0,124,87,84
  Data 65,66,0,0,0,0,87,84,88,84
  Data 0,0,0,0



