forked from Dwayne-Phillips/CIPS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdpfilt.c
133 lines (96 loc) · 3.13 KB
/
dpfilt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/********************************************************/
/********************************************************/
/*
This program prepares an ASCII file for
input into MS Word. It removes the hard
carraige returns from the end of each line.
It keeps the return when there are two in
a row. That makes the paragraphs correct
for MS Word.
*/
/********************************************************/
/******************* includes ***************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/******************* defines ****************************/
#define MIN_PARAMS 3
#define MAX_PARAMS 3
#define VERSION "DPFILT Version 1.0 - 3 November 1996"
#define B 30000
/**************** function declarations *****************/
void print_usage(void);
/********************************************************/
/*
main
*/
char in_buffer[B], out_buffer[B];
int main (int argc, char *argv[])
{
size_t ip = 0, op = 0;
FILE *input_file, *output_file;
size_t bytes_read = 0;
printf("\n\n%s\n\n", VERSION);
if(argc < MIN_PARAMS || argc > MAX_PARAMS){
print_usage();
exit(0);
} /* ends if argc */
if(strcmp(argv[1], argv[2]) == 0){
printf(
"\nERROR: Input and output file names must be different");
print_usage();
exit(0);
} /* ends if strcmp */
if((input_file = fopen(argv[1], "r")) == NULL){
printf("\nERROR: Cannot open file %s", argv[1]);
exit(1);
} /* ends if input_file */
if((output_file = fopen(argv[2], "w")) == NULL){
printf("\nERROR: Cannot create file %s", argv[2]);
fclose(input_file);
exit(1);
} /* ends if output_file */
bytes_read = B;
while(bytes_read == B){
ip = 0;
op = 0;
bytes_read = fread(in_buffer, sizeof(char), B, input_file);
printf("\nRead %ld (%d)", bytes_read, B);
while(ip < bytes_read){
if(in_buffer[ip] == '\n'){
if(in_buffer[ip+1] == '\n'){
out_buffer[op] = in_buffer[ip];
op = op+1;
ip = ip+2;
}
else{
out_buffer[op] = ' ';
ip++;
op++;
}
}
else{
out_buffer[op++] = in_buffer[ip++];
}
} /* ends while */
fwrite(out_buffer, sizeof(char), op, output_file);
} /* ends while */
fclose(input_file);
fclose(output_file);
printf("\n\n%s\n\n", VERSION);
} /* ends main */
/********************************************************/
/*
print_usage()
*/
void print_usage()
{
printf("\nusage: dpfilt in-file out-file");
printf("\n"
"\nThis program prepares an ASCII file for"
"\ninput into MS Word. It removes the hard"
"\ncarraige returns from the end of each line."
"\nIt keeps the return when there are two in"
"\na row. That makes the paragraphs correct"
"\nfor MS Word.");
} /* ends print_usage */