Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
csv2ini.php
Go to the documentation of this file.
1 <?php
2 
3 $inputPath = null;
4 $outputPath = null;
5 $dicts = array();
6 
7 foreach ($argv as $arg ){
8  if ( preg_match('/\.csv$/', $arg) ){
9  $inputPath = $arg;
10  continue;
11  }
12 
13  if ( preg_match('/\.php$/', $arg ) ) continue;
14 
15  if ( is_dir($arg) ){
16  $outputPath = dirname($arg).'/';
17  } else {
18  $outputPath = $arg.'.';
19  }
20 }
21 
22 if ( !isset($outputPath) ){
23 
24  fwrite( STDERR, "No output path was supplied.\n");
25  exit;
26 }
27 
28 
29 if ( !isset($inputPath) ){
30  fwrite(STDERR, "No input CSV file was specified.\n");
31  exit;
32 
33 }
34 
35 
36 
37 $fp = fopen($inputPath, 'r');
38 if ( !$fp ){
39  fwrite(STDERR, "The Input file $inputPath could not be opened for reading.\n");
40  exit;
41 }
42 
43 $filenames = fgetcsv($fp);
44 array_shift($filenames); // get rid of Key column.
45 $filenames = array_map('basename', $filenames);
46 foreach ( $filenames as $file ){
47  $dicts[$file] = array();
48 }
49 
50 while ( $row = fgetcsv($fp) ){
51  $key = array_shift($row);
52  foreach ( $filenames as $i=>$file ){
53  if ( isset($row[$i]) and !empty($row[$i]) ){
54  $dicts[$file][$key] = $row[$i];
55  }
56  }
57 }
58 fclose($fp);
59 
60 foreach ( $dicts as $file => $contents ){
61 
62  $df = fopen($outputPath.$file, 'w');
63  foreach ($contents as $key=>$val){
64  fwrite($df, $key.' = "'.str_replace('"', '"XATAFACEQ"', $val).'"'."\r\n");
65 
66  }
67  fclose($df);
68 
69 }
70 
71 fwrite(STDOUT, "Conversions complete.\n");
72 
73 
74 
75 
76 
77