File: TrackParser.cpp

package info (click to toggle)
psemu-drive-cdrmooby 2.8%2Bo-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 356 kB
  • ctags: 899
  • sloc: cpp: 3,286; ansic: 2,069; makefile: 46
file content (272 lines) | stat: -rw-r--r-- 7,596 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/************************************************************************

Copyright mooby 2002

CDRMooby2 TrackParser.cpp
http://mooby.psxfanatics.com

  This file is protected by the GNU GPL which should be included with
  the source code distribution.

************************************************************************/

#pragma warning(disable:4786)

#include "TrackParser.hpp"
#include "Utils.hpp"

using namespace std;


TrackParser* TrackParserFactory(const std::string& filename,
                                const FileInterface* fi)
{
      // try to open a track listing sheet
   std::string thisFile;
   if ( (thisFile = CCDParser::fileExists(filename)) != std::string())
      return new CCDParser(thisFile);
   else if ( (thisFile = CueParser::fileExists(filename)) != std::string())
      return new CueParser(thisFile);
   else
      return new NullParser(fi->getFileName());
}

   // just opens the file for parsing
TrackParser::TrackParser(const std::string& filename)
   : cuename(filename), pregapLength(CDTime(0,0,0))
{
   if (!filename.empty()) theCueSheet.open(filename.c_str());
}


   // post processing on the list
void TrackParser::postProcess(const CDTime& CDLength)
{
   vector<TrackInfo>::size_type index;

   CDTime thisCDLength(CDLength);

      // and if there's a pregap flag, add that time
   thisCDLength += pregapLength;

   // calculate the track lengths, except for the last track
   // which needs CDLength
   // The track length is the start time of the next track - the start time of this track -
   //   1 frame
   if (tiv.size() > 0)
   {
      for(index = 0; index < tiv.size() - 1; index++)
      {
         tiv[index].trackLength = tiv[index + 1].trackStart - 
            tiv[index].trackStart;
      }
         // finally, at the end of the disc, there's a 2 second gap as well...
      tiv[index].trackLength = thisCDLength - CDTime(0,2,0) - tiv[index].trackStart;
   }
   // if there is no cue sheet, just make a single track with length CDLength.
   else
   {
      tiv.insert(tiv.begin(), TrackInfo(thisCDLength - CDTime(0,2,0)));
   }

	  // set the ending time
   for(index = 0; index < tiv.size(); index++)
   {
      // there's an extra 2 seconds not accounted for somewhere....
      tiv[index].trackStart += CDTime(0,2,0);
      // and the track end is 1 frame less...
      tiv[index].trackEnd    = tiv[index].trackStart + tiv[index].trackLength - CDTime(0,0,1);
   }

   // insert the total length at tiv[0]
   TrackInfo track0;
   track0.trackEnd = tiv[tiv.size()-1].trackEnd + CDTime(0,0,1);
   track0.trackStart = track0.trackEnd;
   track0.trackLength = track0.trackEnd;
   track0.trackNumber = 0;
   tiv.insert(tiv.begin(), track0);
}

std::ostream& operator<<(std::ostream& o, const TrackParser& cp)
{
   vector<TrackInfo>::size_type index;
   for (index = 0; index < cp.tiv.size(); index++)
   {
      o << cp.tiv[index] << endl;
   }
   return o;
}


NullParser::NullParser(const std::string& filename)
      : TrackParser(filename)
{}

// parses a CUE file
void CueParser::parse() throw(Exception)
{
   if (!theCueSheet)
   {
      // if there's a file error here, then there's either a file error
      // or there's no cue sheet.  in either case, we'll ignore it.
      // a cue sheet is nice, but not necessary
      return;
   }

   bool doneReading = false;

   theCueSheet.exceptions(ios::eofbit|ios::badbit|ios::failbit);
   string thisLine;

   try
   {
      TrackInfo thisTrack;

      getline(theCueSheet, thisLine);
      doneReading = true;
			// the file name is whatever is in the " marks
      std::string::size_type firstpos = thisLine.find('"');
      std::string::size_type lastpos = thisLine.rfind('"');
      cuefilename = thisLine.substr(firstpos + 1, lastpos - firstpos - 1);

      while(theCueSheet)
      {
         getline(theCueSheet, thisLine);
         string firstWord = word(thisLine, 1);
         if (firstWord == "TRACK")
         {
            thisTrack.trackNumber = atoi(word(thisLine,2).c_str());
            doneReading = false;
         }
         else if (firstWord == "PREGAP")
         {
            pregapLength = CDTime(word(thisLine,2));
         }
         else if (firstWord == "INDEX")
         {
            // we need INDEX 01
            if (atoi(word(thisLine,2).c_str()) == 1)
            {
               thisTrack.trackStart = CDTime(word(thisLine,3));
               thisTrack.trackStart += pregapLength;
               tiv.push_back(thisTrack);
               thisTrack = TrackInfo();
               doneReading = true;
            }
         }
         else
         {
            // whatever, we'll just skip this...
         }
      }
   }
   catch(std::exception& e)
   {
      if (!doneReading)
      {
         Exception exc(string("Error reading cue sheet ") + cuename);
         exc.addText(string(e.what()));
         THROW(exc);
      }
   }   
}

std::string CueParser::fileExists(const std::string& file)
{
   {
      std::ifstream is; 
      std::string cueName = file + std::string(".cue");
      is.open(cueName.c_str());
      if (is)
      {
         return cueName;
      }
   }
   return string();
}

// parses a CCD file
void CCDParser::parse() throw(Exception)
{
   if (!theCueSheet)
   {
      // if there's a file error here, then there's either a file error
      // or there's no cue sheet.  in either case, we'll ignore it.
      // a cue sheet is nice, but not necessary
      return;
   }

   bool doneReading = false;
   theCueSheet.exceptions(ios::eofbit|ios::badbit|ios::failbit);
   string thisLine;

   try
   {
      TrackInfo thisTrack;

      doneReading = false;

			// the file name is whatever the .ccd is changed to .img
      cuefilename = cuename.substr(0,cuename.rfind('.')) + std::string(".img");

      while(theCueSheet)
      {
         getline(theCueSheet, thisLine);
         string firstWord = word(thisLine, 1);
         if (firstWord == "[TRACK")
         {
            thisTrack.trackNumber = atoi(word(thisLine,2).c_str());
         }
         else if (firstWord == "INDEX")
         {
            // we need INDEX 01
            if (atoi(word(thisLine,2).c_str()) == 1)
            {
                  // the number after the = sign is the 
                  // absolute frame
               std::string frame(thisLine.substr(thisLine.find('=')+1));
               thisTrack.trackStart = CDTime(atoi(frame.c_str()), CDTime::abFrame);
               tiv.push_back(thisTrack);
               thisTrack = TrackInfo();
               doneReading = true;
            }
         }
         else
         {
            // whatever, we'll just skip this...
         }
      }
   }
   catch(std::exception& e)
   {
      if (!doneReading)
      {
         Exception exc(string("Error reading cue sheet ") + cuename);
         exc.addText(string(e.what()));
         THROW(exc);
      }
   }   
}

std::string CCDParser::fileExists(const std::string& file)
{
   {
      std::ifstream is; 
      std::string ccdName = file + std::string(".ccd");
      is.open(ccdName.c_str());
      if (is)
      {
         return ccdName;
      }
   }
   {
      std::ifstream is; 
      std::string ccdName = file + std::string(".CCD");
      is.open(ccdName.c_str());
      if (is)
      {
         return ccdName;
      }
   }
   return std::string();
}