[asterisk-scf-commits] asterisk-scf/release/ice.git branch "iceutil-propertyhandler" created.

Commits to the Asterisk SCF project code repositories asterisk-scf-commits at lists.digium.com
Fri Jun 17 16:16:38 CDT 2011


branch "iceutil-propertyhandler" has been created
        at  7d3fe1b27c0a03ecb242e6af8e195ab5f557ed2b (commit)

- Log -----------------------------------------------------------------
commit 7d3fe1b27c0a03ecb242e6af8e195ab5f557ed2b
Author: Kevin P. Fleming <kpfleming at digium.com>
Date:   Tue Mar 15 17:26:10 2011 -0500

    Move parseLine() over to PropertiesHandler, leaving the Ice-specific bits
    behind.

diff --git a/cpp/include/IceUtil/PropertiesHandler.h b/cpp/include/IceUtil/PropertiesHandler.h
index 06f1fa7..34c3a99 100644
--- a/cpp/include/IceUtil/PropertiesHandler.h
+++ b/cpp/include/IceUtil/PropertiesHandler.h
@@ -61,6 +61,15 @@ private:
 
     friend class Ice::PropertiesI;
 
+    enum ParseLineResult
+    {
+        ParseLineOK,
+        ParseLineSkip,
+        ParseLineInvalid
+    };
+
+    ParseLineResult parseLine(const std::string&, std::pair<std::string, std::string>&);
+
     struct PropertyValue
     {
         PropertyValue() :
diff --git a/cpp/src/Ice/PropertiesI.cpp b/cpp/src/Ice/PropertiesI.cpp
index 9152590..e995e8b 100644
--- a/cpp/src/Ice/PropertiesI.cpp
+++ b/cpp/src/Ice/PropertiesI.cpp
@@ -181,7 +181,11 @@ Ice::PropertiesI::parseCommandLineOptions(const string& prefix, const StringSeq&
                 opt += "=1";
             }
             
-            parseLine(opt.substr(2), 0);
+            pair<string, string> keyval;
+            if(_propertiesHandler->parseLine(opt.substr(2), keyval) == IceUtilInternal::PropertiesHandler::ParseLineOK)
+            {
+                setProperty(keyval.first, keyval.second);
+            }
         }
         else
         {
@@ -319,7 +323,19 @@ Ice::PropertiesI::load(const std::string& file)
         string line;
         while(getline(in, line))
         {
-            parseLine(line, _converter);
+            pair<string, string> keyval;
+            switch(_propertiesHandler->parseLine(line, keyval))
+            {
+            case IceUtilInternal::PropertiesHandler::ParseLineOK:
+                setProperty(Ice::UTF8ToNative(_converter, keyval.first),
+                            Ice::UTF8ToNative(_converter, keyval.second));
+                break;
+            case IceUtilInternal::PropertiesHandler::ParseLineSkip:
+                continue;
+            case IceUtilInternal::PropertiesHandler::ParseLineInvalid:
+                getProcessLogger()->warning("invalid config file entry: \"" + line + "\"");
+                break;
+            }
         }
     }
 }
@@ -394,7 +410,11 @@ Ice::PropertiesI::PropertiesI(StringSeq& args, const PropertiesPtr& defaults, co
             {
                 s += "=1";
             }
-            parseLine(s.substr(2), 0);
+            pair<string, string> keyval;
+            if(_propertiesHandler->parseLine(s.substr(2), keyval) == IceUtilInternal::PropertiesHandler::ParseLineOK)
+            {
+                setProperty(keyval.first, keyval.second);
+            }
             loadConfigFiles = true;
         }
         else
@@ -422,179 +442,6 @@ Ice::PropertiesI::PropertiesI(StringSeq& args, const PropertiesPtr& defaults, co
 }
 
 void
