-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBufferWrite.hh
71 lines (66 loc) · 1.67 KB
/
BufferWrite.hh
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
/*
Buffer for writing
*/
#pragma once
#include <string>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <stdio.h>
#include "DoubleToString.hh"
//#include "Components.hh"
class BufferWrite {
private:
char* postbuffer; //just after buffer
char* buffer;
char* phead;
int fn;
uint32_t BUFFER_SIZE = 32768; //32K
uint32_t POSTBUFFER_SIZE = 256;
void flushOnlyBuffer();
public:
void writeG0(double x, double y);
void writeG1(double x, double y, double e);
void flush();
BufferWrite(std::string& fileTarget) {
buffer = new char[BUFFER_SIZE + POSTBUFFER_SIZE];
postbuffer = buffer + BUFFER_SIZE;
phead = buffer;
remove(fileTarget.c_str());
creat(fileTarget.c_str(), S_IREAD | S_IWRITE);
fn = open(fileTarget.c_str(), O_WRONLY | O_APPEND);
}
~BufferWrite() {
flush();
delete[] buffer;
close(fn);
}
friend BufferWrite& operator <<(BufferWrite& buf, const char* ch){
//check buffer is full or not. If full, only flush the buffer
for(int i = 0; ch[i] != '\0'; i++, buf.phead++)
*buf.phead = ch[i];
return buf;
}
friend BufferWrite& operator <<(BufferWrite& buf, char ch){
*buf.phead = ch;
buf.phead++;
return buf;
}
friend BufferWrite& operator <<(BufferWrite& buf, std::string& str){
return buf << str.c_str();
}
friend BufferWrite& operator <<(BufferWrite& buf, double val){
#if 0
//TODO: translating double to string REALLY take so much time!!!
//TODO: make our own doubleToString()
//snprintf(buf.phead, 9, "%lf", val);
//buf.phead += 9;
std::string str = std::to_string(val);
return buf << str;
#endif
#if 1 //faster way for DoubleToString
buf.phead = formatDouble(val, buf.phead);
return buf;
#endif
}
};