[ Index ]
 

Code source de Dotclear 2.0-beta6

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/plugins/importExport/ -> class.backupFile.php (source)

   1  <?php
   2  # ***** BEGIN LICENSE BLOCK *****
   3  # This file is part of DotClear.
   4  # Copyright (c) 2005 Olivier Meunier and contributors. All rights
   5  # reserved.
   6  #
   7  # DotClear is free software; you can redistribute it and/or modify
   8  # it under the terms of the GNU General Public License as published by
   9  # the Free Software Foundation; either version 2 of the License, or
  10  # (at your option) any later version.
  11  # 
  12  # DotClear is distributed in the hope that it will be useful,
  13  # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15  # GNU General Public License for more details.
  16  # 
  17  # You should have received a copy of the GNU General Public License
  18  # along with DotClear; if not, write to the Free Software
  19  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  20  #
  21  # ***** END LICENSE BLOCK *****
  22  
  23  class backupFile
  24  {
  25      protected $fp;
  26      private $line_cols = array();
  27      private $line_name;
  28      
  29      private $replacement = array(
  30          '\n' => "\n",
  31          '\r' => "\r",
  32          '\"' => '"'
  33      );
  34      
  35  	public function __construct($file)
  36      {
  37          if (file_exists($file) && is_readable($file)) {
  38              $this->fp = fopen($file,'rb');
  39          } else {
  40              throw new Exception(__('No file to read.'));
  41          }
  42      }
  43      
  44  	public function __destruct()
  45      {
  46          if ($this->fp) {
  47              fclose($this->fp);
  48          }
  49      }
  50      
  51  	public function getLine()
  52      {
  53          if (feof($this->fp)) {
  54              return false;
  55          }
  56          
  57          $line = trim(fgets($this->fp));
  58          
  59          if (substr($line,0,1) == '[')
  60          {
  61              $this->line_name = substr($line,1,strpos($line,' ')-1);
  62              
  63              $line = substr($line,strpos($line,' ')+1,-1);
  64              $this->line_cols = explode(',',$line);
  65              
  66              return $this->getLine();
  67          }
  68          elseif (substr($line,0,1) == '"')
  69          {
  70              $line = preg_replace('/^"|"$/','',$line);
  71              $line = preg_split('/(^"|","|"$)/m',$line);
  72              
  73              if (count($this->line_cols) != count($line)) {
  74                  throw new Exception('Invalid row count');
  75              }
  76              
  77              $res = array();
  78              
  79              for ($i=0; $i<count($line); $i++) {
  80                  $res[$this->line_cols[$i]] =
  81                  str_replace(array_keys($this->replacement),array_values($this->replacement),$line[$i]);
  82              }
  83              
  84              return new backupFileItem($this->line_name,$res);
  85          }
  86          else
  87          {
  88              return $this->getLine();
  89          }
  90      }
  91  }
  92  
  93  class backupFileItem
  94  {
  95      public $__name;
  96      private $__data = array();
  97      
  98  	public function __construct($name,$data)
  99      {
 100          $this->__name = $name;
 101          $this->__data = $data;
 102      }
 103      
 104      public function f($name)
 105      {
 106          return iconv('UTF-8','UTF-8//IGNORE',$this->__data[$name]);
 107      }
 108      
 109  	public function __get($name)
 110      {
 111          return $this->f($name);
 112      }
 113      
 114  	public function __set($n,$v)
 115      {
 116          $this->__data[$n] = $v;
 117      }
 118      
 119  	public function exists($n)
 120      {
 121          return isset($this->__data[$n]);
 122      }
 123      
 124  	public function drop()
 125      {
 126          foreach (func_get_args() as $n) {
 127              if (isset($this->__data[$n])) {
 128                  unset($this->__data[$n]);
 129              }
 130          }
 131      }
 132      
 133  	public function substitute($old,$new)
 134      {
 135          if (isset($this->__data[$old])) {
 136              $this->__data[$new] = $this->__data[$old];
 137              unset($this->__data[$old]);
 138          }
 139      }
 140  }
 141  ?>


Généré le : Fri Feb 23 22:16:06 2007 par Balluche grâce à PHPXref 0.7