-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tuple.cpp
executable file
·94 lines (78 loc) · 1.9 KB
/
Tuple.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
#include <new>
#include "Tuple.h"
Tuple::Tuple()
{
m_data = NULL;
m_schema = NULL;
}
void Tuple::schema(const Schema * schema)
{
m_schema = schema;
}
const Schema * Tuple::schema() const
{
return m_schema;
}
void Tuple::value(int * buffer, const Attribute & attribute) const
{
value((void *)buffer, attribute);
}
void Tuple::value(char * buffer, const Attribute & attribute) const
{
value((void *)buffer, attribute);
}
void Tuple::value(int * buffer, const std::string & field) const
{
value((void *)buffer, field);
}
void Tuple::value(char * buffer, const std::string & field) const
{
value((void *)buffer, field);
}
void Tuple::value(void * buffer, const std::string & field) const
{
value((void *)buffer, *(*m_schema)[field]);
}
void Tuple::value(void * buffer, const Attribute & attribute) const
{
memset(buffer, 0, attribute.size());
memcpy(buffer, m_data + m_schema->offset(&attribute), attribute.size());
}
void Tuple::map(const Tuple * other)
{
int offset = 0;
memset(m_data, 0, m_schema->rsize());
for (int i = 0; i < m_schema->nitems(); i++)
{
const Attribute * attribute = m_schema->at(i);
other->value(m_data + offset, *attribute);
offset += attribute->size();
}
}
void Tuple::dump(std::ostream & output, char fs, char rs) const
{
int offset = 0;
char * buffer = new char[m_schema->rsize() + 1]; // + 1 for eof marker
for (int i = 0; i < m_schema->nitems(); i++)
{
const Attribute * attribute = m_schema->at(i);
switch (attribute->type())
{
case BIT:
case CHAR:
output << m_data[offset];
break;
case INTEGER:
output << *(int *)(m_data + offset);
break;
case STRING:
memset(buffer, 0, m_schema->rsize() + 1);
memcpy(buffer, m_data + offset, attribute->size());
output << buffer;
break;
}
offset += attribute->size();
output << (i < m_schema->nitems() - 1 ? fs : rs);
}
delete [] buffer;
}