home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 3 / goldfish_volume_3.bin / files / text / hyper / wrapguide / wrapguide.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-16  |  5.2 KB  |  197 lines

  1. /*****************************************************************
  2.  *
  3.  *  WrapGuide.c
  4.  *  Copyright © 1994,1995 Udo Schuermann
  5.  *  All rights reserved
  6.  *
  7.  *  -----------------------------------------------------------
  8.  *
  9.  *  With AmigaGuide V39 and later, .guide documents support
  10.  *  proportonal fonts and wordwrapped text (as well as quite a
  11.  *  few other goodes.)  Unfortunately, AmigaGuide V34, which is
  12.  *  the version supported by Kickstart 2.x, not only fails to
  13.  *  support these new features, but actually crashes the system
  14.  *  when it encounters the long lines which AmigaGuide V39+
  15.  *  would wrap into nice paragraphs.
  16.  *
  17.  *  WrapGuide, therefore, performs an (unconditional) wordwrap
  18.  *  operation on any .guide file, converting blank spaces into
  19.  *  newline characters so that AmigaGuide V34 is kept happy.
  20.  *
  21.  *  To the best of my knowledge, WrapGuide understands all the
  22.  *  various '@' constructs that AmigaGuide understands.  Style
  23.  *  symbols (such as "@{B}") and information not part of the
  24.  *  visible text of links do not occupy "screen space" and
  25.  *  WrapGuide knows this.
  26.  *
  27.  *  The program requires as parameters only the name of a
  28.  *  .guide file.  If not given a 2nd parameter for the text
  29.  *  width, it will use a default of 65 characters.
  30.  *
  31.  *  Limitations:  Lines (paragraphs) may not exceed 8K bytes
  32.  *                in size; WrapGuide uses a static 8K buffer
  33.  *                for its input lines.
  34.  *                   You have the source code, however, so you
  35.  *                should have no trouble altering this if you
  36.  *                have the need.
  37.  *
  38.  *  WrapGuide is freely distributable.  Please do not separate
  39.  *  the executable from the source code if you pass it on to
  40.  *  others.  Naturally, if you merely include the binary with a
  41.  *  project, you need not include the source code, but it would
  42.  *  be nice if you did.
  43.  *
  44.  *  WrapGuide should be pretty much portable to any system.
  45.  *
  46.  *  If you find bugs in this software, have suggestions, or you
  47.  *  make improvments to it, please let me know!  I can be reached
  48.  *  by email as:  walrus@wam.umd.edu
  49.  *
  50.  *  Cheers & Support your Amiga!
  51.  *
  52.  *  ._.  Udo Schuermann
  53.  *  ( )  walrus@wam.umd.edu
  54.  *
  55.  *****************************************************************
  56.  */
  57.  
  58. #include <stdio.h>
  59. #include <stdlib.h>
  60.  
  61.  
  62. #define DEFAULTMARGIN 65
  63. char VERSION[] = "$VER: WrapGuide 1.1 "__AMIGADATE__;
  64. char buf[8192];      /* size of this buffer determines max line length */
  65. short Margin;
  66.  
  67. void Usage(void) {
  68.  
  69.   printf("%s\n"
  70.      "Copyright © 1994,1995 Udo Schuermann\n"
  71.      "All rights reserved\n\n"
  72.          "Usage: WrapGuide filename.guide [ textwidth ]\n"
  73.      "  Inserts newlines so that guide is readable without wordwrap.\n"
  74.      "  NOTE:\tThe default textwidth is %d and must be in range 50..999\n"
  75.      "\tThe given .guide file is modified directly!\n",
  76.      &VERSION[6],DEFAULTMARGIN);
  77.   exit(10);
  78. }
  79.  
  80. int main( int argc, char *argv[] ) {
  81.  
  82.   if( argc >= 2 ) {
  83.     FILE *f;
  84.  
  85.     if( *argv[1] == '?' )
  86.       Usage();
  87.  
  88.     if( argc > 2 ) {
  89.       int i = atol(argv[2]);
  90.       if( (i < 50) || (i > 999) ) {
  91.     printf("WrapGuide: a textwidth of %ld is not allowed (50..999 is legal); using %d\n",i,DEFAULTMARGIN);
  92.     Margin = DEFAULTMARGIN;
  93.       } else
  94.     Margin = i;
  95.     } else
  96.       Margin = DEFAULTMARGIN;
  97.  
  98.     if( f = fopen(argv[1],"r+") ) {
  99.       unsigned long offset=0,i,pos;
  100. //      unsigned long line=0;
  101.       unsigned short dirty = 0;
  102.       unsigned short best,col;
  103.  
  104.       while( fgets(buf,sizeof(buf),f) ) {
  105. //    line++;
  106.     pos = ftell( f );       /* find out where we are at the end of the line */
  107.  
  108.     best = 0;
  109.     col = 1;
  110.  
  111.     /* our buffer now contains bytes 'offset' through 'pos'-1 */
  112.     i = 0;
  113.     while( buf[i] ) {
  114.       if( buf[i] == ' ' ) {                 /* a spot where line breaks are possible */
  115.         if( col < Margin )
  116.           best = i;
  117.         else {
  118.           if( buf[best] == ' ' )
  119.         best = i;
  120.           else
  121.         i = best;
  122.           buf[best] = '\n';
  123.           best = i+1;
  124.           col = 0;
  125.           dirty = 1;
  126.         }
  127.         col++;
  128.       } else
  129.         if( buf[i] == '\\' ) {              /* special escaped character, i.e. \@ */
  130.           if( buf[i+1] )
  131.         i++;
  132.           col++;
  133.         } else
  134.           if( buf[i] == '@' ) {             /* possible link or style code */
  135.         if( buf[i+1] == '{' ) {         /* ...only if followed by '{' */
  136.           i += 2;
  137.           while( buf[i] ) {
  138.             if( buf[i] == '"' )
  139.               break;
  140.             if( buf[i] == '}' )
  141.               break;
  142.             i++;
  143.           } /* while */
  144.           if( buf[i] == '"' ) {
  145.             i++;
  146.             while( buf[i] ) {
  147.               if( buf[i] == '\\' )
  148.             i++;
  149.               else
  150.             if( buf[i] == '"' )
  151.               break;
  152.               col++;
  153.               i++;
  154.             } /* while */
  155.             if( buf[i] == '"' ) {
  156.               i++;
  157.               while( buf[i] ) {
  158.             if( buf[i] == '}' )
  159.               break;
  160.             i++;
  161.               } /* while */
  162.             }
  163.           }
  164.         } else {
  165.           if( i == 0 )  /* '@' WITHOUT following '{' in column 1 is a macro command */
  166.             break;
  167.         }
  168.           } else
  169.         col++;
  170.       i++;
  171.     } /* while */
  172.     
  173.     if( (col >= Margin) && (buf[best] == ' ') ) {
  174.       buf[best] = '\n';
  175.       dirty = 1;
  176.     } /* if */
  177.  
  178.     if( dirty ) {
  179.       fseek( f, offset, SEEK_SET );
  180.       fwrite( buf, 1, i-1, f );
  181.       fseek( f, pos, SEEK_SET );
  182.       dirty = 0;
  183.     } /* if */
  184.  
  185.     offset = pos;           /* preparation for next loop */
  186.       } /* while */
  187.  
  188.       fclose(f);
  189.     } else {
  190.       printf("WrapGuide: cannot open file (%s)\n",argv[1]);
  191.       exit(20);
  192.     }
  193.   } else
  194.     Usage();
  195. } /* main() */
  196.  
  197.