1 /*  cdrdao - write audio CD-Rs in disc-at-once mode
2  *
3  *  Copyright (C) 1998-2004  Denis Leroy <denis@poolshark.org>
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 
20 #ifndef __FORMATCONVERTER_H__
21 #define __FORMATCONVERTER_H__
22 
23 #include <string>
24 #include <map>
25 #include <list>
26 
27 #include "TrackData.h"
28 #include "Toc.h"
29 
30 // Quick abstract class declarations. Format converters should derive
31 // their own FormatSupport and FormatSupportManager.
32 
33 class FormatSupport
34 {
35  public:
~FormatSupport()36   virtual ~FormatSupport() {}
37 
38   typedef enum {
39     FS_SUCCESS,
40     FS_IN_PROGRESS,
41     FS_WRONG_FORMAT,
42     FS_INPUT_PROBLEM,
43     FS_OUTPUT_PROBLEM,
44     FS_DISK_FULL,
45     FS_DECODE_ERROR,
46     FS_OTHER_ERROR,
47   } Status;
48 
49   // Convert source file to destination WAV or RAW. This is a blocking
50   // call until conversion is finished.
51   // Return values:
52   //   0: success
53   //   1: problem with input file
54   //   2: problem with output file
55   //   3: problem with conversion
56   virtual Status convert(const char* from, const char* to) = 0;
57 
58   // Same as above, but asynchronous interface. Call start, then call
59   // continue in a busy loop until it no longer returns
60   // FS_IN_PROGRESS.
61   virtual Status convertStart(const char* from, const char* to) = 0;
62   virtual Status convertContinue() = 0;
63   virtual void   convertAbort() = 0;
64 
65   // Specify what this object converts to. Should only returns either
66   // TrackData::WAVE or TrackData::RAW
67   virtual TrackData::FileType format() = 0;
68 };
69 
70 class FormatSupportManager
71 {
72  public:
~FormatSupportManager()73   virtual ~FormatSupportManager() {}
74 
75   // Acts as virtual constructor. Returns a new converter if this
76   // converter understands the given file extension.
77   virtual FormatSupport* newConverter(const char* extension) = 0;
78 
79   // Add supported file extensions to list. Returns number added.
80   virtual int supportedExtensions(std::list<std::string>&) = 0;
81 };
82 
83 
84 // The global format conversion class. A single global instance of
85 // this class exists and manages all format conversions.
86 
87 class FormatConverter
88 {
89  public:
90   FormatConverter();
91   virtual ~FormatConverter();
92 
93   // Returns true if the converter understands this format
94   bool canConvert(const char* fn);
95 
96   // Convert file, return tempory file with WAV or RAW data (based on
97   // temp file extension).Returns NULL if conversion failed.
98   const char* convert(const char* src, FormatSupport::Status* st = NULL);
99 
100   // Convert all files contained in a given Toc object, and update the
101   // Toc accordingly. This is a big time blocking call.
102   FormatSupport::Status convert(Toc* toc);
103 
104   // Dynamic allocator.
105   FormatSupport* newConverter(const char* src);
106 
107   // Do it yourself. Returns a converter and starts it. Sets dst to
108   // the converter file name (or clears it if no converter
109   // found). Returns NULL if file can't be converted.
110   FormatSupport* newConverterStart(const char* src, std::string& dst,
111                                    FormatSupport::Status* status = NULL);
112 
113   // Add all supported extensions to string list. Returns number added.
114   int supportedExtensions(std::list<std::string>&);
115 
116  private:
117   std::list<std::string*> tempFiles_;
118   std::list<FormatSupportManager*> managers_;
119 };
120 
121 extern FormatConverter formatConverter;
122 
123 // Utility for parsing M3U files
124 
125 bool parseM3u(const char* m3ufile, std::list<std::string>& list);
126 
127 #endif
128