home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / comms / network / dlgtin.lha / source / linefix.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-23  |  2.9 KB  |  114 lines

  1. /*
  2.     LineFix - linewrap a message saved by DLGedit
  3.  
  4.   DLGedit saves messages with a single CR delimiting a paragraph. Before
  5.   passing the message on we have to perform linewrapping and terminate
  6.   each line with LF.
  7.  
  8.   Quoted lines are not touched, but note that currently only : and >
  9.   are recognised as quote chars. If you want to allow your users to set
  10.   their own quote char you will have to be a bit more clever than this.
  11.  
  12. */
  13.  
  14. #include <string.h>
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17.  
  18. main(argc,argv)
  19. int   argc;
  20. char  *argv[];
  21. {
  22. char  path[80],
  23.       outline[81],
  24.       outname[81],
  25.       filename[15],
  26.       *buffer,
  27.       *blarg,
  28.       *loc,
  29.       *shrinkingstring,
  30.       space = ' ',
  31.       slash = '/',
  32.       quotechar = ':',
  33.       altquotechar = '>',
  34.       newline [2];
  35. int   overflow,
  36.       posn,
  37.       wrap_at = 77,
  38.       bufsize = 4096,
  39.       num_read;
  40. FILE  *messagefile,
  41.       *outputfile;
  42.  
  43. strcpy(newline,"\x0A");
  44. strcpy(path,argv[1]);
  45. loc = strrchr(path,slash);
  46. strncpy(filename,loc+1,14);
  47. path[strlen(path) - strlen(filename) +1] = '\x00';
  48. strcpy(outname,path);
  49. strcat(outname,"msg.foo");
  50. if ((messagefile = fopen(argv[1],"r")) == NULL)
  51.    {
  52.    fprintf(stderr,"Cannot open message file.\n");
  53.    return 1;
  54.    }
  55. if ((outputfile = fopen(argv[2],"w")) == NULL)
  56.    {
  57.    fprintf(stderr,"Cannot open temp file.\n");
  58.    return 1;
  59.    }
  60. if ((buffer = malloc(bufsize))== NULL)
  61.    {
  62.    fprintf(stderr,"Cannot allocate buffer.\n");
  63.    return 1;
  64.    }
  65. while ((fgets(buffer,bufsize,messagefile))!=NULL)
  66.    {
  67.    blarg = buffer+strlen(buffer);
  68.    if (strlen(buffer) <= wrap_at)   /* short line, don't wrap */
  69.       {
  70.       fputs(buffer,outputfile);
  71.       }
  72.    else
  73.       {
  74.       if ((buffer[0] == quotechar)|(buffer[0] == altquotechar))   /* quoted line, don't wrap */
  75.          {
  76.          fputs(buffer,outputfile);
  77.          }
  78.       else                          /* long line, wrap */
  79.          {
  80.          shrinkingstring = buffer;
  81.          while (strlen(shrinkingstring) > 0)
  82.             {
  83.             strncpy(outline,shrinkingstring,wrap_at);
  84.             outline[wrap_at] = '\0';
  85.             posn = strlen(outline);
  86.             if (posn == wrap_at)     /* long line - find last space */
  87.                {
  88.                loc = strrchr(outline,space);
  89.                if (loc)             /* found a space */
  90.                   {
  91.                   loc[0] = '\0';    /* truncate line */
  92.                   }                 /* otherwise leave line trunced @ wrap_at */
  93.                }
  94.             posn = strlen(outline);
  95.         if (outline[posn-1] != newline[0])
  96.            {
  97.            strcat(outline,newline);
  98.                }
  99.             fputs(outline,outputfile);
  100.             shrinkingstring = shrinkingstring + posn+1;
  101.         if (shrinkingstring > blarg)
  102.            {                    /* don't overshoot buffer */
  103.            shrinkingstring = blarg;
  104.                }    
  105.             }
  106.          }
  107.       }
  108.    }
  109. free(buffer);
  110. fclose(messagefile);
  111. fclose(outputfile);
  112. return 0;
  113. }
  114.