3 Ann Hell Ex Machina - Music Software
4 Copyright (C) 2003/2007 Angel Ortega <angel@triptico.com>
6 support.c - Miscellaneous support functions
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 http://www.triptico.com
33 #include <sys/types.h>
42 static char **library_path
= NULL
;
43 static int n_library_paths
= 0;
45 static char located_file
[2048];
47 /* global verbose flag */
50 /* global tracing flag */
53 /* transparent converters */
55 char *from
; /* extension from */
56 char *to
; /* extension to */
57 char *convcmd
; /* sprintf() format for converting command */
60 static struct transconv
*transconvs
= NULL
;
61 static int n_transconvs
= 0;
68 * libpath_add - Adds a directory path to the search path
69 * @path: the directory path
70 * @strip: flag to strip the upper level
72 * Adds @path to the list of searchable paths for libpath_fopen(),
73 * optionally stripping the upper level if @strip is set.
74 * The last part of the path is stripped before being stored,
75 * and duplication is avoided.
77 void libpath_add(char *path
, int strip
)
82 /* if path starts with ~, set it to $HOME */
87 if ((home
= getenv("HOME")) == NULL
)
90 if ((new = malloc(strlen(home
) + strlen(path
) + 2)) == NULL
)
95 strcat(new, path
+ 1);
104 /* if no directory path remains, abort */
105 if ((ptr
= strrchr(path
, '/')) == NULL
) {
110 /* strip the filename part */
114 /* now try to find if that path is already stored */
115 for (n
= 0; n
< n_library_paths
; n
++) {
116 if (strcmp(path
, library_path
[n
]) == 0) {
117 /* found; free and return */
123 /* add room for the new path */
125 library_path
= (char **) realloc(library_path
, n_library_paths
* sizeof(char *));
128 library_path
[n_library_paths
- 1] = path
;
133 * libpath_fopen - Opens a file, optionally searching in a path list
134 * @filename: the file name
135 * @mode: the file mode
137 * Opens a file. If the file is found as is, it's opened;
138 * otherwise, the full list of directory paths maintained by
139 * libpath_add() is searched until it's found or the
140 * end of the list is reached. Whenever a file
141 * is successfully opened, its path is also stored.
143 FILE *libpath_fopen(char *filename
, char *mode
)
149 if ((f
= fopen(filename
, mode
)) != NULL
) {
150 strncpy(located_file
, filename
, sizeof(located_file
));
151 located_file
[sizeof(located_file
) - 1] = '\0';
153 libpath_add(filename
, 1);
157 /* couldn't open; try concatenating all stored paths */
158 for (n
= n_library_paths
- 1; n
>= 0; n
--) {
159 snprintf(located_file
, sizeof(located_file
), "%s/%s",
160 library_path
[n
], filename
);
161 located_file
[sizeof(located_file
) - 1] = '\0';
163 if ((f
= fopen(located_file
, mode
)) != NULL
) {
164 libpath_add(located_file
, 1);
174 * libpath_locate - Locates a file inside the path
175 * @filename: the file to be located
177 * Locates a file inside the library path maintained by libpath_fopen()
178 * and add_library_path(). If the file is found, a pointer to a static
179 * buffer containing the real path of the file is returned, or
182 char *libpath_locate(char *filename
)
186 if ((f
= libpath_fopen(filename
, "r")) == NULL
)
194 /** transparent conversions **/
196 void transconv_add(char *from
, char *to
, char *convcmd
)
197 /* adds a converter */
201 GROW(transconvs
, n_transconvs
, struct transconv
);
203 t
= &transconvs
[n_transconvs
++];
205 t
->from
= strdup(from
);
207 t
->convcmd
= strdup(convcmd
);
211 static char *transconv_sha_file(char *file
, char *ext
)
212 /* builds a unique cache file basename using a SHA1 hash */
214 static char c_file
[64];
215 unsigned char sha1
[20];
220 SHA1_Update(&c
, file
, strlen(file
));
221 SHA1_Update(&c
, ext
, strlen(ext
));
222 SHA1_Final(sha1
, &c
);
224 for (n
= 0; n
< sizeof(sha1
); n
++) {
227 snprintf(tmp
, sizeof(tmp
), "%02x", sha1
[n
]);
228 c_file
[n
* 2] = tmp
[0];
229 c_file
[(n
* 2) + 1] = tmp
[1];
232 c_file
[n
* 2] = '\0';
237 static char *transconv_unique_file(char *file
, char *ext
, char *dir
)
238 /* builds a unique cache file name with complete path */
240 static char tmp
[2048];
243 if ((c_path
= getenv("TEMP")) == NULL
)
244 if ((c_path
= getenv("TMP")) == NULL
)
247 /* build the directory cache name */
248 snprintf(tmp
, sizeof(tmp
), "%s/%s-%d", c_path
, dir
, getuid());
249 tmp
[sizeof(tmp
) - 1] = '\0';
251 /* create the cache directory */
255 strcat(tmp
, transconv_sha_file(file
, ext
));
262 char *transconv_pipe(char *cmd
, char *ext
, char *dir
)
263 /* executes cmd as a pipe */
265 char *c_file
= transconv_unique_file(cmd
, ext
, dir
);
267 /* does the file already exist? */
268 if (access(c_file
, R_OK
)) {
271 snprintf(tmp
, sizeof(tmp
), cmd
, c_file
);
272 tmp
[sizeof(tmp
) - 1] = '\0';
275 printf("Converting: %s\n", tmp
);
284 char *transconv(char *file
, char *ext
, char *dir
)
285 /* converts using the transparent converters and the cache, if needed */
292 /* gets this file extension */
293 if (file
== NULL
|| (this_ext
= strrchr(file
, '.')) == NULL
)
296 /* if it's already the desired type, do nothing */
297 if (strcmp(ext
, this_ext
) == 0)
300 /* get a unique name */
301 c_file
= transconv_unique_file(file
, ext
, dir
);
303 /* does the file already exist? */
304 if (access(c_file
, R_OK
) == 0)
307 /* no; look for a suitable converter */
308 for (n
= 0, t
= transconvs
; n
< n_transconvs
; n
++, t
++) {
309 if (strcmp(t
->from
, ".*") == 0 ||
310 (strcmp(ext
, t
->to
) == 0 && strcmp(this_ext
, t
->from
) == 0)) {
313 /* found a converter! just do it */
314 snprintf(tmp
, sizeof(tmp
), t
->convcmd
, c_file
, file
);
315 tmp
[sizeof(tmp
) - 1] = '\0';
318 printf("Executing: %s\n", tmp
);
320 if (system(tmp
) == 0)