-Ice::PropertiesI::parseLine(const string& line, const StringConverterPtr& converter)
-{
-    string key;
-    string value;
-    
-    enum ParseState { Key , Value };
-    ParseState state = Key;
-
-    string whitespace;
-    string escapedspace;
-    bool finished = false;
-    for(string::size_type i = 0; i < line.size(); ++i)
-    {
-        char c = line[i];
-        switch(state)
-        {
-          case Key:
-          {
-            switch(c)
-            {
-              case '\\':
-                if(i < line.length() - 1)
-                {
-                    c = line[++i];
-                    switch(c)
-                    {
-                      case '\\':
-                      case '#':
-                      case '=':
-                        key += whitespace;
-                        whitespace.clear();
-                        key += c; 
-                        break;
-
-                      case ' ':
-                        if(key.length() != 0)
-                        {
-                            whitespace += c;
-                        }
-                        break;
-
-                      default:
-                        key += whitespace;
-                        whitespace.clear();
-                        key += '\\';
-                        key += c;
-                        break;
-                    }
-                }
-                else
-                {
-                    key += whitespace;
-                    key += c;
-                }
-                break;
-
-              case ' ':
-              case '\t':
-              case '\r':
-              case '\n':
-                  if(key.length() != 0)
-                  {
-                      whitespace += c;
-                  }
-                  break;
-
-              case '=':
-                  whitespace.clear();
-                  state = Value;
-                  break;
-
-              case '#':
-                  finished = true;
-                  break;
-              
-              default:
-                  key += whitespace;
-                  whitespace.clear();
-                  key += c;
-                  break;
-            }
-            break;
-          }
-
-          case Value:
-          {
-            switch(c)
-            {
-              case '\\':
-                if(i < line.length() - 1)
-                {
-                    c = line[++i];
-                    switch(c)
-                    {
-                      case '\\':
-                      case '#':
-                      case '=':
-                        value += value.length() == 0 ? escapedspace : whitespace;
-                        whitespace.clear();
-                        escapedspace.clear();
-                        value += c; 
-                        break;
-
-                      case ' ':
-                        whitespace += c;
-                        escapedspace += c;
-                        break;
-
-                      default:
-                        value += value.length() == 0 ? escapedspace : whitespace;
-                        whitespace.clear();
-                        escapedspace.clear();
-                        value += '\\';
-                        value += c;
-                        break;
-                    }
-                }
-                else
-                {
-                    value += value.length() == 0 ? escapedspace : whitespace;
-                      value += c;
-                }
-                break;
-
-              case ' ':
-              case '\t':
-              case '\r':
-              case '\n':
-                  if(value.length() != 0)
-                  {
-                      whitespace += c;
-                  }
-                  break;
-
-              case '#':
-                  value += escapedspace;
-                  finished = true;
-                  break;
-              
-              default:
-                  value += value.length() == 0 ? escapedspace : whitespace;
-                  whitespace.clear();
-                  escapedspace.clear();
-                  value += c;
-                  break;
-            }
-            break;
-          }
-        }
-        if(finished)
-        {
-            break;
-        }
-    }
-    value += escapedspace;
-
-    if((state == Key && key.length() != 0) || (state == Value && key.length() == 0))
-    {
-        getProcessLogger()->warning("invalid config file entry: \"" + line + "\"");
-        return;
-    }
-    else if(key.length() == 0)
-    {
-        return;
-    }
-
-    key = Ice::UTF8ToNative(converter, key);
-    value = Ice::UTF8ToNative(converter, value);
-
-    setProperty(key, value);
-}
-
-void
 Ice::PropertiesI::loadConfig()
 {
     string value = getProperty("Ice.Config");
diff --git a/cpp/src/Ice/PropertiesI.h b/cpp/src/Ice/PropertiesI.h
index 7e346c5..fc580a2 100644
--- a/cpp/src/Ice/PropertiesI.h
+++ b/cpp/src/Ice/PropertiesI.h
@@ -50,8 +50,6 @@ private:
     friend ICE_API PropertiesPtr createProperties(StringSeq&, const PropertiesPtr&, const StringConverterPtr&);
     friend ICE_API PropertiesPtr createProperties(int&, char*[], const PropertiesPtr&, const StringConverterPtr&);
 
-    void parseLine(const std::string&, const StringConverterPtr&);
-
     void loadConfig();
 
     struct PropertyValue
diff --git a/cpp/src/IceUtil/PropertiesHandler.cpp b/cpp/src/IceUtil/PropertiesHandler.cpp
index 85efb95..b4bf6af 100644
--- a/cpp/src/IceUtil/PropertiesHandler.cpp
+++ b/cpp/src/IceUtil/PropertiesHandler.cpp
@@ -9,6 +9,7 @@
 
 #include <IceUtil/DisableWarnings.h>
 #include <IceUtil/StringUtil.h>
+#include <IceUtil/FileUtil.h>
 #include <IceUtil/PropertiesHandler.h>
 
 using namespace std;
@@ -156,3 +157,198 @@ IceUtilInternal::PropertiesHandler::getUnusedProperties()
     }
     return unusedProperties;
 }
