
' ---------------------------------------------------------------
' FILE FUNCTIONS
' ---------------------------------------------------------------

SUB AddFiles()
	DEF filenames[10000]:ISTRING
	DEF filter,filetemp:STRING
	INT pos, setpath=1

	SELECT Language
		CASE 2 ' Creative
			filter="Creative BASIC Source Files (*.cba)|*.cba||"
			filenames=FILEREQUEST("Add source file",d1,1,filter,"cba",@MULTISELECT, LastSourcePath[1])
		CASE 3 ' Aurora
			filter="Aurora Source Files (*.src)|*.src||"
			filenames=FILEREQUEST("Add source files",d1,1,filter,"src",@MULTISELECT, LastSourcePath[2])
		CASE 4 ' C/C++
			filter="C++ Source Files (*.cpp)|*.cpp|C Source Files (*.c)|*.c||"
			filenames=FILEREQUEST("Add source files",d1,1,filter,"cpp",@MULTISELECT, LastSourcePath[3])
		DEFAULT
			filter="IWB Source Files (*.iwb)|*.iwb|EBasic Source Files (*.eba)|*.eba||"
			filenames=FILEREQUEST("Add source files",d1,1,filter,"iwb",@MULTISELECT, LastSourcePath[0])
	ENDSELECT

	DO
		pos=instr(filenames,"|")
		IF pos THEN
			filetemp=left$(filenames,pos-1)
			filenames=mid$(filenames,pos+1)
			IF setpath=1 THEN
				LastSourcePath[Language-1]=filetemp
				_PathRemoveFileSpec(LastSourcePath[Language-1])
				setpath=0
			ENDIF
			REM do something with the file in filetemp
			Treeview2AddFile(filetemp)
		ENDIF
	UNTIL pos=0
ENDSUB

SUB ImportFile()
	DEF filename, filetemp:STRING
	DEF filter:STRING
	FILE myfile
	INT filenumber

	filter="Source Tree files|*.stf||"
	filename = FILEREQUEST("Import",d1,1,filter,"stf",0, GETSTARTPATH)
	IF len(filename) THEN
		OnClearAll()
	
		IF OPENFILE(myfile, filename, "R") = 0 THEN
			READ myfile, filenumber
			READ myfile, Language
			IF filenumber > 0 THEN
				for j = 1 TO filenumber
					READ myfile, filetemp
					Treeview2AddFile(filetemp)
				next j
			ENDIF
			
			CLOSEFILE myfile
		ENDIF
	ENDIF
ENDSUB

SUB ExportFile()
	DEF filename, filetemp, filter :STRING
	FILE myfile
	INT filenumber
	uint itemid
	pointer fdata : SETTYPE fdata, FILEDATA

	filter="Source Tree files|*.stf||"
	filename = FILEREQUEST("Export",d1,0,filter,"stf",0, GETSTARTPATH)
	IF len(filename) THEN
	
		IF OPENFILE(myfile, filename, "W") = 0 THEN
			filenumber = Treeview2GetSubItemsNumber(tvroot_files)
			WRITE myfile, filenumber
			WRITE myfile, Language
			IF filenumber > 0 THEN
				for j = 1 TO filenumber
					itemid = Treeview2GetSubItemId(tvroot_files, j)
					fdata = tvGetItemData(d1, TREEVIEW_2, itemid)
					filetemp = #fdata.fullpath
					WRITE myfile, filetemp
				next j
			ENDIF
			
			CLOSEFILE myfile
		ENDIF
	ENDIF
ENDSUB


SUB ExportPlainProc()
	TVFINDER tvf
	uint itemid, itemid2
	istring subname[MAX_LINE_SIZE] = ""
	FILE myfile
	INT itemnumber, subnumber, itemnumber2
	string filename = ""
	pointer fdata : SETTYPE fdata, FILEDATA
	
	IF OPENFILE(myfile, GETSTARTPATH+"stree.txt", "W") = 0 THEN
	
		' writing the functions of every file 
		itemnumber = Treeview2GetSubItemsNumber(tvroot_files)
		WRITE myfile,  "-=========================================-"
		WRITE myfile,  "        SOURCE TREE - EXPORT INFO"
		WRITE myfile,  "-=========================================-"
		WRITE myfile,  ""
		
		WRITE myfile,  "    FILES          "+ STR$( itemnumber )
		WRITE myfile,  ""
		IF itemnumber>0 THEN
			FOR j = 1 TO itemnumber
				itemid = Treeview2GetSubItemId(tvroot_files, j)
				tvGetItemText(d1, TREEVIEW_2, itemid, filename, 255)
				WRITE myfile,  "-====================================-"
				WRITE myfile,  "    File: " + filename
				WRITE myfile,  "-====================================-"
				WRITE myfile,  ""
				
				WRITE myfile, "    NUMBER         "+ STR$(j) + " -" + STR$(itemnumber)
				fdata = tvGetItemData(d1, TREEVIEW_2, itemid)
				IF fdata THEN WRITE myfile, "    PATH            " + #fdata.fullpath
				WRITE myfile, "    PROCEDURES     " + STR$( Treeview2GetSubItemsNumber(itemid) )
				WRITE myfile,  ""
				
				subnumber = Treeview2GetSubItemsNumber(itemid)
				IF subnumber>0 THEN
					FOR k = 1 TO subnumber
						itemid2 = Treeview2GetSubItemId(itemid, k)
						tvGetItemText(d1, TREEVIEW_2, itemid2, subname, MAX_LINE_SIZE)
						WRITE myfile,  "        <> " + subname
					NEXT k
				ENDIF
				WRITE myfile,  ""
				WRITE myfile,  ""
			NEXT j
			
			' writing all the functions
			IF itemnumber>1 THEN
				itemnumber2 = Treeview2GetSubItemsNumber(tvroot_proc2)
				WRITE myfile,  "-====================================-"
				WRITE myfile,  "    All Procedures"
				WRITE myfile,  "-====================================-"
				WRITE myfile,  ""
				WRITE myfile, "    PROCEDURES     " + STR$( itemnumber2 )
				WRITE myfile,  ""
				FOR t = 1 TO itemnumber2
					itemid = Treeview2GetSubItemId(tvroot_proc2, t)
					tvGetItemText(d1, TREEVIEW_2, itemid, subname, 255)
					WRITE myfile,  "        <> " + subname
				NEXT t
				WRITE myfile,  ""
				WRITE myfile,  ""
			ENDIF
		ENDIF

		' writing the procedure tree
		WRITE myfile,  "-=========================================-"
		WRITE myfile,  "             PROCEDURE TREE"
		WRITE myfile,  "-=========================================-"
		WRITE myfile,  ""
		WRITE myfile, "    ITEM COUNT     " + STR$( SENDMESSAGE(d1, TVM_GETCOUNT, 0, 0, TREEVIEW_4)-1 )
		WRITE myfile,  ""
		itemid = 0
		TvFinderStart(tvf, 0)
		DO
			IF itemid > 0 THEN
				' do something with itemid
				tvGetItemText(d1, TREEVIEW_4, itemid, subname, MAX_LINE_SIZE)
				WRITE myfile,  SPACE$(4*(tvf.nivel-1)) + subname
			ENDIF
			itemid = TvFinderFindNext(tvf)
		UNTIL (tvf.nivel<=0)
		
		CLOSEFILE myfile
		SYSTEM GETSTARTPATH+"stree.txt"
	ENDIF
ENDSUB

SUB ExportRichProc()
	MESSAGEBOX d1, "Not yet implemented", "Source Tree", @MB_ICONEXCLAMATION|@MB_OK
ENDSUB
