home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / filutl / untab4.arc / UNTAB4.C next >
Text File  |  1984-02-25  |  4KB  |  89 lines

  1. /* Compile with DeSmet 2.2 or later */
  2.  
  3. #define MAXSIZE 255     /* maximum line size */
  4. #define CR '\015'       /* code for carriage return (in octal) */
  5.  
  6. main(argc,argv)         /* UNTAB4.C - Feb. 25, 1984 - V. T. Bly */
  7. int  argc;              /* Contact:     Vincent T. Bly          */
  8. char *argv[];           /*              P. O. Box 409           */
  9. {                       /*              Ft. Belvoir, VA  22060  */
  10. /*
  11. FUNCTION:   Take, as input, a file with tab stops expanded to 4 spaces.
  12.             Create a new file with all tabs replaced by spaces.  The
  13.             new file will have the original filename with the extension
  14.             ".tab".  Example: input file - my.c, output file - my.tab.
  15.             The original file is not altered.
  16.  
  17. PURPOSE:    To allow files created by the DeSmet SEE editor (including
  18.             all C source files from C-Ware) to by used with the IBM
  19.             Personal Editor.  Files created with editors such as SEE or
  20.             Vedit treat nonstandard tabs differently than other editors
  21.             such as IBM's Personal Editor.  By processing these files
  22.             (with tabs set to 4) with UNTAB4, the ".tab" file can be
  23.             loaded and displayed by any editor.
  24.  
  25. TO USE:     Type UNTAB4 followed by the filename (including extension)
  26.             of the file you wish to untab.  The file you specify should
  27.             have been created by SEE or a similar editor with tabs set
  28.             to 4.  All C source files from C-ware are supplied this way.
  29.             The new ".tab" file created will be larger than the original,
  30.             since all tabs have been replaced by spaces.  You can shrink
  31.             it back by loading it into the Personal Editor and resaving
  32.             it.  This, again, will substitute tabs for spaces, but in a
  33.             different way than that used by SEE.
  34. */
  35.     int   s_file, d_file, e, i, j, k, l;
  36.     int   tab = 4;
  37.     char  *tabname, c, inbuff[256], outbuff[256];
  38.  
  39.     if (argc < 2) {
  40.         puts("** Need a filename **\t");
  41.         exit(1);
  42.     }
  43.     tabname = tabext(argv[1]);                  /* get destination file name */
  44.     s_file = fopen(argv[1], "r");               /* open source file */
  45.     if (s_file == 0) {                          /* abort if can't open file */
  46.         printf("* Cannot Open: %s *\n", argv[1]);
  47.         exit(1);
  48.     }
  49.     d_file = fopen(tabname, "w");               /* open destination file */
  50.     if (d_file == 0) {                          /* abort if can't open file */
  51.         printf("* Cannot Open: %s *\n", tabname);
  52.         exit(1);
  53.     }
  54.     do {
  55.         e = fgets(inbuff, MAXSIZE, s_file);     /* get a line from the file */
  56.         i = 0;  k = 0;
  57.         while ((c = inbuff[i++]) != '\0') {     /* while not end of line */
  58.             if (c == '\t') {                    /* if character is a tab */
  59.                 l = k;
  60.                 for (j = 0; j < (tab - (l % tab)); j++)
  61.                     outbuff[k++] =' ';          /* expand tab to "tab" spaces */
  62.             } else if (c != CR) {               /* else, if not a CR, */
  63.                 outbuff[k++] = c;               /* output character */
  64.             }
  65.         }
  66.         outbuff[k] = '\0';                      /* output end of line char. */
  67.         fputs(outbuff, d_file);                 /* write line to output file */
  68.         puts("* ");                             /* display "*" on screen */
  69.     } while (e != 0);                           /* loop until end of file */
  70.     close(s_file);                              /* close files */
  71.     close(d_file);
  72. }
  73.  
  74. tabext(oldname)             /* Create a new filename w/.tab extension */
  75. char  *oldname;
  76. {
  77.     int   l;
  78.     char  *index(), *p;
  79.     static char tabname[13];
  80.  
  81.     l = strlen(oldname);            /* get length of oldname */
  82.     p = index(oldname, '.');        /* get pointer to '.' in oldname */
  83.     if (p > 0)
  84.         l = p - oldname;            /* now l = # of chars up to extension */
  85.     strncpy(tabname, oldname, 8);   /* copy name up to extension */
  86.     strncpy(tabname + l, ".tab", 5);/* add ".tab" to form tabname */
  87.     return(tabname);
  88. }
  89.