forked from Dwayne-Phillips/CIPS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpara.c
executable file
·468 lines (347 loc) · 11.7 KB
/
para.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
Para Program
Dwayne Phillips
March 1996
Command Line:
para in-file out-file
Purpose:
This program does a simple form a text formatting.
It takes in an ASCII text file and formats it.
The input is just text. Paragraphs are delimited
by having a blank line between them.
This program borrows from the pageit program.
Pageit formatted text to be printed.
Para formats text for another file with no page
breaks, headers, footers, etc.
Files:
The program uses two files: (1) the input file,
and (2) the output file.
Input File
The input file is unformatted ASCII text
created with vi or something simple like
that. The lines can be as long or short
as you want (so long as they fit on the
screen of course). The separate paragraphs,
leave a blank line.
Output File
The output file is also ASCII text.
Instead of ragged text, the lines all fit
There are no pages. The text flows from
para to para with a blank line between.
Revision History:
Version 1 - January 1999
*/
/*****************************************************/
/*****************************************************/
/*PAGE Includes and data structures */
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
/** #include <malloc.h> **/
#include <string.h>
#define L 100
#define LPP 66
#define CPL 85
#define FOOTER 5
#define LEFT_MARGIN 0
#define RIGHT_MARGIN 20
#define END_OF_LIST 0x00
#define SPACE ' '
#define VERSION "Para Version 1 - January 1999"
struct word_list_struct{
char word[L];
struct word_list_struct *next_word;
};
struct line_list_struct{
char line[L];
struct line_list_struct *next_line;
};
/* Here are the functions I needed to define. */
struct word_list_struct * convert_lines_to_words(
struct line_list_struct *);
void fill_string(char *, int, char);
void output_line(char *, FILE *);
struct word_list_struct * read_a_paragraph(FILE *,
int *);
struct line_list_struct * read_the_lines(FILE *,
int *);
void write_a_paragraph(struct word_list_struct *,
FILE *);
/*****************************************************/
/*****************************************************/
int main(int argc, char *argv[])
{
char in_file_name[L],
out_file_name[L],
title[L];
FILE *in_file, *out_file;
int double_space = 0,
file_done = 0,
i,
line_counter = 0,
lines = LPP,
page_counter = 1,
print_date = 0,
print_page = 0,
print_title = 0;
struct word_list_struct *word_list, *temp;
if(argc != 3){
printf("\n\nusage: para in-file out-file ");
printf(
"\n%s\n", VERSION);
exit(1);
}
if(argc == 3){
strcpy(in_file_name, argv[1]);
strcpy(out_file_name, argv[2]);
} /* ends if argc == 3 */
if((in_file = fopen(in_file_name, "rt")) == NULL){
printf("\nERROR Could not open file %s",
in_file_name);
exit(2);
}
if((out_file = fopen(out_file_name, "wt")) == NULL){
printf("\nERROR Could not open file %s",
out_file_name);
exit(2);
}
while(file_done == 0){
word_list = read_a_paragraph(in_file, &file_done);
write_a_paragraph(word_list, out_file);
} /* ends while file_done is 0 */
fclose(in_file);
fclose(out_file);
} /* ends main */
/*****************************************************/
/*****************************************************/
/*
struct word_list_struct * convert_lines_to_words(...
*/
struct word_list_struct * convert_lines_to_words(
struct line_list_struct *line_list)
{
char aword[L];
int copying = 1,
doit = 1,
first_time = 1,
i,
j,
line_index = 0;
struct line_list_struct *this_line;
struct word_list_struct *new_word,
*result,
*temp;
this_line = line_list;
line_index = 0;
while(doit){
if(this_line->next_line == END_OF_LIST)
doit = 0;
/* still need to do this line of text */
copying = 1;
i = 0;
fill_string(aword, L, '\0');
while(copying){
aword[i] = this_line->line[line_index];
if(aword[i] == SPACE){
copying = 0;
if(this_line->line[line_index+1] == SPACE){
i++;
line_index++;
aword[i] = this_line->line[line_index];
} /* ends if line is SPACE */
} /* ends if aword is SPACE */
else{ /* else aword not a SPACE */
if(aword[i] == '\n'){
aword[i] = SPACE;
copying = 0;
this_line = this_line->next_line;
line_index = -1;
} /* ends if aword is new line */
} /* ends else aword not a SPACE */
i++;
line_index++;
} /* ends while copying */
if(first_time){
first_time = 0;
new_word = (struct word_list_struct *)
calloc(1, sizeof(struct word_list_struct));
strcpy(new_word->word, aword);
new_word->next_word = END_OF_LIST;
result = new_word;
temp = new_word;
} /* ends if first_time */
else{ /* else not first_time */
new_word = (struct word_list_struct *)
calloc(1, sizeof(struct word_list_struct));
strcpy(new_word->word, aword);
new_word->next_word = END_OF_LIST;
temp->next_word = new_word;
temp = new_word;
} /* ends else not first_time */
} /* ends while doit */
return(result);
} /* ends convert_lines_to_words */
/*****************************************************/
/*****************************************************/
/*****************************************************/
/*****************************************************/
/*
void fill_string(...
*/
void fill_string(char *string, int size,
char fill_char)
{
int i;
for(i=0; i<size; i++)
string[i] = fill_char;
} /* ends fill_string */
/*****************************************************/
/*****************************************************/
/*
struct line_list_struct * read_the_lines(...
FILE *, int *);
*/
struct line_list_struct * read_the_lines(
FILE *input_file, int *file_done)
{
char aline[L];
int first_pass = 1,
reading = 1;
struct line_list_struct *new_one,
*result,
*temp;
while(reading){
if(fgets(aline, L, input_file) == NULL){
*file_done = 1;
reading = 0;
} /* ends if fgets is NULL */
if(first_pass){
new_one = (struct line_list_struct *)
calloc(1, sizeof(struct line_list_struct));
temp = new_one;
result = new_one;
first_pass = 0;
new_one->next_line = END_OF_LIST;
strcpy(new_one->line, aline);
} /* ends if first_pass */
else{ /* else not first_pass */
new_one = (struct line_list_struct *)
calloc(1, sizeof(struct line_list_struct));
temp->next_line = new_one;
temp = new_one;
new_one->next_line = END_OF_LIST;
strcpy(new_one->line, aline);
} /* ends else not first_pass */
if(aline[0] == '\n'){
reading = 0;
}
} /* ends while reading */
return(result);
} /* ends read_the_lines */
/*****************************************************/
/*****************************************************/
/*
struct word_list_struct * read_a_paragraph(...
*/
struct word_list_struct * read_a_paragraph(
FILE *input_file,
int *file_done)
{
struct line_list_struct *line_list;
struct word_list_struct *result;
line_list = read_the_lines(input_file, file_done);
result = convert_lines_to_words(line_list);
free(line_list);
return(result);
} /* ends read_a_paragraph */
/*****************************************************/
/*****************************************************/
/*
void write_a_paragraph(...
traversing_list == 0 means this is the last word
in the paragraph
too_long == 1 means this word will not fit on the
current line
traversing_list too_long
0 0 put word on line
print line
THE END
0 1 print line
start new line
put word on line
print line
THE END
1 0 put word on line
go on
1 1 print line
start new line
put word on new line
go on
*/
void write_a_paragraph(
struct word_list_struct *word_list,
FILE *output_file)
{
char line[L];
int i,
new_line = 1,
too_long = 0,
traversing_list = 1;
struct word_list_struct *this_word;
this_word = word_list;
fill_string(line, L, '\0');
fill_string(line, LEFT_MARGIN, SPACE);
while(traversing_list){
/* this is the last word in the paragraph */
if(this_word->next_word == END_OF_LIST)
traversing_list = 0;
/* If this word won't fit on this line, */
if( (strlen(this_word->word) + (strlen(line))) >
(CPL - (RIGHT_MARGIN+LEFT_MARGIN)) )
too_long = 1;
else
too_long = 0;
if(traversing_list == 0 && too_long == 0){
strcat(line, this_word->word);
strcat(line, "\n");
output_line(line, output_file);
} /* ends if 0 0 */
if(traversing_list == 0 && too_long == 1){
strcat(line, "\n");
output_line(line, output_file);
fill_string(line, L, '\0');
fill_string(line, LEFT_MARGIN, SPACE);
strcat(line, this_word->word);
strcat(line, "\n");
output_line(line, output_file);
} /* ends 0 1 */
if(traversing_list == 1 && too_long == 0){
strcat(line, this_word->word);
this_word = this_word->next_word;
} /* ends 1 0 */
if(traversing_list == 1 && too_long == 1){
strcat(line, "\n");
output_line(line, output_file);
fill_string(line, L, '\0');
fill_string(line, LEFT_MARGIN, SPACE);
strcat(line, this_word->word);
this_word = this_word->next_word;
} /* ends 1 1 */
} /* ends while traversing_list */
fill_string(line, L, '\0');
fill_string(line, LEFT_MARGIN, SPACE);
strcat(line, "\n");
output_line(line, output_file);
} /* ends write_a_paragraph */
/*****************************************************/
/*****************************************************/
/*
void output_line(...
*/
void output_line(char *line,
FILE *output_file)
{
fputs(line, output_file);
} /* ends output_line */
/*****************************************************/
/*****************************************************/