Subversion Repositories heating

Rev

Rev 5 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * Copyright (C) 2015 by Andreas Theofilu. All rights reserved!
 *
 * All rights reserved. No warranty, explicit or implicit, provided.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Andreas Theofilu and his suppliers, if any.
 * The intellectual and technical concepts contained
 * herein are proprietary to Andreas Theofilu and its suppliers and
 * may be covered by European and Foreign Patents, patents in process,
 * and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Andreas Theofilu.
 * 
 * Author: Andreas Theofilu <andreas@theosys.at>
 */
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cctype>
#include <cstddef>
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <functional>
#include <cctype>
#include <locale>
#include <unistd.h>
#include <syslog.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/utsname.h>
#include <fcntl.h>
#include "helper.h"

using namespace std;

helper::helper()
{
        strcpy(pname, "this");
        dbg = true;
}

char *helper::readLine(int fd, char *buf, int bufLen)
{
        int i, end;
        char ch, *p;
        
        if (fd <= 0)
                return NULL;
        
        i = end = 0;
        p = buf;
        
        while (read(fd, &ch, 1) > 0)
        {
                end = 1;
                
                if (ch == 0x0a)
                {
                        *p = 0;
                        return buf;
                }
                
                if (ch == 0x0d) // ignore this!
                        continue;
                
                if (i < (bufLen - 1))
                {
                        *p = ch;
                        p++;
                        i++;
                }
        }
        
        *p = 0;
        
        if (end)
                return buf;
        else
                return NULL;
}

char *helper::trim(char *str)
{
char *p1, *p2, *p;

        if (!str)
                return NULL;

        p = str;
        p1 = p2 = NULL;

        while (*p)
        {
                if (!p1 && *p != ' ')
                {
                        p1 = p;
                        break;
                }

                p++;
        }

        p2 = str + (std::char_traits<char>::length(str) - 1);
        
        while (p2 > str && *p2 == ' ')
                p2--;

        if (p2)
                *(p2+1) = 0;

        if (p1)
        {
                std::string buf = std::string(p1);
                strcpy(str, buf.c_str());
        }

        return str;
}

string helper::trim (string str)
{
        if (str.empty())
                return str;

        str.erase(str.begin(), std::find_if(str.begin(), str.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
        str.erase(std::find_if(str.rbegin(), str.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), str.end());
        return str;
}

int helper::compare(char *str1, char *str2)
{
        int len = std::char_traits<char>::length(str1);
        
        while (len)
        {
                if (*str1 && *str2 && *str1 == *str2)
                {
                        str1++;
                        str2++;
                }
                else
                {
                        if (*str1 > *str2)
                                return 1;
                        else
                                return -1;
                }

                len--;
        }

        return 0;
}

int helper::compcase(const char *str1, const char *str2)
{
        int len = std::char_traits<char>::length(str1);
//      debug("compcase: len=" + itostring(len) + ",str1=" + std::string(str1) + ",str2=" + std::string(str2));

        while (len)
        {
                if (*str1 && *str2 && std::tolower(*str1) == std::tolower(*str2))
                {
                        str1++;
                        str2++;
                }
                else
                {
                        if (std::tolower(*str1) > std::tolower(*str2))
                                return 1;
                        else
                                return -1;
                }

                len--;
        }

        return 0;
}

int helper::compcase (string str1, string str2)
{
string s1 = str1;
string s2 = str2;

        if (str1.empty())
                return -1;
        else if (str2.empty())
                return 1;

        transform(s1.begin(), s1.end(), s1.begin(), ::tolower);
        transform(s2.begin(), s2.end(), s2.begin(), ::tolower);
        return s1.compare(s2);
}

char* helper::findc (char* str, int len, char c)
{
        if (str == NULL || *str == 0 || !len)
                return NULL;

        for (int i = 0; i < len; i++)
        {
                if (*(str+i) == c)
                        return str+i;
        }

        return NULL;
}

int helper::findc (string str, char c)
{
        return str.find(c);
}

vector<string> helper::split(string str, char delimiter)
{
vector<string> internal;
stringstream ss(str);           // Turn the string into a stream.
string tok;

        while(getline(ss, tok, delimiter))
                internal.push_back(tok);

        return internal;
}

void helper::setPName(char *name)
{
        if (name == NULL || std::char_traits<char>::length(name) < 1)
                return;

        strncpy(pname, name, sizeof(pname) - 1);
}

string helper::itostring (int i)
{
        return ToString(i);
}

char* helper::assign (string str)
{
char *p;
int len;

        if (str.empty())
                return NULL;

        len = strlen(str.c_str());
        p = new char[len+1];
        memset(p, 0, len+1);
        strncpy(p, str.c_str(), len);
        return p;
}

bool helper::ToBool (char* str)
{
        if (!strcasestr(str, "true") || !strcasestr(str, "yes") || !strcasestr(str, "1") || !strcasestr(str, "on"))
                return true;

        return false;
}

bool helper::ToBool (string s)
{
        if (!compcase(s, "true") || !compcase(s, "yes") || !compcase(s, "1") || !compcase(s, "on"))
                return true;

        return false;
}

bool helper::ToBool (int i)
{
        if (i != 0)
                return true;

        return false;
}

bool helper::isArm()
{
struct utsname uts;
std::string str;

        if (uname(&uts) == -1)
        {
                syslog(LOG_DAEMON, "Error getting architecture!");
                return false;
        }

        if (compcase(uts.machine, "armv5tel") == 0)
                return true;

        str = std::string(uts.machine);

        if (str.find("arm") != std::string::npos || str.find("ARM") != std::string::npos)
                return true;

        return false;
}

void helper::debug(std::string msg)
{
        if (!dbg || msg.empty())
                return;
#ifdef TRACE
        if (!isArm())
#endif
                syslog(LOG_DEBUG, "%s: %s", pname, msg.c_str());
#ifdef TRACE
        else
        {
        std::ofstream df;
        time_t t = time(0);
        char *ct;
        int len;
        struct tm *tinfo = localtime(&t);

                df.open("/var/log/prtg_mini.log", std::ios::app);
                ct = ctime(&t);
                len = strlen(ct);
                *(ct + (len-1)) = 0;
                df << ct << ": " << msg << std::endl;
                df.close();
        }
#endif
}