



." Format *.c  and *.cpp file " cr

."  2025-10-04" cr


: crlf" ( -- c-addr, u )
  0x0d char>"
  0x0a char>"  Join"
;

variable tab_count_modifier
variable tab_count
  
0 tab_count !

 variable case_flag
 0 case_flag  !

variable src_stream         \ source file stream id

variable dst_stream         \ destination file stream id


0x500 sizeof(char) * value sizeof(buffer)
create buffer
sizeof(buffer) 1 + allot
 
 
variable line_length
variable line_start 
variable line_end
variable type_leading 
variable dummy_line 
0 value front_char 
0 value last_char 

 variable fileCount
 variable lineCount
 variable charCount

: leading_rubbish
  line_length @
  IF
     begin

       line_start @ @char
       CASE
         0x09 OF  1 ENDOF
         0x20 OF  1 ENDOF
         0
       ENDCASE
       IF   sizeof(char) line_start +!    -1 line_length +!                false ELSE true THEN
       
       line_length @ IF ELSE drop true THEN 
       
       
     until
  THEN
;
: trailing_rubbish
  line_length @
  IF
 
    
        line_start @  line_length @  1 -   sizeof(char) *  +
    begin
      dup @char
      CASE

      bl  
      OF  
        dup 0 swap !char 
       sizeof(char) -   \ move ptr back one
       -1 line_length +!  \ decrement line length
       line_length @ 
      ENDOF

     0x09 
      OF  
        dup 0 swap !char 
       sizeof(char) -   \ move ptr back one
       -1 line_length +!  \ decrement line length
       line_length @ 
      ENDOF
      
      1
     ENDCASE
     
    UNTIL
    drop
  THEN
;



: get_last ( --   last char before EOL )
  line_start @ line_end !
  0 to last_char 
  begin
    line_end @ @char dup
    IF 
      to last_char
      sizeof(char) line_end +!
      false 
    ELSE 
      drop
       true
    THEN
  until
;

: send-to-file ( c-addr , u -- )

." -- " cr


  2dup 0 DO
    dup @char .hex sizeof(char) + 
    LOOP drop cr
    


    s">achar"
    
      2dup 0 DO
    dup @byte .hex 1 + 
    LOOP drop cr
  
 
   dst_stream @
   file=buffer
   drop
   

;
 

 : send-to-file ( c-addr , u -- )

 

    s">achar"
 
 
   dst_stream @
   file=buffer
   drop
   

;
 

