func_fader() no longer changes the size (Closes: #1107).
[ahxm.git] / support.c
blob15818d8934db7cbf556b86f6d8918da2a7a60336
1 /*
3 Ann Hell Ex Machina - Music Software
4 Copyright (C) 2003/2005 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>
32 #include "annhell.h"
34 /*******************
35 Data
36 ********************/
38 static char ** library_path = NULL;
39 static int n_library_paths = 0;
41 static char located_file[2048];
43 /* global debugging flag */
44 int debug = 0;
46 /* global tracing flag */
47 int trace = 0;
49 /* show progress flag */
50 int show_progress = 0;
52 /*******************
53 Code
54 ********************/
56 /**
57 * add_to_library_path - Adds a directory path to the search path
58 * @path: the directory path
59 * @strip: flag to strip the upper level
61 * Adds @path to the list of searchable paths for path_fopen(),
62 * optionally stripping the upper level if @strip is set.
63 * The last part of the path is stripped before being stored,
64 * and duplication is avoided.
66 void add_to_library_path(char * path, int strip)
68 int n;
69 char * ptr;
71 /* if path starts with ~, pre-prend $HOME */
72 if(*path == '~')
74 char * new;
75 char * home;
77 if((home = getenv("HOME")) == NULL)
78 return;
80 if((new = malloc(strlen(home) + strlen(path) + 2)) == NULL)
81 return;
83 strcpy(new, home);
84 strcat(new, "/");
85 strcat(new, path + 1);
87 path = new;
89 else
91 /* just duplicate */
92 path = strdup(path);
95 /* if no directory path remains, abort */
96 if((ptr = strrchr(path, '/')) == NULL)
98 free(path);
99 return;
102 /* strip the filename part */
103 if(strip) *ptr = '\0';
105 /* now try to find if that path is already stored */
106 for(n = 0;n < n_library_paths;n++)
108 if(strcmp(path, library_path[n]) == 0)
110 /* found; free and return */
111 free(path);
112 return;
116 /* add room for the new path */
117 n_library_paths++;
118 library_path = (char **)realloc(library_path,
119 n_library_paths * sizeof(char *));
121 /* store */
122 library_path[n_library_paths - 1] = path;
127 * path_fopen - Opens a file, optionally searching in a path list
128 * @filename: the file name
129 * @mode: the file mode
131 * Opens a file. If the file is found as is, it's opened;
132 * otherwise, the full list of directory paths maintained by
133 * add_to_library_path() is searched until it's found or the
134 * end of the list is reached. Whenever a file
135 * is successfully opened, its path is also stored.
137 FILE * path_fopen(char * filename, char * mode)
139 int n;
140 FILE * f = NULL;
142 /* try first here */
143 if((f = fopen(filename, mode)) != NULL)
145 strncpy(located_file, filename, sizeof(located_file));
146 located_file[sizeof(located_file) - 1] = '\0';
148 add_to_library_path(filename, 1);
149 return(f);
152 /* couldn't open; try concatenating all stored paths */
153 for(n = n_library_paths - 1;n >= 0;n--)
155 snprintf(located_file, sizeof(located_file), "%s/%s",
156 library_path[n], filename);
157 located_file[sizeof(located_file) - 1] = '\0';
159 if((f = fopen(located_file, mode)) != NULL)
161 add_to_library_path(located_file, 1);
162 break;
166 return(f);
171 * locate_file - Locates a file inside the path
172 * @filename: the file to be located
174 * Locates a file inside the library path maintained by path_fopen()
175 * and add_library_path(). If the file is found, a pointer to a static
176 * buffer containing the real path of the file is returned, or
177 * NULL otherwise.
179 char * locate_file(char * filename)
181 FILE * f;
183 if((f = path_fopen(filename, "r")) == NULL)
184 return(NULL);
186 fclose(f);
187 return(located_file);