ctkUtils.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) 2010 Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.commontk.org/LICENSE
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. #include "ctkUtils.h"
  15. // STD includes
  16. #include <algorithm>
  17. //------------------------------------------------------------------------------
  18. void ctk::qListToSTLVector(const QStringList& list,
  19. std::vector<char*>& vector)
  20. {
  21. // Resize if required
  22. if (list.count() != static_cast<int>(vector.size()))
  23. {
  24. vector.resize(list.count());
  25. }
  26. for (int i = 0; i < list.count(); ++i)
  27. {
  28. // Allocate memory
  29. char* str = new char[list[i].size()+1];
  30. strcpy(str, list[i].toLatin1());
  31. vector[i] = str;
  32. }
  33. }
  34. //------------------------------------------------------------------------------
  35. namespace
  36. {
  37. /// Convert QString to std::string
  38. static std::string qStringToSTLString(const QString& qstring)
  39. {
  40. return qstring.toStdString();
  41. }
  42. }
  43. //------------------------------------------------------------------------------
  44. void ctk::qListToSTLVector(const QStringList& list,
  45. std::vector<std::string>& vector)
  46. {
  47. // To avoid unnessesary relocations, let's reserve the required amount of space
  48. vector.reserve(list.size());
  49. std::transform(list.begin(),list.end(),std::back_inserter(vector),&qStringToSTLString);
  50. }
  51. //------------------------------------------------------------------------------
  52. void ctk::stlVectorToQList(const std::vector<std::string>& vector,
  53. QStringList& list)
  54. {
  55. std::transform(vector.begin(),vector.end(),std::back_inserter(list),&QString::fromStdString);
  56. }