1 /*------------- Telecommunications & Signal Processing Lab -------------
2                            McGill University
3 
4 Routine:
5   void CPoptions (int argc, const char *argv[], int *Mode,
6                   struct CP_FIpar FI[MAXIFILE], int *Nifiles,
7 		  struct CP_FOpar *FO)
8 
9 Purpose:
10   Decode options for CopyAudio
11 
12 Description:
13   This routine decodes options for CopyAudio.
14 
15 Parameters:
16    -> int argc
17       Number of command line arguments
18    -> const char *argv[]
19       Array of pointers to argument strings
20   <-  int *Mode
21       Combine / concatenate mode
22   <-  struct CP_FIpar FI[MAXIFILES]
23       Input file parameters
24   <-  int *Nifiles
25       Number of input file names
26   <-  struct CP_FOpar *FO
27       Output file parameters
28   <-  struct CP_Chgain *Chgain
29       Channel gain matrix
30 
31 Author / revision:
32   P. Kabal  Copyright (C) 2003
33   $Revision: 1.78 $  $Date: 2003/05/13 01:33:07 $
34 
35 ----------------------------------------------------------------------*/
36 
37 #include <libtsp.h>
38 #include <AO.h>
39 #include "CopyAudio.h"
40 
41 /* Option table */
42 static const char *OptTable[] = {
43   "-c", "--com*bine",
44   "-C", "--con*catenate",
45   "-cA#", "--chanA=",
46   "-cB#", "--chanB=",
47   "-cC#", "--chanC=",
48   "-cD#", "--chanD=",
49   "-cE#", "--chanE=",
50   "-cF#", "--chanF=",
51   "-cG#", "--chanG=",
52   "-cH#", "--chanH=",
53   "-cI#", "--chanI=",
54   "-cJ#", "--chanJ=",
55   "-cK#", "--chanK=",
56   "-cL#", "--chanL=",
57   NULL
58 };
59 
60 static const char Ch[MAXNO+1] = "ABCDEFGHIJKL";
61 
62 
63 void
CPoptions(int argc,const char * argv[],int * Mode,struct CP_FIpar FI[MAXIFILE],int * Nifiles,struct CP_FOpar * FO,struct CP_Chgain * Chgain)64 CPoptions (int argc, const char *argv[], int *Mode,
65 	   struct CP_FIpar FI[MAXIFILE], int *Nifiles, struct CP_FOpar *FO,
66 	   struct CP_Chgain *Chgain)
67 
68 {
69   const char *OptArg;
70   struct CP_FIpar FIx;
71   int nF, i, n, mode, FIParSet;
72   int Chset[MAXNO];
73 
74 /* Defaults */
75   mode = M_COMB;
76 
77 /* Input file defaults */
78   FIParSet = 0;
79   FIpar_INIT (&FIx);
80 
81 /* Output file defaults */
82   FOpar_INIT (FO);
83 
84 /* Initialize the channel gain structure */
85   Chgain_INIT (Chgain);
86   VRiZero (Chset, MAXNO);
87 
88 /* Initialization */
89   UTsetProg (PROGRAM);
90   nF = 0;
91 
92 /* Decode options */
93   AOinitOpt (argc, argv);
94   while (1) {
95 
96     /* Decode input file options */
97     n = AOdecFI (&FIx);
98     if (n >= 1) {
99       FIParSet = nF;
100       continue;
101     }
102     /* Decode output file options */
103     n = AOdecFO (FO);
104     if (n >= 1)
105       continue;
106 
107     /* Decode help options */
108     n = AOdecHelp (VERSION, CPMF_Usage);
109     if (n >= 1)
110       continue;
111 
112     /* Decode program options */
113     n = AOdecOpt (OptTable, &OptArg);
114     if (n == -1)
115       break;
116 
117     switch (n) {
118     case -2:
119       UThalt (CPMF_Usage, PROGRAM);
120       break;
121     case 0:
122       /* Filename argument */
123       ++nF;
124       if (nF > MAXFILE)
125 	UThalt ("%s: %s", PROGRAM, CPM_XFName);
126       STcopyMax (OptArg, FO->Fname, FILENAME_MAX-1);
127       STcopyMax (OptArg, FIx.Fname, FILENAME_MAX-1);
128       if (nF <= MAXIFILE)
129 	FI[nF-1] = FIx;
130       break;
131     case 1:
132     case 2:
133       /* Combine mode */
134       mode = M_COMB;
135       break;
136     case 3:
137     case 4:
138       /* Concatenate mode */
139       mode = M_CONCAT;
140       break;
141     default:
142       /* Channel gain expressions */
143       i = (n - 5) / 2;
144       CPdecChan (OptArg, i, Chgain);
145       Chset[i] = 1;
146       break;
147     }
148   }
149 
150 /* Checks */
151   if (nF < 2)
152     UThalt ("%s: %s", PROGRAM, CPM_MFName);
153   for (i = 0; i < Chgain->NO; ++i) {
154     if (Chset[i] == 0)
155       UThalt ("%s: %s %c", PROGRAM, CPM_NoChanSpec, Ch[i]);
156   }
157   if (FIParSet >= nF - 1)
158     UThalt ("%s: %s", PROGRAM, CPM_LateFPar);
159 
160   /* Check for too many stdin specs */
161   AOstdin (FI, nF-1);
162 
163 /* Set return values */
164   *Mode = mode;
165   *Nifiles = nF-1;
166 
167   return;
168 }
169