+
+void
+IceUtilInternal::PropertiesHandler::load(const std::string& file)
+{
+    IceUtilInternal::ifstream in(file);
+    if(!in)
+    {
+        FileException ex(__FILE__, __LINE__);
+        ex.path = file;
+        ex.error = getSystemErrno();
+        throw ex;
+    }
+    
+    string line;
+    while(getline(in, line))
+    {
+        pair<string, string> keyval;
+        if(parseLine(line, keyval) == ParseLineOK)
+        {
+            setProperty(keyval.first, keyval.second);
+        }
+    }
+}
+
+IceUtilInternal::PropertiesHandler::ParseLineResult
+IceUtilInternal::PropertiesHandler::parseLine(const string& line, pair<string, string>& keyval)
+{
+    string key;
+    string value;
+    
+    enum ParseState { Key , Value };
+    ParseState state = Key;
+
+    string whitespace;
+    string escapedspace;
+    bool finished = false;
+    for(string::size_type i = 0; i < line.size(); ++i)
+    {
+        char c = line[i];
+        switch(state)
+        {
+          case Key:
+          {
+            switch(c)
+            {
+              case '\\':
+                if(i < line.length() - 1)
+                {
+                    c = line[++i];
+                    switch(c)
+                    {
+                      case '\\':
+                      case '#':
+                      case '=':
+                        key += whitespace;
+                        whitespace.clear();
+                        key += c; 
+                        break;
+
+                      case ' ':
+                        if(key.length() != 0)
+                        {
+                            whitespace += c;
+                        }
+                        break;
+
+                      default:
+                        key += whitespace;
+                        whitespace.clear();
+                        key += '\\';
+                        key += c;
+                        break;
+                    }
+                }
+                else
+                {
+                    key += whitespace;
+                    key += c;
+                }
+                break;
+
+              case ' ':
+              case '\t':
+              case '\r':
+              case '\n':
+                  if(key.length() != 0)
+                  {
+                      whitespace += c;
+                  }
+                  break;
+
+              case '=':
+                  whitespace.clear();
+                  state = Value;
+                  break;
+
+              case '#':
+                  finished = true;
+                  break;
+              
+              default:
+                  key += whitespace;
+                  whitespace.clear();
+                  key += c;
+                  break;
+            }
+            break;
+          }
+
+          case Value:
+          {
+            switch(c)
+            {
+              case '\\':
+                if(i < line.length() - 1)
+                {
+                    c = line[++i];
+                    switch(c)
+                    {
+                      case '\\':
+                      case '#':
+                      case '=':
+                        value += value.length() == 0 ? escapedspace : whitespace;
+                        whitespace.clear();
+                        escapedspace.clear();
+                        value += c; 
+                        break;
+
+                      case ' ':
+                        whitespace += c;
+                        escapedspace += c;
+                        break;
+
+                      default:
+                        value += value.length() == 0 ? escapedspace : whitespace;
+                        whitespace.clear();
+                        escapedspace.clear();
+                        value += '\\';
+                        value += c;
+                        break;
+                    }
+                }
+                else
+                {
+                    value += value.length() == 0 ? escapedspace : whitespace;
+                      value += c;
+                }
+                break;
+
+              case ' ':
+              case '\t':
+              case '\r':
+              case '\n':
+                  if(value.length() != 0)
+                  {
+                      whitespace += c;
+                  }
+                  break;
+
+              case '#':
+                  value += escapedspace;
+                  finished = true;
+                  break;
+              
+              default:
+                  value += value.length() == 0 ? escapedspace : whitespace;
+                  whitespace.clear();
+                  escapedspace.clear();
+                  value += c;
+                  break;
+            }
+            break;
+          }
+        }
+        if(finished)
+        {
+            break;
+        }
+    }
+    value += escapedspace;
+
+    if((state == Key && key.length() != 0) || (state == Value && key.length() == 0))
+    {
+        return ParseLineInvalid;
+    }
+    else if(key.length() == 0)
+    {
+        return ParseLineSkip;
+    }
+
+    keyval.first = key;
+    keyval.second = value;
+
+    return ParseLineOK;
+}

