4 /* coded by Ketmar // Vampire Avalon (psyc://ketmar.no-ip.org/~Ketmar)
5 * Understanding is not required. Only obedience.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 string
utf2koi (const(char)[] s
) {
32 auto efrom
= EncodingScheme
.create("utf-8");
33 auto eto
= EncodingScheme
.create("koi8-u");
36 const(ubyte)[] ub
= cast(const(ubyte)[])s
;
37 while (ub
.length
> 0) {
38 dchar dc
= efrom
.safeDecode(ub
);
39 if (dc
== INVALID_SEQUENCE
) dc
= '?';
43 return cast(string
)res
;
47 string
koi2utf (const(char)[] s
) {
48 auto efrom
= EncodingScheme
.create("koi8-u");
49 auto eto
= EncodingScheme
.create("utf-8");
52 const(ubyte)[] ub
= cast(const(ubyte)[])s
;
53 while (ub
.length
> 0) {
54 dchar dc
= efrom
.safeDecode(ub
);
55 if (dc
== INVALID_SEQUENCE
) dc
= '?';
56 auto eclen
= eto
.encode(dc
, buf
);
59 return cast(string
)res
;
63 private void showTags (string fname
) {
64 auto tf
= TagFile(fname
);
65 if (tf
.artist
.length
) writeln("ARTIST=", utf2koi(tf
.artist
));
66 if (tf
.album
.length
) writeln("ALBUM=", utf2koi(tf
.album
));
67 if (tf
.title
.length
) writeln("TITLE=", utf2koi(tf
.title
));
68 if (tf
.genre
.length
) writeln("GENRE=", utf2koi(tf
.genre
));
69 if (tf
.year
) writeln("YEAR=", tf
.year
);
70 if (tf
.track
) writeln("TRACKNUMBER=", tf
.track
);
71 if (tf
.comment
.length
) writeln("COMMENT=", utf2koi(tf
.comment
));
75 void main (string
[] args
) {
80 void usage (string opt
=null) {
83 usage: tagtool [--add] filename [tagfile]
84 tagtool [--add] [--tags] filename [name=value]
89 //throw new Exception("nothing to do");
95 if (s
.length
== 0) return 0;
98 if (!isDigit(s
[0])) return 0;
99 res
= res
*10+s
[0]-'0';
105 getopt(args
, std
.getopt
.config
.caseSensitive
, std
.getopt
.config
.bundling
, std
.getopt
.config
.stopOnFirstNonOption
,
110 if (args
.length
< 2) usage();
115 if (args
.length
== 0) {
118 string artist
, album
, title
, genre
, comment
;
121 // load original tags
122 auto tf
= TagFile(fileName
);
123 if (tf
.artist
.length
) artist
= tf
.artist
;
124 if (tf
.album
.length
) album
= tf
.album
;
125 if (tf
.title
.length
) title
= tf
.title
;
126 if (tf
.genre
.length
) genre
= tf
.genre
;
127 if (tf
.comment
.length
) comment
= tf
.comment
;
128 if (tf
.year
) year
= tf
.year
;
129 if (tf
.track
) track
= tf
.track
;
132 writeln(" ARTIST=", utf2koi(artist
));
133 writeln(" ALBUM=", utf2koi(album
));
134 writeln(" TITLE=", utf2koi(title
));
135 writeln(" GENRE=", utf2koi(genre
));
136 writeln(" COMMENT=", utf2koi(comment
));
137 writeln(" YEAR=", year
);
138 writeln(" TRACKNUMBER=", track
);
142 void processLn (string v
) {
145 if (v
.length
== 0 || v
[0] == ';' || v
[0] == '#') return;
146 auto ep
= v
.indexOf('=');
148 string name
= v
[0..ep
].toUpper
;
149 v
= v
[ep
+1..$].strip
;
150 debug stdout
.writeln("<", name
, ">=<", v
, ">");
152 case "ARTIST": artist
= v
; break;
153 case "ALBUM": album
= v
; break;
154 case "TITLE": title
= v
; break;
155 case "GENRE": genre
= v
; break;
156 case "COMMENT": comment
= v
; break;
157 case "YEAR": case "DATE": year
= s2i(v
); break;
158 case "TRACK": case "TRACKNUMBER": track
= s2i(v
); break;
164 foreach (tag
; args
) processLn(tag
);
165 } else if (args
[0] != "-") {
166 foreach (fname
; args
) {
167 foreach (ln
; File(fname
, "r").byLine
) processLn(cast(string
)ln
);
170 foreach (ln
; stdin
.byLine
) processLn(cast(string
)ln
);
174 writeln(" ARTIST=", utf2koi(artist
));
175 writeln(" ALBUM=", utf2koi(album
));
176 writeln(" TITLE=", utf2koi(title
));
177 writeln(" GENRE=", utf2koi(genre
));
178 writeln(" COMMENT=", utf2koi(comment
));
179 writeln(" YEAR=", year
);
180 writeln(" TRACKNUMBER=", track
);
182 auto tf
= TagFile(fileName
);
187 tf
.comment
= comment
;
192 } catch(Exception e
) {
193 debug writeln("EXPECTION: ", e
.msg
);