-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtable.cc
More file actions
123 lines (103 loc) · 3.15 KB
/
Copy pathtable.cc
File metadata and controls
123 lines (103 loc) · 3.15 KB
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
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include "table.h"
#include "ast.h"
#include "parser.h"
using std::sort;
using std::max;
Row::Row(const Row& other) {
header_ = other.header_;
for (vector<Variant*>::const_iterator ci = other.row_.begin(); ci != other.row_.end(); ++ci) {
row_.push_back((*ci)->Clone());
}
}
Row::~Row() {
for (vector<Variant*>::iterator ci = row_.begin(); ci != row_.end(); ++ci) {
delete (*ci);
}
}
const Row& Row::operator=(const Row& other) {
header_ = other.header_;
for (vector<Variant*>::iterator ci = row_.begin(); ci != row_.end(); ++ci) {
delete (*ci);
}
row_.clear();
for (vector<Variant*>::const_iterator ci = other.row_.begin(); ci != other.row_.end(); ++ci) {
row_.push_back((*ci)->Clone());
}
return *this;
}
void Table::Print(bool head) {
int *width = new int[header_.size()];
for (uint32_t i = 0; i < header_.size(); i++) {
width[i] = 1;
for (vector<Row>::iterator ri = rows_.begin(); ri != rows_.end(); ++ri) {
Variant *var = ri->get(header_[i]);
if (var) {
int len = strlen(var->c_str());
width[i] = max(len, width[i]);
} else {
printf("column %s is not available\n", header_[i].c_str());
delete[] width;
return;
}
}
}
width[header_.size() - 1] = -1;
if (head) {
uint32_t i = 0;
for (vector<string>::iterator hi = header_.begin(); hi != header_.end(); ++hi) {
char fmt[10];
sprintf(fmt, "%%%d.*s ", width[i]);
printf(fmt, width[i++], hi->c_str());
}
printf("\n");
}
for (vector<Row>::iterator ri = rows_.begin(); ri != rows_.end(); ++ri) {
for (uint32_t i = 0; i < header_.size(); i++) {
char fmt[10];
sprintf(fmt, "%%%ds ", width[i]);
printf(fmt, ri->get(header_[i])->c_str());
}
printf("\n");
}
delete[] width;
}
class RowCompare {
public:
RowCompare(OrderList *orders) : orders_(orders) {}
bool operator()(const Row& lhs, const Row& rhs) {
for (uint32_t i = 0; i < orders_->attrs.size(); i++) {
int c = lhs.get(orders_->attrs[i])->Compare(rhs.get(orders_->attrs[i]));
if (c < 0 ) return orders_->dirs[i] == TK_ASC;
if (c > 0 ) return orders_->dirs[i] == TK_DESC;
}
return false;
}
private:
OrderList *orders_;
};
void Table::Sort(OrderList *orders) {
sort(rows_.begin(), rows_.end(), RowCompare(orders));
}
static vector<string> full_header_;
const vector<string>& FullHeader() {
if (full_header_.size() != 0 ) return full_header_;
full_header_.push_back(string("name"));
full_header_.push_back(string("path"));
full_header_.push_back(string("perms"));
full_header_.push_back(string("tperms"));
full_header_.push_back(string("nlink"));
full_header_.push_back(string("size"));
full_header_.push_back(string("inode"));
full_header_.push_back(string("uid"));
full_header_.push_back(string("gid"));
full_header_.push_back(string("uname"));
full_header_.push_back(string("gname"));
full_header_.push_back(string("time"));
full_header_.push_back(string("atime"));
full_header_.push_back(string("mtime"));
full_header_.push_back(string("ctime"));
return full_header_;
}