New (incomplete) code created towards closing #1075 and #1076.
[ahxm.git] / main.c
bloba781a56f720bb2c5772cab88c456ffa5683c0195
1 /*
3 Ann Hell Ex Machina - Music Software
4 Copyright (C) 2003/2005 Angel Ortega <angel@triptico.com>
6 main.c - Miscellaneous functions and startup
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 /*******************
42 Code
43 ********************/
45 /**
46 * add_to_library_path - Adds a directory path to the search path
47 * @path: the directory path
49 * Adds @path to the list of searchable paths for path_fopen().
50 * The last part of the path is stripped before being stored,
51 * and duplication is avoided.
53 void add_to_library_path(char * path)
55 int n;
56 char * ptr;
58 /* duplicate */
59 path=strdup(path);
61 /* if no directory path remains, abort */
62 if((ptr=strrchr(path, '/')) == NULL)
64 free(path);
65 return;
68 /* strip the filename part */
69 *ptr='\0';
71 /* now try to find if that path is already stored */
72 for(n=0;n < n_library_paths;n++)
74 if(strcmp(path, library_path[n]) == 0)
76 /* found; free and return */
77 free(path);
78 return;
82 /* add room for the new path */
83 n_library_paths++;
84 library_path=(char **)realloc(library_path,
85 n_library_paths * sizeof(char *));
87 /* store */
88 library_path[n_library_paths - 1]=path;
92 /**
93 * path_fopen - Opens a file, optionally searching in a path list
94 * @filename: the file name
95 * @mode: the file mode
97 * Opens a file. If the file is found as is, it's opened;
98 * otherwise, the full list of directory paths maintained by
99 * add_to_library_path() is searched until it's found or the
100 * end of the list is reached. Whenever a file
101 * is successfully opened, its path is also stored.
103 FILE * path_fopen(char * filename, char * mode)
105 int n;
106 FILE * f=NULL;
108 /* try first here */
109 if((f=fopen(filename, mode)) != NULL)
111 add_to_library_path(filename);
112 return(f);
115 /* couldn't open; try concatenating all stored paths */
116 for(n=n_library_paths - 1;n >= 0;n--)
118 char tmp[4096];
120 snprintf(tmp, sizeof(tmp) - 1, "%s/%s",
121 library_path[n], filename);
122 tmp[sizeof(tmp) - 1]='\0';
124 if((f=fopen(tmp, mode)) != NULL)
126 add_to_library_path(tmp);
127 break;
131 return(f);
135 static void read_config(void)
140 static void main_usage(void)
142 printf("Options:\n\n");
143 printf("-o {output file or device} Set output file [default: driver dependent].\n");
144 printf("-d {driver} Output driver [default: 'default'].\n");
145 printf("-L {path} Add path to script / data library.\n");
146 printf("-r {sampling rate} Set sampling rate [default: 44100]\n");
147 printf("-c {# of channels} Set number of channels [default: 2]\n");
148 printf("-v {master volume} Set master volume [default: 0.5]\n");
149 printf("-i {interpolation} Set interpolation algorithm\n");
150 printf(" (0, none; 1, linear; 2, cubic spline,\n");
151 printf(" 3, Lagrange) [default: 3]\n");
152 printf("-M Output song to MIDI device instead of softsynth.\n");
153 printf("-I Interactive; read from MIDI device and\n");
154 printf(" use {script} as instrument.\n");
158 int annhell_main(int argc, char * argv[])
160 int n=1;
161 char * opt=NULL;
162 char * devfile=NULL;
163 char * driver=NULL;
164 char * script=NULL;
165 int midi=0;
166 int interactive=0;
168 while(n < argc - 1)
170 opt=argv[n++];
172 if(strcmp(opt, "-o") == 0)
174 /* set output device of file */
175 devfile=argv[n++];
177 else
178 if(strcmp(opt, "-d") == 0)
180 /* set driver */
181 driver=argv[n++];
183 else
184 if(strcmp(opt, "-L") == 0)
186 /* add path to library */
187 n++;
189 else
190 if(strcmp(opt, "-r") == 0)
192 /* set sampling rate */
193 ss_frequency=atoi(argv[n++]);
195 else
196 if(strcmp(opt, "-c") == 0)
198 /* set number of channels */
199 ss_nchannels=atoi(argv[n++]);
201 else
202 if(strcmp(opt, "-v") == 0)
204 /* set master volume */
205 ss_master_volume=(double) atof(argv[n++]);
207 else
208 if(strcmp(opt, "-i") == 0)
210 /* set interpolation type */
211 ss_interpolation=atoi(argv[n++]);
213 else
214 if(strcmp(opt, "-I") == 0)
216 /* activate interactive mode */
217 interactive=1;
219 else
220 if(strcmp(opt, "-M") == 0)
222 /* activate MIDI mode */
223 midi=1;
225 else
227 main_usage();
228 return(1);
232 return(0);
236 int main(int argc, char * argv[])
238 return(annhell_main(argc, argv));