commit 1221a61e1fe215fadb0f571f6dfb45be1e4e922b
Author: Kevin P. Fleming <kpfleming at digium.com>
Date:   Tue Mar 15 15:31:50 2011 -0500

    Begin moving the basic functions over to PropertiesHandler.

diff --git a/cpp/include/IceUtil/PropertiesHandler.h b/cpp/include/IceUtil/PropertiesHandler.h
new file mode 100644
index 0000000..06f1fa7
--- /dev/null
+++ b/cpp/include/IceUtil/PropertiesHandler.h
@@ -0,0 +1,88 @@
+// **********************************************************************
+//
+// Copyright (c) 2003-2010 ZeroC, Inc. All rights reserved.
+//
+// This copy of Ice is licensed to you under the terms described in the
+// ICE_LICENSE file included in this distribution.
+//
+// **********************************************************************
+
+#ifndef ICE_UTIL_PROPERTIESHANDLER_H
+#define ICE_UTIL_PROPERTIESHANDLER_H
+
+#include <IceUtil/Config.h>
+#include <IceUtil/Shared.h>
+#include <IceUtil/Handle.h>
+
+#include <string>
+#include <vector>
+#include <map>
+#include <set>
+
+namespace Ice
+{
+
+class PropertiesI;
+
+}
+
+namespace IceUtilInternal
+{
+
+class ICE_UTIL_API PropertiesHandler : public IceUtil::Shared
+{
+public:
+
+    typedef std::vector<std::string> PropertyList;
+    typedef std::map<std::string, std::string> PropertyDict;
+
+    PropertiesHandler();
+    ~PropertiesHandler();
+
+    std::string getProperty(const std::string&);
+    std::string getPropertyWithDefault(const std::string&, const std::string&);
+
+    int getPropertyAsInt(const std::string&);
+    int getPropertyAsIntWithDefault(const std::string&, int);
+
+    PropertyList getPropertyAsList(const std::string&);
+    PropertyList getPropertyAsListWithDefault(const std::string&,
+                                              const PropertyList&);
+
+    PropertyDict getPropertiesForPrefix(const std::string&);
+
+    void setProperty(const std::string&, const std::string&);
+
+    void load(const std::string&);
+
+    std::set<std::string> getUnusedProperties();
+
+private:
+
+    friend class Ice::PropertiesI;
+
+    struct PropertyValue
+    {
+        PropertyValue() :
+            used(false)
+        {
+        }
+
+        PropertyValue(const std::string& v, bool u) :
+            value(v),
+            used(u)
+        {
+        }
+
+        std::string value;
+        bool used;
+    };
+
+    std::map<std::string, PropertyValue> _properties;
+};
+
+typedef IceUtil::Handle<PropertiesHandler> PropertiesHandlerPtr;
+
+}
+
+#endif
diff --git a/cpp/src/Ice/PropertiesI.cpp b/cpp/src/Ice/PropertiesI.cpp
index 5abd8b6..9152590 100644
--- a/cpp/src/Ice/PropertiesI.cpp
+++ b/cpp/src/Ice/PropertiesI.cpp
@@ -28,16 +28,7 @@ Ice::PropertiesI::getProperty(const string& key)
 {
     IceUtil::Mutex::Lock sync(*this);
 
-    map<string, PropertyValue>::iterator p = _properties.find(key);
-    if(p != _properties.end())
-    {
-        p->second.used = true;
-        return p->second.value;
-    }
-    else
-    {
-        return string();
-    }
+    return _propertiesHandler->getProperty(key);
 }
 
 string
