openstudio::path ScriptFolderListView::iterateFileName(const openstudio::path &t_path)
{
  struct BuildFileName
  {
    static openstudio::path doit(const openstudio::path &t_root,
        const std::string &t_stem,
        const std::string &t_extension,
        int iteration)
    {
      std::stringstream numportion;
      if (iteration > 0)
      {
        numportion << "-";
        if (iteration < 10)
        {
          numportion << "0";
        } 
        numportion << iteration;
      }
      return t_root / openstudio::toPath(t_stem + numportion.str() + t_extension);
    }
  };

  std::string stem = openstudio::toString(t_path.stem());

  if (boost::regex_match(openstudio::toString(stem), boost::regex(".*-[0-9][0-9]")))
  {
    stem = stem.substr(0, stem.size() - 3);
  }

  int num = 99;
  openstudio::path p;
  openstudio::path last;
  do
  {
    last = p;
    p = BuildFileName::doit(t_path.parent_path(), openstudio::toString(stem), openstudio::toString(t_path.extension()), num);
    --num;
  } while (!boost::filesystem::exists(p) && num > -1);

  if (!boost::filesystem::exists(p))
  {
    return p;
  } else {
    return last;
  }


}
Example #2
0
  openstudio::path NormalizeURLs::getFilename(const QUrl &t_url, const openstudio::path &t_filename)
  {
    const std::map<QUrl, openstudio::path>::const_iterator itr = m_url_map.find(t_url);

    if (itr != m_url_map.end())
    {
      // we've already mapped this url, let's return the same one
      return itr->second;
    }

    const size_t existingcount = m_filenames.count(t_filename);

    if (existingcount == 0)
    {
      m_filenames.insert(t_filename);
      m_url_map[t_url] = t_filename;
      return t_filename;
    } else {
      openstudio::path newfilename 
        = toPath(t_filename.stem().string() + toPath("-" + boost::lexical_cast<std::string>(existingcount)).string() + t_filename.extension().string());

      // Make sure both the newly generated file name and the passed in file name are tracked for counting purposes
      m_filenames.insert(newfilename);
      m_filenames.insert(t_filename);

      m_url_map[t_url] = newfilename;
      return newfilename;
    }

  }