summaryrefslogtreecommitdiff
path: root/libs/pbd
diff options
context:
space:
mode:
authorPaul Davis <paul@linuxaudiosystems.com>2011-11-23 15:09:06 +0000
committerPaul Davis <paul@linuxaudiosystems.com>2011-11-23 15:09:06 +0000
commit8c4a23cfc2d779931194cfb09f6db8eee95a1490 (patch)
tree28248d5523548c7b5197ea97053b933208532b6a /libs/pbd
parent1636ec0a16d7cc468d450f5b443d9eec5584e0f7 (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
Diffstat (limited to 'libs/pbd')
-rw-r--r--libs/pbd/SConscript1
-rw-r--r--libs/pbd/file_subst.cc131
-rw-r--r--libs/pbd/pbd/file_subst.h32
3 files changed, 164 insertions, 0 deletions
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__ */