/** * Strip invalid DOS characters from a filename. * @param filename Filename. * @param replaceChar Replacement character. * @return Filename with invalid DOS characters replaced with replaceChar. */ QString FilePrivate::StripInvalidDosChars( const QString &filename, const QChar replaceChar) { QString ret(filename); for (int i = (ret.size() - 1); i > 0; i--) { QCharRef chr = ret[i]; // Reference: http://en.wikipedia.org/wiki/8.3_filename#Directory_table switch (chr.unicode()) { case '"': case '*': case '/': case ':': case '<': case '>': case '?': case '\\': case '[': case ']': case '|': // Invalid DOS character. // (Technically, '[' and ']' are legal on Win32, // but we'll exclude them anyway.) chr = replaceChar; break; default: // Valid character. break; } } // Return the adjusted filename. return ret; }
QString OrganiseFormat::GetFilenameForSong(const Song &song) const { QString filename = ParseBlock(format_, song); if (replace_spaces_) filename.replace(QRegExp("\\s"), "_"); if (replace_non_ascii_) { QString stripped; for (int i=0 ; i<filename.length() ; ++i) { const QCharRef c = filename[i]; if (c < 128) stripped.append(c); else { const QString decomposition = c.decomposition(); if (!decomposition.isEmpty() && decomposition[0] < 128) stripped.append(decomposition[0]); else stripped.append("_"); } } filename = stripped; } return filename; }
QString capitalize(const QString& str) { QString ret(str); if(!ret.isEmpty()) { QCharRef c = ret[0]; c = c.toUpper(); } return ret; }
QString OrganiseFormat::GetFilenameForSong(const Song& song) const { QString filename = ParseBlock(format_, song); if (QFileInfo(filename).completeBaseName().isEmpty()) { // Avoid having empty filenames, or filenames with extension only: in this // case, keep the original filename. // We remove the extension from "filename" if it exists, as // song.basefilename() // also contains the extension. filename = Utilities::PathWithoutFilenameExtension(filename) + song.basefilename(); } if (replace_spaces_) filename.replace(QRegExp("\\s"), "_"); if (replace_non_ascii_) { QString stripped; for (int i = 0; i < filename.length(); ++i) { const QCharRef c = filename[i]; if (c < 128) { stripped.append(c); } else { const QString decomposition = c.decomposition(); if (!decomposition.isEmpty() && decomposition[0] < 128) stripped.append(decomposition[0]); else stripped.append("_"); } } filename = stripped; } // Fix any parts of the path that start with dots. QStringList parts = filename.split("/"); for (int i = 0; i < parts.count(); ++i) { QString* part = &parts[i]; for (int j = 0; j < kInvalidPrefixCharactersCount; ++j) { if (part->startsWith(kInvalidPrefixCharacters[j])) { part->replace(0, 1, '_'); break; } } } return parts.join("/"); }
/** * Makes sure that the string does only contain allowed characters, * avoid misuse of the command line parameters * @param in potentially unsafe string * @return string with only digits, numbers and '-', '=' and '%' */ static QString sanitized(const QString &in) { QString out = _(""); QString str = in.simplified(); for (int i = 0; i < str.length(); i++) { QCharRef c = str[i]; if ( c.isLetterOrNumber() || c.isSpace() || (c == QLatin1Char('-')) || (c == QLatin1Char('%')) || (c == QLatin1Char('=')) || (c == QLatin1Char('.')) || (c == QLatin1Char('[')) || (c == QLatin1Char(']')) || (c == QDir::separator()) ) { out += c; } } return out; }