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

  uses(
    'peer.Socket',
    'text.encode.CvsPassword',
    'peer.ProtocolException',
    'peer.AuthenticationException'
  );

  define('CVSR_EXPECT_NONE',     'none');  
  define('CVSR_EXPECT_STRING',   'string');
  define('CVSR_EXPECT_FORMAT',   'format');
  define('CVSR_EXPECT_REGEX',    'regex');
  define('CVSR_EXPECT_OK',       'ok');
  define('CVSR_EXPECT_MESSAGE',  'message');
  define('CVSR_EXPECT_LIST',     'list');

  /**
   * CVS PServer client
   *
   * @experimental
   * @see      http://www.loria.fr/~molli/cvs/doc/cvsclient_toc.html 
   * @see      http://libcvs.cvshome.org/servlets/ProjectSource 
   * @purpose  CVS PServer protocol implementation
   */
  class PServerClient extends Object {
    var
      $cat      = NULL,
      $root     = '',
      $verbs    = array();

    var
      $_sock    = NULL;

    /**
     * Constructor
     *
     * @access  public
     * @param   string host
     * @param   int port default 2401
     */  
    function __construct($host, $port= 2401) {
      $this->_sock= &new Socket($host, $port);
    }
    
    /**
     * Connect to CVS pserver
     *
     * @access  public
     * @return  bool success
     * @throws  io.IOException in case connecting failed
     */  
    function connect() {
      return $this->_sock->connect();
    }

    /**
     * Wrapper that sends a command to the remote host.
     *
     * @access  protected  
     * @param   string format
     * @param   mixed* args
     * @return  bool success
     */
    function _sendcmd() {
      $a= func_get_args();
      $cmd= vsprintf(array_shift($a), $a);
      $this->cat && $this->cat->debug('>>>', $cmd);
      return $this->_sock->write($cmd."\n");
    }
    
    /**
     * Wrapper that reads a line and checks for expected output
     *
     * @access  public
     * @param   string expect one of the CVSR_EXPECT_* constants
     * @param   string data default ''
     * @return  mixed
     * @throws  peer.ProtocolException in case expectation is not met
     * @see     http://www.loria.fr/~molli/cvs/doc/cvsclient_5.html#SEC14
     */
    function _readline($expect= CVSR_EXPECT_NONE, $data= '') {
      $line= $this->_sock->readLine();
      $this->cat && $this->cat->debug('<<<', $line);
      
      switch ($expect) {
        case CVSR_EXPECT_NONE:
          $met= TRUE;
          $return= $line;
          break;

        case CVSR_EXPECT_OK:
          $met= (0 == strncmp('ok', $line, 2));
          $return= TRUE;
          break;

        case CVSR_EXPECT_MESSAGE:
          if ($met= (0 == strncmp('M ', $line, 2))) {
            $return= substr($line, 2);
          }
          break;
          
        case CVSR_EXPECT_STRING:
          $met= (0 == strncmp($data, $line, strlen($data)));
          $return= $line;
          break;

        case CVSR_EXPECT_LIST:
          $met= (0 == strncmp($data, $line, strlen($data)));
          $return= explode(' ', substr($line, strlen($data)+ 1));
          break;
        
        case CVSR_EXPECT_FORMAT:
          $return= sscanf($line, $data);
          $met= !empty($return[0]);
          break;
        
        case CVSR_EXPECT_REGEX:
          $met= preg_match($data, $line, $return);
          break;
      }
      
      if (!$met) {
        return throw(new ProtocolException(sprintf(
          'Unexpected response "%s[...]", expecting (%s: "%s")',
          substr($line, 0, min(strlen($line), 20)),
          $expect,
          $data
        )));
      }
      return $return;
    }
    
    /**
     * Negotiate
     *
     * @access  protected
     * @return  bool success
     * @see     http://www.loria.fr/~molli/cvs/doc/cvsclient_5.html#SEC13
     */
    function negotiate() {
      $this->_sendcmd('Root %s', $this->root);
      $this->_sendcmd(
        'Valid-responses %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s M E F MT',
        'Valid-requests',
        'New-entry',
        'Updated',
        'Created',
        'Update-existing',
        'Merged',
        'Rcs-diff',
        'Patched',
        'Mode',
        'Mod-time',
        'Checksum',
        'Copy-file',
        'Removed',
        'Remove-entry',
        'Set-static-directory',
        'Clear-static-directory',
        'Set-sticky',
        'Clear-sticky',
        'Template',
        'Set-checkin-prog',
        'Set-update-prog',
        'Notified',
        'Module-expansion',
        'Wrapper-rcsOption',
        'ok',
        'error',
        'Checked-in'
      );
      $this->_sendcmd('valid-requests');
      $this->verbs= $this->_readline(CVSR_EXPECT_LIST, 'Valid-requests');
      return $this->_readline(CVSR_EXPECT_OK);
    }

    /**<commit_msg>- QA: Beautify source code in negotiate()<commit_after><?php
/* This class is part of the XP framework
 *
 * $Id$ 
 */

  uses(
    'peer.Socket',
    'text.encode.CvsPassword',
    'peer.ProtocolException',
    'peer.AuthenticationException'
  );

  define('CVSR_EXPECT_NONE',     'none');  
  define('CVSR_EXPECT_STRING',   'string');
  define('CVSR_EXPECT_FORMAT',   'format');
  define('CVSR_EXPECT_REGEX',    'regex');
  define('CVSR_EXPECT_OK',       'ok');
  define('CVSR_EXPECT_MESSAGE',  'message');
  define('CVSR_EXPECT_LIST',     'list');

  /**
   * CVS PServer client
   *
   * @experimental
   * @see      http://www.loria.fr/~molli/cvs/doc/cvsclient_toc.html 
   * @see      http://libcvs.cvshome.org/servlets/ProjectSource 
   * @purpose  CVS PServer protocol implementation
   */
  class PServerClient extends Object {
    var
      $cat      = NULL,
      $root     = '',
      $verbs    = array();

    var
      $_sock    = NULL;

    /**
     * Constructor
     *
     * @access  public
     * @param   string host
     * @param   int port default 2401
     */  
    function __construct($host, $port= 2401) {
      $this->_sock= &new Socket($host, $port);
    }
    
    /**
     * Connect to CVS pserver
     *
     * @access  public
     * @return  bool success
     * @throws  io.IOException in case connecting failed
     */  
    function connect() {
      return $this->_sock->connect();
    }

    /**
     * Wrapper that sends a command to the remote host.
     *
     * @access  protected  
     * @param   string format
     * @param   mixed* args
     * @return  bool success
     */
    function _sendcmd() {
      $a= func_get_args();
      $cmd= vsprintf(array_shift($a), $a);
      $this->cat && $this->cat->debug('>>>', $cmd);
      return $this->_sock->write($cmd."\n");
    }
    
    /**
     * Wrapper that reads a line and checks for expected output
     *
     * @access  public
     * @param   string expect one of the CVSR_EXPECT_* constants
     * @param   string data default ''
     * @return  mixed
     * @throws  peer.ProtocolException in case expectation is not met
     * @see     http://www.loria.fr/~molli/cvs/doc/cvsclient_5.html#SEC14
     */
    function _readline($expect= CVSR_EXPECT_NONE, $data= '') {
      $line= $this->_sock->readLine();
      $this->cat && $this->cat->debug('<<<', $line);
      
      switch ($expect) {
        case CVSR_EXPECT_NONE:
          $met= TRUE;
          $return= $line;
          break;

        case CVSR_EXPECT_OK:
          $met= (0 == strncmp('ok', $line, 2));
          $return= TRUE;
          break;

        case CVSR_EXPECT_MESSAGE:
          if ($met= (0 == strncmp('M ', $line, 2))) {
            $return= substr($line, 2);
          }
          break;
          
        case CVSR_EXPECT_STRING:
          $met= (0 == strncmp($data, $line, strlen($data)));
          $return= $line;
          break;

        case CVSR_EXPECT_LIST:
          $met= (0 == strncmp($data, $line, strlen($data)));
          $return= explode(' ', substr($line, strlen($data)+ 1));
          break;
        
        case CVSR_EXPECT_FORMAT:
          $return= sscanf($line, $data);
          $met= !empty($return[0]);
          break;
        
        case CVSR_EXPECT_REGEX:
          $met= preg_match($data, $line, $return);
          break;
      }
      
      if (!$met) {
        return throw(new ProtocolException(sprintf(
          'Unexpected response "%s[...]", expecting (%s: "%s")',
          substr($line, 0, min(strlen($line), 20)),
          $expect,
          $data
        )));
      }
      return $return;
    }
    
    /**
     * Negotiate
     *
     * @access  protected
     * @return  bool success
     * @see     http://www.loria.fr/~molli/cvs/doc/cvsclient_5.html#SEC13
     */
    function negotiate() {
      $this->_sendcmd('Root %s', $this->root);
      $this->_sendcmd('Valid-responses %s M E F MT', implode(' ', array(
        'Valid-requests',
        'New-entry',
        'Updated',
        'Created',
        'Update-existing',
        'Merged',
        'Rcs-diff',
        'Patched',
        'Mode',
        'Mod-time',
        'Checksum',
        'Copy-file',
        'Removed',
        'Remove-entry',
        'Set-static-directory',
        'Clear-static-directory',
        'Set-sticky',
        'Clear-sticky',
        'Template',
        'Set-checkin-prog',
        'Set-update-prog',
        'Notified',
        'Module-expansion',
        'Wrapper-rcsOption',
        'ok',
        'error',
        'Checked-in'
      )));
      $this->_sendcmd('valid-requests');
      $this->verbs= $this->_readline(CVSR_EXPECT_LIST, 'Valid-requests');
      return $this->_readline(CVSR_EXPECT_OK);
    }

    /**
{'fraction_non_alphanumeric': 0.15570060006316455, 'fraction_numerical': 0.00547426044846826, 'mean_word_length': 1.85628382441371, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7051542', 'n_tokens_mistral': 3137, 'n_tokens_neox': 3102, 'n_words': 655}