@@ -45,16 +36,7 @@ Ice::PropertiesI::getPropertyWithDefault(const string& key, const string& value)
 {
     IceUtil::Mutex::Lock sync(*this);
 
-    map<string, PropertyValue>::iterator p = _properties.find(key);
-    if(p != _properties.end())
-    {
-        p->second.used = true;
-        return p->second.value;
-    }
-    else
-    {
-        return value;
-    }
+    return _propertiesHandler->getPropertyWithDefault(key, value);
 }
 
 Int
@@ -67,22 +49,8 @@ Int
 Ice::PropertiesI::getPropertyAsIntWithDefault(const string& key, Int value)
 {
     IceUtil::Mutex::Lock sync(*this);
-    
-    map<string, PropertyValue>::iterator p = _properties.find(key);
-    if(p != _properties.end())
-    {
-        Int val = value;
-        p->second.used = true;
-        istringstream v(p->second.value);
-        if(!(v >> value) || !v.eof())
-        {
-            Warning out(getProcessLogger());
-            out << "numeric property " << key << " set to non-numeric value, defaulting to " << val;
-            return val;
-        }
-    }
 
-    return value;
+    return _propertiesHandler->getPropertyAsIntWithDefault(key, value);
 }
 
 Ice::StringSeq
@@ -95,48 +63,16 @@ Ice::StringSeq
 Ice::PropertiesI::getPropertyAsListWithDefault(const string& key, const StringSeq& value)
 {
     IceUtil::Mutex::Lock sync(*this);
-    
-    map<string, PropertyValue>::iterator p = _properties.find(key);
-    if(p != _properties.end())
-    {
-        p->second.used = true;
 
-        StringSeq result;
-        if(!IceUtilInternal::splitString(p->second.value, ", \t\r\n", result))
-        {
-            Warning out(getProcessLogger());
-            out << "mismatched quotes in property " << key << "'s value, returning default value";
-        }
-        if(result.size() == 0)
-        {
-            result = value;
-        }
-        return result;
-    }
-    else
-    {
-        return value;
-    }
+    return _propertiesHandler->getPropertyAsListWithDefault(key, value);
 }
 
-
 PropertyDict
 Ice::PropertiesI::getPropertiesForPrefix(const string& prefix)
 {
     IceUtil::Mutex::Lock sync(*this);
 
-    PropertyDict result;
-    map<string, PropertyValue>::iterator p;
-    for(p = _properties.begin(); p != _properties.end(); ++p)
-    {
-        if(prefix.empty() || p->first.compare(0, prefix.size(), prefix) == 0)
-        {
-            p->second.used = true;
-            result[p->first] = p->second.value;
-        }
-    }
-
-    return result;
+    return _propertiesHandler->getPropertiesForPrefix(prefix);
 }
 
 void
