cosmetix
[taglib.d.git] / tagtool.d
blob56465659140480f857db3cf1fb2ea8a2963b9346
1 module tagtool;
2 /* coded by Ketmar // Vampire Avalon (psyc://ketmar.no-ip.org/~Ketmar)
3 * Understanding is not required. Only obedience.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 import core.exception;
19 import std.array;
20 import std.conv;
21 import std.encoding;
22 import std.getopt;
23 import std.stdio;
24 import std.string;
26 import taglib;
27 import k8.encoding;
30 string utf2koi (const(char)[] s) {
31 auto efrom = EncodingScheme.create("utf-8");
32 auto eto = EncodingScheme.create("koi8-u");
33 ubyte[] res;
34 ubyte[8] buf;
35 const(ubyte)[] ub = cast(const(ubyte)[])s;
36 while (ub.length > 0) {
37 dchar dc = efrom.safeDecode(ub);
38 if (dc == INVALID_SEQUENCE) dc = '?';
39 eto.encode(dc, buf);
40 res ~= buf[0];
42 return cast(string)res;
46 string koi2utf (const(char)[] s) {
47 auto efrom = EncodingScheme.create("koi8-u");
48 auto eto = EncodingScheme.create("utf-8");
49 ubyte[] res;
50 ubyte[8] buf;
51 const(ubyte)[] ub = cast(const(ubyte)[])s;
52 while (ub.length > 0) {
53 dchar dc = efrom.safeDecode(ub);
54 if (dc == INVALID_SEQUENCE) dc = '?';
55 auto eclen = eto.encode(dc, buf);
56 res ~= buf[0..eclen];
58 return cast(string)res;
62 private void showTags (string fname) {
63 auto tf = TagFile(fname);
64 if (tf.artist.length) writeln("ARTIST=", utf2koi(tf.artist));
65 if (tf.album.length) writeln("ALBUM=", utf2koi(tf.album));
66 if (tf.title.length) writeln("TITLE=", utf2koi(tf.title));
67 if (tf.genre.length) writeln("GENRE=", utf2koi(tf.genre));
68 if (tf.year) writeln("YEAR=", tf.year);
69 if (tf.track) writeln("TRACKNUMBER=", tf.track);
70 if (tf.comment.length) writeln("COMMENT=", utf2koi(tf.comment));
74 void main (string[] args) {
75 bool optAdd = false;
76 bool optTags = false;
77 string fileName;
79 void usage (string opt=null) {
80 import std.c.process;
81 stdout.write("
82 usage: tagtool [--add] filename [tagfile]
83 tagtool [--add] [--tags] filename [name=value]
84 short options:
85 -a --add
86 -t --tags
87 ");
88 //throw new Exception("nothing to do");
89 throw new ExitException();
90 //exit(1);
93 uint s2i (string s) {
94 import std.ascii;
95 if (s.length == 0) return 0;
96 uint res = 0;
97 while (s.length) {
98 if (!isDigit(s[0])) return 0;
99 res = res*10+s[0]-'0';
100 s = s[1..$];
102 return res;
105 try {
106 getopt(args, std.getopt.config.caseSensitive, std.getopt.config.bundling, std.getopt.config.stopOnFirstNonOption,
107 "add|a", &optAdd,
108 "tags|t", &optTags,
109 "help|h", &usage
111 } catch (Exception e) {
112 stderr.writeln("FATAL: ", e.msg);
113 throw new ExitException();
115 if (args.length < 2) usage();
116 fileName = args[1];
117 args = args[2..$];
119 try {
120 if (args.length == 0) {
121 showTags(fileName);
122 } else {
123 string artist, album, title, genre, comment;
124 uint year, track;
125 if (optAdd) {
126 // load original tags
127 auto tf = TagFile(fileName);
128 if (tf.artist.length) artist = tf.artist;
129 if (tf.album.length) album = tf.album;
130 if (tf.title.length) title = tf.title;
131 if (tf.genre.length) genre = tf.genre;
132 if (tf.comment.length) comment = tf.comment;
133 if (tf.year) year = tf.year;
134 if (tf.track) track = tf.track;
135 debug {
136 writeln("optadd!");
137 writeln(" ARTIST=", utf2koi(artist));
138 writeln(" ALBUM=", utf2koi(album));
139 writeln(" TITLE=", utf2koi(title));
140 writeln(" GENRE=", utf2koi(genre));
141 writeln(" COMMENT=", utf2koi(comment));
142 writeln(" YEAR=", year);
143 writeln(" TRACKNUMBER=", track);
147 void processLn (string v) {
148 v = koi2utf(v.idup);
149 v = v.strip;
150 if (v.length == 0 || v[0] == ';' || v[0] == '#') return;
151 auto ep = v.indexOf('=');
152 if (ep < 1) return;
153 string name = v[0..ep].toUpper;
154 v = v[ep+1..$].strip;
155 debug stdout.writeln("<", name, ">=<", v, ">");
156 switch (name) {
157 case "ARTIST": artist = v; break;
158 case "ALBUM": album = v; break;
159 case "TITLE": title = v; break;
160 case "GENRE": genre = v; break;
161 case "COMMENT": comment = v; break;
162 case "YEAR": case "DATE": year = s2i(v); break;
163 case "TRACK": case "TRACKNUMBER": track = s2i(v); break;
164 default:
168 if (optTags) {
169 foreach (tag; args) processLn(tag);
170 } else if (args[0] != "-") {
171 foreach (fname; args) {
172 foreach (ln; File(fname, "r").byLine) processLn(cast(string)ln);
174 } else {
175 foreach (ln; stdin.byLine) processLn(cast(string)ln);
177 debug {
178 writeln("final:");
179 writeln(" ARTIST=", utf2koi(artist));
180 writeln(" ALBUM=", utf2koi(album));
181 writeln(" TITLE=", utf2koi(title));
182 writeln(" GENRE=", utf2koi(genre));
183 writeln(" COMMENT=", utf2koi(comment));
184 writeln(" YEAR=", year);
185 writeln(" TRACKNUMBER=", track);
187 auto tf = TagFile(fileName);
188 tf.artist = artist;
189 tf.album = album;
190 tf.title = title;
191 tf.genre = genre;
192 tf.comment = comment;
193 tf.year = year;
194 tf.track = track;
195 tf.save();
197 } catch(Exception e) {
198 debug writeln("EXPECTION: ", e.msg);