Use const in ss_output.c where possible.
[ahxm.git] / support.c
blob16eb8a2cb2ef6f56290b251c8c5a78037db09db7
1 /*
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
26 #include "config.h"
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
35 #include "ahxm.h"
36 #include "sha1.h"
38 /*******************
39 Data
40 ********************/
42 static char **library_path = NULL;
43 static int n_library_paths = 0;
45 static char located_file[2048];
47 /* global verbose flag */
48 int verbose = 1;
50 /* global tracing flag */
51 int trace = 0;
53 /* transparent converters */
54 struct transconv {
55 const char *from; /* extension from */
56 const char *to; /* extension to */
57 const char *convcmd; /* sprintf() format for converting command */
60 static struct transconv *transconvs = NULL;
61 static int n_transconvs = 0;
63 /*******************
64 Code
65 ********************/
67 /**
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(const char *path, int strip)
79 int n;
80 char *ptr;
81 char *p;
83 /* if path starts with ~, set it to $HOME */
84 if (*path == '~') {
85 char *new;
86 char *home;
88 if ((home = getenv("HOME")) == NULL)
89 return;
91 if ((new = malloc(strlen(home) + strlen(path) + 2)) == NULL)
92 return;
94 strcpy(new, home);
95 strcat(new, "/");
96 strcat(new, path + 1);
98 p = new;
100 else {
101 /* just duplicate */
102 p = strdup(path);
105 /* if no directory path remains, abort */
106 if ((ptr = strrchr(p, '/')) == NULL) {
107 free(p);
108 return;
111 /* strip the filename part */
112 if (strip)
113 *ptr = '\0';
115 /* now try to find if that path is already stored */
116 for (n = 0; n < n_library_paths; n++) {
117 if (strcmp(p, library_path[n]) == 0) {
118 /* found; free and return */
119 free(p);
120 return;
124 /* add room for the new path */
125 n_library_paths++;
126 library_path = (char **) realloc(library_path, n_library_paths * sizeof(char *));
128 /* store */
129 library_path[n_library_paths - 1] = p;
134 * libpath_fopen - Opens a file, optionally searching in a path list
135 * @filename: the file name
136 * @mode: the file mode
138 * Opens a file. If the file is found as is, it's opened;
139 * otherwise, the full list of directory paths maintained by
140 * libpath_add() is searched until it's found or the
141 * end of the list is reached. Whenever a file
142 * is successfully opened, its path is also stored.
144 FILE *libpath_fopen(const char *filename, const char *mode)
146 int n;
147 FILE *f = NULL;
149 /* try first here */
150 if ((f = fopen(filename, mode)) != NULL) {
151 strncpy(located_file, filename, sizeof(located_file));
152 located_file[sizeof(located_file) - 1] = '\0';
154 libpath_add(filename, 1);
155 return f;
158 /* couldn't open; try concatenating all stored paths */
159 for (n = n_library_paths - 1; n >= 0; n--) {
160 snprintf(located_file, sizeof(located_file), "%s/%s",
161 library_path[n], filename);
162 located_file[sizeof(located_file) - 1] = '\0';
164 if ((f = fopen(located_file, mode)) != NULL) {
165 libpath_add(located_file, 1);
166 break;
170 return f;
175 * libpath_locate - Locates a file inside the path
176 * @filename: the file to be located
178 * Locates a file inside the library path maintained by libpath_fopen()
179 * and add_library_path(). If the file is found, a pointer to a static
180 * buffer containing the real path of the file is returned, or
181 * NULL otherwise.
183 char *libpath_locate(const char *filename)
185 FILE *f;
187 if ((f = libpath_fopen(filename, "r")) == NULL)
188 return NULL;
190 fclose(f);
191 return located_file;
195 /** transparent conversions **/
197 void transconv_add(const char *from, const char *to, const char *convcmd)
198 /* adds a converter */
200 struct transconv *t;
202 GROW(transconvs, n_transconvs, struct transconv);
204 t = &transconvs[n_transconvs++];
206 t->from = strdup(from);
207 t->to = strdup(to);
208 t->convcmd = strdup(convcmd);
212 static char *transconv_sha_file(const char *file, const char *ext)
213 /* builds a unique cache file basename using a SHA1 hash */
215 static char c_file[64];
216 unsigned char sha1[20];
217 SHA_CTX c;
218 int n;
220 SHA1_Init(&c);
221 SHA1_Update(&c, (char *)file, strlen(file));
222 SHA1_Update(&c, (char *)ext, strlen(ext));
223 SHA1_Final(sha1, &c);
225 for (n = 0; n < sizeof(sha1); n++) {
226 char tmp[3];
228 snprintf(tmp, sizeof(tmp), "%02x", sha1[n]);
229 c_file[n * 2] = tmp[0];
230 c_file[(n * 2) + 1] = tmp[1];
233 c_file[n * 2] = '\0';
234 return c_file;
238 static char *transconv_unique_file(const char *file, const char *ext, const char *dir)
239 /* builds a unique cache file name with complete path */
241 static char tmp[2048];
242 char *c_path = NULL;
244 if ((c_path = getenv("TEMP")) == NULL)
245 if ((c_path = getenv("TMP")) == NULL)
246 c_path = "/tmp";
248 /* build the directory cache name */
249 snprintf(tmp, sizeof(tmp), "%s/%s-%d", c_path, dir, getuid());
250 tmp[sizeof(tmp) - 1] = '\0';
252 /* create the cache directory */
253 mkdir(tmp, 0755);
255 strcat(tmp, "/");
256 strcat(tmp, transconv_sha_file(file, ext));
257 strcat(tmp, ext);
259 return tmp;
263 char *transconv_pipe(const char *cmd, const char *ext, const char *dir)
264 /* executes cmd as a pipe */
266 char *c_file = transconv_unique_file(cmd, ext, dir);
268 /* does the file already exist? */
269 if (access(c_file, R_OK)) {
270 char tmp[2048];
272 snprintf(tmp, sizeof(tmp), cmd, c_file);
273 tmp[sizeof(tmp) - 1] = '\0';
275 if (verbose >= 2)
276 printf("Converting: %s\n", tmp);
278 system(tmp);
281 return c_file;
285 char *transconv(const char *file, const char *ext, const char *dir)
286 /* converts using the transparent converters and the cache, if needed */
288 char *this_ext;
289 char *c_file;
290 int n;
291 struct transconv *t;
293 /* gets this file extension */
294 if (file == NULL || (this_ext = strrchr(file, '.')) == NULL)
295 return NULL;
297 /* if it's already the desired type, do nothing */
298 if (strcmp(ext, this_ext) == 0)
299 return (char *)file;
301 /* get a unique name */
302 c_file = transconv_unique_file(file, ext, dir);
304 /* does the file already exist? */
305 if (access(c_file, R_OK) == 0)
306 return c_file;
308 /* no; look for a suitable converter */
309 for (n = 0, t = transconvs; n < n_transconvs; n++, t++) {
310 if (strcmp(t->from, ".*") == 0 ||
311 (strcmp(ext, t->to) == 0 && strcmp(this_ext, t->from) == 0)) {
312 char tmp[2048];
314 /* found a converter! just do it */
315 snprintf(tmp, sizeof(tmp), t->convcmd, c_file, file);
316 tmp[sizeof(tmp) - 1] = '\0';
318 if (verbose >= 2)
319 printf("Executing: %s\n", tmp);
321 if (system(tmp) == 0)
322 break;
323 else
324 unlink(c_file);
328 return c_file;