<commit_before>// LICENSE/*{{{*/
/*
  sxc - Simple Xmpp Client
  Copyright (C) 2008 <NAME>, <NAME>

  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 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
/*}}}*/

/* $Id$ */

// INCLUDES/*{{{*/

#include <string>
#include <fstream>
#include <pthread.h>

#include <errno.h>
#include <sstream>
#include <sys/stat.h>
#include "InputBase.h"
#include "../Exception/FileInputException.h"
#include "../Exception/Errno.h"
#include "../Exception/Type.h"

/*}}}*/


File::InputBase::InputBase()/*{{{*/
: _isFifoValid(false),
  _isLocked(false),
  _isThreadRunning(false)
{
}

/*}}}*/
File::InputBase::~InputBase()/*{{{*/
{
    _close();
}

/*}}}*/
void File::InputBase::initialize()/*{{{*/
{
    // Generate the path string.
    _path = _createPath();

    // Check the FIFO. Normally, the FIFO should exist with 
    // the right permissions, except sxc has not been run before (in this 
    // directory) or someone tampered with the directory structure.
    try {
        _validateFile();
    } catch (Exception::FileInputException e) {
        // If the FIFO is simply missing, try to create it. In any other case 
        // we have encountered an unexpected error and let it bubble up.
        if (Exception::FileMissing != e.getType())
            throw e;
        _create();
    }

    // Everything went fine; path is okay. Open FIFO.
    _fifo.open(_path.c_str());
}

/*}}}*/
void File::InputBase::listen(bool blocking)/*{{{*/
{
    // Do not start to listen if the FIFO is currently locked. Otherwise we 
    // could end up handling the input twice.
    if (_isLocked)
        // FIXME
        return;

    _isLocked = true;
    // Start the thread in the background.
    pthread_create(&_thread, NULL, _listen, this);
    // Join the thread when this functions should read in a blocking way.
    if (true == blocking)
        pthread_join(_thread, NULL);
}

/*}}}*/
void File::InputBase::_read()/*{{{*/
{<commit_msg>Fix InputBase::listen() throws FileInputException when FIFO is locked.<commit_after>// LICENSE/*{{{*/
/*
  sxc - Simple Xmpp Client
  Copyright (C) 2008 <NAME>, <NAME>

  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 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
/*}}}*/

/* $Id$ */

// INCLUDES/*{{{*/

#include <string>
#include <fstream>
#include <pthread.h>

#include <errno.h>
#include <sstream>
#include <sys/stat.h>
#include "InputBase.h"
#include "../Exception/FileInputException.h"
#include "../Exception/Errno.h"
#include "../Exception/Type.h"

/*}}}*/


File::InputBase::InputBase()/*{{{*/
: _isFifoValid(false),
  _isLocked(false),
  _isThreadRunning(false)
{
}

/*}}}*/
File::InputBase::~InputBase()/*{{{*/
{
    _close();
}

/*}}}*/
void File::InputBase::initialize()/*{{{*/
{
    // Generate the path string.
    _path = _createPath();

    // Check the FIFO. Normally, the FIFO should exist with 
    // the right permissions, except sxc has not been run before (in this 
    // directory) or someone tampered with the directory structure.
    try {
        _validateFile();
    } catch (Exception::FileInputException e) {
        // If the FIFO is simply missing, try to create it. In any other case 
        // we have encountered an unexpected error and let it bubble up.
        if (Exception::FileMissing != e.getType())
            throw e;
        _create();
    }

    // Everything went fine; path is okay. Open FIFO.
    _fifo.open(_path.c_str());
}

/*}}}*/
void File::InputBase::listen(bool blocking)/*{{{*/
{
    // Do not start to listen if the FIFO is currently locked. Otherwise we 
    // could end up handling the input twice.
    if (_isLocked) {
        std::string message = _path + " already locked.";
        throw Exception::FileInputException(Exception::FileLocked, message);
        return;
    }

    _isLocked = true;
    // Start the thread in the background.
    pthread_create(&_thread, NULL, _listen, this);
    // Join the thread when this functions should read in a blocking way.
    if (true == blocking)
        pthread_join(_thread, NULL);
}

/*}}}*/
void File::InputBase::_read()/*{{{*/
{
{'fraction_non_alphanumeric': 0.13404050144648022, 'fraction_numerical': 0.0019286403085824494, 'mean_word_length': 3.0295260295260293, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '211460', 'n_tokens_mistral': 1566, 'n_tokens_neox': 1526, 'n_words': 570}