Subversion Repositories HomeAutomation

Rev

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


#include <string>

/***************************************************************************
 *   Copyright (C) 2004 by Mattias Runge                                   *
 *   mattias@mrunge.se                                                     *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   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 General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "tools.h"

string niceTime()
{
    string result;
    struct tm tmStruct;
    time_t t = time(NULL);
    localtime_r(&t, &tmStruct);

    result += lpad(itos(tmStruct.tm_hour), 2, '0');
    result += ":";
    result += lpad(itos(tmStruct.tm_min), 2, '0');
    result += ":";
    result += lpad(itos(tmStruct.tm_sec), 2, '0');

    return result;
}

unsigned int hex2uint(string hex)
{
    return bin2uint(hex2bin(hex));
}

unsigned int bin2uint(string bin)
{
    unsigned int num = 0;

    for (int n = bin.length()-1; n >= 0; n--)
    {
        if (bin[n] == '1')
            num += (unsigned int)pow(2.0f, (int)(bin.length()-1-n));
    }

    return num;
}

int bin2int(string bin)
{
    int num = 0;
    if (bin[0] == '1') {
        for (int n = bin.length()-1; n >= 0; n--)
        {
            if (bin[n] == '0')
                num += (unsigned int)pow(2.0f, (int)(bin.length()-1-n));
        }
    num = (num*(-1)) -1;
    } else {
        for (int n = bin.length()-1; n >= 0; n--)
        {
            if (bin[n] == '1')
                num += (unsigned int)pow(2.0f, (int)(bin.length()-1-n));
        }
    }
    return num;
}

string invert(string bin)
{
    string inverted;

    for (int n = 0; n < bin.length(); n++)
        inverted += (bin[n] == '1' ? '0' : '1');

    return inverted;
}

float bin2float(string bin)
{
    float num = 0;

    if (bin.length() == 0)
        return 0.0f;

    bool isNegative = false;;

    if (bin[0] == '1')
    {
        bin = invert(bin);
        isNegative = true;
    }

    for (int n = bin.length()-1; n > 0; n--)
    {
        if (bin[n] == '1')
            num += pow(2.0f, (int)(bin.length()-1-n));
    }

    num /= 64.0f;

    if (isNegative)
        num = -num;

    return num;
}

string bin2hex(string bin)
{
    string hex;

    while (bin.size() > 0)
    {
        string part;

        try
        {
            part = bin.substr(0, 4);
        }
        catch (std::out_of_range& e)
        {
            cout << "DEBUG: bin2hex exception: " << e.what() << "\n";
            cout << "DEBUG: A bin=" << bin << endl;
            continue;
        }
        
        bin.erase(0, part.size());

        while (part.size() < 4)
            part = "0" + part;

        if (part == "0000")
            hex += '0';
        else if (part == "0001")
            hex += '1';
        else if (part == "0010")
            hex += '2';
        else if (part == "0011")
            hex += '3';
        else if (part == "0100")
            hex += '4';
        else if (part == "0101")
            hex += '5';
        else if (part == "0110")
            hex += '6';
        else if (part == "0111")
            hex += '7';
        else if (part == "1000")
            hex += '8';
        else if (part == "1001")
            hex += '9';
        else if (part == "1010")
            hex += 'a';
        else if (part == "1011")
            hex += 'b';
        else if (part == "1100")
            hex += 'c';
        else if (part == "1101")
            hex += 'd';
        else if (part == "1110")
            hex += 'e';
        else if (part == "1111")
            hex += 'f';
    }

    return hex;
}

string float2bin(float num, int length)
{
    // This is 16 bits, 6 bits is the decimal
    string bin;
    num = num*64;
    int16_t temp = (int16_t)num;
    return int2bin(temp, length);
}

string hex2bin(string hex)
{
    hex = strtolower(hex);

    string bin;

    for (int n = 0; n < hex.length(); n++)
    {
        switch (hex[n])
        {
            case '0': bin += "0000"; break;
            case '1': bin += "0001"; break;
            case '2': bin += "0010"; break;
            case '3': bin += "0011"; break;
            case '4': bin += "0100"; break;
            case '5': bin += "0101"; break;
            case '6': bin += "0110"; break;
            case '7': bin += "0111"; break;
            case '8': bin += "1000"; break;
            case '9': bin += "1001"; break;
            case 'a': bin += "1010"; break;
            case 'b': bin += "1011"; break;
            case 'c': bin += "1100"; break;
            case 'd': bin += "1101"; break;
            case 'e': bin += "1110"; break;
            case 'f': bin += "1111"; break;
        }
    }

    return bin;
}

string uint2bin(unsigned int num, int length)
{
    string bin;
    while (num)
    {
        bin = char((num&1)+'0') + bin;
        num >>= 1;
    }

    while (bin.size() < length)
        bin = "0" + bin;
    
    string bin2;
    for (int i = 0; i < length; i++)
    {
        bin2 = bin2 + bin[i];
    }
    
    return bin2;
}

string int2bin(int num, int length)
{
    string bin;
    if (num < 0) {
    num *= -1;
    num -= 1;
    while (num)
    {
        bin = char((num&1)+'0') + bin;
        num >>= 1;
    }

    while (bin.size() < length)
        bin = "0" + bin;
    for (int i = 0; i < length; i++)
    {
        if (bin[i] == '0')
            bin[i] = '1';
        else
            bin[i] = '0';
    }
} else {
    while (num)
    {
        bin = char((num&1)+'0') + bin;
        num >>= 1;
    }

    while (bin.size() < length)
        bin = "0" + bin;

}
    string bin2;
    for (int i = 0; i < length; i++)
    {
        bin2 = bin2 + bin[i];
    }
    
    return bin2;
}

string int2hex(int num, int length)
{
    return bin2hex(int2bin(num, length));
}

string uint2hex(unsigned int num, int length)
{
    return bin2hex(uint2bin(num, length));
}

vector<string> explode(string delimiter, string str)
{
    vector<string> parts;
    string::size_type startPos = 0;
    string::size_type endPos = 0;

    while ((endPos = str.find(delimiter, startPos)) != string::npos)
    {
        try
        {
            parts.push_back(str.substr(startPos, endPos-startPos));
        }
        catch (std::out_of_range& e)
        {
            cout << "DEBUG: explode exception: " << e.what() << "\n";
            cout << "DEBUG: A str=" << str << " :: startPos=" << startPos << " :: endPos=" << endPos << endl;
            continue;
        }
        
        startPos = endPos+delimiter.length();
    }

    if (startPos < str.length())
    {
        try
        {
            parts.push_back(str.substr(startPos, str.length()-startPos));
        }
        catch (std::out_of_range& e)
        {
            cout << "DEBUG: explode exception: " << e.what() << "\n";
            cout << "DEBUG: B str=" << str << " :: startPos=" << startPos << " :: endPos=" << endPos << endl;
        }
    }

    return parts;
}

string strtoupper(string s)
{
    for (unsigned int n = 0; n < s.size(); n++)
        s[n] = (char)toupper(s[n]);

    return s;
}

string strtolower(string s)
{
    for (unsigned int n = 0; n < s.size(); n++)
        s[n] = (char)tolower(s[n]);

    return s;
}

string trim(string s)
{
    return trim(s, ' ');
}

string trim(const string s, char c)
{
    string result = s;
    while (result[0] == c)
        result.erase(0, 1);

    while (result[result.length()-1] == c)
        result.erase(result.length()-1, 1);

    return result;
}

int stoi(const string s)
{
    istringstream in(s);
    int i;
    in >> i;
    return i;
}

string itos(int i)
{
    ostringstream out;
    out << i;
    return out.str();
}

unsigned int stou(const string s)
{
    istringstream in(s);
    unsigned int i;
    in >> i;
    return i;
}

string utos(unsigned int i)
{
    ostringstream out;
    out << i;
    return out.str();
}

float stof(const string s)
{
    istringstream in(s);
    float f;
    in >> f;
    return f;
}

string ftos(float f)
{
    ostringstream out;
    out << f;
    return out.str();
}

string str_replace(string search, string replace, string str)
{
    vector<string> parts = explode(search, str);

    string result;
    for (int n = 0; n < parts.size(); n++)
    {
        result += parts[n];

        if (n < parts.size()-1)
            result += replace;
    }

    return result;
}

string file_get_contents(string filename)
{
    ifstream srcfile(filename.c_str());

    if (!srcfile.is_open())
    {
        srcfile.close();
        return "";
    }

    string buffer = "";
    string line;

    while (!srcfile.eof())
    {
        getline(srcfile, line);

        if (srcfile.eof())
            break;
        
        buffer += line + "\n";
    };

    srcfile.close();

    return buffer;
}

bool file_exists(string filename)
{
    ifstream file(filename.c_str());
    if (file.is_open())
    {
        file.close();
        return true;
    }

    return false;
}

string escape(string in)
{
    string out;

    for (int n = 0; n < in.length(); n++)
    {
        switch (in[n])
        {
            case '\r':
                break;

            case '\n':
                out += "\\n";
                break;

            case '\'':
                out += "\\'";
                break;

            case '\\':
                out += "\\\\";
                break;

            case '"':
                out += "\\\"";
                break;

            default:
                out += in[n];
                break;
        }
    }

    return out;
}

string rpad(string in, int length, char c)
{
    while (in.length() < length)
    {
        in += c;
    }

    return in;
}

string lpad(string in, int length, char c)
{
    while (in.length() < length)
    {
        in = c + in;
    }

    return in;
}