'===========================================================================
' Subject: FILE SPLITTER                      Date: 04-30-00 (00:18)       
'  Author: Mesut Akcan                        Code: RAPIDQ                 
'  Origin: makcan@SoftHome.net              Packet: RAPIDQ.ABC
'===========================================================================
' QFileSplitter.bas for RapidQ
' Coded by Mesut AKCAN
' updated 22 April 2000
' http://kaynak.cjb.net
' http://makcan.virtualave.net

$apptype gui
$typecheck on 
$INCLUDE "RAPIDQ.INC"

declare sub ExitClick
declare sub AboutClick
declare sub sourceselect
declare sub e2change
declare sub split
declare sub targetSelect
declare function fPath(fPFN as string)as string
''
declare sub OKclick
declare sub CANCELclick

'----------- FORM ---------
CREATE Form AS QFORM
    Center
    Caption = "QFileSplitter v1.0"
    Width = 315
    Height = 265
	create Mainmenu as qmainmenu 	
		create mnuFile as qmenuitem
			caption="&File"
			create mnuSoSel as qmenuitem
				caption="S&ource Select"
				onclick = sourceselect
			end create
			create mnuTarSel as qmenuitem
				caption="&Target Select"
				onclick = targetselect 
			end create
			create mnuSpl as qmenuitem
				caption = "&Split"
				enabled = False
				onclick = split
			end create
			create mnuBrk as qmenuitem
				caption="-"
			end create
			create mnuExit as qmenuitem
				caption="E&xit"
				onclick = ExitClick
			end create
	
		end create
		create mnuHelp as qmenuitem
			caption = "&Help"
			create mnuAbout as qmenuitem
				caption ="&About"
				onclick  = AboutClick
			end create
		end create
	end create
    CREATE Label3 AS QLABEL
        Caption = "Split size(bytes) :"
        Left = 8
        Top = 172
        Width = 100
    END CREATE
    CREATE Label5 AS QLABEL
        Left = 119
        Top = 192
        Transparent = 1
    END CREATE
    CREATE GroupBox1 AS QGROUPBOX
        Caption = "SOURCE"
        Left = 4
        Top = 4
        Width = 292
        Height = 80
        TabOrder = 2
        CREATE Label1 AS QLABEL
            Caption = "Path / File :"
            Left = 7
            Top = 18
            Width = 55
            Transparent = 1
        END CREATE
        CREATE Label2 AS QLABEL
            Caption = "File Size"
            Left = 7
            Top = 58
            Transparent = 1
        END CREATE
        CREATE Edit1 AS QEDIT
            Text = ""
            Left = 7
            Top = 35
            Width = 239
        END CREATE
        CREATE Button1 AS QBUTTON
            Caption = "..."
            Left = 250
            Top = 33
            Width = 29
            TabOrder = 1
		onclick = sourceSelect
        END CREATE
    END CREATE
    CREATE Edit2 AS QEDIT
        Text = "1457152"
        Left = 9
        Top = 187
        Width = 101
	  onchange = e2change
    END CREATE
    CREATE Button2 AS QBUTTON
        Caption = "&Split File"
        Left = 222
        Top = 181
        TabOrder = 1
	  enabled=false
	  onclick = split
    END CREATE
    CREATE GroupBox2 AS QGROUPBOX
        Caption = "TARGET"
        Left = 6
        Top = 89
        Width = 291
        Height = 71
        TabOrder = 3
        CREATE Label4 AS QLABEL
            Caption = "Path :"
            Left = 7
            Top = 18
            Width = 55
            Transparent = 1
        END CREATE
        CREATE Edit3 AS QEDIT
            Text = ""
            Left = 7
            Top = 35
            Width = 239
        END CREATE
        CREATE Button3 AS QBUTTON
            Caption = "..."
            Left = 250
            Top = 33
            Width = 29
            TabOrder = 1
		onclick = targetSelect
        END CREATE
    END CREATE
END CREATE
'----------- FORM2 ---------
CREATE Form2 AS QFORM
    Caption = "Select a Folder"
    Width = 320
    Height = 330
    Center
    CREATE btnOK AS QBUTTON
        Caption = "&OK"
        Left = 223
        Top = 270
		onclick = OKclick
    END CREATE
    CREATE btnCANCEL AS QBUTTON
        Caption = "&Cancel"
        Left = 138
        Top = 270
        TabOrder = 1
		onclick = CANCELclick
    END CREATE
	create dtree as qdirtree
		top = 5
		left = 5
		width = 300
		height = 260
	end create
END CREATE


