summaryrefslogtreecommitdiff
path: root/libs/pbd/file_subst.cc
blob: 44805dc38b6f7fbddd2793d0d35f20fbbf61a5bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
    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
PBD::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 (true) {

                str += in.get();

		if (in.eof()) {
			break;
		}

		if (in.fail()) {
			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 (::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;
                                }
                        }

                        out.close ();

                } else {
                        return -1;
                }

        }

        return 0;
}