2tabs

For discussions about programming and projects not necessarily associated with Porteus.
User avatar
bigbass
Contributor
Contributor
Posts: 151
Joined: 13 Jan 2012, 14:35
Distribution: slackware 14

2tabs

Post#1 by bigbass » 29 Nov 2012, 16:25

I wrote this because many bbcode forums replace or remove spacing in code
and even patebin has problems too
so in a nut shell this converts spaces to tabs then auto justifies indentation

The automatic justify indentation part was quite some work but does the job correctly
since BaCon compiles to C code even though you write in Basic
the result is as if you wrote everything in C so fast code
is produced

this makes a compiled binary called 2tabs
and you call it from the command line
2tabs the-path-and-name-of-file
example 2tabs /root/myapp.txt

I will upload the compiled binary for porteus 1.2 and 2 later but here is the source code

2tab for slackware 14 or Porteus 2 place this in /usr/sbin its a compiled binary if you want to see what it does
the source code is provided also
http://bigbass-porteus.googlecode.com/files/2tabs


what happens it makes you a new file with your corrected code with a .2tabs added to the file name (so its 100% safe your original file is unedited)
this file is what you use to paste into any bbcode forum if you want to preserve indentation and it will also fix any indentation mistakes you made automatically this can be used for any file format from plain text to code :D

here is a test file
http://www.puppy2.org/slaxer/indent-test.txt

you would run it like this

Code: Select all

2tabs /root/Downloads/indent-test.txt
the result will look like this

Code: Select all

this is some text with that  has 1 leading space 
this is some text with that  has 2 leading spaces 
	this is some text with that  has 3 leading spaces 
	this is some text with that  has 4 leading spaces 
	this is some text with that  has 5 leading spaces 
	this is some text with that  has 6 leading spaces 
		this is some text with that  has 7 leading spaces 
		this is some text with that  has 8 leading spaces 
		this is some text with that  has 9 leading spaces 
		this is some text with that  has 10 leading spaces 
			this is some text with that  has 11 leading spaces 
			this is some text with that  has 12 leading spaces 
			this is some text with that  has 13 leading spaces 
			this is some text with that  has 14 leading spaces 
				this is some text with that  has 15 leading spaces 
				this is some text with that  has 16 leading spaces 
				this is some text with that  has 17 leading spaces 
				this is some text with that  has 18 leading spaces 
					this is some text with that  has 19 leading spaces 
					this is some text with that  has 20 leading spaces 
					this is some text with that  has 21 leading spaces 
					this is some text with that  has 22 leading spaces 
						this is some text with that  has 23 leading spaces 
						this is some text with that  has 24 leading spaces 
						this is some text with that  has 25 leading spaces 
						this is some text with that  has 26 leading spaces 
							this is some text with that  has 27 leading spaces 
							this is some text with that  has 28 leading spaces
							this is some text with that  has 29 leading spaces 
							this is some text with that  has 30 leading spaces 
								this is some text with that  has 31 leading spaces 
								this is some text with that  has 32 leading spaces 
								this is some text with that  has 33 leading spaces 
								this is some text with that  has 34 leading spaces     
									this is some text with that  has 35 leading spaces 
									this is some text with that  has 36 leading spaces 
									this is some text with that  has 37 leading spaces 
									this is some text with that  has 38 leading spaces 
										this is some text with that  has 39 leading spaces 
										this is some text with that  has 40 leading spaces 
										this is some text with that  has 41 leading spaces
										this is some text with that  has 42 leading spaces 
											this is some text with that  has 43 leading spaces 
											this is some text with that  has 44 leading spaces 
											this is some text with that  has 45 leading spaces 
											this is some text with that  has 46 leading spaces 
											this is some text with that  has 47 leading spaces 
											this is some text with that  has 48 leading spaces 
											this is some text with that  has 49 leading spaces 
											this is some text with that  has 50 leading spaces 
											this is some text with that  has 51 leading spaces 
											this is some text with that  has 52 leading spaces 
											this is some text with that  has 53 leading spaces 
											this is some text with that  has 54 leading spaces 
											this is some text with that  has 55 leading spaces 
											this is some text with that  has 56 leading spaces 
						



















