#!/usr/bin/php
<?php // version="5+"

   /*
    *   Guess codepage - adaptive version
    *   A tool which determines in which codepage is the input
    *
    *   (C) 2005, Rozsnyo Daniel <daniel@rozsnyo.com>
    *
    *   This modification reads 4KB blocks while the codepage could not be determined
    *
    */

    
class codepageGuess {
    
        const   
BLOCKSIZE 4096;
    
        private 
$handle;
        private 
$counts = array();
        
        public static function 
fromFile$filename ) {
            
$guess = new codepageGuessfopen($filename,'rb') );
            
$cp    $guess->doGuess();
            
fclose($guess->handle);
            return 
$cp;
        }
        
        private function 
__construct$handle ) {
            
// initialize input
            
$this->handle $handle;
            
// initialize histogram
            
for($i=0$i<=255$i++) $this->counts[$i] = 0;
        }
        
        private function 
processInputBlock() {
            
$toGo self::BLOCKSIZE;
            while((
$toGo-->0)&&($c=fgetc($this->handle))!==false$this->counts[ord($c)]++;
            return 
$toGo<0;
        }
        
        private function 
doGuess() {
            while(
true) {
                
// read a bit..
                
$more $this->processInputBlock();
                
// make indices
                
$iso  $this->counts[0xA9]+$this->counts[0xAE]+$this->counts[0xB9]+$this->counts[0xBE];
                
$utf  $this->counts[0xC3]+$this->counts[0xC4]+$this->counts[0xC5]+$this->counts[0xC6];
                
$win  $this->counts[0x8A]+$this->counts[0x8E]+$this->counts[0x9A]+$this->counts[0x9E];
                
// guess
                
if ( $utf > ($iso+$win) ) return 'UTF-8';
                if ( 
$iso $win ) return 'ISO-8859-2';
                if ( 
$iso $win ) return 'WINDOWS-1250';
                
// ..or not
                
if (!$more) return 'WINDOWS-1250'// default guess due to our geographical location
            
}
        }
        
    }
    
    echo 
codepageGuess::fromFile('php://stdin'), "\n";
    
?>