-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_writer.cpp
26 lines (22 loc) · 1.04 KB
/
file_writer.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
// FileUtils - File Writer Helper Functions
// by Frank Gennari
// 08/06/2022
#include <ostream>
using std::ostream;
template<typename T> void write_val(T const val, ostream &out) { // default
out << val;
}
void write_int_val(int val, ostream &out) {
if (val < 0 ) {out.put('-'); val = -val;} // negative
if (val < 10 ) {out.put('0' + char(val));} // 1 digit
else if (val < 100 ) {out.put('0' + char(val/10 )); out.put('0' + char(val%10));} // 2 digits
else if (val < 1000) {out.put('0' + char(val/100)); out.put('0' + char((val/10)%10)); out.put('0' + char(val%10));} // 3 digits
else {out << val;} // 4+ digits
}
void write_val(int const val, ostream &out) {write_int_val(val, out);}
void write_val(short const val, ostream &out) {write_int_val(val, out);}
void write_val(unsigned const val, ostream &out) {write_int_val(val, out);}
void write_val(unsigned short const val, ostream &out) {write_int_val(val, out);}
void write_val(bool const val, ostream &out) {
out.put(val ? '1' : '0');
}