Example #1
0
bool Annotation::read(const QString& p_sFileName, Annotation &p_Annotation)
{
    p_Annotation.clear();

    printf("Reading annotation...\n");
    QFile t_File(p_sFileName);
    QFileInfo fileInfo(t_File.fileName());

    p_Annotation.m_sFileName = fileInfo.fileName();
    p_Annotation.m_sFilePath = fileInfo.filePath();

    if (!t_File.open(QIODevice::ReadOnly))
    {
        printf("\tError: Couldn't open the file\n");
        return false;
    }

    QDataStream t_Stream(&t_File);
    t_Stream.setByteOrder(QDataStream::BigEndian);

    qint32 numEl;
    t_Stream >> numEl;

    p_Annotation.m_Vertices = VectorXi(numEl);
    p_Annotation.m_LabelIds = VectorXi(numEl);

    for(qint32 i = 0; i < numEl; ++i)
    {
        t_Stream >> p_Annotation.m_Vertices[i];
        t_Stream >> p_Annotation.m_LabelIds[i];
    }

    qint32 hasColortable;
    t_Stream >> hasColortable;
    if (hasColortable)
    {
        p_Annotation.m_Colortable.clear();

        //Read colortable
        qint32 numEntries;
        t_Stream >> numEntries;
        qint32 len;
        if(numEntries > 0)
        {

            printf("\tReading from Original Version\n");
            p_Annotation.m_Colortable.numEntries = numEntries;
            t_Stream >> len;
            QByteArray tmp;
            tmp.resize(len);
            t_Stream.readRawData(tmp.data(),len);
            p_Annotation.m_Colortable.orig_tab = tmp;

            for(qint32 i = 0; i < numEntries; ++i)
                p_Annotation.m_Colortable.struct_names.append("");

            p_Annotation.m_Colortable.table = MatrixXi(numEntries,5);

            for(qint32 i = 0; i < numEntries; ++i)
            {
                t_Stream >> len;
                tmp.resize(len);
                t_Stream.readRawData(tmp.data(),len);

                p_Annotation.m_Colortable.struct_names[i]= tmp;

                for(qint32 j = 0; j < 4; ++j)
                    t_Stream >> p_Annotation.m_Colortable.table(i,j);

                p_Annotation.m_Colortable.table(i,4) = p_Annotation.m_Colortable.table(i,0)
                        + p_Annotation.m_Colortable.table(i,1) * 256       //(2^8)
                        + p_Annotation.m_Colortable.table(i,2) * 65536     //(2^16)
                        + p_Annotation.m_Colortable.table(i,3) * 16777216; //(2^24);
            }
        }
        else
        {
Example #2
0
void
Decoder::decodeVariation(Consumer& consumer, util::ByteStream& data, ByteStream& text)
{
	MarkSet			marks;
	MoveInfoSet		moveInfo;
	Annotation		annotation;
	mstl::string	buf;
	Comment			comment;
	Comment			preComment;
	bool				hasNote(false);	// satisfies the compiler
	unsigned			pieceNum(0);		// satisfies the compiler
	Move				move;
	Move				lastMove;

	while (true)
	{
		Byte b = m_strm.get();

		if (__builtin_expect(b > token::Last, 1))
		{
			if (move)
			{
				if (hasNote)
				{
					consumer.putMove(move, annotation, preComment, comment, marks);
					if (!moveInfo.isEmpty())
					{
						consumer.putMoveInfo(moveInfo);
						moveInfo.clear();
					}
					marks.clear();
					annotation.clear();
					comment.clear();
					preComment.clear();
					hasNote = false;
				}
				else
				{
					consumer.putMove(move);
				}

				m_position.doMove(move, pieceNum);
			}
			else
			{
				if (lastMove)
				{
					m_position.doMove(lastMove, pieceNum);
					lastMove.clear();
				}

				if (hasNote)
				{
					consumer.putPrecedingComment(comment, annotation, marks);
					marks.clear();
					annotation.clear();
					comment.clear();
					hasNote = false;
				}
			}

			pieceNum = decodeMove(b, move);
		}
		else
		{
			switch (b)
			{
				case token::End_Marker:
					if (move)
					{
						if (hasNote)
						{
							consumer.putMove(move, annotation, preComment, comment, marks);
							if (!moveInfo.isEmpty())
								consumer.putMoveInfo(moveInfo);
						}
						else
						{
							consumer.putMove(move);
						}
					}
					else if (hasNote)
					{
						consumer.putPrecedingComment(comment, annotation, marks);
						hasNote = false;
					}
					switch (m_strm.get())
					{
						case token::Comment:
							{
								uint8_t flag = data.get();

								buf.clear();
								text.get(buf);
								comment.swap(buf, bool(flag & comm::Ante_Eng), bool(flag & comm::Ante_Oth));
								consumer.putTrailingComment(comment);
								break;
							}

						case token::End_Marker: break;
						default: return; //IO_RAISE(Game, Corrupted, "unexpected token");
					}
					return;

				case token::Start_Marker:
					if (move)
					{
						if (hasNote)
						{
							consumer.putMove(move, annotation, preComment, comment, marks);
							if (!moveInfo.isEmpty())
							{
								consumer.putMoveInfo(moveInfo);
								moveInfo.clear();
							}
							marks.clear();
							annotation.clear();
							comment.clear();
							preComment.clear();
							hasNote = false;
						}
						else
						{
							consumer.putMove(move);
						}

						lastMove = move;
						move.clear();
					}

					//M_ASSERT(!hasNote);

					m_position.push();
					m_position.board().undoMove(lastMove);
					consumer.startVariation();
					decodeVariation(consumer, data, text);
					consumer.finishVariation();
					m_position.pop();
					break;

				case token::Nag:
					{
						nag::ID nag = nag::ID(m_strm.get());

						if (nag == 0)
						{
							move.setLegalMove(false);
						}
						else
						{
							annotation.add(nag);
							hasNote = true;
						}
					}
					break;

				case token::Mark:
					if (data.peek() & 0x80)
						moveInfo.add().decode(data);
					else
						marks.add().decode(data);
					hasNote = true;
					break;

				case token::Comment:
					{
						uint8_t flag = data.get();

						if (flag & comm::Ante)
						{
							buf.clear();
							text.get(buf);
							preComment.swap(buf, bool(flag & comm::Ante_Eng), bool(flag & comm::Ante_Oth));
						}

						if (flag & comm::Post)
						{
							buf.clear();
							text.get(buf);
							comment.swap(buf, bool(flag & comm::Post_Eng), bool(flag & comm::Post_Oth));
						}

						hasNote = true;
					}
					break;
			}
		}
	}
}