Repository navigation
Expand file tree
/
Copy pathconverter.php
More file actions
61 lines (49 loc) · 1.96 KB
/
Copy pathconverter.php
File metadata and controls
61 lines (49 loc) · 1.96 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
<?php
$files = scandir('original');
if (!file_exists("converted")) {
mkdir("converted");
}
foreach ($files as $file) {
if (in_array($file,['.','..'])) continue;
$headers = [];
$reachedData = false;
$relation = "";
$destination = false;
$origin = fopen("original/$file",'r');
$filename = (pathinfo("original/$file")['filename']);
while (($buffer = fgets($origin)) !== false) {
if (trim($buffer) === "" || substr(trim($buffer),0,1) === "%") continue;
if (!$reachedData) {
if (preg_match("/^@RELATION (')?(?'relation'[^']*?)(?(1)')$/i",
trim($buffer),$matches)) {
// $destination = fopen("converted/$filename-{$matches['relation']}.csv",'w+');
$destination = fopen("converted/$filename.csv",'w+');
} else if (preg_match("/^@ATTRIBUTE\s+(')?(?'attribute'(?(1)[^']*|[^'\s]*))(?(1)')\s.*$/i",
trim($buffer),$matches)) {
$headers[] = '"'.trim($matches['attribute']).'"';
} else if (preg_match("/^@DATA$/i",trim($buffer))) {
if (!is_resource($destination)) {
echo "ERROR processing $file: @DATA statement before @RELATION".PHP_EOL;
break;
}
if (count($headers) === 0) {
echo "ERROR processing $file: @DATA statement before any @ATTRIBUTE".PHP_EOL;
break;
}
fwrite($destination,join(',',$headers));
$reachedData = true;
}
} else {
if (strstr($buffer,',') === false) {
$buffer = preg_replace('/\s+/',',',trim($buffer));
}
fwrite($destination,PHP_EOL.preg_replace(
"/(['\"])?((?(1)[^'\"]*|[^'\",]+))(?(1)['\"])/","\"\\2\"",trim($buffer)
));
}
}
if (is_resource($destination)){
fclose($destination);
}
fclose($origin);
}