functions.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * functions.cpp
  3. *
  4. * Created on: Oct 6, 2011
  5. * Author: Gapchich Vladislav
  6. */
  7. #include "functions.h"
  8. #include <QString>
  9. #include <QChar>
  10. #include <QDomDocument>
  11. #include <QDomNode>
  12. #include <QDomText>
  13. #include <QDebug>
  14. //! Gets number from a string which is located between aFirstStr and aSecondStr
  15. /*!
  16. * \param[in] aString pointer to the source string containing the number we need
  17. * \param[in] aFirstStr a string which is located to the left side of the number
  18. * \param[in] aSecondStr a string which is located to the right side of the number
  19. * \param[in,out] anOkFlag a pointer to the bool flag indicating the conversation errors
  20. *
  21. * example: aString contains "0: Poly #0; LabelID: 1; points:..."
  22. * aFirstStr "LabelID: "
  23. * aSecondStr ";"
  24. * function returns 1
  25. */
  26. int
  27. getNumFromString(
  28. QString *aString,
  29. const QString &aFirstStr,
  30. const QString &aSecondStr,
  31. bool *anOkFlag
  32. )
  33. {
  34. int numPos = aString->indexOf(aFirstStr) + aFirstStr.size();
  35. if (numPos < 0) {
  36. *anOkFlag = 0;
  37. return -1;
  38. /* NOTREACHED */
  39. }
  40. int numLength = -1;
  41. for (int i = numPos; i < aString->size(); i++) {
  42. if (aSecondStr == aString->at(i)) {
  43. numLength = i - numPos;
  44. break;
  45. }
  46. }
  47. if (numLength <= 0) {
  48. *anOkFlag = 0;
  49. return -1;
  50. /* NOTREACHED */
  51. }
  52. QString numString = aString->mid(numPos, numLength);
  53. if (numString.isEmpty()) {
  54. *anOkFlag = 0;
  55. return -1;
  56. /* NOTREACHED */
  57. }
  58. bool ok = 0;
  59. int num = numString.toInt(&ok, 10);
  60. if (!ok) {
  61. *anOkFlag = 0;
  62. return -1;
  63. /* NOTREACHED */
  64. }
  65. *anOkFlag = 1;
  66. return num;
  67. }
  68. //! Adds given suffix to the file name
  69. /*
  70. * example: /home/user/file.dot -> /home/user/file_altered.dot
  71. */
  72. QString
  73. alterFileName(const QString &aFilename, const QString &aSuffix)
  74. {
  75. /* altering the name of a new file */
  76. QString newFileName = aFilename;
  77. int dotPos = newFileName.lastIndexOf('.');
  78. if (-1 == dotPos)
  79. dotPos = newFileName.size();
  80. else
  81. newFileName.remove(dotPos, newFileName.size() - dotPos);
  82. newFileName.insert(dotPos, aSuffix);
  83. return newFileName;
  84. }
  85. //! Removes the path from filename
  86. /*!
  87. * example: /home/user/file -> file
  88. */
  89. QString
  90. removePath(const QString &aFilename)
  91. {
  92. QString newFileName = aFilename;
  93. int slashPos = newFileName.lastIndexOf('/');
  94. newFileName.remove(0, slashPos + 1);
  95. return newFileName;
  96. }
  97. //! Gets path from filename
  98. /*!
  99. * example: /home/user/file.dot -> /home/user
  100. */
  101. QString
  102. getPathFromFilename(const QString &aFilename)
  103. {
  104. QString path = aFilename;
  105. int slashPos = path.lastIndexOf('/');
  106. path = path.mid(0, slashPos + 1);
  107. return path;
  108. }
  109. /*
  110. *
  111. */