/* PROGRAM TABOUT 2.7 converts tabs to spaces usage : TABOUT.exe Myfile.TXT - expands tabs to spaces - trims off ^z Eof char - ensures each line ends in CrLf - trims trailing spaces of each line. */ /* Roedy Green Canadian Mind Products #101 - 2536 Wark Street Victoria, BC Canada V8T 4G8 tel:(250) 361-9093 mailto:roedyg@mindprod.com http://mindprod.com */ /* Windows console application. For Visual C++ Express options see http://mindprod.com/jgloss/cpp.html#COMPILING Version History: 2.0 1990-04-29 2.1 1993-06-08 embed new address and phone 2.2 1996-10-25 embed Quathiaski address. simplify code to create temporary workfile. simplify code to check extensions. new algorithm that does not have a maximum line length 2.3 1997-03-06 add *.SQL, *.HTM, *.INI, *.DLL file types 2.4 1998-11-08 embed Barker address 2.5 2008-02-03 convert from DOS to Windows to handle long file names use safe character library POSIX compliant display banner on all errors add icon, pad, docs 2.6 2009-03-12 add ANT build script 2.7 2010-01-16 build in more good/bad extensions */ /* The program will expand tabs from files with the following extensions: .ASM .PAS .BAT .CTL .CMD .LST .MAC .TXT .USE .KEY .ANS .C .H etc. The program will refuse to expand tabs from files with the following extensions: bad =.EXE .COM .OBJ etc. For all other extensions the program will prompt with a warning message ask and for confirmation before continuing. *.DOC are often MS-Word files - which must be left alone. Source and excutables may be freely used for any purpose except military. */ /* ==================================== */ #define Esc '\x1B' #define TabSpacing 8 #include /* exit, putenv, _splitpath */ #include /* fclose, fgetc, printf, remove, rename, setvbuf */ #include /* strcat, strcmp, strupr */ #include /* getch, putch */ #include /* isUpper isLower toUpper */ /* ==================================== */ /* use all global variables and no parameter passing for simplicity. */ static FILE *Before; /* input file containing no ^Z chars, except possibly at the end */ static FILE *After; /* output file with tabs expanded to tabs */ static char * BFilename; /* name of file we will convert */ static char * AFilename; /* name of the temporary output file */ /* ==================================== */ /* P R O T O T Y P E S */ int main (int argc, char *argv[]); void Banner (void); void Die (void); void Honk (void); void OpenAFile (void); void OpenBFile (void); void SafeFilename (void); /* ==================================== */ int main( int argc, char *argv[] ) /* main TABOUT */ { int c; /* process char by char */ int col = 0; /* 1-based column we have written */ /* 0 = no non-blank chars on line yet */ int pendingSpaces = 0; /* spaces we may later drop or emit */ int i; /* local loop counter */ if ( argc != 2 /* 0=TabOut.Exe 1=MyFile.Txt */ ) { Banner(); printf("Oops! usage: TABOUT Myfile.TXT"); printf("Only one file allowed on the command line.\n"); Die(); } BFilename = *++argv; /* want first arg, not progname */ OpenBFile(); /* Open input "before" file. */ /* Make sure file exists before */ /* song and dance about extension. */ SafeFilename(); /* make sure filename has sane extension */ OpenAFile(); /* open output "after" file */ printf("Converting tabs to spaces in %s ...\n",BFilename); while ( (c=fgetc(Before)) != EOF ) { switch ( c ) { case ' ': /* save up spaces, so trailing spaces can be dropped. */ ++pendingSpaces; break; case '\t': /* effectively expand tab to 1 to 8 spaces */ pendingSpaces += (TabSpacing - ((col+pendingSpaces) % TabSpacing)); break; case '\n': /* ignore pending/trailing spaces */ fputc('\n',After); /* runtime library expands to CrLf */ col = 0; pendingSpaces = 0; break; default: /* ordinary non-blank char */ /* squirt out any pent up spaces */ for ( i=0; i