1 /*-------------- Telecommunications & Signal Processing Lab ---------------
2                              McGill University
3 
4 Routine:
5   int AOsetFtype (const struct AO_FOpar *FO)
6 
7 Purpose:
8   Determine an output file type
9 
10 Description:
11   This routine determines an output file type from the output file parameter
12   structure. If the file type is set in the structure, that value is used.
13   Otherwise the file type is determined by the output file name extension.
14   If the file name extension is non-standard, a default file type is used.
15 
16   The mapping from file name extensions to file types is as follows.  The
17   asterisk indicates that a shortened form can be used.
18     .wav*e        - FTW_WAVE, Wave file
19     .au           - FTW_AU, AU audio file
20     .aif*f        - FTW_AIFF, AIFF sound file
21     .afc or .aifc - FTW_AIFF_C, AIFF-C sound file
22     .raw, .txt, .dat, or .nh - FTW_NH_NATIVE, headerless file
23   A special case occurs if the file type is not resolved via the file name
24   extension and the data format is text. In that case, the file type is set
25   to headerless.
26 
27   For use in error messages, the program name should be set using the routine
28   UTsetProg.
29 
30 Parameters:
31   <-  int AOsetFormat
32       Output code for the file type
33    -> const struct AO_FOpar *FO
34       Output file parameters
35 
36 Author / revision:
37   P. Kabal  Copyright (C) 2003
38   $Revision: 1.1 $  $Date: 2003/05/12 23:54:38 $
39 
40 -------------------------------------------------------------------------*/
41 
42 #include <stdio.h>	/* FILENAME_MAX */
43 
44 #include <libtsp.h>		/* defines AFILE, used by AFpar.h */
45 #include <libtsp/AFpar.h>
46 #include <AO.h>
47 
48 #define ROUTINE		"AOsetFtype"
49 #define PGM		((UTgetProg ())[0] == '\0' ? ROUTINE : UTgetProg ())
50 
51 static const char *Ext_keys[] = {
52   ".wav*e",
53   ".au",
54   ".aif*f",
55   ".afc", ".aifc",
56   ".raw", ".nh", ".dat", ".txt",
57   NULL
58 };
59 
60 /* Values corresponding to extensions */
61 static const int Ftype_values[] = {
62   FTW_WAVE,
63   FTW_AU,
64   FTW_AIFF,
65   FTW_AIFF_C, FTW_AIFF_C,
66   FTW_NH_NATIVE, FTW_NH_NATIVE, FTW_NH_NATIVE, FTW_NH_NATIVE,
67 };
68 
69 
70 int
AOsetFtype(const struct AO_FOpar * FO)71 AOsetFtype (const struct AO_FOpar *FO)
72 
73 {
74   int Ftype, n;
75 
76   char Ename[FILENAME_MAX];
77 
78   Ftype = FO->Ftype;
79   if (Ftype != FTW_UNDEF)
80     return Ftype;
81 
82   /* Get the file name extension */
83   FLextName (FO->Fname, Ename);
84   STstrLC (Ename, Ename);
85 
86   /* Check for a match to the extension */
87   n = STkeyMatch (Ename, Ext_keys);
88   if (n >= 0)
89     Ftype = Ftype_values[n];
90   else if (FO->DFormat.Format == FD_TEXT)
91     Ftype = FTW_NH_NATIVE;
92   else
93     Ftype = AO_FTYPEO_DEFAULT;
94 
95   return Ftype;
96 }
97