-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplates.cc
158 lines (129 loc) · 3.3 KB
/
templates.cc
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* Template handling functions for TagReport. */
#include "config.h"
#include "templates.h"
#include "util.h"
#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#ifdef HAVE_LIBIBERTY_H
#include <libiberty.h>
#endif
#include "tagreport.h"
using namespace std;
#include "default.h"
extern int yyparse (void);
extern FILE *yyin;
/* Default templates */
key template_title (DEFAULT_TITLE), template_body_tag(DEFAULT_BODY_TAG);
key template_stats (DEFAULT_STATS), template_prebody(DEFAULT_PREBODY);
key template_body (DEFAULT_BODY), template_footer(DEFAULT_FOOTER);
key template_head_body, template_header; /* unset by default */
const struct replace_map header_map[] = {
{ "$count", COUNT, 6 },
{ "$date", DATE, 5 },
{ "$directory", DIRECTORY, 10 },
};
const struct replace_map body_map[] = {
{ "$artist", ARTIST, 7 },
{ "$title", TITLE, 6 },
{ "$num", NUMBER, 4 },
{ "$a-t", ARTIST_TITLE, 4 }
};
string replace_header (const string & in, int count, const string & directory)
{
unsigned int i, j;
string out = in;
/* Replace all \n with actual newlines */
while ((i = out.find ("\\n")) != string::npos)
out.replace (i, 2, "\n");
for (i = 0; i < ARRAY_SIZE(header_map); i++)
{
while ((j = out.find (header_map[i].term)) != string::npos)
{
ostringstream s;
char* t;
switch (header_map[i].i)
{
case COUNT:
s << count;
break;
case DATE:
t = get_time_string();
s << t;
delete[] t;
break;
case DIRECTORY:
s << directory;
break;
default: /* huh?!? this better not happen */
abort();
}
out.replace (j, header_map[i].term_length, s.str());
}
}
return out;
}
string replace_body (const string & artist, const string & title, unsigned int n)
{
string out = template_body.get();
unsigned int i, j;
/* replace \n with actual newlines */
while ((i = out.find ("\\n")) != string::npos)
out.replace (i, 2, "\n");
/* Look for each term in the list of replacements */
for (i = 0; i < ARRAY_SIZE(body_map); i++)
{
/* find all occurrences */
while ((j = out.find(body_map[i].term)) != string::npos)
{
switch (body_map[i].i)
{
case ARTIST:
out.replace (j, body_map[i].term_length, artist);
break;
case TITLE:
out.replace (j, body_map[i].term_length, title);
break;
case ARTIST_TITLE:
if (artist == "") /* Left unset by traverse_dir */
out.replace (j, body_map[i].term_length, title);
else
{
ostringstream s;
s << artist << " - " << title;
out.replace (j, body_map[i].term_length, s.str());
}
break;
case NUMBER:
{
ostringstream s;
s << n;
out.replace (j, body_map[i].term_length, s.str());
break;
}
default: /* wha? */
abort();
}
}
}
return out;
}
void read_template_file (char * template_fn)
{
FILE * template_file = NULL;
if ((template_file = fopen (template_fn, "r")) == NULL)
{
cerr << "Error reading template " << template_fn << ": " << strerror(errno) << endl;
free(template_fn);
if (!force)
exit(1);
}
else
{
yyin = template_file;
yyparse();
fclose(template_file);
free(template_fn);
}
}