Exemple #1
0
bool StringUtils::isPossibleListItem(const wchar_t * word) {
	if (wcslen(word) == 1) {
		return true;
	}
	if (isChapterNumber(word)) {
		return true;
	}
	if (isRomanNumeral(word)) {
		return true;
	}
	return false;
}
//-----------------------------------------------------------------
QString Utils::formatString(const QString filename,
                            const Utils::FormatConfiguration &conf,
                            bool add_mp3_extension)
{
  // works for filenames and plain strings
  QString formattedName = filename;

  auto fileInfo = QFileInfo(filename);
  if(fileInfo.exists())
  {
    formattedName = fileInfo.absoluteFilePath().split('/').last();

    auto extension = formattedName.split('.').last();
    auto extension_id = QString("*.") + extension;
    bool identified = WAVE_FILE_EXTENSIONS.contains(extension_id)   ||
                      MODULE_FILE_EXTENSIONS.contains(extension_id) ||
                      MOVIE_FILE_EXTENSIONS.contains(extension_id);

    if (identified)
    {
      formattedName.remove(formattedName.lastIndexOf('.'), extension.length() + 1);
    }
  }

  if (conf.apply)
  {
    // delete specified chars
    for (int i = 0; i < conf.chars_to_delete.length(); ++i)
    {
      formattedName.remove(conf.chars_to_delete[i], Qt::CaseInsensitive);
    }

    // replace specified strings
    for (int i = 0; i < conf.chars_to_replace.size(); ++i)
    {
      auto charPair = conf.chars_to_replace[i];
      formattedName.replace(charPair.first, charPair.second, Qt::CaseInsensitive);
    }

    // remove consecutive spaces
    QStringList parts = formattedName.split(' ');
    parts.removeAll("");

    formattedName.clear();
    int index = 0;

    // adjust the number prefix and insert the default separator.
    // Format 1: 01 ...
    QRegExp re1("\\d*");
    auto re1_match = re1.exactMatch(parts[index]);

    // Format 2: 1-01 ...
    QRegExp re2("\\d-\\d*");
    auto re2_match = re2.exactMatch(parts[index]);

    // only check number format if it exists
    if (re1_match || re2_match)
    {
      QString number_string, number_disc_id;
      if(re1_match)
      {
        number_string = parts[index];
      }
      else
      {
        auto splits = parts[index].split('-');
        number_disc_id = splits.first();
        number_string = splits.last();
      }

      while (conf.number_of_digits > number_string.length())
      {
        number_string = "0" + number_string;
      }

      if (index != parts.size() - 1)
      {
        if(parts[index + 1] != QString(conf.number_and_name_separator))
        {
          number_string += QString(' ' + conf.number_and_name_separator + ' ');
        }
        else
        {
          parts[index + 1] = QString(' ' + conf.number_and_name_separator);
        }
      }

      if(!number_disc_id.isEmpty() && conf.prefix_disk_num)
      {
        number_string = number_disc_id + QString("-") + number_string;
      }
      formattedName = number_string;
      ++index;
    }

    // capitalize the first letter of every word
    if (conf.to_title_case)
    {
      int i = index;
      while (i < parts.size())
      {
        if (parts[i].isEmpty()) continue;
        bool starts_with_parenthesis = false;
        bool ends_with_parenthesis = false;
        bool starts_with_quote = false;
        bool ends_with_quote = false;
        int begin_quote_num = 0;
        int end_quote_num = 0;

        if(parts[i].startsWith('(') && parts[i].size() > 1)
        {
          starts_with_parenthesis = true;
          parts[i].remove('(');
        }

        if(parts[i].endsWith(')') && parts[i].size() > 1)
        {
          ends_with_parenthesis = true;
          parts[i].remove(')');
        }
        
        if(parts[i].startsWith('\'') && parts[i].size() > 1)
        {
          starts_with_quote = true;
          while(parts[i].at(begin_quote_num) == QChar('\'') && begin_quote_num < parts[i].size())
          {
            ++begin_quote_num;
          }
        }
        
        if(parts[i].endsWith('\'') && parts[i].size() > 1)
        {
          ends_with_quote = true;
          auto part_size = parts[i].size() - 1;
          while(parts[i].at(part_size - end_quote_num) == QChar('\'') && end_quote_num < parts[i].size())
          {
            ++end_quote_num;
          }
        }

        if(starts_with_quote || ends_with_quote)
        {
          parts[i].remove(QChar('\''));
        }

        if(!isRomanNumeral(parts[i]))
        {
          parts[i] = parts[i].toLower();
          parts[i].replace(0, 1, parts[i].at(0).toUpper());
        }

        if(starts_with_quote)
        {
          while(begin_quote_num > 0)
          {
            parts[i].insert(0, QChar('\''));
            --begin_quote_num;
          }
        }

        if(ends_with_quote)
        {
          while(end_quote_num > 0)
          {
            parts[i].append(QChar('\''));
            --end_quote_num;
          }
        }

        if (starts_with_parenthesis)
        {
          parts[i] = QString('(') + parts[i];
        }

        if (ends_with_parenthesis)
        {
          parts[i] = parts[i] + QString(')');
        }

        ++i;
      }
    }

    if(index < parts.size())
    {
      formattedName += parts[index++];
    }

    // compose the name.
    while (index < parts.size())
    {
      formattedName += ' ' + parts[index++];
    }
  }

  if(add_mp3_extension)
  {
    formattedName += ".mp3";
  }

  // remove any unwanted spaces
  formattedName = formattedName.simplified();

  // check for unwanted unicode chars
  while(formattedName.toLatin1().contains('?'))
  {
    auto index = formattedName.toLatin1().indexOf('?');

    switch(formattedName.at(index).category())
    {
      case QChar::Punctuation_InitialQuote:
      case QChar::Punctuation_FinalQuote:
        formattedName = formattedName.replace(formattedName.at(index), QString("''"));
        break;
      case QChar::Punctuation_Dash:
        formattedName = formattedName.replace(formattedName.at(index), '-');
        break;
      default:
        formattedName = formattedName.replace(formattedName.at(index), ' ');
    }
  }

  return formattedName;
}