view dmutil.cpp @ 107:2b30217a3c39 default tip

Fix verbose build echos.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 29 Feb 2024 21:48:47 +0200
parents 9909014498f0
children
line wrap: on
line source

//
// GLDragon - OpenGL PLY model viewer / simple benchmark
// -- Miscellaneous utility functions
// Programmed and designed by Matti 'ccr' Hämäläinen <ccr@tnsp.org>
// (C) Copyright 2019 Tecnic Software productions (TNSP)
//
// See file "COPYING" for license information.
//
#include "dmutil.h"
#include <fstream>


void dmMsg_V(const char *fmt, va_list ap)
{
    fprintf(stdout, "INFO: ");
    vfprintf(stdout, fmt, ap);
}


void dmMsg(const char *fmt, ...)
{
    va_list ap;
    va_start(ap, fmt);
    dmMsg_V(fmt, ap);
    va_end(ap);
}


void dmError_V(const char *fmt, va_list ap)
{
    fprintf(stdout, "ERROR: ");
    vfprintf(stdout, fmt, ap);
}


void dmError(const char *fmt, ...)
{
    va_list ap;
    va_start(ap, fmt);
    dmError_V(fmt, ap);
    va_end(ap);
}


std::string dmStrLTrim(const std::string& str, const std::string& delim)
{
    return str.substr(str.find_first_not_of(delim));
}


std::string dmStrRTrim(const std::string& str, const std::string& delim)
{
    return str.substr(0, str.find_last_not_of(delim));
}


std::string dmStrTrim(const std::string& str, const std::string& delim)
{
    if (str.empty())
        return str;

    size_t start = str.find_first_not_of(delim);
    return str.substr(start, str.find_last_not_of(delim) - start + 1);
}


std::vector<std::string> dmStrSplit(const std::string& str, const std::string& delim)
{
    std::vector<std::string> result;
    size_t oldpos = 0, newpos;

    do
    {
        newpos = str.find_first_of(delim, oldpos);
        std::string tmp = dmStrTrim(str.substr(oldpos, newpos - oldpos));

        if (!tmp.empty())
            result.push_back(tmp);

        oldpos = newpos + 1;
    } while (newpos != std::string::npos);

    return result;
}


std::string dmStrJoin(const std::vector<std::string> &list, const std::string &delim)
{
    switch (list.size())
    {
        case 0:
            return "";

        case 1:
            return list[0];

        default:
            std::string result;
            bool first = true;
            for (const auto &elem : list)
            {
                if (!first)
                    result += delim;
                else
                    first = false;
                result += elem;
            }
            return result;
    }
}


std::string dmGetPath(const std::string &path)
{
    size_t dirsep = path.find_last_of("/\\");
    return (dirsep != std::string::npos) ? path.substr(0, dirsep + 1) : "";
}


bool dmReadText(const std::string &filename, std::string &buf, const int maxSize)
{
    std::ifstream in(filename.c_str(), std::fstream::in);

    if (!in.is_open())
    {
        dmError("Unable to open file '%s'.\n",
            filename.c_str());
        return false;
    }

    in.seekg(0, std::ios::end);

    if (in.tellg() > maxSize)
    {
        dmError("File '%s' is too large.\n",
            filename.c_str());
        return false;
    }

    buf.reserve(in.tellg());
    in.seekg(0, std::ios::beg);

    buf.assign((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());

    return true;
}


bool dmFileExists(const std::string &filename, std::ios_base::openmode mode)
{
    std::ifstream infile(filename.c_str(), mode);
    return infile.good();
}