Code Formatter tool?

Miscellaneous Forums/General Discussion/Code Formatter tool?

Hi!

Is there a freeware tool that can format a bmx source code?

After I switched back to Windows XP my source code alignment got lost. :( - Therefore I'm looking for an easy way to reformat my code.

Thanks!
Grisu

Here's some old B2D code...

dropped$ = Lower$(Replace$(CommandLine$(), Chr$(34), ""))

If FileType(dropped$) <> 2 Then RuntimeError "'" + dropped$ + "' is Not a directory"

dir = ReadDir(dropped$)

bb_file = 0
c = 0
op$ = "*/+-=><"

file$ = NextFile$(dir)

While file$ <> ""
	indent = 0
	if_flag = 0
	case_flag = 0
	select_flag = 0
	out$ = ""
		
	If Lower$(Right$(file$, 3)) = ".bb"
		bb_file = bb_file + 1
		
		in = ReadFile(dropped$ + "" + file$)
		If (Not in) Then RuntimeError "Couldn't open '" + file$ + "'"
		
		out$ = Replace(out$, "End If", "EndIf")
		
		While (Not Eof(in))
			lin$ = Trim$(ReadLine$(in)) + Chr$(13) + Chr$(10)
			l$ = Left$(lin$, 1)

			Select l$
				Case ""
									
				Case ";"
										
				Default
					Select command$(lin$)
						Case "If"
							If Instr(lin$, " Then ") = 0
								out$ = out$ + String$(Chr$(9), indent) + lin$ 
								indent = indent + 1
								if_flag = if_flag + 1
							EndIf

						Case "For", "Repeat", "While", "Function", "Type"
							out$ = out$ + String$(Chr$(9), indent) + lin$ 
							indent = indent + 1
							
						Case "Select"
							out$ = out$ + String$(Chr$(9), indent) + lin$ 
							indent = indent + 1
							select_flag = 1

						Case "Case"
							If (case_flag = 0) Or (select_flag > 0)
								out$ = out$ + String$(Chr$(9), indent) + lin$
								indent = indent + 1
								case_flag = case_flag + 1
								select_flag = 0
							Else
								out$ = out$ + String$(Chr$(9), indent - 1) + lin$
							EndIf
						
						Case "Default"
							If case_flag
								out$ = out$ + String$(Chr$(9), indent - 1) + lin$
							EndIf

						Case "Else", "ElseIf"
							If if_flag > 0
								out$ = out$ + String$(Chr$(9), indent - 1) + lin$
							EndIf
							
						Case "EndIf"
							If if_flag > 0
								if_flag = if_flag - 1
								If indent > 0 Then indent = indent - 1
								out$ = out$ + String$(Chr$(9), indent) + lin$
							EndIf
							
						Case "Next", "Until", "Wend", "End"
							If command$(lin$, 5) = "Select"
								If case_flag > 0
									case_flag = case_flag - 1
									If indent > 0 Then indent = indent - 1
								EndIf
								
								select_flag = 0
							EndIf
								
							If indent > 0 Then indent = indent - 1
							out$ = out$ + String$(Chr$(9), indent) + lin$
						
						Default
							out$ = out$ + String$(Chr$(9), indent) + lin$
					End Select
			End Select
		Wend		
		
		CloseFile in
		
		out$ = Replace(out$, ",", ", ")
		For c=1 To Len(op$)
			out$ = Replace(out$, Mid$(op$, c, 1), " " + Mid$(op$, c, 1) + " ")
		Next
		
		For c=10 To 2 Step -1
			out$ = Replace(out$, String$(" ", c), " ")
		Next
		
		out$ = Replace(out$, ">  =", ">=")
		out$ = Replace(out$, "<  =", "<=")
		out$ = Replace(out$, "<  >", "<>")
		
		outfile = WriteFile(dropped$ + "" + file$ + ".txt")
		
		For c=1 To Len(out$)
			WriteByte outfile, Asc(Mid$(out$, c, 1))
		Next
		
		CloseFile outfile
			
		Print "Processed : '" + file$ + "'"
	EndIf	
	
	file$ = NextFile$(dir)
Wend

CloseDir(dir)

Print
Print "Total Files : " + bb_file

WaitKey()

End

Function command$(in$, start=1)
	Local c, out$, ch
	
	For c=start To Len(in$)
		ch = Asc(Mid$(in$, c, 1))
		If ch > 31
			If ch = 32   
				Return out$
			Else
				out$ = out$ + Chr$(ch)
			EndIf
		EndIf
	Next
	
	Return out$
End Function


...Should be easy enough to convert to/for BMax.

Reinstalled B3D, oh boy this wasn't on my hdd for a long time. - Anyway, the code crashes on the replace function.

I don't need a fast/fancy runtime code. Just one that works.

Everything is better than doing it by hand for 10.000 lines and more... :(

You could always write one shouldn't be to hard.

If not I am pretty sure there is some stuff in the Code Archives about doing it.

I haven't got a clue what you're doing there Grisu, but I just tried that code on an old B3d project (13 files totalling about 18,000 lines of code) and it worked fine?

Anyway, that's besides the point as it was intended as a base for you to create your own version for BMax.

B2D / B3D Only... But should be easy enough to blitzmaxilise it.

http://www.blitzbasic.com/codearcs/codearcs.php?code=1229

Thanks guys!

What Im doing is to compile the codes given and use the exe on my bmx project file, which doesn't work for me.

Seems like I have to reinvent the wheel...

Why would formatting be lost? Is it because of different end-of-line conventions?

If so you could try a unix2dos utility. That just looks for the LF character and inserts a CR.