<commit_before><?php
/* This class is part of the XP framework
 *
 * $Id$
 */

  uses('util.mp3.ID3Genre');
  
  // Versions 
  define('ID3_VERSION_UNKNOWN',               '?');
  define('ID3_VERSION_1',                     '1');
  define('ID3_VERSION_1_1',                 '1.1');
  define('ID3_VERSION_2',                     '2');
  
  /**
   * This class represents an ID3 tag
   *
   * Usage:
   * <code>
   *   // [...]
   *   $mp3= &new MP3File(new File($file));
   *   try(); {
   *     $id3= &$mp3->getID3Tag();
   *   } if (catch('Exception', $e)) {
   *     $e->printStackTrace();
   *     exit();
   *   }
   *   echo $id3->toString();
   * </code>
   *
   * @purpose  ID3 Tag object
   * @see      http://www.id3.org/id3v1.html
   */
  class ID3Tag extends Object {
    var 
      $version  = ID3_VERSION_UNKNOWN,
      $tag      = '',
      $name     = '',
      $artist   = '',
      $album    = '',
      $year     = 0,
      $comment  = '',
      $genre    = -1,
      $track    = 0;
    
    /**
     * Create a string representation
     *
     * @access  public
     * @return  string
     */
    function toString() {
      static $ver= array(
        ID3_VERSION_UNKNOWN => 'ID3_VERSION_UNKNOWN',
        ID3_VERSION_1       => 'ID3_VERSION_1',
        ID3_VERSION_1_1     => 'ID3_VERSION_1_1',
        ID3_VERSION_2       => 'ID3_VERSION_2'
      );
      
      $c= &$this->getClass();
      return sprintf(
        "%s {\n".
        "\t[version] %s\n".
        "\t[tag    ] %s\n".
        "\t[name   ] %s\n".
        "\t[artist ] %s\n".
        "\t[album  ] %s\n".
        "\t[year   ] %s\n".
        "\t[comment] %s\n".
        "\t[genre  ] %d {%s}\n".
        "}",
        $c->getName(),
        $ver[$this->version],
        $this->tag,
        $this->name,
        $this->artist,
        $this->album,
        $this->year,
        $this->comment,
        $this->genre->id,
        $this->genre->toString()
      );
    }
    
    /**
     * Creates an ID3 Tag from a string
     *
     * @model   static
     * @access  public
     * @param   &string buf
     * @param   string version one of the ID3_VERSION_* constants
     * @return  &util.mp3.ID3Tag a tag
     */
    function &fromString(&$buf, $version) {
      $tag= &new ID3Tag();
      
      switch ($version) {
        case ID3_VERSION_1:
          $data= unpack('a3tag/a30name/a30artist/a30album/a4year/a30comment/C1genre', $buf);
          break;
          
        case ID3_VERSION_1_1:
          $data= unpack('a3tag/a30name/a30artist/a30album/a4year/a28comment/x1/C1track/C1genre', $buf);
          break;
        <commit_msg>- Throw a formatexception when tag is corrupt<commit_after><?php
/* This class is part of the XP framework
 *
 * $Id$
 */

  uses('util.mp3.ID3Genre');
  
  // Versions 
  define('ID3_VERSION_UNKNOWN',               '?');
  define('ID3_VERSION_1',                     '1');
  define('ID3_VERSION_1_1',                 '1.1');
  define('ID3_VERSION_2',                     '2');
  
  /**
   * This class represents an ID3 tag
   *
   * Usage:
   * <code>
   *   // [...]
   *   $mp3= &new MP3File(new File($file));
   *   try(); {
   *     $id3= &$mp3->getID3Tag();
   *   } if (catch('Exception', $e)) {
   *     $e->printStackTrace();
   *     exit();
   *   }
   *   echo $id3->toString();
   * </code>
   *
   * @purpose  ID3 Tag object
   * @see      http://www.id3.org/id3v1.html
   */
  class ID3Tag extends Object {
    var 
      $version  = ID3_VERSION_UNKNOWN,
      $tag      = '',
      $name     = '',
      $artist   = '',
      $album    = '',
      $year     = 0,
      $comment  = '',
      $genre    = -1,
      $track    = 0;
    
    /**
     * Create a string representation
     *
     * @access  public
     * @return  string
     */
    function toString() {
      static $ver= array(
        ID3_VERSION_UNKNOWN => 'ID3_VERSION_UNKNOWN',
        ID3_VERSION_1       => 'ID3_VERSION_1',
        ID3_VERSION_1_1     => 'ID3_VERSION_1_1',
        ID3_VERSION_2       => 'ID3_VERSION_2'
      );
      
      $c= &$this->getClass();
      return sprintf(
        "%s {\n".
        "\t[version] %s\n".
        "\t[tag    ] %s\n".
        "\t[name   ] %s\n".
        "\t[artist ] %s\n".
        "\t[album  ] %s\n".
        "\t[year   ] %s\n".
        "\t[comment] %s\n".
        "\t[genre  ] %d {%s}\n".
        "}",
        $c->getName(),
        $ver[$this->version],
        $this->tag,
        $this->name,
        $this->artist,
        $this->album,
        $this->year,
        $this->comment,
        $this->genre->id,
        $this->genre->toString()
      );
    }
    
    /**
     * Creates an ID3 Tag from a string
     *
     * @model   static
     * @access  public
     * @param   &string buf
     * @param   string version one of the ID3_VERSION_* constants
     * @return  &util.mp3.ID3Tag a tag
     */
    function &fromString(&$buf, $version) {
      $tag= &new ID3Tag();
      
      switch ($version) {
        case ID3_VERSION_1:
          $data= unpack('a3tag/a30name/a30artist/a30album/a4year/a30comment/C1genre', $buf);
          if ('TAG' != $data['tag']) return throw(new FormatException('Tag corrupt'));
          break;
          
        case ID3_VERSION_1_1:
          $data= unpack('a3tag/a30name/a30artist/a30album/a4year/a28comment/x1/C1track/C1genre', $buf);
          if ('TAG' != $data['tag']) return throw(new FormatException('Tag corrupt'));
          break;
        
{'fraction_non_alphanumeric': 0.16868575624082233, 'fraction_numerical': 0.02936857562408223, 'mean_word_length': 1.5474520804114071, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2472308', 'n_tokens_mistral': 1987, 'n_tokens_neox': 1955, 'n_words': 353}