diff options
author | Paul Davis <paul@linuxaudiosystems.com> | 2011-11-23 15:09:06 +0000 |
---|---|---|
committer | Paul Davis <paul@linuxaudiosystems.com> | 2011-11-23 15:09:06 +0000 |
commit | 8c4a23cfc2d779931194cfb09f6db8eee95a1490 (patch) | |
tree | 28248d5523548c7b5197ea97053b933208532b6a | |
parent | 1636ec0a16d7cc468d450f5b443d9eec5584e0f7 (diff) |
initial rev of file_subst() and its initial use (testing for now) for converting old OS X bindings from Meta to Mod2 modifiers
git-svn-id: svn://localhost/ardour2/branches/2.0-ongoing@10793 d708f5d6-7413-0410-9779-e7cbd77b26cf
-rw-r--r-- | gtk2_ardour/keyboard.cc | 12 | ||||
-rw-r--r-- | libs/pbd/SConscript | 1 | ||||
-rw-r--r-- | libs/pbd/file_subst.cc | 131 | ||||
-rw-r--r-- | libs/pbd/pbd/file_subst.h | 32 |
4 files changed, 176 insertions, 0 deletions
diff --git a/gtk2_ardour/keyboard.cc b/gtk2_ardour/keyboard.cc index 1fe5ff5833..aadade264c 100644 --- a/gtk2_ardour/keyboard.cc +++ b/gtk2_ardour/keyboard.cc @@ -32,6 +32,7 @@ #include <gdk/gdkkeysyms.h> #include <pbd/error.h> +#include <pbd/file_subst.h> #include "keyboard.h" #include "gui_thread.h" @@ -613,6 +614,17 @@ Keyboard::setup_keybindings () bool Keyboard::load_keybindings (string path) { +#ifdef GTKOSX + /* various versions of the GTK/OSX stack that we have used have had different + modifier masks associated with the command key. The current version (November 2011) + uses Mod2, previous versions used Meta. Edit the file before loading to change + any Meta entries to Mod2 + */ + std::map<string,string> dict; + dict.push_back (pair ("<Meta>", "<Mod2>")); + (void) PBD::file_subst (path, dict); +#endif + try { cerr << "loading bindings from " << path << endl; diff --git a/libs/pbd/SConscript b/libs/pbd/SConscript index 1c2ee943db..c9751e80ac 100644 --- a/libs/pbd/SConscript +++ b/libs/pbd/SConscript @@ -28,6 +28,7 @@ enumwriter.cc dmalloc.cc epa.cc error.cc +file_subst.cc fpu.cc id.cc localeguard.cc diff --git a/libs/pbd/file_subst.cc b/libs/pbd/file_subst.cc new file mode 100644 index 0000000000..e2e9c01086 --- /dev/null +++ b/libs/pbd/file_subst.cc @@ -0,0 +1,131 @@ +/* + Copyright (C) 2011 Paul Davis + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ + +#include <iostream> +#include <fstream> +#include <sstream> +#include <cstdio> /* for ::rename() */ +#include <unistd.h> +#include <errno.h> +#include <string.h> + +#include "pbd/file_subst.h" +#include "pbd/error.h" +#include "pbd/replace_all.h" +#include "pbd/compose.h" + +#include "i18n.h" + +using namespace PBD; +using namespace std; + +/* this implementation assumes that the contents of the file are small enough to fit reasonably into + memory. +*/ + +int +file_subst (const string& path, const map<string,string>& dict) +{ + ifstream in (path.c_str()); + int replacements = 0; + + if (!in) { + return -1; + } + + /* Get the size of the file */ + in.seekg(0,std::ios::end); + std::streampos length = in.tellg(); + in.seekg(0,std::ios::beg); + + /* allocate a string to hold the data */ + + string str; + + try { + str.reserve (length); + } catch (exception& e) { + error << string_compose (_("could not reserve space to read substitution data from %1 (err: %2"), + path, e.what()) << endmsg; + in.close (); + return -1; + } + + /* read the file contents into the string */ + + while (!in.eof()) { + str += in.get(); + } + + if (!in) { + error << string_compose (_("could not read data for substitution from %1 (err: %2)"), + path, strerror (errno)) << endmsg; + in.close (); + return -1; + } + + in.close (); + + /* do the replacement */ + + for (map<string,string>::const_iterator i = dict.begin(); i != dict.end(); ++i) { + replacements += replace_all (str, i->first, i->second); + } + + if (replacements) { + char suffix[64]; + snprintf (suffix, sizeof (suffix), ".fs_%d", getpid()); + string s = path + suffix; + + + ofstream out (s.c_str()); + + if (out) { + + out << str; + + if (!out) { + /* ignore error since we're failing anyway, although + it will leave the file around. + */ + out.close (); + (void) ::unlink (s.c_str()); + return -1; + } else { +#if 0 + if (::rename (s.c_str(), path.c_str())) { + error << string_compose (_("Could not rename substituted file %1 to %2 (err: %3)"), + s, path, strerror (errno)) << endmsg; + (void) ::unlink (s.c_str()); + out.close (); + return -1; + } +#endif + } + + out.close (); + + } else { + return -1; + } + + } + + return 0; +} diff --git a/libs/pbd/pbd/file_subst.h b/libs/pbd/pbd/file_subst.h new file mode 100644 index 0000000000..543a7473c8 --- /dev/null +++ b/libs/pbd/pbd/file_subst.h @@ -0,0 +1,32 @@ +/* + Copyright (C) 2011 Paul Davis + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ + +#ifndef __pbd_file_subst_h__ +#define __pbd_file_subst_h__ + +#include <string> +#include <map> + +namespace PBD { + + int file_subst (std::string& path, const std::map<std::string,std::string>&); + +} //namespace PBD + +#endif /* __pbd_file_subst_h__ */ |