N
name
Back for more critique.
----------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#define MAX 10000
/*
wrap.c inserts newlines in place of spaces according to specified
line length. Output filename is {filename}.wrap. Takes two arguments,
filename and line length.
*/
void wordwrap(FILE *ifp, FILE *ofp, char *wl)
{
char buf[MAX];
int c, i, space, count, length;
length = atoi(wl);
for (i = 0; i < MAX && ((c=getc(ifp)) != EOF); ++i)
buf = (char)c;
for (i = 0; buf != '\0'; ++i) {
if ((i == 0) || ((buf == '\n' || buf == '\t') && buf[i-1] == '\n'))
count = space = 0;
if (buf == ' ')
space = i;
++count;
if ((count == length) && (space != 0)) {
buf[space] = '\n';
count = i - space;
}
}
for (i = 0; buf != '\0'; ++i) {
c = (int)buf;
putc(c, ofp);
}
}
int main(int argc, char *argv[])
{
FILE *fp1;
FILE *fp2;
char *prog = argv[0];
char *filename1 = argv[1];
char filename2[40];
char *wl = argv[2];
if (argc != 3) {
printf("Usage: %s: filename, wrap length\n", prog);
return EXIT_FAILURE;
} else if (strlen(argv[1]) > 32) {
printf("Filename limited to 32 characters. Sorry...\n");
return EXIT_FAILURE;
}
strcpy(filename2, argv[1]);
strcat(filename2, ".wrap");
if (atoi(wl) > 80) {
printf("Line length limit: 80. Better is < 75.\n");
return EXIT_FAILURE;
} else if (atoi(wl) < 0) {
printf("Line length must be a positive number.\n");
return EXIT_FAILURE;
} else if ((fp1 = fopen(filename1, "r")) == NULL) {
fprintf(stderr, "%s: can't open %s\n", prog, filename1);
return EXIT_FAILURE;
} else if ((fp2 = fopen(filename2, "w")) == NULL) {
fprintf(stderr, "%s: can't open %s\n", prog, filename2);
return EXIT_FAILURE;
} else {
printf("Wrapping %s at %s\n", filename1, wl);
printf("Output file adds .wrap to input filename.\n");
wordwrap(fp1, fp2, wl);
fclose(fp1);
fclose(fp2);
}
if (ferror(fp2)) {
fprintf(stderr, "%s: error writing %s\n", prog, filename2);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
-----------------------------------------------------------------
There are some number of different "canonical formats" for C code. This
approximates one of them. I don't find it as readable as the style I use,
but this is for those who didn't like my style. Hope this serves.
Thanks for reading.
----------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#define MAX 10000
/*
wrap.c inserts newlines in place of spaces according to specified
line length. Output filename is {filename}.wrap. Takes two arguments,
filename and line length.
*/
void wordwrap(FILE *ifp, FILE *ofp, char *wl)
{
char buf[MAX];
int c, i, space, count, length;
length = atoi(wl);
for (i = 0; i < MAX && ((c=getc(ifp)) != EOF); ++i)
buf = (char)c;
for (i = 0; buf != '\0'; ++i) {
if ((i == 0) || ((buf == '\n' || buf == '\t') && buf[i-1] == '\n'))
count = space = 0;
if (buf == ' ')
space = i;
++count;
if ((count == length) && (space != 0)) {
buf[space] = '\n';
count = i - space;
}
}
for (i = 0; buf != '\0'; ++i) {
c = (int)buf;
putc(c, ofp);
}
}
int main(int argc, char *argv[])
{
FILE *fp1;
FILE *fp2;
char *prog = argv[0];
char *filename1 = argv[1];
char filename2[40];
char *wl = argv[2];
if (argc != 3) {
printf("Usage: %s: filename, wrap length\n", prog);
return EXIT_FAILURE;
} else if (strlen(argv[1]) > 32) {
printf("Filename limited to 32 characters. Sorry...\n");
return EXIT_FAILURE;
}
strcpy(filename2, argv[1]);
strcat(filename2, ".wrap");
if (atoi(wl) > 80) {
printf("Line length limit: 80. Better is < 75.\n");
return EXIT_FAILURE;
} else if (atoi(wl) < 0) {
printf("Line length must be a positive number.\n");
return EXIT_FAILURE;
} else if ((fp1 = fopen(filename1, "r")) == NULL) {
fprintf(stderr, "%s: can't open %s\n", prog, filename1);
return EXIT_FAILURE;
} else if ((fp2 = fopen(filename2, "w")) == NULL) {
fprintf(stderr, "%s: can't open %s\n", prog, filename2);
return EXIT_FAILURE;
} else {
printf("Wrapping %s at %s\n", filename1, wl);
printf("Output file adds .wrap to input filename.\n");
wordwrap(fp1, fp2, wl);
fclose(fp1);
fclose(fp2);
}
if (ferror(fp2)) {
fprintf(stderr, "%s: error writing %s\n", prog, filename2);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
-----------------------------------------------------------------
There are some number of different "canonical formats" for C code. This
approximates one of them. I don't find it as readable as the style I use,
but this is for those who didn't like my style. Hope this serves.
Thanks for reading.