home *** CD-ROM | disk | FTP | other *** search
- /*
- LineFix - linewrap a message saved by DLGedit
-
- DLGedit saves messages with a single CR delimiting a paragraph. Before
- passing the message on we have to perform linewrapping and terminate
- each line with LF.
-
- Quoted lines are not touched, but note that currently only : and >
- are recognised as quote chars. If you want to allow your users to set
- their own quote char you will have to be a bit more clever than this.
-
- */
-
- #include <string.h>
- #include <stdlib.h>
- #include <stdio.h>
-
- main(argc,argv)
- int argc;
- char *argv[];
- {
- char path[80],
- outline[81],
- outname[81],
- filename[15],
- *buffer,
- *blarg,
- *loc,
- *shrinkingstring,
- space = ' ',
- slash = '/',
- quotechar = ':',
- altquotechar = '>',
- newline [2];
- int overflow,
- posn,
- wrap_at = 77,
- bufsize = 4096,
- num_read;
- FILE *messagefile,
- *outputfile;
-
- strcpy(newline,"\x0A");
- strcpy(path,argv[1]);
- loc = strrchr(path,slash);
- strncpy(filename,loc+1,14);
- path[strlen(path) - strlen(filename) +1] = '\x00';
- strcpy(outname,path);
- strcat(outname,"msg.foo");
- if ((messagefile = fopen(argv[1],"r")) == NULL)
- {
- fprintf(stderr,"Cannot open message file.\n");
- return 1;
- }
- if ((outputfile = fopen(argv[2],"w")) == NULL)
- {
- fprintf(stderr,"Cannot open temp file.\n");
- return 1;
- }
- if ((buffer = malloc(bufsize))== NULL)
- {
- fprintf(stderr,"Cannot allocate buffer.\n");
- return 1;
- }
- while ((fgets(buffer,bufsize,messagefile))!=NULL)
- {
- blarg = buffer+strlen(buffer);
- if (strlen(buffer) <= wrap_at) /* short line, don't wrap */
- {
- fputs(buffer,outputfile);
- }
- else
- {
- if ((buffer[0] == quotechar)|(buffer[0] == altquotechar)) /* quoted line, don't wrap */
- {
- fputs(buffer,outputfile);
- }
- else /* long line, wrap */
- {
- shrinkingstring = buffer;
- while (strlen(shrinkingstring) > 0)
- {
- strncpy(outline,shrinkingstring,wrap_at);
- outline[wrap_at] = '\0';
- posn = strlen(outline);
- if (posn == wrap_at) /* long line - find last space */
- {
- loc = strrchr(outline,space);
- if (loc) /* found a space */
- {
- loc[0] = '\0'; /* truncate line */
- } /* otherwise leave line trunced @ wrap_at */
- }
- posn = strlen(outline);
- if (outline[posn-1] != newline[0])
- {
- strcat(outline,newline);
- }
- fputs(outline,outputfile);
- shrinkingstring = shrinkingstring + posn+1;
- if (shrinkingstring > blarg)
- { /* don't overshoot buffer */
- shrinkingstring = blarg;
- }
- }
- }
- }
- }
- free(buffer);
- fclose(messagefile);
- fclose(outputfile);
- return 0;
- }
-