Controllable focus notification API
[ardour.git] / libs / pbd / enumwriter.cc
index 134a00a20746cb2057c0a60a3329b10d38633a4e..7b3aba9053bafc4b956b7b4194cec809b4e97915 100644 (file)
@@ -1,4 +1,4 @@
-/* 
+/*
     Copyright (C) 2006 Paul Davis
 
     This program is free software; you can redistribute it and/or modify
@@ -19,6 +19,7 @@
 */
 
 #include <cctype>
+#include <algorithm>
 
 #include <cstring>
 #include <cstdlib>
 using namespace std;
 using namespace PBD;
 
-#include "i18n.h"
+#include "pbd/i18n.h"
 
 EnumWriter* EnumWriter::_instance = 0;
 map<string,string> EnumWriter::hack_table;
 
-static int 
-nocase_cmp(const string & s1, const string& s2) 
+static int
+nocase_cmp(const string & s1, const string& s2)
 {
        string::const_iterator it1 = s1.begin();
        string::const_iterator it2 = s2.begin();
-       
-       while ((it1 != s1.end()) && (it2 != s2.end())) { 
+
+       while ((it1 != s1.end()) && (it2 != s2.end())) {
                if(::toupper(*it1) != ::toupper(*it2))  {//letters differ?
                        // return -1 to indicate 'smaller than', 1 otherwise
-                       return (::toupper(*it1) < ::toupper(*it2)) ? -1 : 1; 
+                       return (::toupper(*it1) < ::toupper(*it2)) ? -1 : 1;
                }
 
                ++it1;
@@ -63,11 +64,25 @@ nocase_cmp(const string & s1, const string& s2)
        return (size1 < size2) ? -1 : 1;
 }
 
-EnumWriter::EnumWriter ()
+EnumWriter&
+EnumWriter::instance()
 {
        if (_instance == 0) {
-               _instance = this;
+               _instance = new EnumWriter;
        }
+
+       return *_instance;
+}
+
+void
+EnumWriter::destroy ()
+{
+       delete _instance;
+       _instance = 0;
+}
+
+EnumWriter::EnumWriter ()
+{
 }
 
 EnumWriter::~EnumWriter ()
@@ -82,7 +97,7 @@ EnumWriter::register_distinct (string type, vector<int> v, vector<string> s)
 
        newpair.first = type;
        newpair.second = EnumRegistration (v, s, false);
-       
+
        result = registry.insert (newpair);
 
        if (!result.second) {
@@ -98,7 +113,7 @@ EnumWriter::register_bits (string type, vector<int> v, vector<string> s)
 
        newpair.first = type;
        newpair.second = EnumRegistration (v, s, true);
-       
+
        result = registry.insert (newpair);
 
        if (!result.second) {
@@ -113,7 +128,7 @@ EnumWriter::write (string type, int value)
 
        if (x == registry.end()) {
                error << string_compose (_("EnumWriter: unknown enumeration type \"%1\""), type) << endmsg;
-               throw unknown_enumeration();
+               throw unknown_enumeration (type);
        }
 
        if (x->second.bitwise) {
@@ -130,7 +145,7 @@ EnumWriter::read (string type, string value)
 
        if (x == registry.end()) {
                error << string_compose (_("EnumWriter: unknown enumeration type \"%1\""), type) << endmsg;
-               throw unknown_enumeration();
+               throw unknown_enumeration (type);
        }
 
        if (x->second.bitwise) {
@@ -138,7 +153,7 @@ EnumWriter::read (string type, string value)
        } else {
                return read_distinct (x->second, value);
        }
-}      
+}
 
 string
 EnumWriter::write_bits (EnumRegistration& er, int value)
@@ -151,7 +166,7 @@ EnumWriter::write_bits (EnumRegistration& er, int value)
                if (value & (*i)) {
                        if (!result.empty()) {
                                result += ',';
-                       } 
+                       }
                        result += (*s);
                }
        }
@@ -174,6 +189,56 @@ EnumWriter::write_distinct (EnumRegistration& er, int value)
        return string();
 }
 
