1: <?php
2:
3: namespace Liberty;
4:
5:
6: 7: 8: 9: 10: 11: 12: 13: 14: 15:
16:
17:
18:
19: class SSV {
20:
21: 22: 23: 24:
25: protected $ssv;
26:
27:
28: 29: 30: 31: 32: 33:
34: public function __construct($fullPath)
35: {
36: $this->ssv = $fullPath;
37:
38: if (!file_exists($this->ssv)) {
39: if($ssv->columnsCount() > 0) {
40: return true;
41: } else {
42: return "SSV file is not valid.";
43: }
44:
45: } else {
46: return "Cannot find " . $this->ssv . " file.";
47: }
48: }
49:
50:
51:
52:
53:
54:
55: 56: 57: 58: 59: 60:
61: public function add($array)
62: {
63:
64: foreach($array as $key => $value) {
65: $array[$key] = self::_translate($value);
66: }
67:
68: $columns = $this->columns();
69: $row = "";
70:
71: for($a=0; $a<count($columns); $a++) {
72: if(isset($array[$columns[$a]])) {
73: $row .= $array[$columns[$a]];
74: } else {
75: $row .= "<none/>";
76: }
77:
78: if ($a != (count($columns) - 1)) {
79: $row .= " ";
80: }
81: }
82:
83: $resource = fopen($this->ssv, "a");
84: fputs($resource, "$row\n");
85: fclose($resource);
86:
87: return true;
88: }
89:
90:
91:
92: 93: 94: 95: 96: 97: 98:
99: public function edit($line, $array)
100: {
101:
102: foreach($array as $key => $value) {
103: $array[$key] = self::_translate($value);
104: }
105:
106: $columns = $this->columns();
107: $row = "";
108:
109: for($a=0; $a<count($columns); $a++) {
110: if(isset($array[$columns[$a]])) {
111: $row .= $array[$columns[$a]];
112: } else {
113: $row .= "<none/>";
114: }
115:
116: if ($a != (count($columns) - 1)) {
117: $row .= " ";
118: }
119: }
120:
121: $rows = file($this->ssv);
122: $rows[$line] = "$row\n";
123:
124: $resource = fopen($this->ssv, "wb");
125:
126: foreach($rows as $value) {
127: fputs($resource, $value);
128: }
129:
130: fclose($resource);
131:
132: return true;
133: }
134:
135:
136: 137: 138: 139: 140: 141:
142: public function delete($line)
143: {
144: $rows = file($this->ssv);
145:
146: $resource = fopen($this->ssv, "wb");
147:
148: for($a=0; $a<count($rows); $a++) {
149: if($a != $line) {
150: fputs($resource, $rows[$a]);
151: }
152: }
153:
154: fclose($resource);
155:
156: return true;
157: }
158:
159:
160: 161: 162: 163: 164:
165: public function columns()
166: {
167: $resource = fopen($this->ssv, "rb");
168: $head = fgets($resource, 4096);
169: fclose($resource);
170:
171: $head = rtrim($head);
172: $columns = explode(" ", $head);
173: return $columns;
174: }
175:
176:
177: 178: 179: 180: 181:
182: public function columnsCount()
183: {
184: return count($this->columns());
185: }
186:
187:
188: 189: 190: 191: 192:
193: public function rowsCount()
194: {
195: $rows = file($this->ssv);
196: return count($rows) - 1;
197: }
198:
199:
200:
201:
202: 203: 204: 205: 206: 207:
208: public function row($line)
209: {
210: $rows = file($this->ssv);
211: if( $line > count($rows) - 1 ) return false;
212: if( $line == 0 ) return false;
213:
214: $records = explode(" ", rtrim($rows[$line]));
215:
216: for($a=0; $a<count($records); $a++) {
217: $records[$a] = self::_translate($records[$a]);
218: }
219:
220: $columnArray = $this->columns();
221:
222: for($a=0; $a<count($columnArray); $a++) {
223: if(!isset($records[$a])) {
224: $assoc[$columnArray[$a]] = "";
225: } else {
226: $assoc[$columnArray[$a]] = $records[$a];
227: }
228: }
229:
230: return $assoc;
231: }
232:
233:
234:
235:
236: 237: 238: 239: 240: 241: 242: 243:
244: public function rowsBy($column="", $order="ASC", $limit="0,100")
245: {
246: $rows = file($this->ssv);
247: $lim = explode(",", $limit);
248:
249: if($column == "") {
250: $c = 0;
251: } else {
252: $columns = $this->columns();
253: for($a=0; $a<count($columns); $a++) {
254: if( $column == $columns[$a] ) {
255: $c = $a;
256: break;
257: }
258: }
259: }
260:
261: for($a=1; $a<count($rows); $a++) {
262: $line = explode(" ", rtrim($rows[$a]));
263: $indexed[$a-1] = $line[$c] . " " . $a;
264: }
265:
266: if($order == "ASC") {
267: sort($indexed);
268: }
269:
270: if($order == "DESC") {
271: rsort($indexed);
272: }
273:
274: if(count($indexed) < $lim[1]) $lim[1] = count($indexed);
275: if(count($indexed) <= $lim[0]) return array();
276:
277: $b=0;
278: for($a=$lim[0]; $a<$lim[1]; $a++) {
279: $x = explode(" ", $indexed[$a]);
280: $index[$b] = $x[1];
281: $b++;
282: }
283:
284: return $index;
285: }
286:
287:
288:
289: 290: 291: 292: 293: 294: 295:
296: public function findBy($keyword, $column="")
297: {
298: if($keyword == "") return false;
299: $rows = file($this->ssv);
300: $columns = $this->columns();
301: $c = 0;
302: $index = array();
303: $int = 0;
304:
305: for($a=0; $a<count($columns); $a++) {
306: if( $column == $columns[$a] ) {
307: $c = $a;
308: break;
309: }
310: }
311:
312: for($a=1; $a<count($rows); $a++) {
313: $records = explode(" ", rtrim($rows[$a]));
314: $records[$c] = self::_translate($records[$c]);
315:
316: if(strpos($records[$c], $keyword) !== false) {
317: $index[$int] = $a;
318: $int++;
319: }
320: }
321:
322: return $index;
323: }
324:
325:
326:
327:
328: 329: 330: 331: 332: 333: 334:
335: public static function filecreate($ssvfile, $columns)
336: {
337: if (!file_exists($ssvfile)) {
338: $fp = fopen($ssvfile, "a");
339: $numelem = count($columns);
340: $string = "";
341:
342: for($a=0; $a<$numelem; $a++) {
343:
344:
345: $string .= self::_translate($columns[$a]);
346:
347: if ($a != ($numelem - 1)) {
348: $string .= " ";
349: }
350: }
351: fputs($fp, $string . "\n");
352: fclose($fp);
353:
354: return true;
355: } else {
356: return false;
357: }
358: }
359:
360:
361:
362:
363: 364: 365: 366: 367: 368:
369: public static function fileerase($ssvfile)
370: {
371: $resource = fopen($ssvfile, "rb");
372: $head = fgets($resource, 4096);
373: fclose($resource);
374: $resource = fopen($ssvfile, "wb");
375: fwrite($resource, $head);
376: fclose($resource);
377: return true;
378: }
379:
380:
381:
382:
383: 384: 385: 386: 387: 388:
389: public static function filedelete($ssvfile)
390: {
391: if(file_exists($ssvfile)) {
392: unlink($ssvfile);
393: return true;
394: }
395: }
396:
397:
398:
399:
400: protected static function _translate($string)
401: {
402: if($string == "") return "<none/>";
403: if($string == "<none/>") return "";
404:
405: $sp = array("<sp/>" => chr(0x20), chr(0x20) => "<sp/>");
406: $string = strtr($string, $sp);
407:
408: $lf = array("<lf/>" => "\n", "\n" => "<lf/>");
409: $string = strtr($string, $lf);
410: $cr = array("<cr/>" => "\r", "\r" => "<cr/>");
411: $string = strtr($string, $cr);
412: return $string;
413: }
414:
415: }
416:
417:
418: ?>
419: