File: 02-filename-format.patch

package info (click to toggle)
asunder 3.0.1%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,264 kB
  • sloc: ansic: 8,493; makefile: 36
file content (137 lines) | stat: -rw-r--r-- 4,186 bytes parent folder | download | duplicates (2)
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
Author: Jens Peter Secher <jps@debian.org>
Last-Update: 2010-01-10
Description: Filename simplification options.
 Adds filename simplification options %a, %t, and %l (cf. %A, %t, %L)
 which make all letters lowercase and replace anything but letters,
 digits, and dashes with underscores.
Bug: https://littlesvr.ca/bugs/show_bug.cgi?id=58

Index: b/src/util.c
===================================================================
--- a/src/util.c
+++ b/src/util.c
@@ -450,6 +450,7 @@
 {
     unsigned i = 0;
     int len = 0;
+    int tmplen = 0;
     char * ret = NULL;
     int pos = 0;
     int tnsize = 0;
@@ -460,9 +461,11 @@
         {
             switch (format[i+1])
             {
+                case 'a':
                 case 'A':
                     if (artist) len += strlen(artist);
                     break;
+                case 'l':
                 case 'L':
                     if (album) len += strlen(album);
                     break;
@@ -476,6 +479,7 @@
                 case 'Y':
                     if (year) len += strlen(year);
                     break;
+                case 't':
                 case 'T':
                     if (title) len += strlen(title);
                     break;
@@ -503,6 +507,15 @@
         {
             switch (format[i+1])
             {
+                case 'a':
+                    if (artist)
+                    {
+                        tmplen = strlen(artist);
+                        strncpy(&ret[pos], artist, tmplen);
+                        simplify_chars(ret, pos, pos+tmplen);
+                        pos += tmplen;
+                    }
+                    break;
                 case 'A':
                     if (artist)
                     {
@@ -510,6 +523,15 @@
                         pos += strlen(artist);
                     }
                     break;
+                case 'l':
+                    if (album)
+                    {
+                        tmplen = strlen(album);
+                        strncpy(&ret[pos], album, tmplen);
+                        simplify_chars(ret, pos, pos+tmplen);
+                        pos += tmplen;
+                    }
+                    break;
                 case 'L':
                     if (album)
                     {
@@ -530,6 +552,15 @@
                         pos += strlen(year);
                     }
                     break;
+                case 't':
+                    if (title)
+                    {
+                        tmplen = strlen(title);
+                        strncpy(&ret[pos], title, tmplen);
+                        simplify_chars(ret, pos, pos+tmplen);
+                        pos += tmplen;
+                    }
+                    break;
                 case 'T':
                     if (title)
                     {
@@ -758,6 +789,32 @@
     }
 }
 
+// makes the specified range within the string lowercase and replaces
+// all problematic characters with underscores
+//
+// str - the string to simplify
+// begin - start index (included)
+// end - end index (not included)
+void simplify_chars(char * str, int begin, int end)
+{
+    int i;
+    int c;
+
+    // sanitize range
+    if (0 > begin)
+        begin = 0;
+    if (strlen(str) < end)
+        end = strlen(str);
+
+    for (i=begin; i<end; ++i)
+    {
+        c = tolower(str[i]);
+        if (!(c >= 'a' && c <= 'z' || c >= '0' && c <= '9' || c == '-'))
+            c = '_';
+        str[i] = c;
+    }
+}
+
 // removes leading and trailing whitespace as defined by isspace()
 //
 // str - the string to trim
Index: b/src/util.h
===================================================================
--- a/src/util.h
+++ b/src/util.h
@@ -76,3 +76,11 @@
 // period (dot) because it screws up my file name database software. YMMV
 // 13may2013: removed '.' from the list, it's a valid character.
 #define	BADCHARS	"/?*|><:\"\\"
+
+// makes the specified range within the string lowercase and replaces
+// all problematic characters with underscores
+//
+// str - the string to simplify
+// begin - start index (included)
+// end - end index (not included)
+void simplify_chars(char * str, int begin, int end);