Repository navigation
Expand file tree
/
Copy pathchomp.c
More file actions
88 lines (70 loc) · 1.52 KB
/
Copy pathchomp.c
File metadata and controls
88 lines (70 loc) · 1.52 KB
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
/* *********************************************************
* includes
*/
#include "chomp.h"
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
/* *********************************************************
* API functions
*/
int
sistool_chomp(
FILE* in
, FILE* out
, int flags
)
{
int const no_chomp_back = 0 != (SISTOOL_CHOMP_F_NO_CHOMP_BACK & flags);
int const chomp_front = 0 != (SISTOOL_CHOMP_F_CHOMP_FRONT & flags);
int const flatten = 0 != (SISTOOL_CHOMP_F_FLATTEN_N & flags);
int ch;
int nlf = 0;
int in_front = 1;
for (; EOF != (ch = fgetc(in)); )
{
if ('\n' == ch)
{
++nlf;
}
else
{
if (chomp_front)
{
if (in_front)
{
nlf = 0;
}
}
if (flatten)
{
if (0 != nlf)
{
nlf = 1;
}
}
for (; 0 != nlf; --nlf)
{
fputc('\n', out);
}
fputc(ch, out);
in_front = 0;
}
}
if (no_chomp_back)
{
if (flatten)
{
if (0 != nlf)
{
nlf = 1;
}
}
for (; 0 != nlf; --nlf)
{
fputc('\n', out);
}
}
return EXIT_SUCCESS;
}
/* ///////////////////// end of file //////////////////// */