ctkUtils.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) 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. #ifdef _MSC_VER
  18. #pragma warning(disable: 4996)
  19. #endif
  20. //------------------------------------------------------------------------------
  21. void ctk::qListToSTLVector(const QStringList& list,
  22. std::vector<char*>& vector)
  23. {
  24. // Resize if required
  25. if (list.count() != static_cast<int>(vector.size()))
  26. {
  27. vector.resize(list.count());
  28. }
  29. for (int i = 0; i < list.count(); ++i)
  30. {
  31. // Allocate memory
  32. char* str = new char[list[i].size()+1];
  33. strcpy(str, list[i].toLatin1());
  34. vector[i] = str;
  35. }
  36. }
  37. //------------------------------------------------------------------------------
  38. namespace
  39. {
  40. /// Convert QString to std::string
  41. static std::string qStringToSTLString(const QString& qstring)
  42. {
  43. return qstring.toStdString();
  44. }
  45. }
  46. //------------------------------------------------------------------------------
  47. void ctk::qListToSTLVector(const QStringList& list,
  48. std::vector<std::string>& vector)
  49. {
  50. // To avoid unnessesary relocations, let's reserve the required amount of space
  51. vector.reserve(list.size());
  52. std::transform(list.begin(),list.end(),std::back_inserter(vector),&qStringToSTLString);
  53. }
  54. //------------------------------------------------------------------------------
  55. void ctk::stlVectorToQList(const std::vector<std::string>& vector,
  56. QStringList& list)
  57. {
  58. std::transform(vector.begin(),vector.end(),std::back_inserter(list),&QString::fromStdString);
  59. }