-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdecomment.cpp
241 lines (216 loc) · 4.59 KB
/
decomment.cpp
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
# include <cstdlib>
# include <iostream>
# include <fstream>
# include <ctime>
# include <cstring>
using namespace std;
int main ( int argc, char *argv[] );
void handle ( string input_filename, string output_filename );
void timestamp ( );
//****************************************************************************80
int main ( int argc, char *argv[] )
//****************************************************************************80
//
// Purpose:
//
// MAIN is the main program for DECOMMENT.
//
// Discussion:
//
// DECOMMENT makes a copy of a file, skipping all comment lines.
//
// A comment line is exactly any line that begins with the "#" character.
// I want this program because I commonly use a data file format that includes
// such comment lines, but occasionally I need to delete such comments in
// order to make an input file acceptable to a more uncouth program.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 23 April 2010
//
// Author:
//
// John Burkardt
//
{
string input_filename;
string output_filename;
bool VERBOSE = false;
if ( VERBOSE )
{
timestamp ( );
cout << "\n";
cout << "DECOMMENT:\n";
cout << " C++ version\n";
cout << "\n";
cout << " Compiled on " << __DATE__ << " at " << __TIME__ << ".\n";
cout << "\n";
cout << " Make a copy of a file, in which all # comments are gone.\n";
}
//
// If the input file was not specified, get it now.
//
if ( argc <= 1 )
{
cout << "\n";
cout << "DECOMMENT:\n";
cout << " Please enter the name of the file to be decommented.\n";
cin >> input_filename;
}
else
{
input_filename = argv[1];
}
//
// If the output file was not on the command line, get it now.
//
if ( argc < 3 )
{
cout << "\n";
cout << "DECOMMENT:\n";
cout << " Please enter the OUTPUT file name:\n";
cin >> output_filename;
}
else
{
output_filename = argv[2];
}
handle ( input_filename, output_filename );
if ( VERBOSE )
{
cout << "\n";
cout << "DECOMMENT:\n";
cout << " Normal end of execution.\n";
cout << "\n";
timestamp ( );
}
return 0;
}
//****************************************************************************80
void handle ( string input_filename, string output_filename )
//****************************************************************************80
//
// Purpose:
//
// HANDLE handles a single file.
//
// Discussion:
//
// The routine reads a line, and if it does not begin with '#' it copies
// it to the output file.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 23 April 2010
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, string INPUT_FILE, the name of the input file.
//
// Input, string OUTPUT_FILE, the name of the output file.
//
{
int comment_count;
ifstream input;
ofstream output;
string line;
//
// Open the input file.
//
input.open ( input_filename.c_str ( ) );
if ( !input )
{
cout << "\n";
cout << "HANDLE - Fatal error!\n";
cout << " Cannot open \"" << input_filename << "\"\n";
return;
}
//
// Open the output file.
//
output.open ( output_filename.c_str ( ) );
if ( !output )
{
cout << "\n";
cout << "HANDLE - Fatal error!\n";
cout << " Cannot open \"" << output_filename << "\"\n";
return;
}
comment_count = 0;
while ( 1 )
{
getline ( input, line );
if ( line[0] != '#' )
{
output << line << "\n";
}
else
{
comment_count = comment_count + 1;
}
if ( input.eof() )
{
break;
}
}
//
// Close the files.
//
input.close ( );
output.close ( );
cout << input_filename << " contained " << comment_count << " comments.\n";
return;
}
//****************************************************************************80
void timestamp ( )
//****************************************************************************80
//
// Purpose:
//
// TIMESTAMP prints the current YMDHMS date as a time stamp.
//
// Example:
//
// May 31 2001 09:45:54 AM
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 04 October 2003
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// None
//
{
# define TIME_SIZE 40
static char time_buffer[TIME_SIZE];
const struct tm *tm;
size_t len;
time_t now;
now = time ( NULL );
tm = localtime ( &now );
len = strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm );
cout << time_buffer << "\n";
return;
# undef TIME_SIZE
}