functions.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. /*
  57. * adds given suffix to the file name and removes path from it
  58. */
  59. QString alterFileName(const QString &aFilename, const QString &aSuffix)
  60. {
  61. /* altering the name of a new file */
  62. QString newFileName = aFilename;
  63. int dotPos = newFileName.lastIndexOf('.');
  64. if (-1 == dotPos)
  65. dotPos = newFileName.size();
  66. else
  67. newFileName.remove(dotPos, newFileName.size() - dotPos);
  68. newFileName.insert(dotPos, aSuffix);
  69. int slashPos = newFileName.lastIndexOf('/');
  70. newFileName.remove(0, slashPos);
  71. return newFileName;
  72. }
  73. QString
  74. getDirFromPath(const QString *aPath)
  75. {
  76. int lastSlash = aPath->lastIndexOf("/");
  77. int symbolCount = aPath->size() - (aPath->size() - lastSlash);
  78. QString dir = aPath->mid(0, symbolCount);
  79. return dir;
  80. }
  81. QString
  82. getFilenameFromPath(const QString *aPath)
  83. {
  84. int lastSlash = aPath->lastIndexOf("/") + 1;
  85. int strLen = aPath->size() - lastSlash;
  86. QString dir = aPath->mid(lastSlash, strLen);
  87. return dir;
  88. }
  89. /*
  90. *
  91. */