notice I posted code in a bbcode form and the indentation is respected below
bacon as a dependency is only needed if you want to recompile the source code
it is not a run time dependency

Code: Select all


'--- ***************************************************** '
'--- PROGRAM: 2tabs.bac '
'--- PURPOSE: indent phpBB code '
'--- AUTHOR: big_bass Joe Arose '
'--- DEPENDS: gcc, bacon, '
'--- PLATFORM: linux '
'--- DATE: Nov-28-2011 replaced with TABS$ 7/14/2012'
'--- NOTES: will work on any text or code '
'--- LICENSE: GPL version 3 or later '
'--- *****************************************************'

'--- this is only for modifing of code with justified indentation
'--- USUAGE: ./2tabs /path-and-filename" ---'
'--- note the file will get a .2tabs added to the filename ---'
'--- on error print usage ---'
'--- this part was added to for variables to be passed at command line ---'


TRAP LOCAL
SPLIT ARGUMENT$ BY " " TO arg$ SIZE dim
	IF LEN(arg$[1]) EQ 0 THEN
		PRINT "Copy file to /tmp"
		PRINT "USUAGE: ./2tabs /path-and-filename"
	END IF

Tab$ = CHR$(9)
FOUR_BLANKS$ = SPC$(4)



'--- copy_this$ is the first argument passed from the command line ---'
'--- path-and-filename is arg$[1] this is a valid bacon argument ---'
'--- give it a string name for clarity ---'
copy_this$ = arg$[1]


'--- another code snippet from GatorDog thanks ---'
	IF ISFALSE(FILEEXISTS(copy_this$)) THEN
		PRINT NL$, "=>> ", copy_this$, " <<== not found."
		PRINT "Please check your directory for assistance ;>)", NL$
	END FALSE
	END IF

	
COPY copy_this$ TO CONCAT$(copy_this$,".2tabs")


