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