summaryrefslogtreecommitdiffstats
path: root/testing/mp3info/escape_chars.patch
blob: ec697a28630c8f10b195e186da5cbad963e7a3b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
--- orig/mp3info-0.8.5a/mp3info.c	Mon Nov  6 04:03:17 2006
+++ mp3info-0.8.5a/mp3info.c	Thu Mar 19 12:33:27 2009
@@ -32,6 +32,7 @@
 #undef __MAIN
 #include <sys/stat.h>
 
+char *escape_chars=NULL;
 char FILENAME_FORMAT_STRING[]="File: %F\n";
 char ID3_FORMAT_STRING[]="Title:   %-30t Track: %n\nArtist:  %a\nAlbum:   %-30l Year:  %y\nComment: %-30c Genre: %g [%G]\n";
 char TECH_FORMAT_STRING[]="Media Type:  MPEG %2.1v Layer %L\nAudio:       %r KB/s, %qKHz (%o)\nEmphasis:    %e\nCRC:         %E\nCopyright:   %C\nOriginal:    %O\nPadding:     %p\nLength:      %m:%02s\n";
@@ -66,7 +67,7 @@
 
 
 
-   while ((c=getopt(argc,argv,"vhGidfxFt:a:l:y:c:n:g:p:r:"))!=-1) {
+   while ((c=getopt(argc,argv,"vhGidfxFmt:a:l:y:c:n:g:p:r:e:"))!=-1) {
 
 	switch(c) {
 		case 'v': /* View mode is now automatic when no changes are
@@ -159,6 +160,12 @@
 			}
 			break;
 		case 'F': quickscan=0; break;
+		case 'e':
+			escape_chars = optarg;
+			break;
+		case 'm':
+			escape_chars = "'\"\b\n\r\t\\";
+			break;
 	}
    }
 
--- orig/mp3info-0.8.5a/mp3info.h	Fri Nov 10 00:14:52 2006
+++ mp3info-0.8.5a/mp3info.h	Thu Mar 19 10:42:07 2009
@@ -111,6 +111,7 @@
 	extern int galphagenreindex [MAXGENRE+2];
 #endif
 
+extern char *escape_chars;
 void tagedit_curs(char *filename, int filenum, int fileoutof, id3tag *tag);
 
 
--- orig/mp3info-0.8.5a/textfunc.c	Mon Nov  6 08:59:12 2006
+++ mp3info-0.8.5a/textfunc.c	Thu Mar 19 12:24:52 2009
@@ -24,6 +24,7 @@
 */
 
 #include "mp3info.h"
+#include "libgen.h"
 
 char *layer_text[] = {
 	"I", "II", "III"
@@ -70,6 +71,8 @@
 		"\t-n track\tSpecify ID3 v1.1 track number\n"\
 		"\t-t title\tSpecify ID3 track title\n"\
 		"\t-y year\t\tSpecify ID3 copyright year\n\n"\
+		"\t-m\t\tMySQL safe output\n"\
+		"\t-e\t\tCharacters to escape\n"\
 		"\t-G\t\tDisplay valid genres\n"\
 		"\t-h\t\tDisplay this help page\n"\
 		"\t-x\t\tDisplay technical attributes of the MP3 file\n"\
@@ -85,6 +88,7 @@
 		"\n\t\tConversion Specifiers\n\n"\
 		"\t\t%%f\tFilename without the path (string)\n"\
 		"\t\t%%F\tFilename with the path (string)\n"\
+		"\t\t%%d\tPath without filename (string)\n"\
 		"\t\t%%k\tFile size in KB (integer)\n"\
 		"\n\t\t%%a\tArtist (string)\n"\
 		"\t\t%%c\tComment (string)\n"\
@@ -217,7 +221,27 @@
 	}	
 }
 
+/* const char *escape_chars = "'\"\b\n\r\t\\%_"; */
+/* escape the chars so its sql safe */
+char *escape_string(char *str)
+{
+	static char buf[8192];
+	char *src = str;
+	char *dest = buf;
 
+	/* we might want a cmdline flag to enable escaping */
+	if (escape_chars == NULL || str == NULL)
+		return str;
+	
+	while (*src && dest < (buf + sizeof(buf) - 1)) {
+		if (strchr(escape_chars, *src))
+			*dest++ = '\\';
+		*dest++ = *src++;
+	}
+	*dest = '\0';
+	return buf;
+}
+
 void format_output (char *format_string,mp3info *mp3, int vbr_report) {
 
 	char genre[40]="";
@@ -242,16 +266,16 @@
 			mod[modlen]=0;
 			mod[modlen-1]='s';
 			switch (*code) {
-				case 't': printf(mod,mp3->id3.title); break;
+				case 't': printf(mod,escape_string(mp3->id3.title)); break;
 				case 'f': pos = (pos=strrchr(mp3->filename,'/')) ? 
 						pos+1 : mp3->filename;
-					  printf(mod,pos); break;
-				case 'F': printf(mod,mp3->filename); break;
-				case 'a': printf(mod,mp3->id3.artist); break;
-				case 'l': printf(mod,mp3->id3.album); break;
+					  printf(mod,escape_string(pos)); break;
+				case 'F': printf(mod,escape_string(mp3->filename)); break;
+				case 'a': printf(mod,escape_string(mp3->id3.artist)); break;
+				case 'l': printf(mod,escape_string(mp3->id3.album)); break;
 				case 'k': mod[modlen-1] = 'd'; printf(mod,mp3->datasize / 1024); break;
-				case 'y': printf(mod,mp3->id3.year); break;
-				case 'c': printf(mod,mp3->id3.comment); break;
+				case 'y': printf(mod,escape_string(mp3->id3.year)); break;
+				case 'c': printf(mod,escape_string(mp3->id3.comment)); break;
 				case 'n': if(mp3->id3_isvalid && mp3->id3.track[0]) {
 						mod[modlen-1]='d';
 					  	printf(mod, (int) mp3->id3.track[0]);
@@ -259,7 +283,7 @@
 					  break;
 				case 'g': if(mp3->id3_isvalid) {
 						text_genre(mp3->id3.genre,genre);
-					  	printf(mod,genre);
+					  	printf(mod,escape_string(genre));
 					  }
 					  break;
 				case 'G': if(mp3->id3_isvalid) {
@@ -348,6 +372,7 @@
 					  }
 					  break;
 			        case '%': printf("%%"); break;
+				case 'd': printf(mod,escape_string(dirname(mp3->filename))); break;
 				default:  printf("%%%c",*(code=percent+1)); break;
 			}
 			format=code+1;