-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbmc2ly.cpp
More file actions
107 lines (90 loc) · 3.33 KB
/
Copy pathbmc2ly.cpp
File metadata and controls
107 lines (90 loc) · 3.33 KB
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
// Copyright (C) 2011-2012 Mario Lang <mlang@delysid.org>
//
// Distributed under the terms of the GNU General Public License version 3.
// (see accompanying file LICENSE.txt or copy at
// http://www.gnu.org/licenses/gpl-3.0-standalone.html)
#include <bmc/ttb/ttb.h>
#include "config.hpp"
#include <fstream>
#include <boost/spirit/include/qi_parse.hpp>
#include "bmc/braille/parsing/grammar/score.hpp"
#include "bmc/braille/semantic_analysis.hpp"
#include <boost/program_options.hpp>
#include "bmc/lilypond.hpp"
int bmc2ly(std::wistream &wistream, bool include_locations, std::string instrument, bool no_tagline) {
std::istreambuf_iterator<wchar_t> wcin_begin(wistream.rdbuf()), wcin_end;
std::wstring source(wcin_begin, wcin_end);
typedef std::wstring::const_iterator iterator_type;
iterator_type iter = source.begin();
iterator_type const end = source.end();
typedef music::braille::error_handler<iterator_type> error_handler_type;
error_handler_type error_handler(iter, end);
typedef music::braille::score_grammar<iterator_type> parser_type;
parser_type parser(error_handler);
boost::spirit::traits::attribute_of<parser_type>::type score;
bool const success = parse(iter, end, parser, score);
if (success and iter == end) {
music::braille::compiler<error_handler_type> compile(error_handler);
if (compile(score)) {
music::lilypond::generator generate(std::cout, true, true, include_locations);
if (not instrument.empty()) generate.instrument(instrument);
if (no_tagline) generate.remove_tagline();
generate(score);
return EXIT_SUCCESS;
} else {
std::wcerr << "Failed to compile:" << std::endl << source << std::endl;
}
} else {
std::wcerr << "Failed to Parse:" << std::endl << source << std::endl;
}
return EXIT_FAILURE;
}
int
main(int argc, char const *argv[])
{
std::locale::global(std::locale(""));
using namespace boost::program_options;
std::string instrument;
bool locations;
bool no_tagline = false;
std::vector<std::string> input_files;
options_description desc("Allowed options");
desc.add_options()
("help,h", "print usage message")
("input-file", value(&input_files), "input file")
("instrument,i", value(&instrument), "default MIDI instrument")
("locations,l", bool_switch(&locations), "Include braille locations in LilyPond output")
("no-tagline", bool_switch(&no_tagline)->default_value(false), "Supress LilyPond default tagline")
;
positional_options_description positional_desc;
positional_desc.add("input-file", -1);
variables_map vm;
try {
store(command_line_parser(argc, argv)
.options(desc).positional(positional_desc).run(), vm);
notify(vm);
} catch (unknown_option) {
std::cerr << "Unknown option" << std::endl << desc << std::endl;
return EXIT_FAILURE;
}
if (vm.count("help")) {
std::cout << desc << std::endl;
return 0;
}
{
char *localeTable = selectTextTable(TABLES_DIRECTORY);
if (localeTable) {
replaceTextTable(TABLES_DIRECTORY, localeTable);
free(localeTable);
}
}
int status = EXIT_SUCCESS;
for (auto const &file: input_files) {
if (file == "-") status = bmc2ly(std::wcin, locations, instrument, no_tagline);
else {
std::wifstream f(file);
if (f.good()) status = bmc2ly(f, locations, instrument, no_tagline);
}
}
return status;
}