PHP Classes

File: cmd_class_php5.inc

Recommend this page to a friend!
  Classes of Steffen Stollfu▀  >  cmd class  >  cmd_class_php5.inc  >  Download  
File: cmd_class_php5.inc
Role: Class source
Content type: text/plain
Description: Class to handle command line in and output with php5
Class: cmd class
Access input/output data from command line scripts
Author: By
Last change: - fixed a typo in function name for scientific numbers
Date: 7 years ago
Size: 5,048 bytes
 

Contents

Class file image Download
<?php
/***************************************************************************
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 ***************************************************************************/
/**
 * Class to handle command line in and output.
 *
 * @author Steffen 'j0inty' Stollfu├č
 * @class CommandLine
 */

class CommandLine
{
   
/**
     * Write the $str to the given $handle
     *
     * @param string $str
     * @param resource $handle STDOUT|STDERR
     * @return int|false
     *
     * @see fwrite() function documentation
     */
   
static public function write( $str, $handle = STDOUT)
    {
        return @
fwrite($handle, $str, strlen($str));
    }
   
/**
     * Append PHP_EOL to $str and write the line to the given handle
     *
     * @param string $str
     * @param resource $handle STDOUT|STDERR
     * @return int|false
     */
   
static public function writeline( $line, $handle = STDOUT)
    {
        return
self::write($line. PHP_EOL, $handle);
    }
   
/**
     * Write the given lines given as an array to the given handle
     *
     * @param array $lines
     * @param resource $handle STDOUT|STDERR
     * @return int|false
     */
   
static public function writelines( array &$lines, $handle = STDOUT)
    {
       
$bytes_written = 0;
        foreach (
$lines as &$line)
        {
            if( !
$bytes = self::writeline($line, $handle) )
            {
                return
false;
            }
           
$bytes_written += $bytes;
        }
        return
$bytes_written;
    }
   
/**
     * Read data from STDIN with fscanf() until $end is reached
     *
     * @param mixed &$data
     * @param string $pattern
     * @param string $end
     *
     * @return string|boolean
     */
   
static public function read( &$data, $pattern = "%s", $end )
    {
       
self::fscanf($pattern . $end, $data);
    }
   
/**
     * Read a line from STDIN with fscanf() which ended with PHP_EOL as default.
     * You can choose another lineend by the second parameter
     *
     * @param type $end
     * @return type
     */
   
static public function readLine(&$line, $end = PHP_EOL)
    {
        return
self::read($line, "%s", $end);
    }
   
   
/**
     * Read an integer from STDIN with fscanf()
     *
     * @param integer $number
     * @param boolean $unsigned
     * @param string $end
     */
   
static public function readInt(&$number, $unsigned = false, $end = PHP_EOL)
    {
       
self::fscanf( (($unsigned) ? "%u" : "%d") . $end, $number);
    }
   
/**
     * Read a floating number from STDIN with fscanf()
     *
     * @param float $number
     * @param boolean $aware_local_settings
     * @param string $end
     */
   
static public function readFloat(&$number, $aware_local_settings = true, $end = PHP_EOL)
    {
       
self::fscanf( (($aware_local_settings) ? "%f" : "%F" ) . $end, $number);
    }
   
/**
     * Read a hexadecimal number from STDIN with fscanf()
     *
     * @param type $hex
     * @param boolean $upperCase
     * @param string $end
     */
   
static public function readHex(&$hex, $upperCase = false, $end = PHP_EOL)
    {
       
self::fscanf( (($upperCase) ? "%X" : "%x") . $end, $hex);
    }
   
/**
     * Read a octal number from STDIN with fscanf()
     *
     * @param array $octal
     */
   
static public function readOctal(&$octal, $end = PHP_EOL)
    {
       
self::fscanf("%o".$end, $octal);
    }
   
/**
     * Read a binary number from STDIN with fscanf()
     *
     * @param array $binary
     * @param string $end
     */
   
static public function readBinary(&$binary, $end = PHP_EOL)
    {
       
self::fscanf("%b".$end, $binary);
    }
   
/**
     * Read a ascii from STDIN with fscanf() and return the according character
     *
     * @param array $char
     * @param string $end
     */
   
static public function readAsciiToChar(&$char, $end = PHP_EOL)
    {
       
self::fscanf("%c".$end, $char);
    }
   
/**
     * Read a scientific number with fscanf()
     *
     * Scientific notation (e.g. 1.2e+2)
     *
     * @param string &$number
     * @param string $end
     */
   
static public function readScientificNumber(&$number, $end = PHP_EOL)
    {
       
self::fscanf("%e" . $end, $number);
    }
   
/**
     * Security Note:
     *
     * Although it is a very powerful technique, keep in mind that it is easily deceived.
     * Many successful exploits have been based on scanf attacks.
     * It should not be used on untrusted input without a lot of additional validation.
     *
     * @param type $pattern
     * @param mixed $data
     */
   
static public function fscanf($pattern, &$data)
    {
        @
fscanf(STDIN, $pattern, $data);
    }
   
/**
     * Read a csv line from STDIN with fgetcsv()
     *
     * @see fgetcsv()
     *
     * @param type &$arrCSV
     * @param type $length
     * @param type $delimiter
     * @param type $enclosure
     * @param type $escape
     */
   
static public function readCSV(&$arrCSV, $length = 0, $delimiter = ",", $enclosure = "\"", $escape = "\\")
    {
       
stream_set_blocking(STDIN, 0);
       
$arrCSV = fgetcsv(STDIN, $length, $delimiter, $enclosure, $escape);
       
stream_set_blocking(STDIN, 1);
    }
}

For more information send a message to info at phpclasses dot org.