-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhelper.cpp
More file actions
203 lines (170 loc) · 4.13 KB
/
Copy pathhelper.cpp
File metadata and controls
203 lines (170 loc) · 4.13 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
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
// Standard Includes
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cerrno>
#include <sstream>
#include <cstdlib>
#include <string>
// Namespace
using std::cerr;
using std::cout;
using std::exit;
using std::ofstream;
using std::string;
using std::stringstream;
// Custom headers
#include "helper.h"
#include "output.h"
/////////////////////////////////////////////
// Accessory functions for handling errors //
/////////////////////////////////////////////
void error (string const& s)
{
writeLog( "\n[Error] " + s + "\n");
exit( RETURN_ERROR );
}
void screenError (string const& s)
{
cerr << "\n[screen only] Error: " << s << "\n";
exit( RETURN_ERROR );
}
void shutdown()
{
exit( RETURN_GOOD );
}
////////////////////////////////////////////////////////
// Nicely print the program information on invocation //
////////////////////////////////////////////////////////
void writeHeader()
{
// size of copyright
unsigned int cRightSize = CRIGHTLINE.length() + 2;
// size of prog line
unsigned int progLineSize = 2 + PROGNAME.length() + 3 + VERSION.length() + RELEASE.length() + 2 + DATE.length() + 2;
// PROGNAME with space padding on internal side, VERSION & RELEASE space padded + 'v' for version
// 2 padding + DATE and two additional chars for '|' separators
unsigned int myLength = 0;
if (cRightSize > progLineSize)
{
unsigned int myLength = cRightSize;
// make spacer
string spacer = "|";
for (unsigned int i=0; i<myLength; ++i)
{
spacer += "-";
}
spacer += "|";
// make pad
string pad = "";
for (unsigned int i = 0; i < (myLength - progLineSize); ++i)
{
pad += " ";
}
// writeLog in output. LOG declared in main file
writeLog(
"\n"
"" + spacer + "\n"
"| " + PROGNAME + " | v" + VERSION + RELEASE + " | " + DATE + pad + " |\n"
"" + spacer + "\n"
"| " + CRIGHTLINE + " |\n"
"" + spacer + "\n\n");
}
else
{
myLength = progLineSize;
// make spacer
string spacer = "|";
for (unsigned int i=0; i<myLength; ++i)
{
spacer += "-";
}
spacer += "|";
// make pad
string pad = "";
for (unsigned int i = 0; i < (myLength - cRightSize); ++i)
{
pad += " ";
}
// writeLog in output. LOG declared in main file
writeLog(
"\n"
"" + spacer + "\n"
"| " + PROGNAME + " | v" + VERSION + RELEASE + " | " + DATE + " |\n"
"" + spacer + "\n"
"| " + CRIGHTLINE + pad + " |\n"
"" + spacer + "\n\n");
}
}
//////////////////////////////////////////////////
// Accessory function for file reading: //
// Reads a character at a time. Puts characters //
// together in a string and returns a bool for //
// whether a string was actually read //
//////////////////////////////////////////////////
bool readString (ifstream& f, string& s)
{
s = "";
char c = ' ';
while (!f.eof())
{
f.get( c );
if (c == ' ' or c == '\t' or c == '\n' or f.eof()) { break; }
else if (c == '\r') { continue; }
else { s += c; }
}
return s.length() != 0 ? true : false;
}
bool readWhiteSpacePaddedString( ifstream& f, string& s, char& c )
{
s = "";
c = ' ';
while (!f.eof())
{
f.get( c );
if (c == '\n' or f.eof()) { break; }
else if (c == '\r') { continue; }
else if (c == ' ' or c == '\t')
{
if (s.length() != 0) { return true; } // short circuits to return b/c a break would perform the same test again
else { continue; }
}
else { s += c; }
}
return s.length() != 0 ? true : false;
}
bool readChar( ifstream& f, char& c )
{
c = ' ';
while (!f.eof())
{
f.get( c );
if (c == '\n' or f.eof()) { return false; }
else if (c == ' ' or c == '\t' or c == '\r') { continue; }
else { return true; }
}
return false;
}
void readCommentLine( ifstream& f )
{
char c = ' ';
while (c != '\n' and !f.eof()) { f.get( c ); }
}
int string2Int( string const& s )
{
stringstream ss( s );
int i = 0;
if ( (ss >> i).fail() ) error( "Program died converting string to int" );
return i;
}
double string2Double( string const& s )
{
stringstream ss( s );
double d = 0.0;
if ( (ss >> d).fail() ) error( "Program died converting string to double" );
return d;
}
string string2Filename( string const& s )
{
return "[ " + s + " ]";
}