-
Notifications
You must be signed in to change notification settings - Fork 0
/
tlv.h
241 lines (218 loc) · 7.27 KB
/
tlv.h
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/* By BC van Zuiden -- Leiden, February 2016 */
/* Probably very buggy USE AT OWN RISK this will brick everything you own */
/* NOBODY but YOU is liable for anything that happened in result of using this */
/* WARNING: DON'T RUN THIS PROGRAM THIS WILL DESTROY YOUR COMPUTER AND/OR HOUSE */
/* Any copyrighted piece of code within this code is NOT mine */
/* Inclusion of that code is forced upon me by a scary anonymous guy with a gun*/
/* https://github.com/originalsouth/tlv */
#ifndef __TLV__
#define __TLV__
#if __cplusplus < 201103L
#error "C++11 not detetected: tlv requires C++11 to work (update your compiler)."
#else
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string>
#include <vector>
namespace tlv
{
struct svg
{
double width,height;
std::string header;
std::vector<std::string> data;
svg();
svg(double x);
svg(double w,double h);
bool write(std::string fout_name);
void prepend(svg *img);
void append(svg *img);
void import(svg *img);
void clear();
};
template<typename ...Args> bool tlv_hdr(svg *img,std::string str,Args... args);
template<typename ...Args> bool tlv_drw(svg *img,std::string str,Args... args);
template<typename ...Args> bool tlv_opn(svg *img,std::string str,Args... args);
bool tlv_cls(svg *img,std::string str);
template<typename ...Args> bool tlv_yfo(svg *img,std::string marker,std::string content,std::string str,Args... args);
template<typename ...Args> bool tlv_obj(svg *img,std::string marker,svg *object,std::string str,Args... args);
template<typename ...Args,typename lambda> bool tlv_for(svg *img,std::string marker,lambda x,std::string str,Args... args);
std::string rgb(unsigned char r,unsigned char g,unsigned char b);
std::string hsv(double h,double s,double v);
}
tlv::svg::svg()
{
width=height=1e3;
tlv_hdr(this,"width=\"%g\" height=\"%g\"",width,height);
}
tlv::svg::svg(double x)
{
width=height=x;
tlv_hdr(this,"width=\"%g\" height=\"%g\"",width,height);
}
tlv::svg::svg(double w,double h)
{
width=w,height=h;
tlv_hdr(this,"width=\"%g\" height=\"%g\"",width,height);
}
bool tlv::svg::write(std::string fout_name)
{
FILE *fout=fopen(fout_name.c_str(),"w");
if(fout)
{
fprintf(fout,"%s\n",header.c_str());
for(std::string str: data) fprintf(fout,"%s\n",str.c_str());
fprintf(fout,"</svg>\n");
fclose(fout);
return true;
}
else return false;
}
void tlv::svg::prepend(svg *img)
{
data.insert(data.begin(),img->data.begin(),img->data.end());
}
void tlv::svg::append(svg *img)
{
data.insert(data.end(),img->data.begin(),img->data.end());
}
void tlv::svg::import(svg *img)
{
data=img->data;
}
void tlv::svg::clear()
{
data.clear();
}
template<typename ...Args> bool tlv::tlv_hdr(svg *img,std::string str,Args... args)
{
const std::string svd="<svg xmlns=\"http://www.w3.org/2000/svg\" "+str+">";
const size_t size=1+snprintf(nullptr,0,svd.c_str(),args...);
char *buffer=new(std::nothrow) char[size];
if(buffer==nullptr) return false;
else
{
snprintf(buffer,size,svd.c_str(),args...);
img->header=std::string(buffer);
delete[] buffer;
return true;
}
}
template<typename ...Args> bool tlv::tlv_drw(svg *img,std::string str,Args... args)
{
const std::string svd="<"+str+"/>";
const size_t size=1+snprintf(nullptr,0,svd.c_str(),args...);
char *buffer=new(std::nothrow) char[size];
if(buffer==nullptr) return false;
else
{
snprintf(buffer,size,svd.c_str(),args...);
img->data.push_back(std::string(buffer));
delete[] buffer;
return true;
}
}
template<typename ...Args> bool tlv::tlv_opn(svg *img,std::string str,Args... args)
{
const std::string svd="<"+str+">";
const size_t size=1+snprintf(nullptr,0,svd.c_str(),args...);
char *buffer=new(std::nothrow) char[size];
if(buffer==nullptr) return false;
else
{
snprintf(buffer,size,svd.c_str(),args...);
img->data.push_back(std::string(buffer));
delete[] buffer;
return true;
}
}
bool tlv::tlv_cls(svg *img,std::string str)
{
const std::string svd="</"+str+">";
img->data.push_back(svd);
return true;
}
template<typename ...Args> bool tlv::tlv_yfo(svg *img,std::string marker,std::string content,std::string str,Args... args)
{
const std::string svd="<"+marker+" "+str+">";
const size_t size=1+snprintf(nullptr,0,svd.c_str(),args...);
char *buffer=new(std::nothrow) char[size];
if(buffer==nullptr) return false;
else
{
snprintf(buffer,size,svd.c_str(),args...);
img->data.push_back(std::string(buffer)+content+std::string("</")+marker+std::string(">"));
delete[] buffer;
return true;
}
}
template<typename ...Args> bool tlv::tlv_obj(svg *img,std::string marker,svg *object,std::string str,Args... args)
{
const std::string svd="<"+marker+" "+str+">";
const size_t size=1+snprintf(nullptr,0,svd.c_str(),args...);
char *buffer=new(std::nothrow) char[size];
if(buffer==nullptr) return false;
else
{
snprintf(buffer,size,svd.c_str(),args...);
img->data.push_back(std::string(buffer));
img->append(object);
img->data.push_back(std::string("</")+marker+std::string(">"));
delete[] buffer;
return true;
}
}
template<typename ...Args,typename lambda> bool tlv::tlv_for(svg *img,std::string marker,lambda x,std::string str,Args... args)
{
const std::string svd="<"+marker+" "+str+">";
const size_t size=1+snprintf(nullptr,0,svd.c_str(),args...);
char *buffer=new(std::nothrow) char[size];
if(buffer==nullptr) return false;
else
{
snprintf(buffer,size,svd.c_str(),args...);
img->data.push_back(std::string(buffer));
x();
img->data.push_back(std::string("</")+marker+std::string(">"));
delete[] buffer;
return true;
}
}
std::string tlv::rgb(unsigned char r,unsigned char g,unsigned char b)
{
char cstr[3];
std::string retval=std::string("#");
snprintf(cstr,3,"%02X",r);
retval+=std::string(cstr);
snprintf(cstr,3,"%02X",g);
retval+=std::string(cstr);
snprintf(cstr,3,"%02X",b);
retval+=std::string(cstr);
return retval;
}
std::string tlv::hsv(double h,double s,double v)
{
h/=60.0,v*=256.0;
if(v>255.0) v=255.0;
const double f=h-std::floor(h),p=v*(1.0-s),q=v*(1.0-s*f),t=v*(1.0-(1.0-f)*s);
switch((int)h)
{
case 0: return tlv::rgb(v,t,p);
case 1: return tlv::rgb(q,v,p);
case 2: return tlv::rgb(p,v,t);
case 3: return tlv::rgb(p,q,v);
case 4: return tlv::rgb(t,p,v);
case 5: return tlv::rgb(v,p,q);
default: return tlv::rgb(0,0,0);
}
}
#define tlv_hdr(img,str,...) tlv::tlv_hdr(img,#str,##__VA_ARGS__)
#define tlv_drw(img,str,...) tlv::tlv_drw(img,#str,##__VA_ARGS__)
#define tlv_opn(img,str,...) tlv::tlv_opn(img,#str,##__VA_ARGS__)
#define tlv_cls(img,str,...) tlv::tlv_cls(img,#str,##__VA_ARGS__)
#define tlv_yfo(img,mrk,txt,str,...) tlv::tlv_yfo(img,mrk,txt,#str,##__VA_ARGS__)
#define tlv_obj(img,mrk,object,str,...) tlv::tlv_obj(img,mrk,object,#str,##__VA_ARGS__)
#define tlv_for(img,mrk,lambda,str,...) tlv::tlv_for(img,mrk,lambda,#str,##__VA_ARGS__)
#endif
#endif