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
;
30 string
utf2koi (const(char)[] s
) {
31 auto efrom
= EncodingScheme
.create("utf-8");
32 auto eto
= EncodingScheme
.create("koi8-u");
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
= '?';
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");
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
);
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
) {
79 void usage (string opt
=null) {
82 usage: tagtool [--add] filename [tagfile]
83 tagtool [--add] [--tags] filename [name=value]
88 //throw new Exception("nothing to do");
89 throw new ExitException();
95 if (s
.length
== 0) return 0;
98 if (!isDigit(s
[0])) return 0;
99 res
= res
*10+s
[0]-'0';
106 getopt(args
, std
.getopt
.config
.caseSensitive
, std
.getopt
.config
.bundling
, std
.getopt
.config
.stopOnFirstNonOption
,
111 } catch (Exception e
) {
112 stderr
.writeln("FATAL: ", e
.msg
);
113 throw new ExitException();
115 if (args
.length
< 2) usage();
120 if (args
.length
== 0) {
123 string artist
, album
, title
, genre
, comment
;
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
;
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
) {
150 if (v
.length
== 0 || v
[0] == ';' || v
[0] == '#') return;
151 auto ep
= v
.indexOf('=');
153 string name
= v
[0..ep
].toUpper
;
154 v
= v
[ep
+1..$].strip
;
155 debug stdout
.writeln("<", name
, ">=<", v
, ">");
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;
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
);
175 foreach (ln
; stdin
.byLine
) processLn(cast(string
)ln
);
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
);
192 tf
.comment
= comment
;
197 } catch(Exception e
) {
198 debug writeln("EXPECTION: ", e
.msg
);