functions.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. int
  15. getNumFromString(
  16. QString *aString,
  17. const QString &aFirstStr,
  18. const QString &aSecondStr,
  19. bool *anOkFlag
  20. )
  21. {
  22. int numPos = aString->indexOf(aFirstStr) + aFirstStr.size();
  23. if (numPos < 0) {
  24. *anOkFlag = 0;
  25. return -1;
  26. /* NOTREACHED */
  27. }
  28. int numLength = -1;
  29. for (int i = numPos; i < aString->size(); i++) {
  30. if (aSecondStr == aString->at(i)) {
  31. numLength = i - numPos;
  32. break;
  33. }
  34. }
  35. if (numLength <= 0) {
  36. *anOkFlag = 0;
  37. return -1;
  38. /* NOTREACHED */
  39. }
  40. QString numString = aString->mid(numPos, numLength);
  41. if (numString.isEmpty()) {
  42. *anOkFlag = 0;
  43. return -1;
  44. /* NOTREACHED */
  45. }
  46. bool ok = 0;
  47. int num = numString.toInt(&ok, 10);
  48. if (!ok) {
  49. *anOkFlag = 0;
  50. return -1;
  51. /* NOTREACHED */
  52. }
  53. *anOkFlag = 1;
  54. return num;
  55. }
  56. //! Adds given suffix to the file name
  57. /*
  58. * example: /home/user/file.dot -> /home/user/file_altered.dot
  59. */
  60. QString
  61. alterFileName(const QString &aFilename, const QString &aSuffix)
  62. {
  63. /* altering the name of a new file */
  64. QString newFileName = aFilename;
  65. int dotPos = newFileName.lastIndexOf('.');
  66. if (-1 == dotPos)
  67. dotPos = newFileName.size();
  68. else
  69. newFileName.remove(dotPos, newFileName.size() - dotPos);
  70. newFileName.insert(dotPos, aSuffix);
  71. return newFileName;
  72. }
  73. //! Removes the path from filename
  74. /*!
  75. * example: /home/user/file -> file
  76. */
  77. QString
  78. removePath(const QString &aFilename)
  79. {
  80. QString newFileName = aFilename;
  81. int slashPos = newFileName.lastIndexOf('/');
  82. newFileName.remove(0, slashPos + 1);
  83. return newFileName;
  84. }
  85. //! Gets path from filename
  86. /*!
  87. * example: /home/user/file.dot -> /home/user
  88. */
  89. QString
  90. getPathFromFilename(const QString &aFilename)
  91. {
  92. QString path = aFilename;
  93. int slashPos = path.lastIndexOf('/');
  94. path = path.mid(0, slashPos + 1);
  95. return path;
  96. }
  97. /*
  98. *
  99. */