Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CueSheet.cpp « DSUtilLite « common - github.com/mpc-hc/LAVFilters.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: caf8e703eefa3c4cf91cb8c220c08f896e23b95c (plain)
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
/*
 *      Copyright (C) 2010-2015 Hendrik Leppkes
 *      http://www.1f0.de
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "stdafx.h"
#include "CueSheet.h"

#include <algorithm>
#include <sstream>
#include <regex>

using namespace std;

enum class ParserState {
  GLOBAL, FILE, TRACK
};

typedef string::value_type char_t;

static char_t up_char(char_t ch)
{
  return use_facet< ctype< char_t > >(locale()).toupper(ch);
}

static string toupper(const string &src)
{
  string result;
  transform(src.begin(), src.end(), back_inserter(result), up_char);
  return result;
}

static void str_replace(string &s, const string &search, const string &replace)
{
  for (string::size_type pos = 0; ; pos += replace.length())
  {
    pos = s.find(search, pos);
    if (pos == string::npos) break;

    s.erase(pos, search.length());
    s.insert(pos, replace);
  }
}

static string GetCueParam(string line, bool firstWord = false)
{
  const string delims(" \t\n\r\"'");
  string::size_type idx;
  // Find beginning of the command word 
  idx = line.find_first_not_of(delims);
  // Find end of the command word
  idx = line.find_first_of(delims, idx);
  // Find beginning of param
  idx = line.find_first_not_of(delims, idx);
  if (idx == string::npos)
    return string();
  string param = line.substr(idx);
  // trim spaces off the end
  param = param.substr(0, param.find_last_not_of(delims) + 1);
  // replace escaped quotes
  str_replace(param, "\\\"", "\"");

  if (firstWord) {
    idx = param.find_first_of(delims);
    if (idx != string::npos)
      param = param.substr(0, idx);
  }
  return param;
}

static REFERENCE_TIME ParseCueIndex(string line)
{
  int index, m, s, f, ret;
  ret = sscanf_s(line.c_str(), " INDEX %d %d:%d:%d", &index, &m, &s, &f);
  if (ret == 4)
    return (m * 60i64 + s) * 10000000i64 + (f * 10000000i64 / 75);
  else
    return 0;
}

CCueSheet::CCueSheet()
{
}


CCueSheet::~CCueSheet()
{
}

HRESULT CCueSheet::Parse(string cueSheet)
{
  DbgLog((LOG_TRACE, 10, L"CCueSheet::Parse(): Parsing Cue Sheet"));
  int trackCount = 0;
  ParserState state(ParserState::GLOBAL);
  stringstream cueSheetStream(cueSheet);
  string line;
  while (getline(cueSheetStream, line)) {
    string word;
    (stringstream(line)) >> word;
    word = toupper(word);
    switch (state) {
    case ParserState::GLOBAL:
      if (word == "PERFORMER") {
        m_Performer = GetCueParam(line);
      } else if (word == "TITLE") {
        m_Title = GetCueParam(line);
      } else if (word == "FILE") {
        state = ParserState::FILE;
      }
      break;
    case ParserState::FILE:
    case ParserState::TRACK:
      if (word == "FILE") {
        DbgLog((LOG_TRACE, 10, L"CCueSheet::Parse(): Multiple FILE segments not supported."));
        return E_FAIL;
      }
      if (word == "TRACK") {
        state = ParserState::TRACK;
        trackCount++;

        string id = GetCueParam(line, true);
        Track track{trackCount-1, id, "Title " + id, 0, ""};
        m_Tracks.push_back(track);
      } else if (state == ParserState::TRACK) {
        if (word == "TITLE") {
          m_Tracks.back().Title = GetCueParam(line);
        } else if (word == "INDEX") {
          m_Tracks.back().Time = ParseCueIndex(line);
        } else if (word == "PERFORMER") {
          m_Tracks.back().Performer = GetCueParam(line);
        }
      }
      break;
    }
  }

  return S_OK;
}

std::string CCueSheet::FormatTrack(Track & track)
{
  string trackFormat = track.Id + ". ";
  if (!track.Performer.empty())
    trackFormat += track.Performer + " - ";
  trackFormat += track.Title;
  return trackFormat;
}