graphlcd-base/glcddrivers/config.c
author root@rika
Wed, 06 Feb 2008 17:32:55 +0000
changeset 4 df6a40031aa5
permissions -rw-r--r--
added graphlcd-base
root@4
     1
/*
root@4
     2
 * GraphLCD driver library
root@4
     3
 *
root@4
     4
 * config.c  -  config file classes
root@4
     5
 *
root@4
     6
 * This file is released under the GNU General Public License. Refer
root@4
     7
 * to the COPYING file distributed with this package.
root@4
     8
 *
root@4
     9
 * (c) 2004 Andreas Regel <andreas.regel AT powarman.de>
root@4
    10
 */
root@4
    11
root@4
    12
#include <syslog.h>
root@4
    13
#include <fstream>
root@4
    14
root@4
    15
#include "common.h"
root@4
    16
#include "config.h"
root@4
    17
#include "drivers.h"
root@4
    18
root@4
    19
root@4
    20
namespace GLCD
root@4
    21
{
root@4
    22
root@4
    23
cDriverConfig::cDriverConfig()
root@4
    24
:   name(""),
root@4
    25
    driver(""),
root@4
    26
    id(kDriverUnknown),
root@4
    27
    device(""),
root@4
    28
    port(0),
root@4
    29
    width(0),
root@4
    30
    height(0),
root@4
    31
    upsideDown(false),
root@4
    32
    invert(false),
root@4
    33
    brightness(100),
root@4
    34
    contrast(5),
root@4
    35
    backlight(true),
root@4
    36
    adjustTiming(0),
root@4
    37
    refreshDisplay(5)
root@4
    38
{
root@4
    39
}
root@4
    40
root@4
    41
cDriverConfig::cDriverConfig(const cDriverConfig & rhs)
root@4
    42
{
root@4
    43
    name = rhs.name;
root@4
    44
    driver = rhs.driver;
root@4
    45
    id = rhs.id;
root@4
    46
    device = rhs.device;
root@4
    47
    port = rhs.port;
root@4
    48
    width = rhs.width;
root@4
    49
    height = rhs.height;
root@4
    50
    upsideDown = rhs.upsideDown;
root@4
    51
    invert = rhs.invert;
root@4
    52
    brightness = rhs.brightness;
root@4
    53
    contrast = rhs.contrast;
root@4
    54
    backlight = rhs.backlight;
root@4
    55
    adjustTiming = rhs.adjustTiming;
root@4
    56
    refreshDisplay = rhs.refreshDisplay;
root@4
    57
    for (unsigned int i = 0; i < rhs.options.size(); i++)
root@4
    58
        options.push_back(rhs.options[i]);
root@4
    59
}
root@4
    60
root@4
    61
cDriverConfig::~cDriverConfig()
root@4
    62
{
root@4
    63
}
root@4
    64
root@4
    65
cDriverConfig & cDriverConfig::operator=(const cDriverConfig & rhs)
root@4
    66
{
root@4
    67
    if (this == &rhs)
root@4
    68
        return *this;
root@4
    69
root@4
    70
    name = rhs.name;
root@4
    71
    driver = rhs.driver;
root@4
    72
    id = rhs.id;
root@4
    73
    device = rhs.device;
root@4
    74
    port = rhs.port;
root@4
    75
    width = rhs.width;
root@4
    76
    height = rhs.height;
root@4
    77
    upsideDown = rhs.upsideDown;
root@4
    78
    invert = rhs.invert;
root@4
    79
    brightness = rhs.brightness;
root@4
    80
    contrast = rhs.contrast;
root@4
    81
    backlight = rhs.backlight;
root@4
    82
    adjustTiming = rhs.adjustTiming;
root@4
    83
    refreshDisplay = rhs.refreshDisplay;
root@4
    84
    options.clear();
root@4
    85
    for (unsigned int i = 0; i < rhs.options.size(); i++)
root@4
    86
        options.push_back(rhs.options[i]);
root@4
    87
root@4
    88
    return *this;
root@4
    89
}
root@4
    90
root@4
    91
bool cDriverConfig::Parse(const std::string & line)
root@4
    92
{
root@4
    93
    std::string::size_type pos;
root@4
    94
    tOption option;
root@4
    95
root@4
    96
    pos = line.find("=");
root@4
    97
    if (pos == std::string::npos)
root@4
    98
        return false;
root@4
    99
    option.name = trim(line.substr(0, pos));
root@4
   100
    option.value = trim(line.substr(pos + 1));
root@4
   101
    //printf("D %s = %s\n", option.name.c_str(), option.value.c_str());
root@4
   102
root@4
   103
    if (option.name == "Driver")
root@4
   104
    {
root@4
   105
        int driverCount;
root@4
   106
        tDriver * drivers = GetAvailableDrivers(driverCount);
root@4
   107
        for (int i = 0; i < driverCount; i++)
root@4
   108
        {
root@4
   109
            if (option.value == drivers[i].name)
root@4
   110
            {
root@4
   111
                driver = drivers[i].name;
root@4
   112
                id = drivers[i].id;
root@4
   113
                break;
root@4
   114
            }
root@4
   115
        }
root@4
   116
    }
root@4
   117
    else if (option.name == "Device")
root@4
   118
    {
root@4
   119
        device = option.value;
root@4
   120
    }
root@4
   121
    else if (option.name == "Port")
root@4
   122
    {
root@4
   123
        port = GetInt(option.value);
root@4
   124
    }
root@4
   125
    else if (option.name == "Width")
root@4
   126
    {
root@4
   127
        width = GetInt(option.value);
root@4
   128
    }
root@4
   129
    else if (option.name == "Height")
root@4
   130
    {
root@4
   131
        height = GetInt(option.value);
root@4
   132
    }
root@4
   133
    else if (option.name == "UpsideDown")
root@4
   134
    {
root@4
   135
        upsideDown = GetBool(option.value);
root@4
   136
    }
root@4
   137
    else if (option.name == "Invert")
root@4
   138
    {
root@4
   139
        invert = GetBool(option.value);
root@4
   140
    }
root@4
   141
    else if (option.name == "Brightness")
root@4
   142
    {
root@4
   143
        brightness = GetInt(option.value);
root@4
   144
    }
root@4
   145
    else if (option.name == "Contrast")
root@4
   146
    {
root@4
   147
        contrast = GetInt(option.value);
root@4
   148
    }
root@4
   149
    else if (option.name == "Backlight")
root@4
   150
    {
root@4
   151
        backlight = GetBool(option.value);
root@4
   152
    }
root@4
   153
    else if (option.name == "AdjustTiming")
root@4
   154
    {
root@4
   155
        adjustTiming = GetInt(option.value);
root@4
   156
    }
root@4
   157
    else if (option.name == "RefreshDisplay")
root@4
   158
    {
root@4
   159
        refreshDisplay = GetInt(option.value);
root@4
   160
    }
root@4
   161
    else
root@4
   162
    {
root@4
   163
        options.push_back(option);
root@4
   164
    }
root@4
   165
    return true;
root@4
   166
}
root@4
   167
root@4
   168
int cDriverConfig::GetInt(const std::string & value)
root@4
   169
{
root@4
   170
    return strtol(value.c_str(), NULL, 0);
root@4
   171
}
root@4
   172
root@4
   173
bool cDriverConfig::GetBool(const std::string & value)
root@4
   174
{
root@4
   175
    return value == "yes";
root@4
   176
}
root@4
   177
root@4
   178
root@4
   179
cConfig::cConfig()
root@4
   180
:   waitMethod(kWaitGettimeofday),
root@4
   181
    waitPriority(0)
root@4
   182
{
root@4
   183
}
root@4
   184
root@4
   185
cConfig::~cConfig()
root@4
   186
{
root@4
   187
}
root@4
   188
root@4
   189
bool cConfig::Load(const std::string & filename)
root@4
   190
{
root@4
   191
    std::fstream file;
root@4
   192
    char readLine[1000];
root@4
   193
    std::string line;
root@4
   194
    bool inSections = false;
root@4
   195
    int section = 0;
root@4
   196
root@4
   197
#if (__GNUC__ < 3)
root@4
   198
    file.open(filename.c_str(), std::ios::in);
root@4
   199
#else
root@4
   200
    file.open(filename.c_str(), std::ios_base::in);
root@4
   201
#endif
root@4
   202
    if (!file.is_open())
root@4
   203
        return false;
root@4
   204
root@4
   205
    while (!file.eof())
root@4
   206
    {
root@4
   207
        file.getline(readLine, 1000);
root@4
   208
        line = trim(readLine);
root@4
   209
        if (line.length() == 0)
root@4
   210
            continue;
root@4
   211
        if (line[0] == '#')
root@4
   212
            continue;
root@4
   213
        if (line[0] == '[' && line[line.length() - 1] == ']')
root@4
   214
        {
root@4
   215
            if (!inSections)
root@4
   216
                inSections = true;
root@4
   217
            else
root@4
   218
                section++;
root@4
   219
            driverConfigs.resize(section + 1);
root@4
   220
            driverConfigs[section].name = line.substr(1, line.length() - 2);
root@4
   221
            continue;
root@4
   222
        }
root@4
   223
        if (!inSections)
root@4
   224
        {
root@4
   225
            Parse(line);
root@4
   226
        }
root@4
   227
        else
root@4
   228
        {
root@4
   229
            driverConfigs[section].Parse(line);
root@4
   230
        }
root@4
   231
    }
root@4
   232
root@4
   233
    file.close();
root@4
   234
    return true;
root@4
   235
}
root@4
   236
root@4
   237
bool cConfig::Parse(const std::string & line)
root@4
   238
{
root@4
   239
    std::string::size_type pos;
root@4
   240
    tOption option;
root@4
   241
root@4
   242
    pos = line.find("=");
root@4
   243
    if (pos == std::string::npos)
root@4
   244
        return false;
root@4
   245
    option.name = trim(line.substr(0, pos));
root@4
   246
    option.value = trim(line.substr(pos + 1));
root@4
   247
    //printf("%s = %s\n", option.name.c_str(), option.value.c_str());
root@4
   248
root@4
   249
    if (option.name == "WaitMethod")
root@4
   250
    {
root@4
   251
        waitMethod = GetInt(option.value);
root@4
   252
    }
root@4
   253
    else if (option.name == "WaitPriority")
root@4
   254
    {
root@4
   255
        waitPriority = GetInt(option.value);
root@4
   256
    }
root@4
   257
    else
root@4
   258
    {
root@4
   259
        syslog(LOG_ERR, "Config error: unknown option %s given!\n", option.value.c_str());
root@4
   260
        return false;
root@4
   261
    }
root@4
   262
    return true;
root@4
   263
}
root@4
   264
root@4
   265
int cConfig::GetInt(const std::string & value)
root@4
   266
{
root@4
   267
    return strtol(value.c_str(), NULL, 0);
root@4
   268
}
root@4
   269
root@4
   270
bool cConfig::GetBool(const std::string & value)
root@4
   271
{
root@4
   272
    return value == "yes";
root@4
   273
}
root@4
   274
root@4
   275
int cConfig::GetConfigIndex(const std::string & name)
root@4
   276
{
root@4
   277
    for (int i = 0; i < (int)driverConfigs.size(); i++)
root@4
   278
        if (driverConfigs[i].name == name)
root@4
   279
            return i;
root@4
   280
    syslog(LOG_ERR, "Config error: configuration %s not found!\n", name.c_str());
root@4
   281
    return -1;
root@4
   282
}
root@4
   283
root@4
   284
cConfig Config;
root@4
   285
root@4
   286
} // end of namespace