/*
 * Copyright 2020 UBports foundation
 *
 * This program 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; version 3.
 *
 * 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Author: Marius Gripsgard <marius@ubports.com>
 */

#pragma once

#include "leds.h"

#include <errno.h>
#include <unistd.h>

#include <iostream>
#include <sstream>
#include <vector>

namespace hfd {
namespace utils {
    inline std::vector<std::string> split(std::string strToSplit, char delimeter) {
        std::stringstream ss(strToSplit);
        std::string item;
        std::vector<std::string> splittedStrings;
        while (std::getline(ss, item, delimeter))
        {
        splittedStrings.push_back(item);
        }
        return splittedStrings;
    };

    inline State toState(int state) {
        if (state == 1)
            return State::On;
        return State::Off;
    }

    class FileDescGuard
    {
    public:
        FileDescGuard(int fd) : m_fd(fd) {}

        ~FileDescGuard() {
            if (close(m_fd) != 0)
            {
                std::cerr << "Failed to close file descriptor " << m_fd << ": errno = " << errno << std::endl;
            }
        }
    private:
        int m_fd = -1;
    };
}
}
