 |
Bsoft
2.1.0
Bernard's software package
|
Go to the documentation of this file.
23 "Usage: bfix [options] file.in file.out",
24 "--------------------------------------",
25 "Replaces single characters in a file.",
26 "If only one file name is given, the output is sent to stdout.",
27 "If a \".\" character is used for output, the input file name is used",
28 "(without the path).",
29 "Note: replace and delete are mutually exclusive - the last option wins",
32 "-n Replace <cr> with <nl> (To UNIX style)",
33 "-c Replace <nl> with <cr> (To MAC style)",
34 "-replace 9,32 Replace all character values 9 with character values 32",
35 "-delete 13 Delete all character values 13",
38 "-verbose 7 Verbosity of output",
43 int main(
int argc,
char **argv)
45 int char_out(0), char_in(0);
53 for ( curropt = option; curropt; curropt = curropt->
next ) {
55 if ( curropt->
tag ==
"c" ) { replace_flag = 1; char_in = 10; char_out = 13; }
56 if ( curropt->
tag ==
"n" ) { replace_flag = 2; char_in = 13; char_out = 10; }
57 if ( curropt->
tag ==
"replace" ) {
58 if ( curropt->
values(char_in, char_out) < 1 ) {
59 cerr <<
"-replace: At least one character must be specified!" << endl;
64 if ( curropt->
tag ==
"delete" ) {
66 cerr <<
"-delete: A character value must be specified!" << endl;
76 filename = argv[optind++];
78 ifstream fin(filename.
c_str());
80 cerr <<
"Error: File " << filename <<
" not read!" << endl;
85 fin.seekg (0, ios::end);
86 long filesize = fin.tellg();
87 fin.seekg (0, ios::beg);
90 cout <<
"Input file size: " << filesize << endl;
93 switch ( replace_flag ) {
94 case 1: cout <<
"Converting all NL in " << filename <<
" to CR" << endl;
break;
95 case 2: cout <<
"Converting all CR in " << filename <<
" to NL" << endl;
break;
96 case 3: cout <<
"Replacing all " << char_in <<
" in " << filename <<
" to " << char_out << endl;
break;
97 case 4: cout <<
"Deleting all " << char_in <<
" in " << filename << endl;
break;
102 long bufsize = (long) (filesize*1.2);
103 unsigned char* buf =
new unsigned char[bufsize];
105 fin.read((
char *)buf, bufsize);
111 if ( optind < argc ) {
112 filename = argv[optind];
122 if ( replace_flag ) {
123 if ( replace_flag < 4 )
130 if ( filename.
length() ) {
131 fout.open(filename.
c_str());
133 cerr <<
"Error: File " << filename <<
" not opened!" << endl;
137 cout <<
"Writing: " << filename << endl;
138 cout <<
"Output file size: " << filesize << endl;
140 fout.write((
char *)buf, filesize);
157 for ( i=n=0; i<size; i++ ) {
158 if ( buf[i] == chin ) {
165 cout <<
"Characters replaced: " << n << endl << endl;
174 for ( i=j=n=0; i<size; i++ ) {
184 cout <<
"Characters deleted: " << n << endl << endl;
A class for a command line input option.
long delete_a_character(unsigned char *buf, long size, int c)
int bexit(int value)
Exit function for cleanup and debugging.
long replace_a_character(unsigned char *buf, long size, int chin, int chout)
Boption * get_option_list(const char *use[], int argc, char *argv[], int &optind)
Parses command line arguments based on a template.
int get_option_verbose(char *optarg)
Sets the verbose option.
Header file for option handlers.
int main(int argc, char **argv)
long values(T1 &v1, T2 &v2)
Utilities for timing functions.
double timer_start()
Starts timer and prints the time.
Header file for general utilities.
double timer_report(double lasttime)
Reports the time elapsed since a given time.
int option_kill(Boption *option)
Deallocates a linked list of option structures.
bool contains(const Bstring &s)