Menu

[bbe056]: / util.cpp  Maximize  Restore  History

Download this file

147 lines (120 with data), 3.8 kB

  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
/*
* This file is part of Catalog Builder - A movie catalog building software.
* Copyright (C) (2009) (Siva Chandran P) <siva.chandran.p@gmail.com>
*
* 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 3 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <QFile>
#include <QStringList>
#include <QDebug>
#include "util.h"
Util *Util::m_Instance = NULL;
Util::Util()
:m_YearRegExp("\\d{4}"),
m_BracketsRegExp("(\\(.*\\))|(\\[.*\\])|(\\{.*\\})"),
m_StartTrimRegExp("^[ \\-_]*"),
m_MiddleTrimRegExp("\\.( )*\\."),
m_EndTrimRegExp("[ \\-_]*$"),
m_ImdbIdRegExp("tt\\d+")
{
QFile file("words-to-ignore.txt");
QByteArray contents;
if (file.open(QIODevice::ReadOnly | QIODevice::Text))
{
contents = file.readAll();
file.close();
}
else
{
qWarning("Can't open words-to-ignore.txt file");
return;
}
QStringList wordsToIgnore;
wordsToIgnore = QString(contents).split("\n", QString::SkipEmptyParts);
for (int j = 0; j < wordsToIgnore.size(); j++)
{
QString pattern;
pattern.append("((^|[ \\-\\._])" + wordsToIgnore[j] + "([ \\-\\._]|$))");
m_WordsRegExp.append(new QRegExp(pattern));
}
}
Util *Util::instance()
{
if (Util::m_Instance == NULL)
Util::m_Instance = new Util();
return m_Instance;
}
QString Util::stripFileName(QString fileName, int *year /*= NULL*/,
QString *imdbId /*= NULL*/)
{
int index;
// qDebug() << "Input file name: " << fileName;
// removes imdb id if exist
index = m_ImdbIdRegExp.indexIn(fileName);
if (index >= 0)
{
if (imdbId)
*imdbId = m_ImdbIdRegExp.cap(0);
fileName = fileName.replace(m_ImdbIdRegExp, " ");
}
// removes year if exist
index = m_YearRegExp.indexIn(fileName);
if (index >= 0)
{
int parsedYear = m_YearRegExp.cap(0).toInt();
if (parsedYear >= 1900)
{
if (year)
*year = parsedYear;
fileName = fileName.replace(m_YearRegExp, " ");
}
}
// qDebug() << "File name after removing year: " << fileName;
// removes anything inside brackets
index = m_BracketsRegExp.indexIn(fileName);
if (index >= 0)
fileName = fileName.replace(m_BracketsRegExp, " ");
// qDebug() << "File name after brackets year: " << fileName;
// removes all the ignored words if exist
for (int j = 0; j < m_WordsRegExp.size(); j++)
{
index = m_WordsRegExp[j]->indexIn(fileName);
if (index >= 0)
fileName = fileName.replace(*m_WordsRegExp[j], " ");
}
// qDebug() << "File name after ignored words: " << fileName;
// lets trim(remove blank spaces) the string
index = m_StartTrimRegExp.indexIn(fileName);
if (index >= 0)
fileName = fileName.remove(m_StartTrimRegExp);
index = m_MiddleTrimRegExp.indexIn(fileName);
if (index >= 0)
fileName = fileName.remove(m_MiddleTrimRegExp);
index = m_EndTrimRegExp.indexIn(fileName);
if (index >= 0)
fileName = fileName.remove(m_EndTrimRegExp);
// qDebug() << "File name after trimming: " << fileName;
// replace dot(.) or hyphen(-) with blank space( )
if (fileName.count(' ') == 0)
{
if (fileName.count('.') > 0)
fileName = fileName.replace('.', ' ');
else if (fileName.count('-') > 0)
fileName = fileName.replace('-', ' ');
fileName = fileName.trimmed();
}
return fileName;
}