home *** CD-ROM | disk | FTP | other *** search
/ Borland Programmer's Resource / Borland_Programmers_Resource_CD_1995.iso / utils / rtfprsr / rtfinden.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-19  |  1.5 KB  |  110 lines

  1. /*
  2.     rtfindent - indent RTF file
  3.  
  4.     Useful for revealing nesting structure of RTF documents.
  5.     Also makes them somewhat more readable.
  6.  
  7.     The output is legal RTF but will not format exactly as the input.
  8.     The extra newlines that are introduced might not make a difference,
  9.     but the extra spaces used to effect indenting levels would.
  10.  
  11.     28 Jan 91    Paul DuBois    dubois@primate.wisc.edu
  12.  
  13.     28 Jan 90 V1.0. Created.
  14.     28 Feb 91 V1.01. Updated for distribution 1.05.
  15. */
  16.  
  17. # include    <stdio.h>
  18.  
  19.  
  20. int    indLevel = 0;
  21. int    indAmt = 2;
  22. int    c;
  23. int    nChars = 0;
  24. int    escNext = 0;
  25.  
  26.  
  27. int main (argc, argv)
  28. int    argc;
  29. char    **argv;
  30. {
  31.     /* not clever; only allows stdin or one named file to be read */
  32.  
  33.     if (argc > 1)
  34.     {
  35.         if (freopen (argv[1], "r", stdin) == NULL)
  36.         {
  37.             fprintf (stderr, "Can't open \"%s\"\n", argv[1]);
  38.             exit (1);
  39.         }
  40.     }
  41.  
  42.     while ((c = getchar ()) != EOF)
  43.     {
  44.         if (escNext)
  45.         {
  46.             escNext = 0;
  47.             Put (c);
  48.             continue;
  49.         }
  50.         if (c == '\\')    /* this would be wrong for a general reader */
  51.         {
  52.             escNext = 1;
  53.             Put (c);
  54.             continue;
  55.         }
  56.         if (c == '{')
  57.         {
  58.             Flush ();
  59.             Put (c);
  60.             Flush ();
  61.             ++indLevel;
  62.         }
  63.         else if (c == '}')
  64.         {
  65.             Flush ();
  66.             --indLevel;
  67.             Put (c);
  68.             Flush ();
  69.         }
  70.         else if (c == '\r')
  71.         {
  72.             Put ('\n');
  73.             Flush ();
  74.         }
  75.         else
  76.             Put (c);
  77.     }
  78.     Flush ();
  79. }
  80.  
  81.  
  82. Flush ()
  83. {
  84.     if (nChars > 0)
  85.     {
  86.         Put ('\n');
  87.         nChars = 0;
  88.     }
  89. }
  90.  
  91.  
  92. Put (c)
  93. int    c;
  94. {
  95. int    i, j;
  96.     if (nChars == 0)    /* beginning of line, dump out indent */
  97.     {
  98.         for (i = 0; i < indLevel; i++)
  99.         {
  100.             for (j = 0; j < indAmt; j++)
  101.             {
  102.                 putchar (' ');
  103.                 ++nChars;
  104.             }
  105.         }
  106.     }
  107.     putchar (c);
  108.     ++nChars;
  109. }
  110.