-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv-to-flat.php
More file actions
executable file
·115 lines (104 loc) · 2.97 KB
/
Copy pathcsv-to-flat.php
File metadata and controls
executable file
·115 lines (104 loc) · 2.97 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
#!/usr/bin/php -f
<?php
$arguments = $argv;
array_shift($arguments);
// echo "ARGV[0]=[$argv[0]]\n";
// echo "ARGUMENTS[0]=[$arguments[0]]\n";
$sep = ',';
if ($arguments[0] == '-d') {
$dummy = array_shift($arguments);
$sep = array_shift($arguments);
}
// echo "SEP=[$sep]\n";
$key_indices = array(1);
if ($arguments[0] == '-k') {
array_shift($arguments);
$key_indices = explode(',', array_shift($arguments));
}
$string_content_re = '([^"]|"")*';
$field_re = '("' . $string_content_re . '"|[^' . $sep . '"]*)[' . $sep . "\n]";
$pattern = '/' . $field_re . '/';
// echo 'PATTERN: [' . $pattern . "]\n";
$incomplete_pattern = '/^(' . $field_re . ')*"' . $string_content_re . '$/';
$input_file = fopen('php://stdin', 'r');
$output_file = fopen('php://stdout', 'w');
function format_record($field_names, $record) {
$fields = array();
$first = True;
foreach ($field_names as $field_name) {
$field = $record[$field_name];
if ($field) {
$field = '"' . preg_replace('/"/', '""', $field) . '"';
}
array_push($fields, $field);
}
return implode(',', $fields);
}
function fgetcleans($file) {
$line = fgets($file);
$len = strlen($line);
if ($len > 1 && substr($line, $len - 2) == "\r\n") {
$line = substr($line, 0, $len - 2) . "\n";
}
return $line;
}
$field_names = array('?');
$first = true;
while ($line = fgetcleans($input_file)) {
while (preg_match($incomplete_pattern, $line) > 0) {
$line = $line . fgetcleans($input_file);
// echo '>>> ' . $line;
}
// echo '>>> ' . substr(rtrim($line), 0, 60) . "\n";
$pos = 0;
$fields = array();
$matches = array();
while (preg_match($pattern, $line, $matches, PREG_OFFSET_CAPTURE, $pos) > 0) {
$match = $matches[1];
if ($match[1] != $pos) {
echo "ERROR: " . $pos . ": " . print_r($matches, true) . "[" . substr($line, $pos) . "]\n";
exit(1);
}
// print_r($match);
$field = $match[0];
// echo '1.Field: [' . $field . "]\n";
// echo '[' . substr($field, 0, 1) . "]\n";
if (substr($field, 0, 1) == '"') {
// echo "String!\n";
$field = substr($field, 1, strlen($field) - 2);
}
// echo '2.Field: [' . $field . "]\n";
$field = preg_replace('/""/', '"', $field);
// echo ' Field: [' . $field . "]\n";
$pos = $pos + strlen($matches[0][0]);
array_push($fields,$field);
}
if ($first) {
$field_names = $fields;
$first = false;
} else {
$prefix = array();
foreach ($key_indices as $key_index) {
$prefix[] = $fields[$key_index - 1];
}
$prefix = implode('/', $prefix);
foreach ($fields as $column_index => $value) {
$column_nr = $column_index + 1;
$column_name = $field_names[$column_index];
$line_prefix = "${prefix}[$column_nr:$column_name]";
if (strpos($value, "\n")) {
$lines = explode("\n", $value);
foreach ($lines as $line_idx => $line) {
$line_nr = $line_idx + 1;
fwrite($output_file, "$line_prefix:$line_nr: $line\n");
}
} else {
fwrite($output_file, "$line_prefix:: $value\n");
}
}
fwrite($output_file, "\n");
}
}
fclose($output_file);
fclose($input_file);
?>