OW_ConfigFile.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2004 Vintela, Inc. All rights reserved.
00003 *
00004 * Redistribution and use in source and binary forms, with or without
00005 * modification, are permitted provided that the following conditions are met:
00006 *
00007 *  - Redistributions of source code must retain the above copyright notice,
00008 *  this list of conditions and the following disclaimer.
00009 *
00010 *  - Redistributions in binary form must reproduce the above copyright notice,
00011 *  this list of conditions and the following disclaimer in the documentation
00012 *  and/or other materials provided with the distribution.
00013 *
00014 *  - Neither the name of Vintela, Inc. nor the names of its
00015 *  contributors may be used to endorse or promote products derived from this
00016 *  software without specific prior written permission.
00017 *
00018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
00019 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 * ARE DISCLAIMED. IN NO EVENT SHALL Vintela, Inc. OR THE CONTRIBUTORS
00022 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00023 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00024 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00026 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00027 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00028 * POSSIBILITY OF SUCH DAMAGE.
00029 *******************************************************************************/
00030 
00036 #include "OW_config.h"
00037 #include "OW_ConfigFile.hpp"
00038 #include "OW_ConfigException.hpp"
00039 #include "OW_Format.hpp"
00040 
00041 #include <fstream>
00042 
00043 namespace OW_NAMESPACE
00044 {
00045 namespace ConfigFile
00046 {
00047 
00049 void loadConfigFile(const String& filename, ConfigMap& rval)
00050 {
00051    std::ifstream file(filename.c_str());
00052    if (!file)
00053    {
00054       OW_THROW(ConfigException, Format("Unable to read config"
00055                " file: %1", filename).c_str());
00056    }
00057    
00058    String line;
00059    int lineNum = 0;
00060    while (file)
00061    {
00062       lineNum++;
00063       line = String::getLine(file);
00064       if (!line.empty())
00065       {
00066          // If comment line, ignore
00067          if (line[0] == '#' || line[0] == ';')
00068          {
00069             continue;
00070          }
00071          size_t idx = line.indexOf('=');
00072          if (idx != String::npos)
00073          {
00074             if (idx + 1 < line.length())
00075             {
00076                String itemValue = line.substring(idx + 1).trim();
00077                if (!itemValue.empty())
00078                {
00079                   String item = line.substring(0, idx).trim();
00080                   rval[item].push_back(ItemData(filename, itemValue));
00081                }
00082             }
00083          }
00084          else
00085          {
00086             OW_THROW(ConfigException, Format("Error in config file:"
00087                " %1 at line %2.\n  Line is %3", filename, lineNum,
00088                line).c_str());
00089          }
00090       }
00091    }
00092 }
00093 
00095 String
00096 getConfigItem(const ConfigMap& configItems, const String &itemName, const String& defRetVal)
00097 {
00098    ConfigMap::const_iterator i = configItems.find(itemName);
00099    if (i != configItems.end() && i->second.size() > 0)
00100    {
00101       return i->second.back().value;
00102    }
00103    else
00104    {
00105       return defRetVal;
00106    }
00107 }
00108 
00110 StringArray
00111 getMultiConfigItem(const ConfigMap& configItems, const String &itemName, const StringArray& defRetVal, const char* tokenizeSeparator)
00112 {
00113    ConfigMap::const_iterator item = configItems.find(itemName);
00114    if (item != configItems.end())
00115    {
00116       StringArray rv;
00117       rv.reserve(item->second.size());
00118       for (size_t i = 0; i < item->second.size(); ++i)
00119       {
00120          if (tokenizeSeparator)
00121          {
00122             StringArray tokenizedValue(item->second[i].value.tokenize(tokenizeSeparator));
00123             rv.insert(rv.end(), tokenizedValue.begin(), tokenizedValue.end());
00124          }
00125          else
00126          {
00127             rv.push_back(item->second[i].value);
00128          }
00129       }
00130       return rv;
00131    }
00132    else
00133    {
00134       return defRetVal;
00135    }
00136 }
00137 
00139 void
00140 setConfigItem(ConfigMap& configItems, const String& itemName,
00141    const String& value, EOverwritePreviousFlag overwritePrevious)
00142 {
00143    ConfigMap::iterator it = configItems.find(itemName);
00144    if (it == configItems.end())
00145    {
00146       configItems[itemName].push_back(ItemData("", value));
00147    }
00148    else if (overwritePrevious == E_OVERWRITE_PREVIOUS)
00149    {
00150       ItemDataArray& values = configItems[itemName];
00151       values.clear();
00152       values.push_back(ItemData("", value));
00153    }
00154    // else overwritePrevious == E_PRESERVE_PREVIOUS, and do nothing
00155 }
00156 
00157 
00158 } // end namespace ConfigFile
00159 } // end namespace OW_NAMESPACE
00160 

Generated on Thu Feb 9 08:47:57 2006 for openwbem by  doxygen 1.4.6