: process ( c-addr , u --)
 

   CA: FileName
 
	FileName type cr
	0 case_flag ! 
   
   0 lv: byPass
   0 lv: origlen
   
   \ treat the input as standard characters
   \ write the output as standard characters
   \ if the OUTPUT should be in a different format then re-open and convert.
   
 fileCount @ 1 + fileCount !
 
	FileName     fam=ro  fam>cli    open-file swap src_stream ! 
 
	0= 
   IF
 
   
      s" out.txt"    fam=wo fam>bin     open-file drop dst_stream !
 
  
		0 dummy_line !
		0 tab_count ! 
		0 tab_count_modifier !
		BEGIN
    
         buffer sizeof(buffer)  src_stream @ read-cline 0=
			IF  ( not error )
         

				IF ( not EOF )
               dup to origlen
               line_length !
               buffer line_start !

               leading_rubbish  
               trailing_rubbish 
 
						s" /*FORMATOFF"    line_start @ over compare 0= 
                  if
                      1 to byPass
                  THEN 
						s" /*FORMATON"    line_start @ over compare 0= 
                  if
                      0 to byPass
                  THEN 
                  

                  
               line_length @ 	
					IF
						\ line length is > 0
					
						1 type_leading  !


                   lineCount @ 1 + lineCount !
                 


						\ Get first relevant character on a line
						line_start @ @char to front_char

						tab_count @ 0=
						IF
							\ the following leading strings FORCE the tab spacing to be ZERO
							s" void"      line_start @ over compare 0= 
							s" VOID"      line_start @ over compare 0= OR
							s" NTSTATUS"  line_start @ over compare 0= OR
							s" unsigned"  line_start @ over compare 0= OR
							s" BOOLEAN"   line_start @ over compare 0= OR
							s" int"       line_start @ over compare 0= OR
							s" char"      line_start @ over compare 0= OR
							s" FILE"      line_start @ over compare 0= OR
							s" #el"       line_start @ over compare 0= OR
							s" #end"      line_start @ over compare 0= OR
						
							IF  
								0  tab_count_modifier  ! 
							THEN
						
						ELSE

							s" #el" line_start @ over compare 0= 
							IF  -1  tab_count  +! THEN

							s" #end" line_start @ over compare 0= 
							IF   -1 tab_count  +! THEN
						THEN
						

					

						front_char [char] { = IF  0 tab_count_modifier ! THEN
						front_char [char] } = IF -1 tab_count +!         THEN  
						front_char [char] \ = If  0 type_leading !       THEN
 
						front_char [char] / = 
						line_start @ sizeof(char)  + @char [char] * =  AND 
						If tab_count @ 2  < IF 0 type_leading ! THEN THEN

						front_char [char] * = 
						line_start @ sizeof(char)  + @char [char] / =  AND 
						If tab_count @ 2 < IF 0 type_leading ! THEN THEN

						tab_count @ 0=
						If
							s" ;" line_start @ over compare 0= 
							If  0  tab_count_modifier  ! THEN
						THEN

						\ tab_count @ 0 < IF 0 tab_count ! cr  ." /* --++-- */" cr THEN
						\ tab_count @ 10 > IF 10 tab_count ! cr ." /* --++++ */" cr then

						
						\ display the leading SPACES for the line
						type_leading @
						IF  
							tab_count @   tab_count_modifier @ +  
							dup 0 > 
							IF       
								0 ?DO s"     " send-to-file  Loop 
							ELSE   
								drop 
							THEN 
 						THEN
 


						s" case "    line_start @ over compare 0= 
						s" default " line_start @ over compare 0= OR
						s" default:" line_start @ over compare 0= OR
						case_flag @ 0= AND
						If  1  tab_count  +!   1 case_flag ! THEN
 
 
 
 
 
 
						s" break " line_start @ over compare 0= 
						s" break;" line_start @ over compare 0=  OR
						IF 
							case_flag @ 
				 			If  -1 tab_count  +!  0 case_flag ! THEN
						THEN         

						line_start @ @char [char] { = IF  1 tab_count +!   THEN


						tab_count @ 0=
						If
							s" void"          line_start @ over compare 0= 
							s" VOID"          line_start @ over compare 0= OR
							s" int"           line_start @ over compare 0= OR
							s" unsigned"      line_start @ over compare 0= OR
							s" NTSTATUS"      line_start @ over compare 0= OR
							s" BOOLEAN"       line_start @ over compare 0= OR
							s" char"          line_start @ over compare 0= OR
							s" FILE"          line_start @ over compare 0= OR
							s" CELL"          line_start @ over compare 0= OR
							If  1  tab_count_modifier  ! THEN

							get_last

							s" #define" line_start @ over compare 0= 
							last_char [char] \ = and
							If  1  tab_count_modifier  ! THEN

							s" #if" line_start @ over compare 0= 
							If  1  tab_count_modifier  ! THEN

							s" #el" line_start @ over compare 0= 
							If  1  tab_count_modifier  ! THEN


							s" #end" line_start @ over compare 0= 
							If 0  tab_count_modifier  ! THEN
 
							tab_count_modifier @ 
							IF
								get_last last_char [char] ; = IF 0 tab_count_modifier ! THEN
							THEN
       
						ELSE     
							s" #if" line_start @ over compare 0= 
							If  1  tab_count  +! THEN

							s" #el" line_start @ over compare 0= 
							If  1  tab_count  +! THEN

						THEN
                  
                  0 line_length !
						line_start @  
                  BEGIN
                     dup @char
                  WHILE
                     line_length @ 1 + line_length !
                     sizeof(char) +
                  
                  REPEAT
                  drop
                  
                  charCount @ line_length @  + charCount !
                  
                  byPass  
                  IF
       
                     buffer origlen     send-to-file    
             
            
                  else
                  
                     line_start @  line_length @       send-to-file
                  THEN
                  
       \           crlf" send-to-file  cr
                  
						0 dummy_line !
					ELSE
						\ increment the empty line count
						1 dummy_line +!
  
					THEN 
					dummy_line @ 2 < IF crlf" send-to-file THEN
					false
				ELSE ( EOF )
					TRUE ( to halt REPEAT loop )   
				THEN
			ELSE ( Some sort of file error )
					drop drop
					TRUE ( to halt REPEAT loop )
			THEN
		UNTIL
		src_stream @ close-file drop

		dst_stream @ close-file drop
 
  
	 
    
	 
	 
	 	FileName delete-file drop
	    s" out.txt"  FileName  rename-file   if   ." RENAME FAILED "  cr abort then
	ELSE
		." File " fileName type  ."  not found" cr 
	THEN
  
 

;

  

: dir ( c-addr , u -- )
  file.find=open   dup
  begin                \ enter loop with ( search-id, search-id ) 
      dup -1 =   0=    \ test for no file found  (search-id, bool)
    while              \ jumps over the repeat if  bool  is zero/false
      dup              \ search-id  search-id 
      file.found@name  \ get name of found file (search-id, c-addr, u)  
      process          \ display name (search-id)
      file.find=next   \ go get next file (a new search-id)
  repeat
                       \ search-id  -1
  drop file.find=close \ give search resources back !
;
  s" *.c"    dir  
 