-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileLog.class.php
179 lines (153 loc) · 4.08 KB
/
FileLog.class.php
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
<?php
/**
* Stores array data in simple tsv like file format.
* Retrives data by range, date, keyword search.
* https://github.com/hyeonseok/FileLog
*/
class FileLog {
private $structure;
private $file_name;
public function __construct($file_name) {
date_default_timezone_set('Asia/Seoul');
$this->file_name = $file_name;
if (file_exists($this->file_name)) {
$structure_string = fgets(fopen($this->file_name, 'r'));
$this->structure = explode("\t", trim($structure_string));
}
}
public function get_structure() {
return $this->structure;
}
private function create_file() {
fwrite(fopen($this->file_name, 'w'), implode("\t", $this->structure) . "\n");
}
private function format_row($row) {
$split = explode("\t", $row);
$row_data = array();
foreach ($this->structure as $key => $field) {
$row_data[$field] = $this->decode_string(trim($split[$key]));
}
return $row_data;
}
private function encode_string($str) {
$str = str_replace("\\", "\\\\", $str);
$str = str_replace("\t", "\\t", $str);
$str = str_replace("\r", "\\r", $str);
$str = str_replace("\n", "\\n", $str);
return $str;
}
private function decode_string($str) {
$temp = explode("\\\\", $str);
$count = count($temp);
for ($i = 0; $i < $count; $i++) {
$temp[$i] = str_replace("\\t", "\t", $temp[$i]);
$temp[$i] = str_replace("\\r", "\r", $temp[$i]);
$temp[$i] = str_replace("\\n", "\n", $temp[$i]);
}
return implode("\\", $temp);
}
public function save($data) {
if (!is_array($this->structure)) {
$this->structure = array_keys($data);
$this->create_file();
}
$fp = fopen($this->file_name, 'a');
$items = array();
foreach ($this->structure as $key => $value) {
$items[$value] = $this->encode_string($data[$value]);
}
$str = implode("\t", array_values($items)) . "\n";
fwrite($fp, $str);
fclose($fp);
}
public function load($offset = false, $length = false) {
$data = array();
$count = 0;
if (!file_exists($this->file_name)) {
return $data;
}
$fp = fopen($this->file_name, 'r');
while (($buffer = fgets($fp)) !== false) {
if ($count == 0) {
$count = 1;
continue;
}
$count++;
if (strlen(trim($buffer)) < 1) {
continue;
}
if ($offset !== false && $length !== false && $count - 1 < $offset + 1) {
continue;
}
if ($offset !== false && $length !== false && $count - 1 > $offset + $length) {
break;
}
array_push($data, $formatted_row = $this->format_row($buffer));
if ($offset !== false && $length === false && $offset < 0 && count($data) > $offset * -1) {
array_shift($data);
}
}
fclose($fp);
return $data;
}
public function load_by_match($keyword, $field_name = null, $unique = false) {
$data = array();
if (!file_exists($this->file_name)) {
return $data;
}
$fp = fopen($this->file_name, 'r');
while (($buffer = fgets($fp)) !== false) {
if (strlen(trim($buffer)) < 1) {
continue;
}
$formatted_row = $this->format_row($buffer);
if ($formatted_row[$field_name] != $keyword) {
continue;
}
array_push($data, $formatted_row);
}
fclose($fp);
if ($unique && count($data) > 0) {
$data = $data[count($data) - 1];
}
return $data;
}
public function load_by_search($keyword, $field_name = null) {
$data = array();
if (!file_exists($this->file_name)) {
return $data;
}
$fp = fopen($this->file_name, 'r');
while (($buffer = fgets($fp)) !== false) {
if (strlen(trim($buffer)) < 1) {
continue;
}
$formatted_row = $this->format_row($buffer);
if (strpos($formatted_row[$field_name], $keyword) === false) {
continue;
}
array_push($data, $formatted_row);
}
fclose($fp);
return $data;
}
public function get_max($field_name) {
if ($this->structure === null) {
return 0;
}
$fp = fopen($this->file_name, 'r');
$is_first = true;
$max = 0;
while (($buffer = fgets($fp)) !== false) {
if ($is_first || strlen(trim($buffer)) < 1) {
$is_first = false;
continue;
}
$formatted_row = $this->format_row($buffer);
$max = max($max, $formatted_row[$field_name]);
}
fclose($fp);
return $max;
}
}
?>