@@ -203,23 +139,7 @@ Ice::PropertiesI::setProperty(const string& key, const string& value)
 
     IceUtil::Mutex::Lock sync(*this);
 
-    //
-    // Set or clear the property.
-    //
-    if(!value.empty())
-    {
-        PropertyValue pv(value, false);
-        map<string, PropertyValue>::const_iterator p = _properties.find(currentKey);
-        if(p != _properties.end())
-        {
-            pv.used = p->second.used;
-        }
-        _properties[currentKey] = pv;
-    }
-    else
-    {
-        _properties.erase(currentKey);
-    }
+    _propertiesHandler->setProperty(key, value);
 }
 
 StringSeq
@@ -227,10 +147,11 @@ Ice::PropertiesI::getCommandLineOptions()
 {
     IceUtil::Mutex::Lock sync(*this);
 
+    const map<string, IceUtilInternal::PropertiesHandler::PropertyValue>& props = (*_propertiesHandler)._properties;
     StringSeq result;
-    result.reserve(_properties.size());
-    map<string, PropertyValue>::const_iterator p;
-    for(p = _properties.begin(); p != _properties.end(); ++p)
+    result.reserve(props.size());
+    map<string, IceUtilInternal::PropertiesHandler::PropertyValue>::const_iterator p;
+    for(p = props.begin(); p != props.end(); ++p)
     {
         result.push_back("--" + p->first + "=" + p->second.value);
     }
@@ -414,15 +335,8 @@ set<string>
 Ice::PropertiesI::getUnusedProperties()
 {
     IceUtil::Mutex::Lock sync(*this);
-    set<string> unusedProperties;
-    for(map<string, PropertyValue>::const_iterator p = _properties.begin(); p != _properties.end(); ++p)
-    {
-        if(!p->second.used)
-        {
-            unusedProperties.insert(p->first);
-        }
-    }
-    return unusedProperties;
+
+    return _propertiesHandler->getUnusedProperties();
 }
 
 Ice::PropertiesI::PropertiesI(const PropertiesI* p) :
diff --git a/cpp/src/Ice/PropertiesI.h b/cpp/src/Ice/PropertiesI.h
index c165c4d..7e346c5 100644
--- a/cpp/src/Ice/PropertiesI.h
+++ b/cpp/src/Ice/PropertiesI.h
@@ -11,6 +11,7 @@
 #define ICE_PROPERTIES_I_H
 
 #include <IceUtil/Mutex.h>
+#include <IceUtil/PropertiesHandler.h>
 #include <Ice/Properties.h>
 #include <Ice/StringConverter.h>
 
@@ -71,6 +72,7 @@ private:
     };
     std::map<std::string, PropertyValue> _properties;
     const StringConverterPtr _converter;
+    IceUtilInternal::PropertiesHandlerPtr _propertiesHandler;
 };
 
 
diff --git a/cpp/src/IceUtil/.depend b/cpp/src/IceUtil/.depend
index 188a0b9..5a4212c 100644
--- a/cpp/src/IceUtil/.depend
+++ b/cpp/src/IceUtil/.depend
@@ -21,3 +21,4 @@ Timer$(OBJEXT): Timer.cpp $(includedir)/IceUtil/Timer.h $(includedir)/IceUtil/Sh
 UUID$(OBJEXT): UUID.cpp $(includedir)/IceUtil/UUID.h $(includedir)/IceUtil/Config.h $(includedir)/IceUtil/Random.h $(includedir)/IceUtil/Exception.h
 Unicode$(OBJEXT): Unicode.cpp $(includedir)/IceUtil/Unicode.h $(includedir)/IceUtil/Config.h $(includedir)/IceUtil/Exception.h ../IceUtil/ConvertUTF.h
 MutexProtocol$(OBJEXT): MutexProtocol.cpp $(includedir)/IceUtil/MutexProtocol.h $(includedir)/IceUtil/Config.h
