<commit_before>/*
 * Copyright (C) 2008 <NAME>
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * As a special exception, you may use this file as part of a free
 * software library without restriction. Specifically, if other files
 * instantiate templates or use macros or inline functions from this
 * file, or you compile this file and link it with other files to
 * produce an executable, this file does not by itself cause the
 * resulting executable to be covered by the GNU General Public
 * License. This exception does not however invalidate any other
 * reasons why the executable file might be covered by the GNU Library
 * General Public License.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <cxxtools/timespan.h>
#include <cxxtools/serializationinfo.h>
#include <cxxtools/serializationerror.h>
#include <cxxtools/convert.h>

#include <sys/time.h>
#include <time.h>
#include <stdio.h>

#include <iostream>

namespace cxxtools
{
    Timespan Timespan::gettimeofday()
    {
        timeval tv;
        ::gettimeofday(&tv, 0);
        return Timespan(tv.tv_sec, tv.tv_usec);
    }

    std::ostream& operator<< (std::ostream& out, const Timespan& ts)
    {
        out << static_cast<double>(ts.totalSeconds()) << 's';
        return out;
    }

    std::ostream& operator<< (std::ostream& out, const Microseconds& ts)
    {
        out << ts.totalUSecs() << "us";
        return out;
    }

    std::ostream& operator<< (std::ostream& out, const Milliseconds& ts)
    {
        out << static_cast<double>(ts.totalMSecs()) << "ms";
        return out;
    }

    std::ostream& operator<< (std::ostream& out, const Seconds& ts)
    {
        out << static_cast<double>(ts.totalSeconds()) << 's';
        return out;
    }

    std::ostream& operator<< (std::ostream& out, const Minutes& ts)
    {
        out << static_cast<double>(ts.totalMinutes()) << "min";
        return out;
    }

    std::ostream& operator<< (std::ostream& out, const Hours& ts)
    {
        out << static_cast<double>(ts.totalHours()) << 'h';
        return out;
    }

    std::ostream& operator<< (std::ostream& out, const Days& ts)
    {
        out << static_cast<double>(ts.totalDays()) << 'd';
        return out;
    }

    namespace tshelper
    {
        void get(std::istream& in, Timespan& ts, uint64_t res)
        {
            double value;
            in >> value;
            if (in)
            {
                int ch = in.peek();
                if (in)
                {
                    switch (std::istream::traits_type::to_char_type(ch))
                    {
                        case 'u':
                            in.get();
                            ts = Microseconds(value);
                            return;

                        case 'm':
                        {
                            in.get();
                            ch = in.peek();
                            if (in)
                            {
                                switch (std::istream::traits_type::to_char_type(ch))
                                {
                                    case 's':
                                        in.get();
                                        ts = Milliseconds(value);
                                        return;

                                    case 'i':
                                        in.get();
                                        ts = Minutes(value);
                                        return;
                                }
                            }

                            in.clear();
                            ts = Minutes(value);
                            return;
                        }

                        case 's':
                            in.get();
                            ts = Seconds(value);
                            return;

                        case 'h':
                            in.get();
                            ts = Hours(value);
                            return;

                        case 'd':
                            in.get();
                            ts = Days(value);
                            return;
                    }
                }

                in.clear();

                ts = Timespan(value * res);
            }
        }

        uint64_t factor(const cxxtools::SerializationInfo& si, uint64_t res)
        {
            if (si.typeName() == "microseconds")
                return 1;
            else if (si.typeName() == "milliseconds")
                return 1000l;
            else if (si.typeName() == "seconds")
                return 1000l*1000l;
            else if (si.typeName() == "minutes")
                return 1000l*1000l*60l;
            else if (si.typeName() == "hours")
                return 1000l*1000l*60l*60l;
            else if (si.typeName() == "days")
                return 1000l*1000l*60l*60l*24l;
            else
                return res;
        }<commit_msg>fix integer overflow on 32 bit systems<commit_after>/*
 * Copyright (C) 2008 <NAME>
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * As a special exception, you may use this file as part of a free
 * software library without restriction. Specifically, if other files
 * instantiate templates or use macros or inline functions from this
 * file, or you compile this file and link it with other files to
 * produce an executable, this file does not by itself cause the
 * resulting executable to be covered by the GNU General Public
 * License. This exception does not however invalidate any other
 * reasons why the executable file might be covered by the GNU Library
 * General Public License.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <cxxtools/timespan.h>
#include <cxxtools/serializationinfo.h>
#include <cxxtools/serializationerror.h>
#include <cxxtools/convert.h>

#include <sys/time.h>
#include <time.h>
#include <stdio.h>

#include <iostream>

namespace cxxtools
{
    Timespan Timespan::gettimeofday()
    {
        timeval tv;
        ::gettimeofday(&tv, 0);
        return Timespan(tv.tv_sec, tv.tv_usec);
    }

    std::ostream& operator<< (std::ostream& out, const Timespan& ts)
    {
        out << static_cast<double>(ts.totalSeconds()) << 's';
        return out;
    }

    std::ostream& operator<< (std::ostream& out, const Microseconds& ts)
    {
        out << ts.totalUSecs() << "us";
        return out;
    }

    std::ostream& operator<< (std::ostream& out, const Milliseconds& ts)
    {
        out << static_cast<double>(ts.totalMSecs()) << "ms";
        return out;
    }

    std::ostream& operator<< (std::ostream& out, const Seconds& ts)
    {
        out << static_cast<double>(ts.totalSeconds()) << 's';
        return out;
    }

    std::ostream& operator<< (std::ostream& out, const Minutes& ts)
    {
        out << static_cast<double>(ts.totalMinutes()) << "min";
        return out;
    }

    std::ostream& operator<< (std::ostream& out, const Hours& ts)
    {
        out << static_cast<double>(ts.totalHours()) << 'h';
        return out;
    }

    std::ostream& operator<< (std::ostream& out, const Days& ts)
    {
        out << static_cast<double>(ts.totalDays()) << 'd';
        return out;
    }

    namespace tshelper
    {
        void get(std::istream& in, Timespan& ts, uint64_t res)
        {
            double value;
            in >> value;
            if (in)
            {
                int ch = in.peek();
                if (in)
                {
                    switch (std::istream::traits_type::to_char_type(ch))
                    {
                        case 'u':
                            in.get();
                            ts = Microseconds(value);
                            return;

                        case 'm':
                        {
                            in.get();
                            ch = in.peek();
                            if (in)
                            {
                                switch (std::istream::traits_type::to_char_type(ch))
                                {
                                    case 's':
                                        in.get();
                                        ts = Milliseconds(value);
                                        return;

                                    case 'i':
                                        in.get();
                                        ts = Minutes(value);
                                        return;
                                }
                            }

                            in.clear();
                            ts = Minutes(value);
                            return;
                        }

                        case 's':
                            in.get();
                            ts = Seconds(value);
                            return;

                        case 'h':
                            in.get();
                            ts = Hours(value);
                            return;

                        case 'd':
                            in.get();
                            ts = Days(value);
                            return;
                    }
                }

                in.clear();

                ts = Timespan(value * res);
            }
        }

        uint64_t factor(const cxxtools::SerializationInfo& si, uint64_t res)
        {
            if (si.typeName() == "microseconds")
                return 1;
            else if (si.typeName() == "milliseconds")
                return 1000l;
            else if (si.typeName() == "seconds")
                return 1000l*1000l;
            else if (si.typeName() == "minutes")
                return 1000l*1000l*60l;
            else if (si.typeName() == "hours")
                return static_cast<uint64_t>(1000)*1000l*60l*60l;
            else if (si.typeName() == "days")
                return static_cast<uint64_t>(1000)*1000l*60l*60l*24l;
            else
                return res;
        }
{'fraction_non_alphanumeric': 0.09912844440531737, 'fraction_numerical': 0.013381459635531296, 'mean_word_length': 1.294485962431832, 'pattern_counts': {'":': 0, '<': 119, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '183112', 'n_tokens_mistral': 3033, 'n_tokens_neox': 2785, 'n_words': 888}