/*
 * functions.cpp
 *
 *  Created on: Oct 6, 2011
 *      Author: Gapchich Vladislav
 */

#include "functions.h"

#include <QString>
#include <QChar>
#include <QDomDocument>
#include <QDomNode>
#include <QDomText>
#include <QDebug>

int
getNumFromString(
	QString *aString,
	const QString &aFirstStr,
	const QString &aSecondStr,
	bool *anOkFlag
)
{
	int numPos = aString->indexOf(aFirstStr) + aFirstStr.size();

	if (numPos < 0) {
		*anOkFlag = 0;
		return -1;
		/* NOTREACHED */
	}

	int numLength = -1;
	for (int i = numPos; i < aString->size(); i++) {
		if (aSecondStr == aString->at(i)) {
			numLength = i - numPos;
			break;
		}
	}

	if (numLength <= 0) {
		*anOkFlag = 0;
		return -1;
		/* NOTREACHED */
	}

	QString numString = aString->mid(numPos, numLength);
	if (numString.isEmpty()) {
		*anOkFlag = 0;
		return -1;
		/* NOTREACHED */
	}

	bool ok = 0;
	int num = numString.toInt(&ok, 10);

	if (!ok) {
		*anOkFlag = 0;
		return -1;
		/* NOTREACHED */
	}

	*anOkFlag = 1;
	return num;
}

/*
 * adds given suffix to the file name and removes path from it
 */
QString alterFileName(const QString &aFilename, const QString &aSuffix)
{
	/* altering the name of a new file */
	QString newFileName = aFilename;
	int dotPos = newFileName.lastIndexOf('.');

	if (-1 == dotPos)
		dotPos = newFileName.size();
	else
		newFileName.remove(dotPos, newFileName.size() - dotPos);

	newFileName.insert(dotPos, aSuffix);
	qDebug() << newFileName;

	int slashPos = newFileName.lastIndexOf('/');
	newFileName.remove(0, slashPos);

	return newFileName;
}

/*
 *
 */