| Bsoft 2.0.5 Bernard's software package

bfix.cpp
Go to the documentation of this file.
1
9 #include "utilities.h"
10 #include "options.h"
11 #include "timer.h"
12 #include <fstream>
13
14 // Declaration of global variables
15 extern int verbose; // Level of output to the screen
16
17 // Function prototype
18 long replace_a_character(unsigned char* buf, long size, int chin, int chout);
19 long delete_a_character(unsigned char* buf, long size, int c);
20
21 const char* use[] = {
22 " ",
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",
30 " ",
31 "Actions:",
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",
36 " ",
37 "Parameters:",
38 "-verbose 7 Verbosity of output",
39 " ",
40 NULL
41 };
42
43 int main(int argc, char **argv)
44 {
45 int char_out(0), char_in(0);
46 Bstring filename;
47
48 int replace_flag(0); // Flag for character replacement
49
50 int optind;
51 Boption* option = get_option_list(use, argc, argv, optind);
52 Boption* curropt;
53 for ( curropt = option; curropt; curropt = curropt->next ) {
54 if ( curropt->tag == "verbose" ) verbose = get_option_verbose(curropt->value);
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;
60 bexit(-1);
61 } else
62 replace_flag = 3;
63 }
64 if ( curropt->tag == "delete" ) {
65 if ( ( char_in = curropt->value.integer() ) < 1 ) {
66 cerr << "-delete: A character value must be specified!" << endl;
67 bexit(-1);
68 } else
69 replace_flag = 4;
70 }
71 }
72 option_kill(option);
73
74 double ti = timer_start();
75
76 filename = argv[optind++];
77
78 ifstream fin(filename.c_str());
79 if ( fin.fail() ) {
80 cerr << "Error: File " << filename << " not read!" << endl;
81 bexit(-1);
82 }
83
84 // Get the file size to determine the space to allocate
85 fin.seekg (0, ios::end);
86 long filesize = fin.tellg();
87 fin.seekg (0, ios::beg);
88
89 if ( verbose )
90 cout << "Input file size: " << filesize << endl;
91
92 if ( verbose & VERB_LABEL ) {
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;
98 }
99 }
100
101 // Add an additional 20 % to the file size to allow for expansion
102 long bufsize = (long) (filesize*1.2);
103 unsigned char* buf = new unsigned char[bufsize];
104
105 fin.read((char *)buf, bufsize);
106 // if ( fin.fail() )
107 // cerr << "Error: File " << filename << " not read!" << endl;
108
109 fin.close();
110
111 if ( optind < argc ) {
112 filename = argv[optind];
113 if ( filename.contains(".") ) {
114 if ( filename.contains("/") )
115 filename = filename.post_rev('/');
116 }
117 } else {
118 filename = 0;
119 if ( verbose & VERB_LABEL ) cout << endl;
120 }
121
122 if ( replace_flag ) {
123 if ( replace_flag < 4 )
124 filesize = replace_a_character(buf, filesize, char_in, char_out);
125 else
126 filesize = delete_a_character(buf, filesize, char_in);
127 }
128
129 ofstream fout;
130 if ( filename.length() ) {
131 fout.open(filename.c_str());
132 if ( fout.fail() ) {
133 cerr << "Error: File " << filename << " not opened!" << endl;
134 bexit(-1);
135 }
136 if ( verbose ) {
137 cout << "Writing: " << filename << endl;
138 cout << "Output file size: " << filesize << endl;
139 }
140 fout.write((char *)buf, filesize);
141 fout.close();
142 } else
143 cout << buf << endl;
144
145 delete[] buf;
146
147 if ( verbose & VERB_TIME )
148 timer_report(ti);
149
150 bexit(0);
151 }
152
153 long replace_a_character(unsigned char* buf, long size, int chin, int chout)
154 {
155 long i, n;
156
157 for ( i=n=0; i<size; i++ ) {
158 if ( buf[i] == chin ) {
159 buf[i] = chout;
160 n++;
161 }
162 }
163
164 if ( verbose & VERB_PROCESS )
165 cout << "Characters replaced: " << n << endl << endl;
166
167 return i;
168 }
169
170 long delete_a_character(unsigned char* buf, long size, int c)
171 {
172 long i, j, n;
173
174 for ( i=j=n=0; i<size; i++ ) {
175 if ( buf[i] != c )
176 buf[j++] = buf[i];
177 else
178 n++;
179 }
180
181 buf[j] = 0;
182
183 if ( verbose & VERB_PROCESS )
184 cout << "Characters deleted: " << n << endl << endl;
185
186 return j;
187 }
188
verbose
int verbose
utilities.h
Header file for general utilities.
Boption::value
Bstring value
Definition: options.h:30
Boption::tag
Bstring tag
Definition: options.h:29
Bstring::length
long length() const
Definition: Bstring.cpp:263
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:107
get_option_list
Boption * get_option_list(const char *use[], int argc, char *argv[], int &optind)
Parses command line arguments based on a template.
Definition: options.cpp:40
get_option_verbose
int get_option_verbose(char *optarg)
Sets the verbose option.
Definition: options.cpp:143
timer.h
Utilities for timing functions.
delete_a_character
long delete_a_character(unsigned char *buf, long size, int c)
Definition: bfix.cpp:170
replace_a_character
long replace_a_character(unsigned char *buf, long size, int chin, int chout)
Definition: bfix.cpp:153
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:584
Boption
A class for a command line input option.
Definition: options.h:26
VERB_TIME
#define VERB_TIME
Definition: utilities.h:110
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
Bstring::post_rev
Bstring post_rev(char c)
Definition: Bstring.cpp:428
main
int main(int argc, char **argv)
Definition: bfix.cpp:43
Boption::next
Boption * next
Definition: options.h:28
Bstring
Definition: Bstring.h:29
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:67
use
const char * use[]
Definition: bfix.cpp:21
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:109
Bstring::integer
long integer()
Definition: Bstring.cpp:539
options.h
Header file for option handlers.
VERB_LABEL
#define VERB_LABEL
Definition: utilities.h:106
Boption::values
long values(T1 &v1, T2 &v2)
Definition: options.h:115
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:273
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:52