fix crash at session close/exit if a midi-control-surface is used
[ardour.git] / libs / pbd / enumwriter.cc
index d6c882e00a46ef0d6f40bcad8509577425852445..f67d305080e62bad89e34ca748d7ae0e4ceaab1d 100644 (file)
     $Id$
 */
 
-#include <stdlib.h>
+#include <cctype>
 
-#include <pbd/enumwriter.h>
-#include <pbd/error.h>
-#include <pbd/compose.h>
+#include <cstring>
+#include <cstdlib>
+
+#include "pbd/enumwriter.h"
+#include "pbd/error.h"
+#include "pbd/compose.h"
 
 using namespace std;
 using namespace PBD;
@@ -30,12 +33,55 @@ using namespace PBD;
 #include "i18n.h"
 
 EnumWriter* EnumWriter::_instance = 0;
+map<string,string> EnumWriter::hack_table;
 
-EnumWriter::EnumWriter ()
+static int 
+nocase_cmp(const string & s1, const string& s2) 
 {
-       if (_instance == 0) {
-               _instance = this;
+       string::const_iterator it1 = s1.begin();
+       string::const_iterator it2 = s2.begin();
+       
+       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; 
+               }
+
+               ++it1;
+               ++it2;
        }
+
+       string::size_type size1 = s1.size();
+       string::size_type size2 = s2.size();
+
+       //return -1,0 or 1 according to strings' lengths
+
+       if (size1 == size2) {
+               return 0;
+       }
+
+       return (size1 < size2) ? -1 : 1;
+}
+
+EnumWriter&
+EnumWriter::instance() 
+{
+       if (_instance == 0) {
+               _instance = new EnumWriter;
+       } 
+
+       return *_instance;
+}
+
+void
+EnumWriter::destroy ()
+{
+       delete _instance;
+       _instance = 0;
+}
+
+EnumWriter::EnumWriter ()
+{
 }
 
 EnumWriter::~EnumWriter ()
@@ -81,7 +127,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) {
@@ -98,7 +144,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) {
@@ -142,6 +188,41 @@ EnumWriter::write_distinct (EnumRegistration& er, int value)
        return string();
 }
 
+int
+EnumWriter::validate (EnumRegistration& er, int val)
+{
+        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::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::read_bits (EnumRegistration& er, string str)
 {
@@ -154,16 +235,24 @@ 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 (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);
+        }
+
        do {
                
                comma = str.find_first_of (',');
                string segment = str.substr (0, comma);
 
                for (i = er.values.begin(), s = er.names.begin(); i != er.values.end(); ++i, ++s) {
-                       if (segment == (*s)) {
+                       if (segment == *s || nocase_cmp (segment, *s) == 0) {
                                result |= (*i);
                                found = true;
                        }
@@ -178,7 +267,7 @@ EnumWriter::read_bits (EnumRegistration& er, string str)
        } while (true);
 
        if (!found) {
-               throw unknown_enumeration();
+               throw unknown_enumeration (str);
        }
 
        return result;
@@ -190,19 +279,48 @@ EnumWriter::read_distinct (EnumRegistration& er, string str)
        vector<int>::iterator i;
        vector<string>::iterator s;
 
+       /* first, check to see if there a hack for the name we're looking up */
+
+       map<string,string>::iterator x;
+
+       if ((x  = hack_table.find (str)) != hack_table.end()) {
+
+               cerr << "found hack for " << str << " = " << x->second << endl;
+
+               str = x->second;
+
+               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);
+                       }
+               }
+       }
+
        /* 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 (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)) {
+               if (str == (*s) || nocase_cmp (str, *s) == 0) {
                        return (*i);
                }
        }
 
-       throw unknown_enumeration();
+       throw unknown_enumeration(str);
 }
 
-
+void
+EnumWriter::add_to_hack_table (string str, string hacked)
+{
+       hack_table[str] = hacked;
+}