dim fsize as long , splsize as long , rmn as long 
dim spl as integer
dim file as QFileStream , nfile as QFileStream
dim crLF as string , c34 as string
crLF = chr$(13) + chr$(10)
c34  = chr$(34)

'--------------------------------------------------------------
sub split
 DIM memory AS QMemoryStream
 dim n as integer , poz as long
 dim sPF as string 	'source path/file
 dim tPF as string 	'target path
 dim sFName as string 	'source file name
 dim tFName as string 	'target file name
 dim tFull as string , fBAT as string , a as string
 dim bf as string
 sPF = edit1.text 	'source file pathname
 tPF = edit3.text 	'target file pathname
 sFName = sPF - fPath(sPF) 'source file name
 IF file.open(sPF , fmOpenRead) = FALSE then 'open source file
	showmessage "Problem with reading " + sPF : EXIT SUB
 END IF
 memory.CopyFrom(file, fsize)
 file.close
 fBAT = "@echo off" + crLF
 a = ""
 for n = 0 to  spl-1
	poz = n * splsize
	memory.Position = poz
	if n = spl-1 then splsize = fsize - poz 'remain
    	tFName = sFName + "." + right$ (("00" + str$(n)) , 3) ' target filename
	a = c34 + sFName + c34 + "+" 
	if n=0 then a=""
	fBAT = fBAT + "copy /b " + a + c34 + tFName + c34 + " " + c34 + sFName + c34 + crLF		
	tFull  = tPF + tFName 	'target file full path name
	poz = n * splsize
	IF nfile.open(tFull , fmCreate) = FALSE then  'create target file
		showmessage "Problem with creating " + tFull : EXIT SUB
	END IF
	nfile.close
	IF nfile.open(tFull , fmOpenWrite)=FALSE then  'open target file for write
		showmessage "Problem with writing " + tFull : EXIT SUB
	END IF
	nfile.CopyFrom(memory, splsize)   ' write target file
	nfile.close
 next
 memory.close
 bf =  tPF + "merge_" + sFName + ".bat" 	 ' batch file name
 IF file.open(bf , fmCreate) = FALSE then  ' create bat file
	showmessage "Problem with creating " + bf
 	EXIT SUB 
 END IF 
 file.close
 if file.open(bf, fmOpenReadWrite) = FALSE then  ' if error
 	showmessage "Problem with writing " + bf
	EXIT SUB
 END IF 
 fBAT = fBAT + "REM This file was created by QFileSplitter" + crLF + "REM http://kaynak.cjb.net"
 file.WriteStr(fBAT,len(fBAT))      ' write to batch file
 file.close
 showmessage sFName + " SPLITTED !" ' OK.
end sub

sub sourceSelect
  DIM OpenDialog AS QOpenDialog
  dim FName as string
  IF OpenDialog.Execute THEN
	edit1.text 	= OpenDialog.FileName
	FName 	= edit1.text
 	IF file.open(FName , fmOpenReadWrite) = FALSE then 'open source file
		showmessage "Problem with reading " + FName : EXIT SUB
	END IF
	fsize	= file.size
	file.close
	label2.caption = str$(fsize) + " bytes"
	Edit3.text = fpath(FName)
  END IF
  CALL e2change
end sub

sub e2change
	splsize = val(edit2.text)
	spl = int(fsize/splsize)
	if splsize >= fsize then 
		button2.enabled  = false
		mnuSpl.enabled = false
		label5.caption=""
	else 
		button2.enabled  = true
		mnuSpl.enabled = true
		rmn = fsize mod splsize
		spl = int(fsize/splsize)
		if rmn>0 then spl++
		label5.caption = str$(spl) + " split files"
	end if
end sub

function fPath(fPFN as string)as string
	dim poz as integer
	dim lastpoz as integer
	do
		poz = instr(lastpoz + 1 , fPFN,"\" )
		if poz=0 then
			exit do
		end if
		lastpoz = poz
	loop
	fPath = left$(fPFN,lastpoz)
end function

sub targetSelect
  if edit3.text then 
	dtree.initialdir = edit3.text 
  else
	dtree.initialdir = CURDIR$ 
  end if
  form2.showmodal
end sub

sub OKclick
  edit3.text= dtree.Directory
  form2.close
end sub

sub CANCELclick
  form2.close
end sub 

sub ExitClick
	form.close
end sub

sub AboutClick
	showmessage "QFileSplitter v1.0" + crLF + "Programmed by Mesut AKCAN" + crLF _
		+ "web: http://kaynak.cjb.net" + crLF + "mail: makcan@softhome.net"
end sub

'**************

Form.ShowModal