+int
+EnumWriter::validate (EnumRegistration& er, int val) const
+{
+        if (er.values.empty()) {
+                return val;
+        }
+
+        if (val == 0) {
+                /* zero is always a legal value for our enumerations, just about
+                 */
+                return val;
+        }
+
+        vector<int>::iterator i;
+        string enum_name = _("unknown enumeration");
+
+        for (Registry::const_iterator x = registry.begin(); x != registry.end(); ++x) {
+                if (&er == &(*x).second) {
+                        enum_name = (*x).first;
+                }
+        }
+
+
+        for (i = er.values.begin(); i != er.values.end(); ++i) {
+                if (*i == val) {
+                        return val;
+                }
+        }
+
+        warning << string_compose (_("Illegal value loaded for %1 (%2) - %3 used instead"),
+                                   enum_name, val, er.names.front())
+                << endmsg;
+        return er.values.front();
+}
+
+int
+EnumWriter::validate_bitwise (EnumRegistration& er, int val) const
+{
+       int result = 0;
+       for (int p = 1; p <= val; p = p << 1) {
+               if (std::find (er.values.begin(), er.values.end(), p) == er.values.end()) {
+                       continue;
+               }
+               if (p & val) {
+                       result |= p;
+               }
+       }
+       return result;
+}
+
 int
 EnumWriter::read_bits (EnumRegistration& er, string str)
 {
@@ -186,17 +251,19 @@ EnumWriter::read_bits (EnumRegistration& er, string str)
        /* catch old-style hex numerics */
 
        if (str.length() > 2 && str[0] == '0' && str[1] == 'x') {
-               return strtol (str.c_str(), (char **) 0, 16);
+               int val = strtol (str.c_str(), (char **) 0, 16);
+                return validate_bitwise (er, val);
        }
 
        /* catch old style dec numerics */
 
        if (strspn (str.c_str(), "0123456789") == str.length()) {
-               return strtol (str.c_str(), (char **) 0, 10);
-       }
+               int val = strtol (str.c_str(), (char **) 0, 10);
+                return validate_bitwise (er, val);
+        }
 
        do {
-               
+
                comma = str.find_first_of (',');
                string segment = str.substr (0, comma);
 
@@ -216,7 +283,7 @@ EnumWriter::read_bits (EnumRegistration& er, string str)
        } while (true);
 
        if (!found) {
-               throw unknown_enumeration();
+               throw unknown_enumeration (str);
        }
 
        return result;
@@ -228,25 +295,7 @@ EnumWriter::read_distinct (EnumRegistration& er, string str)
        vector<int>::iterator i;
        vector<string>::iterator s;
 
-       /* catch old-style hex numerics */
-
-       if (str.length() > 2 && str[0] == '0' && str[1] == 'x') {
-               return strtol (str.c_str(), (char **) 0, 16);
-       }
-
-       /* catch old style dec numerics */
-
-       if (strspn (str.c_str(), "0123456789") == str.length()) {
-               return strtol (str.c_str(), (char **) 0, 10);
-       }
-
-       for (i = er.values.begin(), s = er.names.begin(); i != er.values.end(); ++i, ++s) {
-               if (str == (*s) || nocase_cmp (str, *s) == 0) {
-                       return (*i);
-               }
-       }
-
-       /* failed to find it as-is. check to see if there a hack for the name we're looking up */
+       /* first, check to see if there a hack for the name we're looking up */
 
        map<string,string>::iterator x;
 
@@ -263,7 +312,27 @@ EnumWriter::read_distinct (EnumRegistration& er, string str)
                }
        }
 
-       throw unknown_enumeration();
+       /* catch old-style hex numerics */
+
+       if (str.length() > 2 && str[0] == '0' && str[1] == 'x') {
+               int val = strtol (str.c_str(), (char **) 0, 16);
+                return validate (er, val);
+       }
+
+       /* catch old style dec numerics */
+
+       if (strspn (str.c_str(), "0123456789") == str.length()) {
+               int val = strtol (str.c_str(), (char **) 0, 10);
+                return validate (er, val);
+        }
+
+       for (i = er.values.begin(), s = er.names.begin(); i != er.values.end(); ++i, ++s) {
+               if (str == (*s) || nocase_cmp (str, *s) == 0) {
+                       return (*i);
+               }
+       }
+
+       throw unknown_enumeration(str);
 }
 
 void