summaryrefslogtreecommitdiff
path: root/libs/mediaScan/src/MediainfoReader.cc
blob: cd1f3f6331ccc8f7fe9c45056d208340498b39e3 (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
/*
 * File:   MediainfoReader.cc
 * Author: django
 *
 * Created on 30. Juli 2012, 15:03
 */
#include <MediainfoReader.h>
#include <LineReader.h>
#include <stddef.h>
#include <pcrecpp.h>
#include <vector>

cMediainfoReader::cMediainfoReader(cLineReader *LineReader)
 : reader(LineReader)
{
}

cMediainfoReader::~cMediainfoReader()
{
  Close();
}

void cMediainfoReader::AddValuableKey(const char* Key)
{
  valuableKeys.push_back(Key);
}

void cMediainfoReader::Close(void)
{
  if (reader) {
     delete reader;
     reader = NULL;
     }
}

bool cMediainfoReader::IsValuable(std::string &key)
{
  for (size_t i=0; i < valuableKeys.size(); ++i) {
      if (!strcmp(key.c_str(), valuableKeys[i].c_str()))
         return true;
      }
  return false;
}

cMediainfoReader::InfoEntry *cMediainfoReader::ReadEntry()
{
  if (!reader) return NULL;
  const char *line = reader->ReadLine();
  static pcrecpp::RE comment("^\\s*#.*$");
  static pcrecpp::RE emptyLine("^\\s*$");
  static pcrecpp::RE entry("^\\s*(.+?)\\s*:\\s*(.+?)\\s*$");
  std::string name, value;
  InfoEntry *rv = NULL;

  while (line && (comment.FullMatch(line) || emptyLine.FullMatch(line)))
        line = reader->ReadLine();
  while (line && !entry.FullMatch(line, &name, &value))
        line = reader->ReadLine();
  while (line && entry.FullMatch(line, &name, &value)) {
        if (!IsValuable(name)) {
           line = reader->ReadLine();
           continue;
           }
        rv = new InfoEntry(name, value);
        break;
        }
  return rv;
}