mirror of
				https://github.com/limosek/zaf-plugins.git
				synced 2025-10-31 17:47:37 +01:00 
			
		
		
		
	CSV fields are counted from 1
This commit is contained in:
		
							
								
								
									
										3
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										3
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| @@ -1 +1,2 @@ | |||||||
| *~ | *~ | ||||||
|  | /nbproject/private/ | ||||||
							
								
								
									
										72
									
								
								csv/discovery
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										72
									
								
								csv/discovery
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,72 @@ | |||||||
|  | #!/usr/bin/php | ||||||
|  | <?php | ||||||
|  |  | ||||||
|  | require_once(__DIR__."/functions.inc.php"); | ||||||
|  |  | ||||||
|  | if ($argc<6) { | ||||||
|  | 	fprintf(STDERR,"Missing arguments!\n"); | ||||||
|  | 	fprintf(STDERR,"send file.csv delim mode hostfield [item1=field1] [item2=field2] ...\n"); | ||||||
|  | 	fprintf(STDERR,"mode is stdout or send,\n"); | ||||||
|  | 	fprintf(STDERR,"hostfield is host for item to send,\n"); | ||||||
|  | 	fprintf(STDERR,"itemx is key for item to send,\n"); | ||||||
|  | 	fprintf(STDERR,"fieldx is data to send send,\n"); | ||||||
|  | 	fprintf(STDERR,"In hostfield, itemx and fieldx are replaced this macros:\n"); | ||||||
|  | 	fprintf(STDERR,"{COLUMN:x} is replaced by value of column x\n"); | ||||||
|  | 	fprintf(STDERR,"{column:x} is replaced by lowercased value of column x\n"); | ||||||
|  | 	fprintf(STDERR,"x can be column index (x starts with zero) or header name.\n"); | ||||||
|  | 	fprintf(STDERR,"CSV must include header line.\n\n"); | ||||||
|  | 	exit(1); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | $csv=$argv[1]; | ||||||
|  | if ($csv=="-") { | ||||||
|  | 	$csv="php://stdin"; | ||||||
|  | } | ||||||
|  | $delim=$argv[2]; | ||||||
|  | $mode=$argv[3]; | ||||||
|  | $host=$argv[4]; | ||||||
|  | $items=Array(); | ||||||
|  | for ($i=5;$i<$argc;$i++) { | ||||||
|  | 	List($key,$field)=preg_split("/=/",$argv[$i]); | ||||||
|  | 	$items[]=Array( | ||||||
|  | 		"key" => $key, | ||||||
|  | 		"field" => $field | ||||||
|  | 	); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | $c=fopen($csv,"r"); | ||||||
|  |  | ||||||
|  | $header=fgetcsv($c,false,$delim); | ||||||
|  |  | ||||||
|  | $data=""; | ||||||
|  | while ($row=fgetcsv($c,false,$delim)) { | ||||||
|  | 	if (count($row)==1 && trim($row[0])=="") continue; | ||||||
|  | 	$p=get_replacements($header,$row); | ||||||
|  | 	$hostr=preg_replace($p["patterns"],$p["replacements"],$host); | ||||||
|  | 	foreach ($items as $item) { | ||||||
|  | 		$data.=sprintf("%s %s %s\n", | ||||||
|  | 			$hostr, | ||||||
|  | 			preg_replace($p["patterns"],$p["replacements"],$item["key"]), | ||||||
|  | 			addcslashes(preg_replace($p["patterns"],$p["replacements"],$item["field"]),"\0..\40\"") | ||||||
|  | 		); | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | fclose($c); | ||||||
|  |  | ||||||
|  | if ($mode=="send") { | ||||||
|  | 	$h = popen(getenv("ZAF_BIN")." send", "w"); | ||||||
|  | 	fputs($h,$data); | ||||||
|  | 	fclose($h); | ||||||
|  | 	exit; | ||||||
|  | } | ||||||
|  | if ($mode=="stdout") { | ||||||
|  | 	echo $data; | ||||||
|  | 	exit; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | fprintf(STDERR,"Bad mode $mode\n!"); | ||||||
|  | exit(1); | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
| @@ -1,28 +1,28 @@ | |||||||
| #!/usr/bin/php | #!/usr/bin/php | ||||||
| <?php | <?php | ||||||
|  | require_once(__DIR__ . "/functions.inc.php"); | ||||||
|  |  | ||||||
| require_once(__DIR__."/functions.inc.php"); | $csv = getenv("csv"); | ||||||
|  | $range = parse_colnum(getenv("columns")); | ||||||
|  | $rangef = array_flip($range); | ||||||
|  | $delim = getenv("delimiter"); | ||||||
|  |  | ||||||
| $csv=getenv("csv"); | $c = fopen($csv, "r"); | ||||||
| $range=parse_colnum(getenv("columns")); | $header = fgetcsv($c, false, $delim); | ||||||
| $rangef=array_flip($range); |  | ||||||
| $delim=getenv("delimiter"); |  | ||||||
|  |  | ||||||
| $c=fopen($csv,"r"); |  | ||||||
| $header=fgetcsv($c,false,$delim); |  | ||||||
| fclose($c); | fclose($c); | ||||||
|  |  | ||||||
| json_init(); | json_init(); | ||||||
|  |  | ||||||
| $last1=end($header); | $last1 = end($header); | ||||||
| $last2=end($range); | $last2 = end($range); | ||||||
|  |  | ||||||
| foreach ($header as $num=>$column) { | foreach ($header as $num => $column) { | ||||||
| 	if (!array_key_exists($num,$range)) continue; |     if (!array_key_exists($num, $range)) | ||||||
| 	json_row(); |         continue; | ||||||
| 	json_column("COLUMN",$num); |     json_row(); | ||||||
| 	json_column("NAME",addslashes($column),true); |     json_column("COLUMN", $num+1); | ||||||
| 	json_row_end($last1==$column||$last2==($num+1)); |     json_column("NAME", addslashes($column), true); | ||||||
|  |     json_row_end($last1 == $column || $last2 == ($num + 1)); | ||||||
| } | } | ||||||
|  |  | ||||||
| json_end(); | json_end(); | ||||||
|   | |||||||
| @@ -1,45 +1,50 @@ | |||||||
| #!/usr/bin/php | #!/usr/bin/php | ||||||
| <?php | <?php | ||||||
|  | require_once(__DIR__ . "/functions.inc.php"); | ||||||
|  |  | ||||||
| require_once(__DIR__."/functions.inc.php"); | $csv = getenv("csv"); | ||||||
|  | $range = parse_colnum(getenv("columns")); | ||||||
|  | $rows = parse_colnum(getenv("rows")); | ||||||
|  | $rowsf = array_flip($rows); | ||||||
|  | $rangef = array_flip($range); | ||||||
|  | $head = getenv("header"); | ||||||
|  | $delim = getenv("delimiter"); | ||||||
|  |  | ||||||
| $csv=getenv("csv"); | $c = fopen($csv, "r"); | ||||||
| $range=parse_colnum(getenv("columns")); |  | ||||||
| $rows=parse_colnum(getenv("rows")); |  | ||||||
| $rowsf=array_flip($rows); |  | ||||||
| $rangef=array_flip($range); |  | ||||||
| $head=getenv("header"); |  | ||||||
| $delim=getenv("delimiter"); |  | ||||||
|  |  | ||||||
| $c=fopen($csv,"r"); |  | ||||||
|  |  | ||||||
|  | $header = Array(); | ||||||
|  | $i = 1; | ||||||
| if ($head) { | if ($head) { | ||||||
| 	$header=fgetcsv($c,false,$delim); |     $h = fgetcsv($c, false, $delim); | ||||||
|  |     foreach ($h as $val) { | ||||||
|  |         $header[$i] = $val; | ||||||
|  |         $i++; | ||||||
|  |     } | ||||||
| } else { | } else { | ||||||
| 	$header=Array(); |     foreach ($range as $r) { | ||||||
| 	$i=0; |         $header[$i] = sprintf("FIELD%d", $i + 1); | ||||||
| 	foreach ($range as $r) { |         $i++; | ||||||
| 		$header[$i]=sprintf("FIELD%d",$i); |     } | ||||||
| 		$i++; |  | ||||||
| 	} |  | ||||||
| } | } | ||||||
|  |  | ||||||
| json_init(); | json_init(); | ||||||
| $line=0; | $line = 0; | ||||||
| $last=end($range); | $last = end($range); | ||||||
| $lastrow=max($rows); | $lastrow = max($rows); | ||||||
|  | $numrows = intval(exec("wc -l '$csv'")); | ||||||
|  |  | ||||||
| while ($row=fgetcsv($c,false,$delim)) { | while ($row = fgetcsv($c, false, $delim)) { | ||||||
| 	$line++; |     $line++; | ||||||
| 	if (!array_key_exists($line,$rowsf)) continue; |     if (!array_key_exists($line, $rowsf)) | ||||||
| 	json_row(); |         continue; | ||||||
| 	json_column("ROW","$line"); |     json_row(); | ||||||
| 	foreach ($range as $num) { |     json_column("ROW", "$line"); | ||||||
| 		$col=$num-1; |     foreach ($range as $col) { | ||||||
| 		json_column("COLUMN$col",$header[$col]); |         json_column("COLUMN$col", $header[$col]); | ||||||
| 		json_column("VALUE$col",addslashes($row[$col]),$last==$num); |         json_column("VALUE$col", addslashes($row[$col - 1]), $last == $col); | ||||||
| 	} |     } | ||||||
| 	json_row_end(feof($c)||$line==$lastrow); |     echo feof($c); | ||||||
|  |     json_row_end(feof($c) || $line == $lastrow || $line == $numrows-1); | ||||||
| } | } | ||||||
| json_end(); | json_end(); | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	