<commit_before>/**
 * @file reader.hpp
 * @author <NAME> <me (at) invokr (dot) org>
 * @version 1.0
 *
 * @par License
 *    Alice Replay Parser
 *    Copyright 2014 <NAME>
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 *
 */

#ifndef _DOTA_READER_HPP_
#define _DOTA_READER_HPP_

#include <fstream>
#include <string>
#include <utility>
#include <type_traits>

#include <alice/exception.hpp>
#include <alice/handler.hpp>
#include <alice/gamestate.hpp>
#include <alice/config.hpp>

/// Defines the first 7 bytes to check as the header ID
#define DOTA_DEMHEADERID "PBUFDEM"
/// Defines a fixed amount of memory to allocate for the internal buffer
#define DOTA_BUFSIZE 0x60000

namespace dota {
    /// @defgroup EXCEPTIONS Exceptions
    /// @{

    /// Thrown when the file in inaccesible (e.g. wrong permissions or wrong path)
    CREATE_EXCEPTION( demFileNotAccessible,  "Unable to open file." )
    /// Thrown when the file has an unlikely small file size
    CREATE_EXCEPTION( demFileTooSmall,       "Filesize is too small." )
    /// Thrown when the header of the file is not matching #DOTA_DEMHEADERID
    CREATE_EXCEPTION( demHeaderMismatch,     "Header ID is not matching." )
    /// Thrown if the end of the file is reached before an operation is completed
    CREATE_EXCEPTION( demUnexpectedEOF,      "Unexpected end of file." )
    /// Thrown when the file is likely corrupted
    CREATE_EXCEPTION( demCorrupted,          "Demo file appears to be corruped." )
    /// Thrown when snappy fails to decompress content
    CREATE_EXCEPTION( demInvalidCompression, "Data uncompression failed." )
    /// Thrown when protobuf fails to parse a message
    CREATE_EXCEPTION( demParsingError, "Parsing protouf message failed." )
    /// Thrown when the size of a single messages would cause a buffer overflow
    CREATE_EXCEPTION( demMessageToBig,       "Messagesize is to big." )

    /// @}

    /// @defgroup CORE Core
    /// @{

    /** This class reads all messages for a given replay and reports them to the registered handlers and the gamestate. */
    class reader {
        public:
            /** Constructor, takes path to the replay as a first argument. */
            reader(const std::string& file);

            /** Destructor, closed file and resets handler */
            ~reader() {
                if (fstream.is_open())
                    fstream.close();

                delete h;
                delete buffer;
                delete bufferCmp;
            }

            /** Returns pointer to handler */
            handler_t* getHandler() {
                return h;
            }

            /** Returns reference to gamestate */
            gamestate& getState() {
                return db;
            }

            /** Reads a single message */
            void readMessage(bool skip = false);

            /** Returns true if there are still messages left to read */
            inline bool good() {
                return (fstream.good() && (state != 2));
            }

            /** Reads until the next tick and returns */
            inline void readUntilNextTick() {
                const uint32_t tTick = cTick;
                while (cTick == tTick && good()) {
                    readMessage();
                }<commit_msg>Fixed memory leak in reader destructor<commit_after>/**
 * @file reader.hpp
 * @author <NAME> <me (at) invokr (dot) org>
 * @version 1.0
 *
 * @par License
 *    Alice Replay Parser
 *    Copyright 2014 <NAME>
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 *
 */

#ifndef _DOTA_READER_HPP_
#define _DOTA_READER_HPP_

#include <fstream>
#include <string>
#include <utility>
#include <type_traits>

#include <alice/exception.hpp>
#include <alice/handler.hpp>
#include <alice/gamestate.hpp>
#include <alice/config.hpp>

/// Defines the first 7 bytes to check as the header ID
#define DOTA_DEMHEADERID "PBUFDEM"
/// Defines a fixed amount of memory to allocate for the internal buffer
#define DOTA_BUFSIZE 0x60000

namespace dota {
    /// @defgroup EXCEPTIONS Exceptions
    /// @{

    /// Thrown when the file in inaccesible (e.g. wrong permissions or wrong path)
    CREATE_EXCEPTION( demFileNotAccessible,  "Unable to open file." )
    /// Thrown when the file has an unlikely small file size
    CREATE_EXCEPTION( demFileTooSmall,       "Filesize is too small." )
    /// Thrown when the header of the file is not matching #DOTA_DEMHEADERID
    CREATE_EXCEPTION( demHeaderMismatch,     "Header ID is not matching." )
    /// Thrown if the end of the file is reached before an operation is completed
    CREATE_EXCEPTION( demUnexpectedEOF,      "Unexpected end of file." )
    /// Thrown when the file is likely corrupted
    CREATE_EXCEPTION( demCorrupted,          "Demo file appears to be corruped." )
    /// Thrown when snappy fails to decompress content
    CREATE_EXCEPTION( demInvalidCompression, "Data uncompression failed." )
    /// Thrown when protobuf fails to parse a message
    CREATE_EXCEPTION( demParsingError, "Parsing protouf message failed." )
    /// Thrown when the size of a single messages would cause a buffer overflow
    CREATE_EXCEPTION( demMessageToBig,       "Messagesize is to big." )

    /// @}

    /// @defgroup CORE Core
    /// @{

    /** This class reads all messages for a given replay and reports them to the registered handlers and the gamestate. */
    class reader {
        public:
            /** Constructor, takes path to the replay as a first argument. */
            reader(const std::string& file);

            /** Destructor, closed file and resets handler */
            ~reader() {
                if (fstream.is_open())
                    fstream.close();

                delete h;
                delete[] buffer;
                delete[] bufferCmp;
            }

            /** Returns pointer to handler */
            handler_t* getHandler() {
                return h;
            }

            /** Returns reference to gamestate */
            gamestate& getState() {
                return db;
            }

            /** Reads a single message */
            void readMessage(bool skip = false);

            /** Returns true if there are still messages left to read */
            inline bool good() {
                return (fstream.good() && (state != 2));
            }

            /** Reads until the next tick and returns */
            inline void readUntilNextTick() {
                const uint32_t tTick = cTick;
                while (cTick == tTick && good()) {
                    readMessage();
                }
{'fraction_non_alphanumeric': 0.08720701846274716, 'fraction_numerical': 0.00523765876653136, 'mean_word_length': 2.1904761904761907, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '7420856', 'n_tokens_mistral': 2149, 'n_tokens_neox': 2015, 'n_words': 801}