-
Notifications
You must be signed in to change notification settings - Fork 0
/
Report.cpp
99 lines (85 loc) · 2.76 KB
/
Report.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
/*
* Report.cpp
*
* Created on: 18 Dec, 2014
* Author: Mehrdad Tahernia
*/
#include "Report.h" //reportbuf
#include "Functions.h" //isdoubledigit
#include <string.h> //strcmp
////////////////////////////////////////////////////////////////////////////
// Cout
////////////////////////////////////////////////////////////////////////////
reportbuf ReportBuf;
std::ostream ReportOut(&ReportBuf);
//=========================================================================================
void reportbuf::OpenFile(char *FileName) {
// Make lowercase copy of FileName
char buffer[1000];
int i;
for (i = 0; FileName[i] != '\0'; i++)
buffer[i] = tolower(FileName[i]);
buffer[i] = '\0';
// If Filename is "err"
if (strcmp(buffer, "err") == 0)
IsDirectedToErr = /*true*/1;
else
fp = fopen(FileName, "w");
};
//=========================================================================================
int reportbuf::overflow(int nCh) {
if (!IsDirectedToErr) {
if (fp == NULL) {
printf("Reportbuf::overflow: file not opened\n");
exit(1);
}
putchar(nCh); // Display the output
fputc(nCh, fp); // Write the output to file buffer
fflush(fp); // Flush the buffer to file
}
else
{ // Just write output to stderr
fputc(nCh, stderr);
}
return 0;
};
//=========================================================================================
int reportbuf::underflow() {
if (!IsDirectedToErr)
fflush(fp);
return 0;
}
//=========================================================================================
//=========================================================================================
// These operator overloads are used in reading configurations from file
// Operator Overload for >> (reads the first number in file and writes it in d)
std::ifstream &operator>>(std::ifstream &file, double &d) {
static char buffer[100];
int i = 0;
// Skip until you reach to a number
while (!is_double_digit(file.peek()))
file.get();
// Read the number until end of that number
do
file >> buffer[i++];
while (is_double_digit(file.peek()));
// Specify end of the string read in previous part
buffer[i] = '\0';
d = atof(buffer); //Convert string to double
return file;
}
//=========================================================================================
// Operator Overload for >> (reads the first number in file and writes it in d) for integers
std::ifstream &operator>>(std::ifstream &file, int &num) {
static char buffer[100];
int i = 0;
while (!is_double_digit(file.peek()) || (file.peek() == 'e'))
file.get();
do
file >> buffer[i++];
while (is_double_digit(file.peek()));
buffer[i] = '\0';
num = atoi(buffer);
return file;
}
//=========================================================================================