ctkUtils.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. All rights reserved.
  5. Distributed under a BSD License. See LICENSE.txt file.
  6. This software is distributed "AS IS" WITHOUT ANY WARRANTY; without even
  7. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the above copyright notice for more information.
  9. =========================================================================*/
  10. #include "ctkUtils.h"
  11. // STD includes
  12. #include <algorithm>
  13. //------------------------------------------------------------------------------
  14. void ctkUtils::qListToSTLVector(const QStringList& list,
  15. std::vector<char*>& vector)
  16. {
  17. // Resize if required
  18. if (list.count() != static_cast<int>(vector.size()))
  19. {
  20. vector.resize(list.count());
  21. }
  22. for (int i = 0; i < list.count(); ++i)
  23. {
  24. // Allocate memory
  25. char* str = new char[list[i].size()+1];
  26. strcpy(str, list[i].toLatin1());
  27. vector[i] = str;
  28. }
  29. }
  30. //------------------------------------------------------------------------------
  31. namespace
  32. {
  33. /// Convert QString to std::string
  34. static std::string qStringToSTLString(const QString& qstring)
  35. {
  36. return qstring.toStdString();
  37. }
  38. }
  39. //------------------------------------------------------------------------------
  40. void ctkUtils::qListToSTLVector(const QStringList& list,
  41. std::vector<std::string>& vector)
  42. {
  43. // To avoid unnessesary relocations, let's reserve the required amount of space
  44. vector.reserve(list.size());
  45. std::transform(list.begin(),list.end(),std::back_inserter(vector),&qStringToSTLString);
  46. }
  47. //------------------------------------------------------------------------------
  48. void ctkUtils::stlVectorToQList(const std::vector<std::string>& vector,
  49. QStringList& list)
  50. {
  51. std::transform(vector.begin(),vector.end(),std::back_inserter(list),&QString::fromStdString);
  52. }