~ubuntu-branches/ubuntu/trusty/hugin/trusty-proposed

« back to all changes in this revision

Viewing changes to src/hugin_base/makefilelib/Makefile.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Metzler
  • Date: 2011-01-06 14:28:24 UTC
  • mfrom: (1.1.9 upstream) (0.1.21 experimental)
  • Revision ID: james.westby@ubuntu.com-20110106142824-zn9lxylg5z44dynn
* Drop Cyril Brulebois from Uploaders. Thank you very much for your work.
* Bump package version. (rc3 was re-released as 2010.4.0).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
This file is part of hugin.
 
3
 
 
4
hugin is free software: you can redistribute it and/or modify
 
5
it under the terms of the GNU General Public License as published by
 
6
the Free Software Foundation, either version 2 of the License, or
 
7
(at your option) any later version.
 
8
 
 
9
hugin is distributed in the hope that it will be useful,
 
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
GNU General Public License for more details.
 
13
 
 
14
You should have received a copy of the GNU General Public License
 
15
along with hugin.  If not, see <http://www.gnu.org/licenses/>.
 
16
*/
 
17
 
 
18
/**
 
19
 * @file Makefile.cpp
 
20
 * @brief
 
21
 *  Created on: May 25, 2010
 
22
 * @author Florian Achleitner <florian.achleitner.2.6.31@gmail.com>
 
23
 */
 
24
 
 
25
#include "Makefile.h"
 
26
#include "MakefileItem.h"
 
27
 
 
28
namespace makefile
 
29
{
 
30
 
 
31
// intialize static singelton instance pointer
 
32
Makefile* Makefile::instance = NULL;
 
33
 
 
34
// static
 
35
/// Used for numeric output to get a decimal dot anyways.
 
36
const std::locale Makefile::locale(std::locale("C"));
 
37
 
 
38
const std::locale GetMakefileLocale()
 
39
{
 
40
    return Makefile::locale;
 
41
};
 
42
 
 
43
Makefile& Makefile::getSingleton()
 
44
{
 
45
        if(!instance)
 
46
                instance = new Makefile();
 
47
        return *instance;
 
48
}
 
49
 
 
50
void Makefile::clean()
 
51
{
 
52
        if(instance)
 
53
                delete instance;
 
54
        instance = NULL;
 
55
}
 
56
 
 
57
void Makefile::remove(MakefileItem* item)
 
58
{
 
59
        if(instance && !instance->written)
 
60
        {
 
61
                std::cerr <<
 
62
                "WARNING: A MakefileItem was removed before the Makefile::writeMakefile() was called.\n"
 
63
                "This is likely to be a programming error (out of scope?)" << std::endl;
 
64
        }
 
65
        clean();
 
66
}
 
67
 
 
68
//#define WIN32
 
69
/**
 
70
 * Quotes and escapes characters using regular expressions. Two modes are currently distinguished,
 
71
 * depending on the usage of the string.
 
72
 * The regular expressions need a lot of backslash escaping, eg. \\\\ means backslash. We need to get
 
73
 * through the compiler and the boost::regex library with the special characters
 
74
 *
 
75
 * The replacements in detail:
 
76
 * - Shell mode
 
77
 *   - WIN32: $ --> $$, \ --> /, # --> \#, and surround with quotes
 
78
 *   - others: $ --> \$$, [\ ~"|'`{}[]()*#:=] --> escape with backslash
 
79
 * - Make mode
 
80
 *   - WIN32: $ --> $$, [ #=] --> escape with backslash
 
81
 *   - others: $ --> $$, [ #:=] --> escape with backslash
 
82
 *
 
83
 * All replacements take care of variable references and do not replace $(varname) patterns.
 
84
 *
 
85
 * @note src/hugin_base/hugin_utils/platform.h is where the information is from.
 
86
 * Unfortunately there are lots of comments in that code, which say that the correctness
 
87
 * of the escaping rules is rather unsure.
 
88
 *
 
89
 * @param in Input string.
 
90
 * @param mode switch.
 
91
 * @return A new string containing the processed content.
 
92
 */
 
93
string Makefile::quote(const string& in, Makefile::QuoteMode mode)
 
94
{
 
95
        regex toescape;
 
96
        string output;
 
97
        switch(mode)
 
98
        {
 
99
        case Makefile::SHELL:
 
100
#ifdef WIN32
 
101
                toescape.assign(cstr("(\\$[^\\(])|(\\\\)|(\\#)"));
 
102
                // uses a nice regex feature "recursive expressions" for doing it all in one (subexpression) cascade.
 
103
                output.assign(cstr("(?1\\$$&)(?2/)(?3\\\\$&)"));
 
104
                return string(cstr("\"") + boost::regex_replace(in, toescape, output, boost::match_default | boost::format_all) + cstr("\""));
 
105
#else
 
106
                // because parenthesis are replaced too, the first pattern detects variable references and passes them unchanged.
 
107
                toescape.assign(cstr("(\\$\\([^\\)]+\\))|(\\$)|([\\\\ \\~\"\\|\\'\\`\\{\\}\\[\\]\\(\\)\\*\\#\\:\\=])"));
 
108
                output.assign(cstr("(?1$&)(?2\\\\\\$$&)(?3\\\\$&)"));
 
109
                return boost::regex_replace(in, toescape, output, boost::match_default | boost::format_all);
 
110
#endif
 
111
                break;
 
112
        case Makefile::MAKE:
 
113
#ifdef WIN32
 
114
                toescape.assign(cstr("(\\$[^\\(])|(\\\\)|([ \\#\\=])"));
 
115
                output.assign(cstr("(?1\\$$&)(?2/)(?3\\\\$&)"));
 
116
                return boost::regex_replace(in, toescape, output, boost::match_default | boost::format_all);
 
117
#else
 
118
                // do not replace $ if followed by a (. To allow variable references.
 
119
                toescape.assign(cstr("(\\$[^\\(])|([ \\#\\:\\=])"));
 
120
                output.assign(cstr("(?1\\$$&)(?2\\\\$&)"));
 
121
                return boost::regex_replace(in, toescape, output, boost::match_default | boost::format_all);
 
122
#endif
 
123
                break;
 
124
        default:
 
125
                return string(in);
 
126
        }
 
127
}
 
128
 
 
129
int Makefile::writeMakefile(ostream& out)
 
130
{
 
131
        for(std::vector<MakefileItem*>::iterator i = items.begin(); i != items.end(); i++)
 
132
        {
 
133
                out << **i;
 
134
        }
 
135
        written = true;
 
136
        return items.size();
 
137
}
 
138
 
 
139
}