functions.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. qDebug() << newFileName;
  70. int slashPos = newFileName.lastIndexOf('/');
  71. newFileName.remove(0, slashPos);
  72. return newFileName;
  73. }
  74. QString
  75. getDirFromPath(QString *aPath)
  76. {
  77. int lastSlash = aPath->lastIndexOf("/");
  78. int symbolsToCut = aPath->size() - (aPath->size() - lastSlash);
  79. QString dir = aPath->mid(0, symbolsToCut);
  80. return dir;
  81. }
  82. /*
  83. *
  84. */