]> code.delx.au - gnu-emacs-elpa/blob - packages/ampc/ampc_tagger.cpp
Merge commit '0cda39255827f283e7578cd469ae42daad9556a2' from js2-mode
[gnu-emacs-elpa] / packages / ampc / ampc_tagger.cpp
1 // ampc_tagger.el --- Asynchronous Music Player Controller Tagger
2
3 // Copyright (C) 2012 Free Software Foundation, Inc.
4
5 // Author: Christopher Schmidt <christopher@ch.ristopher.com>
6 // Maintainer: Christopher Schmidt <christopher@ch.ristopher.com>
7 // Created: 2012-07-17
8
9 // This file is part of ampc.
10
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation, either version 3 of the License, or
14 // (at your option) any later version.
15
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20
21 // You should have received a copy of the GNU General Public License
22 // along with this program. If not, see <http://www.gnu.org/licenses/>.
23
24 #include <iostream>
25 #include <sstream>
26
27 #include <taglib/fileref.h>
28 #include <taglib/tag.h>
29 #include <taglib/id3v1genres.h>
30
31 std::wstring const version=L"0.1";
32 std::locale original_wcout_locale;
33
34 bool get(std::string const& file)
35 {
36 using namespace TagLib;
37
38 FileRef file_ref(file.c_str());
39 Tag* tag;
40 if(file_ref.isNull() || !(tag=file_ref.tag()))
41 {
42 std::wcerr << L"ERROR: Failed opening file." << std::endl;
43 return true;
44 }
45
46 std::wcout << L"Title: " << tag->title().toWString() << std::endl <<
47 L"Artist: " << tag->artist().toWString() << std::endl <<
48 L"Album: " << tag->album().toWString() << std::endl <<
49 L"Comment: " << tag->comment().toWString() << std::endl <<
50 L"Genre: " << tag->genre().toWString() << std::endl;
51 if(tag->year())
52 {
53 std::wcout << L"Year: ";
54 std::locale new_locale=std::wcout.imbue(original_wcout_locale);
55 std::wcout << tag->year();
56 std::wcout.imbue(new_locale);
57 std::wcout << std::endl;
58 }
59 if(tag->track())
60 {
61 std::wcout << L"Track: ";
62 std::locale new_locale=std::wcout.imbue(original_wcout_locale);
63 std::wcout << tag->track();
64 std::wcout.imbue(new_locale);
65 std::wcout << std::endl;
66 }
67
68 return false;
69 }
70
71 bool set(std::string const& file)
72 {
73 using namespace TagLib;
74
75 FileRef file_ref(file.c_str());
76 Tag* tag;
77 if(file_ref.isNull() || !(tag=file_ref.tag()))
78 {
79 std::wcerr << L"ERROR: Failed opening file." << std::endl;
80 return true;
81 }
82
83 for(;;)
84 {
85 if(!std::wcin)
86 {
87 std::wcerr << L"ERROR: invalid input data." << std::endl;
88 return true;
89 }
90
91 std::wstring tag_to_set;
92 getline(std::wcin, tag_to_set);
93 if(tag_to_set == L"")
94 {
95 break;
96 }
97
98 std::wstring value;
99 getline(std::wcin, value);
100
101 std::wcout << L"Setting " << tag_to_set <<
102 L" to " << value << std::endl;
103
104 if(tag_to_set == L"Title")
105 {
106 tag->setTitle(value);
107 }
108 else if(tag_to_set == L"Artist")
109 {
110 tag->setArtist(value);
111 }
112 else if(tag_to_set == L"Album")
113 {
114 tag->setAlbum(value);
115 }
116 else if(tag_to_set == L"Comment")
117 {
118 tag->setComment(value);
119 }
120 else if(tag_to_set == L"Genre")
121 {
122 tag->setGenre(value);
123 }
124 else if(tag_to_set == L"Year")
125 {
126 unsigned int ival;
127 if(value == L"")
128 {
129 ival=0;
130 }
131 else
132 {
133 std::wistringstream val(value);
134 val >> ival;
135 }
136 tag->setYear(ival);
137 }
138 else if(tag_to_set == L"Track")
139 {
140 unsigned int ival;
141 if(value == L"")
142 {
143 ival=0;
144 }
145 else
146 {
147 std::wistringstream val(value);
148 val >> ival;
149 }
150 tag->setTrack(ival);
151 }
152 else
153 {
154 std::wcerr << L"Unknown tag " << tag_to_set << std::endl;
155 return true;
156 }
157 }
158
159 if(!file_ref.save())
160 {
161 std::wcerr << L"Failed saving file." << std::endl;
162 return true;
163 }
164
165 return false;
166 }
167
168 int main(int const argc, char**const argv)
169 {
170 std::locale loc("");
171 original_wcout_locale=std::wcout.imbue(loc);
172 std::wcin.imbue(loc);
173 std::locale::global(loc);
174
175 std::string action;
176 if(argc >= 2)
177 {
178 action=argv[1];
179 }
180
181 if(action == "--version")
182 {
183 std::wcout << version << std::endl;
184 return 0;
185 }
186 else if(action == "--genres")
187 {
188 using namespace TagLib;
189 StringList genres=ID3v1::genreList();
190 for(StringList::ConstIterator genre=genres.begin();
191 genre!=genres.end();
192 ++genre)
193 {
194 std::wcout << genre->toWString() << std::endl;
195 }
196 return 0;
197 }
198 else if(action == "--get" && argc == 3)
199 {
200 return get(argv[2]) ? 1 : 0;
201 }
202 else if(action == "--set" && argc == 3)
203 {
204 return set(argv[2]) ? 1 : 0;
205 }
206 else
207 {
208 std::wcerr <<
209 L"Usage: ampc_tagger [--version|--genres|--set file|--get file]" <<
210 std::endl;
211 return 1;
212 }
213 }
214
215 // Local Variables:
216 // fill-column: 80
217 // indent-tabs-mode: nil
218 // End: