MCPcopy Create free account
hub / github.com/QNapi/qnapi / ppRemoveLinesContainingWords

Method ppRemoveLinesContainingWords

libqnapi/src/subtitlepostprocessor.cpp:123–176  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

121}
122
123bool SubtitlePostProcessor::ppRemoveLinesContainingWords(
124 const QString& subtitleFilePath, QStringList wordList) const {
125 if (!QFileInfo(subtitleFilePath).exists()) return false;
126
127 wordList = wordList.filter(QRegExp("^(.+)$"));
128
129 QString fromCodec = encodingUtils.detectFileEncoding(subtitleFilePath);
130
131 if (fromCodec.isEmpty()) fromCodec = ppConfig.encodingFrom();
132
133 QFile f(subtitleFilePath);
134 if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) return false;
135
136 QList<QByteArray> lines = f.readAll().split('\n');
137 QList<QByteArray> out;
138
139 foreach (QByteArray line, lines) {
140 int i;
141 while ((i = line.indexOf('\r')) >= 0) line.remove(i, 1);
142
143 QTextStream ts(line);
144 ts.setCodec(qPrintable(fromCodec));
145 QString encLine = ts.readAll();
146
147 if (encLine.isEmpty()) {
148 out << line;
149 continue;
150 }
151
152 bool found = false;
153 foreach (QString word, wordList) {
154 if (encLine.contains(word, Qt::CaseInsensitive)) {
155 found = true;
156 break;
157 }
158 }
159 if (found) continue;
160
161 out << line;
162 }
163
164 f.close();
165
166 if (!f.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate))
167 return false;
168
169 foreach (QByteArray line, out) {
170 f.write(line);
171 f.write("\n", 1);
172 }
173 f.close();
174
175 return true;
176}

Callers

nothing calls this directly

Calls 3

detectFileEncodingMethod · 0.80
encodingFromMethod · 0.80
closeMethod · 0.45

Tested by

no test coverage detected