int main()
{
	qcin.setCodec(QTextCodec::codecForName("UTF-8"));
	qcout.setCodec(QTextCodec::codecForName("UTF-8"));

	QString curGroup;

	QRegExp groupRegExp( "^\\[(.*)\\]" );
	QRegExp keyRegExp( "^([a-zA-Z0-9:, _-]*)\\s*=\\s*(.*)\\s*" );
	QRegExp commentRegExp( "^(#.*)?$" );

	while ( !qcin.atEnd() )
	{
		QString line = qcin.readLine();

		if ( commentRegExp.exactMatch( line ) )
		{
			// We found a comment, leave unchanged
			qcout << line << endl;
		}
		else if ( groupRegExp.exactMatch( line ) )
		{
			// We found the start of a group, leave unchanged
			qcout << line << endl;

			curGroup = groupRegExp.capturedTexts()[ 1 ];
		}
		else if ( keyRegExp.exactMatch( line ) )
		{
			// We found the a key line
			parseKey( curGroup, keyRegExp.capturedTexts()[ 1 ], keyRegExp.capturedTexts()[ 2 ], line );
		}
		else
		{
			qcerr << "** Unknown input line: " << line << endl;
		}
	}

	return 0;
}
Exemple #2
0
void ContactRegistry::fromFile(QString const& filename, bool dryRun) {
	if (!QFile::exists(filename)) {
		throw IllegalArgumentException() << QString("Could not open the specified contacts file as it does not exist: %1").arg(filename).toStdString();
	}
	QFile inputFile(filename);
	if (!inputFile.open(QFile::ReadOnly | QFile::Text)) {
		throw IllegalArgumentException() << QString("Could not open the specified contacts file for reading: %1").arg(filename).toStdString();
	}

	QRegExp commentRegExp("^\\s*#.*$", Qt::CaseInsensitive, QRegExp::RegExp2);
	QRegExp identityRegExp("^\\s*([A-Z0-9]{8})\\s*:\\s*([a-fA-F0-9]{64})\\s*(?::\\s*(.*)\\s*)?$", Qt::CaseInsensitive, QRegExp::RegExp2);
	QRegExp groupRegExp("^\\s*([a-fA-F0-9]{16})\\s*:\\s*([A-Z0-9]{8})\\s*:\\s*([A-Z0-9]{8}(?:\\s*,\\s*[A-Z0-9]{8})*)\\s*:\\s*(.*)\\s*$", Qt::CaseInsensitive, QRegExp::RegExp2);

	QTextStream in(&inputFile);
	in.setCodec("UTF-8"); // change the file codec to UTF-8.
	while (!in.atEnd()) {
		QString line = in.readLine().trimmed();
		if (line.isEmpty() || commentRegExp.exactMatch(line)) {
			continue;
		} else if (identityRegExp.exactMatch(line)) {
			if (!dryRun) {
				accessMutex.lock();

				ContactId const contactId(identityRegExp.cap(1));
				IdentityContact* iContact = new IdentityContact(contactId, PublicKey::fromHexString(identityRegExp.cap(2)));
				if (!identityRegExp.cap(3).trimmed().isEmpty()) {
					// Nickname given.
					iContact->setNickname(identityRegExp.cap(3).trimmed());
				}
				connectContact(iContact);
				identityToIdentityContactHashMap.insert(contactId, iContact);

				accessMutex.unlock();
			}
		} else if (groupRegExp.exactMatch(line)) {
			if (!dryRun) {
				accessMutex.lock();

				quint64 groupIdentity = IdentityHelper::groupIdByteArrayToUint64(QByteArray::fromHex(groupRegExp.cap(1).toLatin1()));
				quint64 groupOwner = IdentityHelper::identityStringToUint64(groupRegExp.cap(2));
				GroupContact* gContact = new GroupContact(GroupId(groupOwner, groupIdentity));
				if (!groupRegExp.cap(4).trimmed().isEmpty()) {
					gContact->setGroupName(groupRegExp.cap(4).trimmed());
				}

				QStringList ids = groupRegExp.cap(3).split(',', QString::SkipEmptyParts);
				if (ids.size() == 0) {
					throw IllegalArgumentException() << QString("Invalid or ill-formated line in contacts file \"%1\".\nProblematic line: %2").arg(filename).arg(line).toStdString();
				}

				QStringList::const_iterator it = ids.constBegin();
				QStringList::const_iterator end = ids.constEnd();
				for (; it != end; ++it) {
					QString trimmedId(it->trimmed());
					ContactId const memberId(trimmedId);
					gContact->addMember(memberId);
				}

				connectContact(gContact);
				identityToGroupContactHashMap.insert(GroupId(groupOwner, groupIdentity), gContact);

				accessMutex.unlock();
			}
		} else {
			throw IllegalArgumentException() << QString("Invalid or ill-formated line in contacts file \"%1\".\nProblematic line: %2").arg(filename).arg(line).toStdString();
		}
	}

	inputFile.close();
	if (!dryRun) {
		emit identitiesChanged();
	}
}