+PropertiesHandler$(OBJEXT): PropertiesHandler.cpp $(includedir)/IceUtil/PropertiesHandler.h $(includedir)/IceUtil/Config.h $(includedir)/IceUtil/Shared.h $(includedir)/IceUtil/Handle.h $(includedir)/IceUtil/Exception.h $(includedir)/IceUtil/DisableWarnings.h
diff --git a/cpp/src/IceUtil/.depend.mak b/cpp/src/IceUtil/.depend.mak
index 1b37d08..20ade2b 100644
--- a/cpp/src/IceUtil/.depend.mak
+++ b/cpp/src/IceUtil/.depend.mak
@@ -21,3 +21,4 @@ Timer$(OBJEXT): Timer.cpp "$(includedir)/IceUtil/Timer.h" "$(includedir)/IceUtil
 UUID$(OBJEXT): UUID.cpp "$(includedir)/IceUtil/UUID.h" "$(includedir)/IceUtil/Config.h" "$(includedir)/IceUtil/Random.h" "$(includedir)/IceUtil/Exception.h"
 Unicode$(OBJEXT): Unicode.cpp "$(includedir)/IceUtil/Unicode.h" "$(includedir)/IceUtil/Config.h" "$(includedir)/IceUtil/Exception.h" ../IceUtil/ConvertUTF.h
 MutexProtocol$(OBJEXT): MutexProtocol.cpp "$(includedir)/IceUtil/MutexProtocol.h" "$(includedir)/IceUtil/Config.h"
+PropertiesHandler$(OBJEXT): PropertiesHandler.cpp "$(includedir)/IceUtil/PropertiesHandler.h" "$(includedir)/IceUtil/Config.h" "$(includedir)/IceUtil/Shared.h" "$(includedir)/IceUtil/Handle.h" "$(includedir)/IceUtil/Exception.h" "$(includedir)/IceUtil/DisableWarnings.h"
diff --git a/cpp/src/IceUtil/Makefile b/cpp/src/IceUtil/Makefile
index 903c3ed..cf69403 100644
--- a/cpp/src/IceUtil/Makefile
+++ b/cpp/src/IceUtil/Makefile
@@ -37,7 +37,8 @@ OBJS		= ArgVector.o \
 		  Timer.o \
 		  UUID.o \
 		  Unicode.o \
-		  MutexProtocol.o
+		  MutexProtocol.o \
+		  PropertiesHandler.o
 
 SRCS		= $(OBJS:.o=.cpp)
 
diff --git a/cpp/src/IceUtil/Makefile.mak b/cpp/src/IceUtil/Makefile.mak
index b384ecb..72ff0e5 100644
--- a/cpp/src/IceUtil/Makefile.mak
+++ b/cpp/src/IceUtil/Makefile.mak
@@ -36,7 +36,8 @@ OBJS		= ArgVector.obj \
 		  Timer.obj \
 		  UUID.obj \
 		  Unicode.obj \
-		  MutexProtocol.obj
+		  MutexProtocol.obj \
+		  PropertiesHandler.obj
 
 SRCS		= $(OBJS:.obj=.cpp)
 
