-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathout.cpp
89 lines (71 loc) · 1.69 KB
/
out.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
/*
* =====================================================================================
*
* Filename: out.cpp
*
* Description:
*
* Version: 1.0
* Created: 04/28/2020 10:04:07
* Revision: none
* Compiler: gcc
*
* Author: Andy (), [email protected]
* Company:
*
* =====================================================================================
*/
#include "out.h"
Out::Out(const char *filename) {
if (filename == NULL || strlen(filename) == 0) {
handle = STDOUT_FILENO;
noisy = true;
return;
}
handle = open(filename, O_CREAT | O_RDWR | O_APPEND, FILE_MODE);
noisy = true;
}
Out::~Out() { close(handle); }
void Out::setHandle(int fd) { handle = fd; }
int Out::setHandle(const char *filename) {
if ((handle = open(filename, O_CREAT | O_RDWR | O_APPEND, FILE_MODE)) < 0) {
perror(filename);
return errno;
}
return 0;
}
int Out::getHandle() { return handle; }
void Out::on() { noisy = true; }
void Out::off() { noisy = false; }
int Out::print(const char *str) {
if (noisy == false) {
return 0;
}
if (handle < 0) {
return -1;
}
if ((write(handle, str, strlen(str))) != (int)strlen(str)) {
return errno;
}
return 0;
}
int Out::get(char *destr) {
if (handle < 0) {
return -1;
}
lseek(handle, 0, SEEK_SET);
if ((read(handle, destr, 1024)) < 0) {
return errno;
}
return 0;
}
Out &Out::operator<<(const string &s) {
this->print(s.c_str());
return *this;
}
Out &Out::operator<<(const char &c) {
char s[2];
sprintf(s, "%c", c);
this->print(s);
return *this;
}