[xiph-cvs] cvs commit: vorbis-tools/oggenc encode.h oggenc.c platform.h
Michael Smith
msmith at xiph.org
Sat Sep 15 08:10:36 PDT 2001
msmith 01/09/15 08:10:35
Modified: oggenc encode.h oggenc.c platform.h
Log:
Implement configurable name-format replacement (along the lines of tr), heavily
based on a patch contributed by Simon Hosie <simon.hosie at clear.net.nz>
Default settings on windows (remove /\:<>|), and other (remove /)
Revision Changes Path
1.11 +2 -0 vorbis-tools/oggenc/encode.h
Index: encode.h
===================================================================
RCS file: /usr/local/cvsroot/vorbis-tools/oggenc/encode.h,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- encode.h 2001/09/15 11:56:10 1.10
+++ encode.h 2001/09/15 15:10:34 1.11
@@ -56,6 +56,8 @@
int raw_channels;
char *namefmt;
+ char *namefmt_remove;
+ char *namefmt_replace;
char *outfile;
/* All 3 in kbps */
int min_bitrate;
1.29 +94 -30 vorbis-tools/oggenc/oggenc.c
Index: oggenc.c
===================================================================
RCS file: /usr/local/cvsroot/vorbis-tools/oggenc/oggenc.c,v
retrieving revision 1.28
retrieving revision 1.29
diff -u -r1.28 -r1.29
--- oggenc.c 2001/09/15 11:56:10 1.28
+++ oggenc.c 2001/09/15 15:10:34 1.29
@@ -34,6 +34,8 @@
{"album",1,0,'l'},
{"title",1,0,'t'},
{"names",1,0,'n'},
+ {"name-remove",1,0,'X'},
+ {"name-replace",1,0,'P'},
{"output",1,0,'o'},
{"version",0,0,'v'},
{"raw",0,0,'r'},
@@ -51,17 +53,20 @@
{NULL,0,0,0}
};
-char *generate_name_string(char *format, char *artist, char *title, char *album, char *track, char *date);
-void parse_options(int argc, char **argv, oe_options *opt);
-void build_comments(vorbis_comment *vc, oe_options *opt, int filenum,
- char **artist, char **album, char **title, char **tracknum, char **date);
-void usage(void);
+static char *generate_name_string(char *format, char *remove_list,
+ char *replace_list, char *artist, char *title, char *album,
+ char *track, char *date);
+static void parse_options(int argc, char **argv, oe_options *opt);
+static void build_comments(vorbis_comment *vc, oe_options *opt, int filenum,
+ char **artist,char **album, char **title, char **tracknum, char **date);
+static void usage(void);
int main(int argc, char **argv)
{
/* Default values */
oe_options opt = {"ISO-8859-1", NULL, 0, NULL, 0, NULL, 0, NULL, 0, NULL,
- 0, NULL, 0, 0, 0,16,44100,2, NULL,NULL, -1,128,-1, -1.0f,0};
+ 0, NULL, 0, 0, 0,16,44100,2, NULL,DEFAULT_NAMEFMT_REMOVE,
+ DEFAULT_NAMEFMT_REPLACE, NULL, -1,128,-1, -1.0f,0};
int i;
char **infiles;
@@ -202,7 +207,8 @@
}
else if(opt.namefmt)
{
- out_fn = generate_name_string(opt.namefmt, artist, title, album, track,date);
+ out_fn = generate_name_string(opt.namefmt, opt.namefmt_remove,
+ opt.namefmt_replace, artist, title, album, track,date);
}
else if(opt.title)
{
@@ -277,7 +283,7 @@
}
-void usage(void)
+static void usage(void)
{
fprintf(stdout,
VERSION_STRING
@@ -316,6 +322,14 @@
" %%n, %%d replaces by artist, title, album, track number,\n"
" and date, respectively (see below for specifying these).\n"
" %%%% gives a literal %%.\n"
+ " -X, --name-remove=s Remove the specified characters from parameters to the\n"
+ " -n format string. Useful to ensure legal filenames.\n"
+ " -P, --name-replace=s Replace characters remove by --name-remove with the\n"
+ " characters specified. If this string shorter than the\n"
+ " --name-remove list or is not specified, the extra\n"
+ " characters are just removed.\n"
+ " Default settings for the above two arguments are platform\n"
+ " specific.\n"
" -c, --comment=c Add the given string as an extra comment. This may be\n"
" used multiple times.\n"
" -d, --date Date for track (usually date of performance)\n"
@@ -347,12 +361,49 @@
"\n");
}
-char *generate_name_string(char *format,
- char *artist, char *title, char *album, char *track, char *date)
+static int strncpy_filtered(char *dst, char *src, int len, char *remove_list,
+ char *replace_list)
{
+ char *hit, *drop_margin;
+ int used=0;
+
+ if(remove_list == NULL || *remove_list == 0)
+ {
+ strncpy(dst, src, len-1);
+ dst[len-1] = 0;
+ return strlen(dst);
+ }
+
+ drop_margin = remove_list + (replace_list == NULL?0:strlen(replace_list));
+
+ while(*src && used < len-1)
+ {
+ if((hit = strchr(remove_list, *src)) != NULL)
+ {
+ if(hit < drop_margin)
+ {
+ *dst++ = replace_list[hit - remove_list];
+ used++;
+ }
+ }
+ else
+ {
+ *dst++ = *src;
+ used++;
+ }
+ src++;
+ }
+ *dst = 0;
+
+ return used;
+}
+
+static char *generate_name_string(char *format, char *remove_list,
+ char *replace_list, char *artist, char *title, char *album,
+ char *track, char *date)
+{
char *buffer;
char next;
- int len;
char *string;
int used=0;
int buflen;
@@ -373,33 +424,28 @@
break;
case 'a':
string = artist?artist:"(none)";
- len = strlen(string);
- strncpy(buffer+used, string, buflen-used);
- used += len;
+ used += strncpy_filtered(buffer+used, string, buflen-used,
+ remove_list, replace_list);
break;
case 'd':
string = date?date:"(none)";
- len = strlen(string);
- strncpy(buffer+used, string, buflen-used);
- used += len;
+ used += strncpy_filtered(buffer+used, string, buflen-used,
+ remove_list, replace_list);
break;
case 't':
string = title?title:"(none)";
- len = strlen(string);
- strncpy(buffer+used, string, buflen-used);
- used += len;
+ used += strncpy_filtered(buffer+used, string, buflen-used,
+ remove_list, replace_list);
break;
case 'l':
string = album?album:"(none)";
- len = strlen(string);
- strncpy(buffer+used, string, buflen-used);
- used += len;
+ used += strncpy_filtered(buffer+used, string, buflen-used,
+ remove_list, replace_list);
break;
case 'n':
string = track?track:"(none)";
- len = strlen(string);
- strncpy(buffer+used, string, buflen-used);
- used += len;
+ used += strncpy_filtered(buffer+used, string, buflen-used,
+ remove_list, replace_list);
break;
default:
fprintf(stderr, "WARNING: Ignoring illegal escape character '%c' in name format\n", *(format - 1));
@@ -413,12 +459,12 @@
return buffer;
}
-void parse_options(int argc, char **argv, oe_options *opt)
+static void parse_options(int argc, char **argv, oe_options *opt)
{
int ret;
int option_index = 1;
- while((ret = getopt_long(argc, argv, "a:b:B:c:C:d:e:hl:m:M:n:N:o:q:QrR:s:t:v",
+ while((ret = getopt_long(argc, argv, "a:b:B:c:C:d:e:hl:m:M:n:N:o:P:q:QrR:s:t:vX:",
long_options, &option_index)) != -1)
{
switch(ret)
@@ -486,6 +532,24 @@
}
opt->namefmt = strdup(optarg);
break;
+ case 'X':
+ if(opt->namefmt_remove && opt->namefmt_remove !=
+ DEFAULT_NAMEFMT_REMOVE)
+ {
+ fprintf(stderr, "WARNING: Multiple name format filters specified, using final\n");
+ free(opt->namefmt_remove);
+ }
+ opt->namefmt_remove = strdup(optarg);
+ break;
+ case 'P':
+ if(opt->namefmt_replace && opt->namefmt_replace !=
+ DEFAULT_NAMEFMT_REPLACE)
+ {
+ fprintf(stderr, "WARNING: Multiple name format filter replacements specified, using final\n");
+ free(opt->namefmt_replace);
+ }
+ opt->namefmt_replace = strdup(optarg);
+ break;
case 'o':
if(opt->outfile)
{
@@ -562,7 +626,7 @@
}
}
-void add_tag(vorbis_comment *vc, oe_options *opt, char *name, char *value)
+static void add_tag(vorbis_comment *vc, oe_options *opt,char *name, char *value)
{
char *utf8;
if(utf8_encode(value, &utf8, opt->encoding) == 0)
@@ -577,7 +641,7 @@
fprintf(stderr, "Couldn't convert comment to UTF8, cannot add\n");
}
-void build_comments(vorbis_comment *vc, oe_options *opt, int filenum,
+static void build_comments(vorbis_comment *vc, oe_options *opt, int filenum,
char **artist, char **album, char **title, char **tracknum, char **date)
{
int i;
1.6 +5 -0 vorbis-tools/oggenc/platform.h
Index: platform.h
===================================================================
RCS file: /usr/local/cvsroot/vorbis-tools/oggenc/platform.h,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- platform.h 2001/02/20 08:12:50 1.5
+++ platform.h 2001/09/15 15:10:34 1.6
@@ -18,9 +18,14 @@
void setbinmode(FILE *);
+#define DEFAULT_NAMEFMT_REMOVE "/\\:"
+#define DEFAULT_NAMEFMT_REPLACE NULL
+
#else /* Unix, mostly */
#define setbinmode(x) {}
+#define DEFAULT_NAMEFMT_REMOVE "/"
+#define DEFAULT_NAMEFMT_REPLACE NULL
#endif
--- >8 ----
List archives: http://www.xiph.org/archives/
Ogg project homepage: http://www.xiph.org/ogg/
To unsubscribe from this list, send a message to 'cvs-request at xiph.org'
containing only the word 'unsubscribe' in the body. No subject is needed.
Unsubscribe messages sent to the list will be ignored/filtered.
More information about the commits
mailing list