diff --git a/cpp/src/IceUtil/PropertiesHandler.cpp b/cpp/src/IceUtil/PropertiesHandler.cpp
new file mode 100644
index 0000000..85efb95
--- /dev/null
+++ b/cpp/src/IceUtil/PropertiesHandler.cpp
@@ -0,0 +1,158 @@
+// **********************************************************************
+//
+// Copyright (c) 2003-2010 ZeroC, Inc. All rights reserved.
+//
+// This copy of Ice is licensed to you under the terms described in the
+// ICE_LICENSE file included in this distribution.
+//
+// **********************************************************************
+
+#include <IceUtil/DisableWarnings.h>
+#include <IceUtil/StringUtil.h>
+#include <IceUtil/PropertiesHandler.h>
+
+using namespace std;
+
+string
+IceUtilInternal::PropertiesHandler::getProperty(const string& key)
+{
+    map<string, PropertyValue>::iterator p = _properties.find(key);
+    if(p != _properties.end())
+    {
+        p->second.used = true;
+        return p->second.value;
+    }
+    else
+    {
+        return string();
+    }
+}
+
+string
+IceUtilInternal::PropertiesHandler::getPropertyWithDefault(const string& key, const string& value)
+{
+    map<string, PropertyValue>::iterator p = _properties.find(key);
+    if(p != _properties.end())
+    {
+        p->second.used = true;
+        return p->second.value;
+    }
+    else
+    {
+        return value;
+    }
+}
+
+int
+IceUtilInternal::PropertiesHandler::getPropertyAsInt(const string& key)
+{
+    return getPropertyAsIntWithDefault(key, 0);
+}
+
+int
+IceUtilInternal::PropertiesHandler::getPropertyAsIntWithDefault(const string& key, int value)
+{
+    map<string, PropertyValue>::iterator p = _properties.find(key);
+    if(p != _properties.end())
+    {
+        int val = value;
+        p->second.used = true;
+        istringstream v(p->second.value);
+        if(!(v >> value) || !v.eof())
+        {
+            return val;
+        }
+    }
+
+    return value;
+}
+
+vector<string>
+IceUtilInternal::PropertiesHandler::getPropertyAsList(const string& key)
+{
+    return getPropertyAsListWithDefault(key, vector<string>());
+}
+
+vector<string>
+IceUtilInternal::PropertiesHandler::getPropertyAsListWithDefault(const string& key, const vector<string>& value)
+{
+    map<string, PropertyValue>::iterator p = _properties.find(key);
+    if(p != _properties.end())
+    {
+        p->second.used = true;
+
+        vector<string> result;
+        IceUtilInternal::splitString(p->second.value, ", \t\r\n", result);
+        if(result.size() == 0)
+        {
+            result = value;
+        }
+        return result;
+    }
+    else
+    {
+        return value;
+    }
+}
+
+map<string, string>
+IceUtilInternal::PropertiesHandler::getPropertiesForPrefix(const string& prefix)
+{
+    PropertyDict result;
+    for(map<string, PropertyValue>::iterator p = _properties.begin(); p != _properties.end(); ++p)
+    {
+        if(prefix.empty() || p->first.compare(0, prefix.size(), prefix) == 0)
+        {
+            p->second.used = true;
+            result[p->first] = p->second.value;
+        }
+    }
+
+    return result;
+}
+
+void
+IceUtilInternal::PropertiesHandler::setProperty(const string& key, const string& value)
+{
+    //
+    // Trim whitespace
+    //
+    string currentKey = IceUtilInternal::trim(key);
+    if(currentKey.empty())
+    {
+        // TODO: should this throw an exception?
+        return;
+    }
+
+    //
+    // Set or clear the property.
+    //
+    if(!value.empty())
+    {
+        PropertyValue pv(value, false);
+        map<string, PropertyValue>::const_iterator p = _properties.find(currentKey);
+        if(p != _properties.end())
+        {
+            pv.used = p->second.used;
+        }
+        _properties[currentKey] = pv;
+    }
+    else
+    {
+        _properties.erase(currentKey);
+    }
+}
+
+set<string>
+IceUtilInternal::PropertiesHandler::getUnusedProperties()
+{
+    set<string> unusedProperties;
+    for(map<string, PropertyValue>::const_iterator p = _properties.begin(); p != _properties.end(); ++p)
+    {
+        if(!p->second.used)
+        {
+            unusedProperties.insert(p->first);
+        }
+    }
+    return unusedProperties;
+}

-----------------------------------------------------------------------


-- 
asterisk-scf/release/ice.git



More information about the asterisk-scf-commits mailing list