'--- Rename working file to TMP ext.
RENAME CONCAT$(copy_this$,".2tabs") TO CONCAT$(copy_this$,".2tabsTMP")
OPEN CONCAT$(copy_this$,".2tabs") FOR WRITING AS my_outfile
OPEN CONCAT$(copy_this$,".2tabsTMP") FOR READING AS my_infile
	WHILE NOT(ENDFILE(my_infile))
	READLN txt$ FROM my_infile


	'--- pre step replace TABS$ with four spaces ---'
	txt$ = REPLACE$(txt$, Tab$, FOUR_BLANKS$)


	'--- check if line of text starts with spaces or TABS$ ---'
	IF REGEX(txt$, "^ ") OR REGEX(txt$, "^FOUR_BLANKS$") THEN
		PRINT "Yep, ", txt$, "--> starts with spaces !"
		PRINT
		PRINT
		full_count = LEN(txt$)

		'--- a chop left is used here to remove only left spaces ---'
		chopped_count = LEN(CHOP$(txt$," ",1))
		start_space_count = full_count - chopped_count

		'--- error checking ---'
		'--- blue is ok yellow is error with a limit of 43 spaces ---'
	IF start_space_count < 43 THEN
		PRINT "you have ";
		COLOR FG TO BLUE
		PRINT start_space_count," leading space(s)"
		COLOR RESET
	ELSE
		PRINT "you have ";
		COLOR FG TO YELLOW
		PRINT start_space_count," leading space(s)"
		COLOR RESET
	END IF


	'--- start main ---'
	IF start_space_count < 3 THEN
		PRINT "will chop off this < 3 spaced indented "
		PRINT CHOP$(txt$," ",1)
		WRITELN CHOP$(txt$," ",1) TO my_outfile
	END IF


	IF start_space_count >= 3 AND start_space_count <= 6 THEN
		PRINT "will shift you to four so it looks good "
		'--- pre step replace TABS$ with four spaces ---'

		WRITELN Tab$, CHOP$(txt$," ",1) TO my_outfile
	END IF


	IF start_space_count >= 7 AND start_space_count <= 10 THEN
		PRINT "will shift you to eight so it looks good "
		WRITELN Tab$, Tab$ ,CHOP$(txt$," ",1) TO my_outfile
	END IF


	IF start_space_count >= 11 AND start_space_count <= 14 THEN
		PRINT "will shift you to twelve so it looks good "
		WRITELN Tab$, Tab$ ,Tab$, CHOP$(txt$," ",1) TO my_outfile
	END IF


	IF start_space_count >= 15 AND start_space_count <= 18 THEN
		PRINT "will shift you to sixteen so it looks good "
		WRITELN Tab$, Tab$ ,Tab$ ,Tab$, CHOP$(txt$," ",1) TO my_outfile
	END IF


	IF start_space_count >= 19 AND start_space_count <= 22 THEN
		PRINT "will shift you to twenty so it looks good "
		WRITELN Tab$, Tab$, Tab$ ,Tab$ ,Tab$, CHOP$(txt$," ",1) TO my_outfile
	END IF


	IF start_space_count >= 23 AND start_space_count <= 26 THEN
		PRINT "will shift you to twenty four so it looks good "
		WRITELN Tab$ ,Tab$, Tab$ ,Tab$ ,Tab$ ,Tab$,CHOP$(txt$," ",1) TO my_outfile
	END IF


	IF start_space_count >= 27 AND start_space_count <= 30 THEN
		PRINT "will shift you to twenty eight so it looks good "
		WRITELN Tab$, Tab$ ,Tab$ ,Tab$, Tab$, Tab$ ,Tab$, CHOP$(txt$," ",1) TO my_outfile
	END IF


	IF start_space_count >= 31 AND start_space_count <= 34 THEN
		PRINT "will shift you to thirty two so it looks good "
		WRITELN Tab$, Tab$ ,Tab$ ,Tab$ ,Tab$, Tab$ ,Tab$,Tab$, CHOP$(txt$," ",1) TO my_outfile
	END IF


	IF start_space_count >= 35 AND start_space_count <= 38 THEN
		PRINT "will shift you to thirty six so it looks good "
		WRITELN Tab$, Tab$ ,Tab$ ,Tab$ ,Tab$ ,Tab$ ,Tab$ ,Tab$, Tab$, CHOP$(txt$," ",1) TO my_outfile
	END IF


	IF start_space_count >= 39 AND start_space_count <= 42 THEN
		PRINT "will shift you to forty so it looks good "
		WRITELN Tab$, Tab$ ,Tab$ ,Tab$ ,Tab$ ,Tab$, Tab$ ,Tab$ ,Tab$, Tab$ ,CHOP$(txt$," ",1) TO my_outfile
	END IF

	'--- if over 43 spaces force all 40 spaces ---'
	IF start_space_count > 42 THEN
		PRINT "ERROR more than 42 spaces indented you have " ;
		COLOR FG TO RED
		PRINT start_space_count ," indent limit 40 forced !"
		COLOR RESET
		WRITELN Tab$ ,Tab$ ,Tab$ ,Tab$ ,Tab$ ,Tab$ ,Tab$, Tab$ ,Tab$ ,Tab$,Tab$, CHOP$(txt$," ",1) TO my_outfile
	END IF


	ELSE
		PRINT "no leading spaces "
		PRINT txt$
	WRITELN txt$ TO my_outfile

	WEND
	END IF

	CLOSE FILE my_infile
	CLOSE FILE my_outfile
	DELETE FILE CONCAT$(copy_this$,".2tabsTMP")


PRINT
PRINT
PRINT "The indent conversion is done!"
PRINT copy_this$,".2tabs", " <--- is the modified file "