| 1 | /*************************************************************************** |
| 2 | * Copyright (C) 2009 by The qGo Project * |
| 3 | * * |
| 4 | * This file is part of qGo. * |
| 5 | * * |
| 6 | * qGo is free software: you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU General Public License as published by * |
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
| 9 | * (at your option) any later version. * |
| 10 | * * |
| 11 | * This program is distributed in the hope that it will be useful, * |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | * GNU General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU General Public License * |
| 17 | * along with this program; if not, see <http://www.gnu.org/licenses/> * |
| 18 | * or write to the Free Software Foundation, Inc., * |
| 19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 20 | ***************************************************************************/ |
| 21 | |
| 22 | |
| 23 | /* |
| 24 | * sgfparser.cpp |
| 25 | * This class deals with SGF input. It puts data into a text stream (for cilpboard exchange) |
| 26 | * The parsing delivers orders solely into the game tree - namely the 'tree' class |
| 27 | */ |
| 28 | |
| 29 | |
| 30 | #include "sgfparser.h" |
| 31 | #include "../defines.h" |
| 32 | #include "move.h" |
| 33 | #include "tree.h" |
| 34 | #include "matrix.h" |
| 35 | #include "gamedata.h" |
| 36 | |
| 37 | #include <QMessageBox> |
| 38 | |
| 39 | #ifdef HAVE_CONFIG_H |
| 40 | #include <config.h> |
| 41 | #endif |
| 42 | |
| 43 | |
| 44 | #define STR_OFFSET 2000 |
| 45 | /* #define DEBUG_CODEC */ |
| 46 | |
| 47 | struct Position { int x, y; }; |
| 48 | struct MoveNum {int n; }; |
| 49 | |
| 50 | /* FIXME consider a parser for other file types, what few there are. |
| 51 | * but for instance the tygem protocols use .gibo format and that code |
| 52 | * is super ugly. We could make a better parser for it and put it in here |
| 53 | * also allowing the reading of files. This will become more useful if |
| 54 | * we add in the stuff to retrieve game records from the server. |
| 55 | * oro also has its own format I think and there's a few others. There's |
| 56 | * no reason why we shouldn't be able to handle them. |
| 57 | * IGS also has its own format as probably does cyberoro */ |
| 58 | |
| 59 | class MyString |
| 60 | { |
| 61 | public: |
| 62 | MyString() |
| 63 | { |
| 64 | } |
| 65 | |
| 66 | MyString(const QString &src) |
| 67 | { |
| 68 | Str = src; |
| 69 | strLength = src.length(); |
| 70 | } |
| 71 | |
| 72 | virtual ~MyString() |
| 73 | { |
| 74 | } |
| 75 | |
| 76 | virtual const QChar at(uint i) const { return Str.at(i); } |
| 77 | |
| 78 | virtual uint next_nonspace (uint i) const |
| 79 | { |
| 80 | // ignore lower characters, too |
| 81 | while (at(i).isSpace() || // == ' ' || at(i) == '\n' || at(i) == '\t' || |
| 82 | (at(i) >= 'a' && at(i) <= 'z')) |
| 83 | i++; |
| 84 | return i; |
| 85 | } |
| 86 | |
| 87 | virtual uint isProperty(const char *c, unsigned int index) const |
| 88 | // Check if a property identified by the C string *c begins at index. |
| 89 | // Return poition of '[' (always >0 assuming *c is not empty) if yes, zero if no. |
| 90 | { |
| 91 | if (index+strlen(c) >= strLength) |
| 92 | return 0; |
| 93 | |
| 94 | while (*c != '\0') |
| 95 | { |
| 96 | if (*(c++) != Str.at(index++)) |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | uint tmppos = next_nonspace(index); |
| 101 | if (at(tmppos) == '[') |
| 102 | return tmppos; |
| 103 | else return 0; |
| 104 | } |
| 105 | |
| 106 | virtual int find(const char *c, unsigned int index) const |
| 107 | { |
| 108 | if (index >= strLength) |
| 109 | return -1; |
| 110 | |
| 111 | // Offset. Hope that is enough. TODO Long comments check? |
| 112 | unsigned int l = index+STR_OFFSET<strLength ? index+STR_OFFSET : strLength, |
| 113 | cl = strlen(c), |
| 114 | i, |
| 115 | found; |
| 116 | |
| 117 | do { |
| 118 | found = i = 0; |
| 119 | do { |
| 120 | if (Str.data()[index+i] != c[i]) //calls QChar::unicode() which is slow |
| 121 | break; |
| 122 | found ++; |
| 123 | if (found == cl) |
| 124 | return index; |
| 125 | } while (i++ < cl); |
| 126 | } while (index++ < l-1); |
| 127 | if (index == l) |
| 128 | return -1; |
| 129 | return index; |
| 130 | } |
| 131 | |
| 132 | virtual int find(char c, unsigned int index) const |
| 133 | { |
| 134 | if (index >= strLength) |
| 135 | return -1; |
| 136 | |
| 137 | // Offset. Hope that is enough. TODO Long comments check? |
| 138 | unsigned int l = index+STR_OFFSET<strLength ? index+STR_OFFSET : strLength; |
| 139 | |
| 140 | while (Str.data()[index] != c && index++ < l-1) {}; //calls QChar::unicode() which is slow |
| 141 | if (index == l) |
| 142 | return -1; |
| 143 | return index; |
| 144 | } |
| 145 | |
| 146 | virtual unsigned int length() const |
| 147 | { |
| 148 | return strLength; |
| 149 | } |
| 150 | |
| 151 | unsigned int strLength; |
| 152 | |
| 153 | char * getStr() const {return (char *)Str.toLatin1().constData();} |
| 154 | //private: |
| 155 | QString Str; |
| 156 | }; |
| 157 | |
| 158 | |
| 159 | SGFParser::SGFParser(Tree * _tree) |
| 160 | //: boardHandler(bh) |
| 161 | { |
| 162 | // CHECK_PTR(boardHandler); |
| 163 | stream = NULL; |
| 164 | tree = _tree; |
| 165 | readCodec = 0; |
| 166 | loadedfromfile = false; |
| 167 | // xmlParser = NULL; |
| 168 | } |
| 169 | |
| 170 | SGFParser::~SGFParser() |
| 171 | { |
| 172 | // delete xmlParser; |
| 173 | } |
| 174 | |
| 175 | bool SGFParser::parse(const QString &fileName, const QString &/*filter*/) |
| 176 | { |
| 177 | if (fileName.isNull() || fileName.isEmpty()) |
| 178 | { |
| 179 | qWarning("No filename given!"); |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | // CHECK_PTR(boardHandler); |
| 184 | |
| 185 | QString toParse = loadFile(fileName); |
| 186 | if (toParse.isNull() || toParse.isEmpty()) |
| 187 | return false; |
| 188 | |
| 189 | /* |
| 190 | // Check for filter, if given |
| 191 | if (!filter.isNull()) |
| 192 | { |
| 193 | // XML |
| 194 | if (filter == Board::tr("XML")) |
| 195 | { |
| 196 | // Init xmlparser if not yet done |
| 197 | if (xmlParser == NULL) |
| 198 | xmlParser = new XMLParser(boardHandler); |
| 199 | xmlParser->parse(fileName); |
| 200 | return true; |
| 201 | } |
| 202 | } |
| 203 | */ |
| 204 | if (!initGame(toParse, fileName)) |
| 205 | return corruptSgf(); |
| 206 | return doParse(toParse); |
| 207 | } |
| 208 | /* |
| 209 | // Called from clipboard SGF import |
| 210 | bool SGFParser::parseString(const QString &toParse) |
| 211 | { |
| 212 | if (toParse.isNull() || toParse.isEmpty() || !initGame(toParse, NULL)) |
| 213 | return corruptSgf(); |
| 214 | return doParse(toParse); |
| 215 | } |
| 216 | */ |
| 217 | |
| 218 | QString SGFParser::loadFile(const QString &fileName) |
| 219 | { |
| 220 | qDebug("Trying to load file <%s>", fileName.toUtf8().constData()); |
| 221 | |
| 222 | QFile file(fileName); |
| 223 | |
| 224 | if (!file.exists()) |
| 225 | { |
| 226 | qDebug() << "Could not find file: " << fileName; |
| 227 | return NULL; |
| 228 | } |
| 229 | |
| 230 | if (!file.open(QIODevice::ReadOnly)) |
| 231 | { |
| 232 | qDebug() << "Could not open file: " << fileName; |
| 233 | return NULL; |
| 234 | } |
| 235 | |
| 236 | QTextStream txt(&file); |
| 237 | stream = &txt; |
| 238 | if (!setCodec()) |
| 239 | { |
| 240 | qDebug() << "Invalid text encoding given. Please check preferences!"; |
| 241 | return NULL; |
| 242 | } |
| 243 | |
| 244 | QString toParse; |
| 245 | int i = 10; |
| 246 | while (!txt.atEnd() && i--) |
| 247 | toParse.append(txt.readLine() + "\n"); |
| 248 | QString tmp=""; |
| 249 | if (!parseProperty(toParse, "CA", tmp)) //codec |
| 250 | return NULL; |
| 251 | if (!tmp.isEmpty()) |
| 252 | { |
| 253 | if(!setCodec(tmp)) |
| 254 | return NULL; |
| 255 | toParse.clear(); |
| 256 | txt.seek(0); |
| 257 | } |
| 258 | while (!txt.atEnd()) |
| 259 | toParse.append(txt.readLine() + "\n"); |
| 260 | readCodec = stream->codec(); |
| 261 | file.close(); |
| 262 | #ifdef DEBUG_CODEC |
| 263 | QMessageBox::information(0, "READING", toParse); |
| 264 | #endif |
| 265 | loadedfromfile = true; //no need to check the codec later |
| 266 | return toParse; |
| 267 | } |
| 268 | |
| 269 | bool SGFParser::setCodec(QString c) |
| 270 | { |
| 271 | QTextCodec *codec = NULL; |
| 272 | |
| 273 | if(c == QString()) |
| 274 | { |
| 275 | QSettings settings; |
| 276 | |
| 277 | // TODO : make sure we are getting the proper codec value from settings |
| 278 | if (settings.contains("CODEC")) |
| 279 | codec = QTextCodec::codecForName(settings.value("CODEC").toByteArray()); |
| 280 | } |
| 281 | else |
| 282 | codec = QTextCodec::codecForName(c.toLatin1().constData()); |
| 283 | #ifdef DEBUG_CODEC |
| 284 | QMessageBox::information(0, "LOCALE", codec); |
| 285 | #endif |
| 286 | /* |
| 287 | //switch (static_cast<Codec>(settings.value("CODEC")) |
| 288 | |
| 289 | switch (QTextCodec::codecForName(settings.value("CODEC"))) |
| 290 | { |
| 291 | case codecNone: |
| 292 | #ifdef DEBUG_CODEC |
| 293 | QMessageBox::information(0, "CODEC", "NONE"); |
| 294 | #endif |
| 295 | break; |
| 296 | |
| 297 | case codecBig5: |
| 298 | #ifdef DEBUG_CODEC |
| 299 | QMessageBox::information(0, "CODEC", "Big5"); |
| 300 | #endif |
| 301 | #ifndef Q_OS_WIN |
| 302 | codec = new QBig5Codec(); |
| 303 | #endif |
| 304 | break; |
| 305 | |
| 306 | case codecEucJP: |
| 307 | #ifdef DEBUG_CODEC |
| 308 | QMessageBox::information(0, "CODEC", "EUC JP"); |
| 309 | #endif |
| 310 | codec = new QEucJpCodec(); |
| 311 | break; |
| 312 | |
| 313 | case codecJIS: |
| 314 | #ifdef DEBUG_CODEC |
| 315 | QMessageBox::information(0, "CODEC", "JIS"); |
| 316 | #endif |
| 317 | codec = new QJisCodec(); |
| 318 | break; |
| 319 | |
| 320 | case codecSJIS: |
| 321 | #ifdef DEBUG_CODEC |
| 322 | QMessageBox::information(0, "CODEC", "Shift JIS"); |
| 323 | #endif |
| 324 | codec = new QSjisCodec(); |
| 325 | break; |
| 326 | |
| 327 | case codecEucKr: |
| 328 | #ifdef DEBUG_CODEC |
| 329 | QMessageBox::information(0, "CODEC", "EUC KR"); |
| 330 | #endif |
| 331 | codec = new QEucKrCodec(); |
| 332 | break; |
| 333 | |
| 334 | case codecGBK: |
| 335 | #ifdef DEBUG_CODEC |
| 336 | QMessageBox::information(0, "CODEC", "GBK"); |
| 337 | #endif |
| 338 | codec = new QGbkCodec(); |
| 339 | break; |
| 340 | |
| 341 | case codecTscii: |
| 342 | #ifdef DEBUG_CODEC |
| 343 | QMessageBox::information(0, "CODEC", "TSCII"); |
| 344 | #endif |
| 345 | codec = new QTsciiCodec(); |
| 346 | break; |
| 347 | |
| 348 | default: |
| 349 | return false; |
| 350 | } |
| 351 | */ |
| 352 | if ( stream != NULL) |
| 353 | { |
| 354 | if (codec != NULL) |
| 355 | stream->setCodec(codec); |
| 356 | else |
| 357 | stream->setCodec(QTextCodec::codecForLocale()); |
| 358 | } |
| 359 | |
| 360 | return true; |
| 361 | } |
| 362 | |
| 363 | bool SGFParser::doParse(const QString &toParseStr) |
| 364 | { |
| 365 | if (toParseStr.isNull() || toParseStr.isEmpty()) |
| 366 | { |
| 367 | qWarning("Failed loading from file. Is it empty?"); |
| 368 | return false; |
| 369 | } |
| 370 | QString tmp; |
| 371 | |
| 372 | if(!loadedfromfile) |
| 373 | { |
| 374 | /* This bit of ugliness is because sgfs are used to duplicate boards as well |
| 375 | * as load from file FIXME */ |
| 376 | parseProperty(toParseStr, "CA", tmp); //codec |
| 377 | if (!tmp.isEmpty()) |
| 378 | readCodec = QTextCodec::codecForName(tmp.toLatin1().constData()); |
| 379 | } |
| 380 | |
| 381 | const MyString *toParse = NULL; |
| 382 | |
| 383 | //////TODO if (static_cast<Codec>(setting->readIntEntry("CODEC")) == codecNone) |
| 384 | ///////// toParse = new MySimpleString(toParseStr); |
| 385 | /////// else |
| 386 | toParse = new MyString(toParseStr); |
| 387 | |
| 388 | Q_CHECK_PTR(toParse); |
| 389 | |
| 390 | int pos = 0, |
| 391 | posVarBegin = 0, |
| 392 | posVarEnd = 0, |
| 393 | posNode = 0, |
| 394 | moves = 0, |
| 395 | i, x=-1, y=-1; |
| 396 | |
| 397 | int a_offset = QChar::fromLatin1('a').unicode() - 1 ; |
| 398 | |
| 399 | unsigned int pointer = 0, |
| 400 | strLength = toParse->length(); |
| 401 | bool black = true, |
| 402 | setup = false, |
| 403 | old_label = false; |
| 404 | isRoot = true; |
| 405 | bool remember_root; |
| 406 | QString unknownProperty; |
| 407 | State state; |
| 408 | MarkType markType; |
| 409 | QString moveStr, commentStr; |
| 410 | Position *position; |
| 411 | MoveNum *moveNum; |
| 412 | QStack<Move*> stack; |
| 413 | QStack<MoveNum*> movesStack; |
| 414 | /* FIXME toRemove, et., al., appears unused Remove it */ |
| 415 | QStack<Position*> toRemove; |
| 416 | /* |
| 417 | ////TODO stack.setAutoDelete(false); |
| 418 | movesStack.setAutoDelete(true); |
| 419 | toRemove.setAutoDelete(true); |
| 420 | */ |
| 421 | // Initialises the tree with board size |
| 422 | parseProperty(toParseStr, "SZ", tmp); |
| 423 | // Tree *tree = new Tree(tmp.isEmpty() ? 19 : tmp.toInt()) ;// boardHandler->getTree(); |
| 424 | |
| 425 | state = stateVarBegin; |
| 426 | |
| 427 | bool cancel = false; |
| 428 | //FIXME abort does nothing!! |
| 429 | |
| 430 | // qDebug("File length = %d", strLength); |
| 431 | |
| 432 | tree->setLoadingSGF(true); |
| 433 | |
| 434 | QString sss=""; |
| 435 | do { |
| 436 | posVarBegin = toParse->find('(', pointer); |
| 437 | posVarEnd = toParse->find(')', pointer); |
| 438 | posNode = toParse->find(';', pointer); |
| 439 | |
| 440 | pos = minPos(posVarBegin, posVarEnd, posNode); |
| 441 | |
| 442 | // Switch states |
| 443 | |
| 444 | // Node -> VarEnd |
| 445 | if (state == stateNode && pos == posVarEnd) |
| 446 | state = stateVarEnd; |
| 447 | |
| 448 | // Node -> VarBegin |
| 449 | if (state == stateNode && pos == posVarBegin) |
| 450 | state = stateVarBegin; |
| 451 | |
| 452 | // VarBegin -> Node |
| 453 | else if (state == stateVarBegin && pos == posNode) |
| 454 | state = stateNode; |
| 455 | |
| 456 | // VarEnd -> VarBegin |
| 457 | else if (state == stateVarEnd && pos == posVarBegin) |
| 458 | state = stateVarBegin; |
| 459 | |
| 460 | // qDebug("State after switch = %d", state); |
| 461 | |
| 462 | // Do the work |
| 463 | switch (state) |
| 464 | { |
| 465 | case stateVarBegin: |
| 466 | if (pos != posVarBegin) |
| 467 | { |
| 468 | delete toParse; |
| 469 | return corruptSgf(pos); |
| 470 | } |
| 471 | |
| 472 | // qDebug("Var BEGIN at %d, moves = %d", pos, moves); |
| 473 | |
| 474 | stack.push(tree->getCurrent()); |
| 475 | moveNum = new MoveNum; |
| 476 | moveNum->n = moves; |
| 477 | movesStack.push(moveNum); |
| 478 | pointer = pos + 1; |
| 479 | break; |
| 480 | |
| 481 | case stateVarEnd: |
| 482 | if (pos != posVarEnd) |
| 483 | { |
| 484 | delete toParse; |
| 485 | return corruptSgf(pos); |
| 486 | } |
| 487 | |
| 488 | // qDebug("VAR END"); |
| 489 | |
| 490 | if (!movesStack.isEmpty() && !stack.isEmpty()) |
| 491 | { |
| 492 | Move *m = stack.pop(); |
| 493 | Q_CHECK_PTR(m); |
| 494 | x = movesStack.pop()->n; |
| 495 | |
| 496 | // qDebug("Var END at %d, moves = %d, moves from stack = %d", pos, moves, x); |
| 497 | |
| 498 | for (i=moves; i > x; i--) |
| 499 | { |
| 500 | position = toRemove.pop(); |
| 501 | if (position == NULL) |
| 502 | continue; |
| 503 | /////////////////// boardHandler->getStoneHandler()->removeStone(position->x, position->y); |
| 504 | // tree->removeStone(position->x, position->y); |
| 505 | // qDebug("Removing %d %d from stoneHandler.", position->x, position->y); |
| 506 | } |
| 507 | |
| 508 | moves = x; |
| 509 | |
| 510 | |
| 511 | tree->setCurrent(m); |
| 512 | } |
| 513 | pointer = pos + 1; |
| 514 | break; |
| 515 | |
| 516 | case stateNode: |
| 517 | if (pos != posNode) |
| 518 | { |
| 519 | delete toParse; |
| 520 | return corruptSgf(pos); |
| 521 | } |
| 522 | |
| 523 | // qDebug("Node at %d", pos); |
| 524 | commentStr = QString(); |
| 525 | setup = false; |
| 526 | markType = markNone; |
| 527 | |
| 528 | // Create empty node |
| 529 | remember_root = isRoot; |
| 530 | if (!isRoot) |
| 531 | { |
| 532 | ///////////////////// boardHandler->createMoveSGF(); |
| 533 | // qDebug("############### Before creating move ####################"); |
| 534 | // qDebug(toParse->Str.toLatin1().constData()); |
| 535 | //tree->createMoveSGF(); |
| 536 | /* This does happen, why??? FIXME */ |
| 537 | // qDebug("############### ####################"); |
| 538 | // qDebug(toParse->Str.toLatin1().constData()); |
| 539 | // qDebug("############### After creating move ####################"); |
| 540 | unknownProperty = QString(); |
| 541 | #ifdef FIXME //why is this a warning? this happens on loading a file with time info |
| 542 | if (tree->getCurrent()->getTimeinfo()) |
| 543 | qWarning("*** Timeinfo set !!!!"); |
| 544 | #endif //FIXME |
| 545 | //tree->getCurrent()->setTimeinfo(false); |
| 546 | } |
| 547 | else |
| 548 | isRoot = false; |
| 549 | |
| 550 | Property prop; |
| 551 | pos ++; |
| 552 | |
| 553 | do { |
| 554 | uint tmppos=0; |
| 555 | pos = toParse->next_nonspace (pos); |
| 556 | |
| 557 | if ((tmppos = toParse->isProperty("B",pos))) |
| 558 | { |
| 559 | prop = moveBlack; |
| 560 | pos = tmppos; |
| 561 | black = true; |
| 562 | } |
| 563 | else if ((tmppos = toParse->isProperty("W",pos))) |
| 564 | { |
| 565 | prop = moveWhite; |
| 566 | pos = tmppos; |
| 567 | black = false; |
| 568 | } |
| 569 | else if ((tmppos = toParse->isProperty("N",pos))) |
| 570 | { |
| 571 | prop = nodeName; |
| 572 | pos = tmppos; |
| 573 | } |
| 574 | else if ((tmppos = toParse->isProperty("AB",pos))) |
| 575 | { |
| 576 | prop = editBlack; |
| 577 | pos = tmppos; |
| 578 | setup = true; |
| 579 | black = true; |
| 580 | } |
| 581 | else if ((tmppos = toParse->isProperty("AW",pos))) |
| 582 | { |
| 583 | prop = editWhite; |
| 584 | pos = tmppos; |
| 585 | setup = true; |
| 586 | black = false; |
| 587 | } |
| 588 | else if ((tmppos = toParse->isProperty("AE",pos))) |
| 589 | { |
| 590 | prop = editErase; |
| 591 | pos = tmppos; |
| 592 | setup = true; |
| 593 | } |
| 594 | else if ((tmppos = toParse->isProperty("TR",pos))) |
| 595 | { |
| 596 | prop = editMark; |
| 597 | markType = markTriangle; |
| 598 | pos = tmppos; |
| 599 | } |
| 600 | else if ((tmppos = toParse->isProperty("CR",pos))) |
| 601 | { |
| 602 | prop = editMark; |
| 603 | markType = markCircle; |
| 604 | pos = tmppos; |
| 605 | } |
| 606 | else if ((tmppos = toParse->isProperty("SQ",pos))) |
| 607 | { |
| 608 | prop = editMark; |
| 609 | markType = markSquare; |
| 610 | pos = tmppos; |
| 611 | } |
| 612 | else if ((tmppos = toParse->isProperty("MA",pos))) |
| 613 | { |
| 614 | prop = editMark; |
| 615 | markType = markCross; |
| 616 | pos = tmppos; |
| 617 | } |
| 618 | // old definition |
| 619 | else if ((tmppos = toParse->isProperty("M",pos))) |
| 620 | { |
| 621 | prop = editMark; |
| 622 | markType = markCross; |
| 623 | pos = tmppos; |
| 624 | } |
| 625 | else if ((tmppos = toParse->isProperty("LB",pos))) |
| 626 | { |
| 627 | prop = editMark; |
| 628 | markType = markText; |
| 629 | pos = tmppos; |
| 630 | old_label = false; |
| 631 | } |
| 632 | // Added old L property. This is not SGF4, but many files contain this tag. |
| 633 | else if ((tmppos = toParse->isProperty("L",pos))) |
| 634 | { |
| 635 | prop = editMark; |
| 636 | markType = markText; |
| 637 | pos = tmppos; |
| 638 | old_label = true; |
| 639 | } |
| 640 | else if ((tmppos = toParse->isProperty("C",pos))) |
| 641 | { |
| 642 | prop = comment; |
| 643 | pos = tmppos; |
| 644 | } |
| 645 | else if ((tmppos = toParse->isProperty("TB",pos))) |
| 646 | { |
| 647 | prop = editMark; |
| 648 | markType = markTerrBlack; |
| 649 | pos = tmppos; |
| 650 | black = true; |
| 651 | } |
| 652 | else if ((tmppos = toParse->isProperty("TW",pos))) |
| 653 | { |
| 654 | prop = editMark; |
| 655 | markType = markTerrWhite; |
| 656 | pos = tmppos; |
| 657 | black = false; |
| 658 | } |
| 659 | else if ((tmppos = toParse->isProperty("BL",pos))) |
| 660 | { |
| 661 | prop = timeLeft; |
| 662 | pos = tmppos; |
| 663 | black = true; |
| 664 | } |
| 665 | else if ((tmppos = toParse->isProperty("WL",pos))) |
| 666 | { |
| 667 | prop = timeLeft; |
| 668 | pos = tmppos; |
| 669 | black = false; |
| 670 | } |
| 671 | else if ((tmppos = toParse->isProperty("OB",pos))) |
| 672 | { |
| 673 | prop = openMoves; |
| 674 | pos = tmppos; |
| 675 | black = true; |
| 676 | } |
| 677 | else if ((tmppos = toParse->isProperty("OW",pos))) |
| 678 | { |
| 679 | prop = openMoves; |
| 680 | pos = tmppos; |
| 681 | black = false; |
| 682 | } |
| 683 | else if ((tmppos = toParse->isProperty("PL",pos))) |
| 684 | { |
| 685 | prop = nextMove; |
| 686 | pos = tmppos; |
| 687 | } |
| 688 | else if ((tmppos = toParse->isProperty("RG",pos))) |
| 689 | { |
| 690 | prop = unknownProp; |
| 691 | pos = tmppos; |
| 692 | setup = true; |
| 693 | } |
| 694 | // Empty node |
| 695 | else if (toParse->at(pos) == ';' || toParse->at(pos) == '(' || toParse->at(pos) == ')') |
| 696 | { |
| 697 | qDebug("Found empty node at %d", pos); |
| 698 | while (toParse->at(pos).isSpace()) |
| 699 | pos++; |
| 700 | continue; |
| 701 | } |
| 702 | else |
| 703 | { |
| 704 | // handle like comment |
| 705 | prop = unknownProp; |
| 706 | pos = toParse->next_nonspace (pos); |
| 707 | //qDebug("SGF: next nonspace (1st):" + QString(toParse->at(pos)) + QString(toParse->at(pos+1)) + QString(toParse->at(pos+2))); |
| 708 | } |
| 709 | |
| 710 | //qDebug("Start do loop : FOUND PROP %d, pos at %d now", prop, pos); |
| 711 | //qDebug(toParse->getStr()); //causes crash |
| 712 | // Next is one or more '[xx]'. |
| 713 | // Only one in a move property, several in a setup propery |
| 714 | do { |
| 715 | if (toParse->at(pos) != '[' && prop != unknownProp) |
| 716 | { |
| 717 | delete toParse; |
| 718 | return corruptSgf(pos); |
| 719 | } |
| 720 | |
| 721 | // Empty type |
| 722 | if (toParse->at(pos+1) == ']') |
| 723 | { |
| 724 | // CGoban stores pass as 'B[]' or 'W[]' |
| 725 | if (prop == moveBlack || prop == moveWhite) |
| 726 | { |
| 727 | tree->doPass(true); |
| 728 | |
| 729 | // Remember this move for later, to remove from the matrix. |
| 730 | position = new Position; |
| 731 | position->x = x; |
| 732 | position->y = y; |
| 733 | toRemove.push(position); |
| 734 | moves ++; |
| 735 | } |
| 736 | |
| 737 | pos += 2; |
| 738 | continue; |
| 739 | } |
| 740 | |
| 741 | switch (prop) |
| 742 | { |
| 743 | case moveBlack: |
| 744 | case moveWhite: |
| 745 | // rare case: root contains move or placed stone: |
| 746 | if (remember_root) |
| 747 | { |
| 748 | qDebug("root contains stone -> node created"); |
| 749 | /* Something is screwy here, inconsistencies |
| 750 | * in the way SGF's are treated. Like the below: |
| 751 | * the whole point of "remember_root", FIXME*/ |
| 752 | tree->addEmptyMove(); |
| 753 | isRoot = false; |
| 754 | unknownProperty = QString(); |
| 755 | #ifdef FIXME //why is this a warning? |
| 756 | if (tree->getCurrent()->getTimeinfo()) |
| 757 | qWarning("*** Timeinfo set (2)!!!!"); |
| 758 | #endif //FIXME |
| 759 | //tree->getCurrent()->setTimeinfo(false); |
| 760 | } |
| 761 | case editBlack: |
| 762 | case editWhite: |
| 763 | case editErase: |
| 764 | { |
| 765 | x = toParse->at(pos+1).unicode() - a_offset ;// - 'a';// + 1; |
| 766 | y = toParse->at(pos+2).unicode() - a_offset ; //- 'a' + 1; |
| 767 | |
| 768 | int x1, y1; |
| 769 | bool compressed_list; |
| 770 | |
| 771 | // check for compressed lists |
| 772 | if (toParse->at(pos+3) == ':') |
| 773 | { |
| 774 | x1 = toParse->at(pos+4).unicode() -a_offset;// - 'a' + 1; |
| 775 | y1 = toParse->at(pos+5).unicode() -a_offset;// - 'a' + 1; |
| 776 | compressed_list = true; |
| 777 | } |
| 778 | else |
| 779 | { |
| 780 | x1 = x; |
| 781 | y1 = y; |
| 782 | compressed_list = false; |
| 783 | } |
| 784 | /* |
| 785 | * TODO Do we nned this when the tree is created from file ? |
| 786 | * boardHandler->setModeSGF(setup || compressed_list ? modeEdit : modeNormal); |
| 787 | */ |
| 788 | |
| 789 | int i, j; |
| 790 | for (i = x; i <= x1; i++) |
| 791 | for (j = y; j <= y1; j++) |
| 792 | { |
| 793 | if (prop == editErase) |
| 794 | { |
| 795 | tree->getCurrent()->addStone(stoneErase, i, j); |
| 796 | } |
| 797 | else |
| 798 | { |
| 799 | if(setup) |
| 800 | { |
| 801 | if ((!remember_root) && (stack.top() == tree->getCurrent())) |
| 802 | tree->addEmptyMove(); //if this is first in branch we need to add an empty move |
| 803 | |
| 804 | tree->getCurrent()->addStone(black ? stoneBlack : stoneWhite, i, j); |
| 805 | } |
| 806 | else |
| 807 | { |
| 808 | Move *result = tree->getCurrent()->makeMove(black ? stoneBlack : stoneWhite, i, j); |
| 809 | if (result) |
| 810 | tree->setCurrent(result); |
| 811 | } |
| 812 | } |
| 813 | // tree->getCurrent()->getMatrix()->debug(); |
| 814 | //qDebug("ADDING MOVE %s %d/%d", black?"B":"W", x, y); |
| 815 | |
| 816 | // Remember this move for later, to remove from the matrix. |
| 817 | position = new Position; |
| 818 | position->x = i; |
| 819 | position->y = j; |
| 820 | toRemove.push(position); |
| 821 | moves ++; |
| 822 | } |
| 823 | |
| 824 | if (compressed_list) |
| 825 | // Advance pos by 7 |
| 826 | pos += 7; |
| 827 | else |
| 828 | // Advance pos by 4 |
| 829 | pos += 4; |
| 830 | break; |
| 831 | } |
| 832 | |
| 833 | case nodeName: |
| 834 | { |
| 835 | commentStr = QString(); |
| 836 | bool skip = false; |
| 837 | |
| 838 | while (toParse->at(++pos) != ']') |
| 839 | { |
| 840 | if (static_cast<unsigned int>(pos) > strLength-1) |
| 841 | { |
| 842 | qDebug("SGF: Nodename string ended immediately"); |
| 843 | delete toParse; |
| 844 | return corruptSgf(pos, "SGF: Nodename string ended immediately"); |
| 845 | } |
| 846 | |
| 847 | // white spaces |
| 848 | if (toParse->at(pos) == '\\') |
| 849 | { |
| 850 | while (toParse->at(pos+1).isSpace() && |
| 851 | static_cast<unsigned int>(pos) < strLength-2) |
| 852 | pos++; |
| 853 | if (toParse->at(pos).isSpace()) |
| 854 | pos++; |
| 855 | |
| 856 | // case: "../<cr><lf>]" |
| 857 | if (toParse->at(pos) == ']') |
| 858 | { |
| 859 | pos--; |
| 860 | skip = true; |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | // escaped chars: '\', ']', ':' |
| 865 | if (!(toParse->at(pos) == '\\' && |
| 866 | (toParse->at(pos+1) == ']' || |
| 867 | toParse->at(pos+1) == '\\' || |
| 868 | toParse->at(pos+1) == ':')) && |
| 869 | !skip && |
| 870 | // no formatting |
| 871 | !(toParse->at(pos) == '\n') && |
| 872 | !(toParse->at(pos) == '\r')) |
| 873 | commentStr.append(toParse->at(pos)); |
| 874 | } |
| 875 | |
| 876 | //qDebug("Node name read: %s", commentStr.toLatin1().constData()); |
| 877 | if (!commentStr.isEmpty()) |
| 878 | // add comment; skip 'C[]' |
| 879 | tree->getCurrent()->setNodeName(commentStr); |
| 880 | pos++; |
| 881 | break; |
| 882 | } |
| 883 | |
| 884 | case comment: |
| 885 | { |
| 886 | commentStr = QString(); |
| 887 | bool skip = false; |
| 888 | |
| 889 | while (toParse->at(++pos) != ']' || |
| 890 | (toParse->at(pos-1) == '\\' && toParse->at(pos) == ']')) |
| 891 | { |
| 892 | if (static_cast<unsigned int>(pos) > strLength-1) |
| 893 | { |
| 894 | qDebug("SGF: Comment string ended immediately"); |
| 895 | delete toParse; |
| 896 | return corruptSgf(pos, "SGF: Comment string ended immediately"); |
| 897 | } |
| 898 | |
| 899 | // white spaces |
| 900 | if (toParse->at(pos) == '\\') |
| 901 | { |
| 902 | while (toParse->at(pos+1).isSpace() && |
| 903 | static_cast<unsigned int>(pos) < strLength-2) |
| 904 | pos++; |
| 905 | if (toParse->at(pos).isSpace()) |
| 906 | pos++; |
| 907 | |
| 908 | // case: "../<cr><lf>]" |
| 909 | if (toParse->at(pos) == ']') |
| 910 | { |
| 911 | pos--; |
| 912 | skip = true; |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | // escaped chars: '\', ']', ':' |
| 917 | if (!(toParse->at(pos) == '\\' && |
| 918 | (toParse->at(pos+1) == ']' || |
| 919 | toParse->at(pos+1) == '\\' || |
| 920 | toParse->at(pos+1) == ':')) && |
| 921 | !skip) |
| 922 | commentStr.append(toParse->at(pos)); |
| 923 | } |
| 924 | |
| 925 | //qDebug("Comment read: %s", commentStr.toLatin1().constData()); |
| 926 | if (!commentStr.isEmpty()) |
| 927 | { |
| 928 | // add comment; skip 'C[]' |
| 929 | if(readCodec) |
| 930 | tree->getCurrent()->setComment(readCodec->toUnicode(commentStr.toLatin1().constData())); |
| 931 | else |
| 932 | tree->getCurrent()->setComment(commentStr.toLatin1().constData()); |
| 933 | } |
| 934 | pos ++; |
| 935 | break; |
| 936 | } |
| 937 | |
| 938 | case unknownProp: |
| 939 | { |
| 940 | // skip if property is known anyway |
| 941 | bool skip = false; |
| 942 | |
| 943 | // save correct property name (or p.n. + '[') |
| 944 | commentStr = QString(toParse->at(pos)); |
| 945 | commentStr += toParse->at(tmppos = toParse->next_nonspace (pos + 1)); |
| 946 | pos = tmppos; |
| 947 | |
| 948 | // check if it's really necessary to hold properties |
| 949 | // maybe they are handled at another position |
| 950 | if (commentStr == "WR" || |
| 951 | commentStr == "BR" || |
| 952 | commentStr == "PW" || |
| 953 | commentStr == "PB" || |
| 954 | commentStr == "SZ" || |
| 955 | commentStr == "KM" || |
| 956 | commentStr == "HA" || |
| 957 | commentStr == "RE" || |
| 958 | commentStr == "DT" || |
| 959 | commentStr == "PC" || |
| 960 | commentStr == "CP" || |
| 961 | commentStr == "GN" || |
| 962 | commentStr == "OT" || |
| 963 | commentStr == "TM" || |
| 964 | // now: general options |
| 965 | commentStr == "GM" || |
| 966 | commentStr == "ST" || |
| 967 | commentStr == "AP" || |
| 968 | commentStr == "FF") |
| 969 | { |
| 970 | skip = true; |
| 971 | } |
| 972 | sss= toParse->at(pos); |
| 973 | while (toParse->at(++pos) != ']' || |
| 974 | (toParse->at(pos-1) == '\\' && toParse->at(pos) == ']')) |
| 975 | { |
| 976 | if (static_cast<unsigned int>(pos) > strLength-1) |
| 977 | { |
| 978 | qDebug("SGF: Unknown property ended immediately"); |
| 979 | delete toParse; |
| 980 | return corruptSgf(pos, "SGF: Unknown property ended immediately"); |
| 981 | } |
| 982 | sss= toParse->at(pos); |
| 983 | if (!skip) |
| 984 | commentStr.append(toParse->at(pos)); |
| 985 | } |
| 986 | |
| 987 | if (!skip) |
| 988 | commentStr.append("]"); |
| 989 | |
| 990 | // qDebug("Comment read: %s", commentStr.latin1()); |
| 991 | if ((!commentStr.isEmpty()) && (!skip)) |
| 992 | { |
| 993 | // cumulate unknown properties; skip empty property 'XZ[]' |
| 994 | unknownProperty += commentStr; |
| 995 | tree->getCurrent()->setUnknownProperty(unknownProperty); |
| 996 | } |
| 997 | pos ++; |
| 998 | sss= toParse->at(pos); |
| 999 | break; |
| 1000 | } |
| 1001 | |
| 1002 | case editMark: |
| 1003 | // set moveStr for increment labels of old 'L[]' property |
| 1004 | moveStr = "A"; |
| 1005 | while (toParse->at(pos) == '[' && |
| 1006 | static_cast<unsigned int>(pos) < strLength) |
| 1007 | { |
| 1008 | x = toParse->at(pos+1).unicode() -a_offset;// - 'a' + 1; |
| 1009 | y = toParse->at(pos+2).unicode() -a_offset;// - 'a' + 1; |
| 1010 | // qDebug("MARK: %d at %d/%d", markType, x, y); |
| 1011 | pos += 3; |
| 1012 | |
| 1013 | // 'LB' property? Then we need to get the text |
| 1014 | if (markType == markText && !old_label) |
| 1015 | { |
| 1016 | if (toParse->at(pos) != ':') |
| 1017 | { |
| 1018 | delete toParse; |
| 1019 | return corruptSgf(pos); |
| 1020 | } |
| 1021 | moveStr = ""; |
| 1022 | while (toParse->at(++pos) != ']' && |
| 1023 | static_cast<unsigned int>(pos) < strLength) |
| 1024 | moveStr.append(toParse->at(pos)); |
| 1025 | // qDebug("LB TEXT = %s", moveStr.latin1()); |
| 1026 | // It might me a number mark? |
| 1027 | bool check = false; |
| 1028 | moveStr.toInt(&check); // Try to convert to Integer |
| 1029 | // treat integers as characters... |
| 1030 | check = false; |
| 1031 | |
| 1032 | if (check) |
| 1033 | tree->getCurrent()->getMatrix()-> |
| 1034 | insertMark(x, y, markNumber); // Worked, its a number |
| 1035 | else |
| 1036 | tree->getCurrent()->getMatrix()-> |
| 1037 | insertMark(x, y, markType); // Nope, its a letter |
| 1038 | tree->getCurrent()->getMatrix()-> |
| 1039 | setMarkText(x, y, moveStr); |
| 1040 | |
| 1041 | /*else //fastload |
| 1042 | { |
| 1043 | if (check) // Number |
| 1044 | tree->getCurrent()->insertFastLoadMark(x, y, markNumber); |
| 1045 | else // Text |
| 1046 | tree->getCurrent()->insertFastLoadMark(x, y, markType, moveStr); |
| 1047 | }*/ |
| 1048 | } |
| 1049 | else |
| 1050 | { |
| 1051 | int x1, y1; |
| 1052 | bool compressed_list; |
| 1053 | |
| 1054 | // check for compressed lists |
| 1055 | if (toParse->at(pos) == ':') |
| 1056 | { |
| 1057 | x1 = toParse->at(pos+1).unicode() -a_offset;// - 'a' + 1; |
| 1058 | y1 = toParse->at(pos+2).unicode() -a_offset;// - 'a' + 1; |
| 1059 | compressed_list = true; |
| 1060 | } |
| 1061 | else |
| 1062 | { |
| 1063 | x1 = x; |
| 1064 | y1 = y; |
| 1065 | compressed_list = false; |
| 1066 | } |
| 1067 | |
| 1068 | int i, j; |
| 1069 | for (i = x; i <= x1; i++) |
| 1070 | for (j = y; j <= y1; j++) |
| 1071 | { |
| 1072 | tree->getCurrent()->getMatrix()->insertMark(i, j, markType); |
| 1073 | //else //fastload |
| 1074 | // tree->getCurrent()->insertFastLoadMark(i, j, markType); |
| 1075 | |
| 1076 | // auto increment for old property 'L' |
| 1077 | if (old_label) |
| 1078 | { |
| 1079 | tree->getCurrent()->getMatrix()-> |
| 1080 | setMarkText(x, y, moveStr); |
| 1081 | QChar c1 = moveStr[0]; |
| 1082 | if (c1 == 'Z') |
| 1083 | moveStr = QString("a"); |
| 1084 | else |
| 1085 | moveStr = c1.unicode() + 1; |
| 1086 | } |
| 1087 | } |
| 1088 | |
| 1089 | // new_node = false; |
| 1090 | |
| 1091 | if (compressed_list) |
| 1092 | // Advance pos by 3 |
| 1093 | pos += 3; |
| 1094 | |
| 1095 | if((markType == markTerrWhite || markType == markTerrBlack) && !tree->getCurrent()->isTerritoryMarked()) |
| 1096 | tree->getCurrent()->setTerritoryMarked(); |
| 1097 | } |
| 1098 | |
| 1099 | //old_label = false; |
| 1100 | pos ++; |
| 1101 | while (toParse->at(pos).isSpace()) pos++; |
| 1102 | } |
| 1103 | break; |
| 1104 | |
| 1105 | case openMoves: |
| 1106 | { |
| 1107 | QString tmp_mv; |
| 1108 | while (toParse->at(++pos) != ']') |
| 1109 | tmp_mv += toParse->at(pos); |
| 1110 | tree->getCurrent()->setOpenMoves(tmp_mv.toInt()); |
| 1111 | pos++; |
| 1112 | |
| 1113 | if (!tree->getCurrent()->getTimeinfo()) |
| 1114 | { |
| 1115 | tree->getCurrent()->setTimeinfo(true); |
| 1116 | tree->getCurrent()->setTimeLeft(0); |
| 1117 | } |
| 1118 | break; |
| 1119 | } |
| 1120 | |
| 1121 | case timeLeft: |
| 1122 | { |
| 1123 | QString tmp_mv; |
| 1124 | while (toParse->at(++pos) != ']') |
| 1125 | tmp_mv += toParse->at(pos); |
| 1126 | tree->getCurrent()->setTimeLeft(tmp_mv.toFloat()); |
| 1127 | pos++; |
| 1128 | |
| 1129 | if (!tree->getCurrent()->getTimeinfo()) |
| 1130 | { |
| 1131 | tree->getCurrent()->setTimeinfo(true); |
| 1132 | tree->getCurrent()->setOpenMoves(0); |
| 1133 | } |
| 1134 | break; |
| 1135 | } |
| 1136 | |
| 1137 | case nextMove: |
| 1138 | if (toParse->at(++pos) == 'W') |
| 1139 | tree->getCurrent()->setPLinfo(stoneWhite); |
| 1140 | else if (toParse->at(pos) == 'B') |
| 1141 | tree->getCurrent()->setPLinfo(stoneBlack); |
| 1142 | |
| 1143 | pos += 2; |
| 1144 | break; |
| 1145 | |
| 1146 | default: |
| 1147 | break; |
| 1148 | } |
| 1149 | |
| 1150 | while (toParse->at(pos).isSpace()) |
| 1151 | pos++; |
| 1152 | |
| 1153 | sss= toParse->at(pos); |
| 1154 | |
| 1155 | } while (setup && toParse->at(pos) == '['); |
| 1156 | |
| 1157 | // tree->getCurrent()->getMatrix()->debug(); |
| 1158 | // qDebug("end do loop"); |
| 1159 | // qDebug(toParse->getStr()); |
| 1160 | |
| 1161 | while (toParse->at(pos).isSpace()) |
| 1162 | pos++; |
| 1163 | |
| 1164 | } while (toParse->at(pos) != ';' && toParse->at(pos) != '(' && toParse->at(pos) != ')' && static_cast<unsigned int>(pos) < strLength); |
| 1165 | |
| 1166 | // Advance pointer |
| 1167 | pointer = pos; |
| 1168 | |
| 1169 | break; |
| 1170 | |
| 1171 | default: |
| 1172 | delete toParse; |
| 1173 | return corruptSgf(pointer); |
| 1174 | } |
| 1175 | |
| 1176 | } while (pointer < strLength && pos >= 0); |
| 1177 | |
| 1178 | tree->setLoadingSGF(false); |
| 1179 | |
| 1180 | delete toParse; |
| 1181 | return !cancel; |
| 1182 | } |
| 1183 | |
| 1184 | bool SGFParser::corruptSgf(int where, QString reason) |
| 1185 | { |
| 1186 | QMessageBox::warning(0, PACKAGE, QObject::tr("Corrupt SGF file at position") + " " + |
| 1187 | QString::number(where) + "\n\n" + |
| 1188 | (reason.isNull() || reason.isEmpty() ? QString("") : reason)); |
| 1189 | tree->setLoadingSGF(false); |
| 1190 | return false; |
| 1191 | } |
| 1192 | |
| 1193 | |
| 1194 | int SGFParser::minPos(int n1, int n2, int n3) |
| 1195 | { |
| 1196 | int min; |
| 1197 | |
| 1198 | if (n1 != -1) |
| 1199 | min = n1; |
| 1200 | else if (n2 != -1) |
| 1201 | min = n2; |
| 1202 | else |
| 1203 | min = n3; |
| 1204 | |
| 1205 | if (n1 < min && n1 != -1) |
| 1206 | min = n1; |
| 1207 | |
| 1208 | if (n2 < min && n2 != -1) |
| 1209 | min = n2; |
| 1210 | |
| 1211 | if (n3 < min && n3 != -1) |
| 1212 | min = n3; |
| 1213 | |
| 1214 | return min; |
| 1215 | } |
| 1216 | |
| 1217 | // Return false: corrupt sgf, true: sgf okay. result = 0 when property not found |
| 1218 | bool SGFParser::parseProperty(const QString &toParse, const QString &prop, QString &result) |
| 1219 | { |
| 1220 | int pos, strLength=toParse.length(); |
| 1221 | result = ""; |
| 1222 | |
| 1223 | pos = toParse.indexOf(prop+"["); |
| 1224 | if (pos == -1) |
| 1225 | return true; |
| 1226 | |
| 1227 | pos += 2; |
| 1228 | if (toParse[pos] != '[') |
| 1229 | return corruptSgf(pos); |
| 1230 | while (toParse[++pos] != ']' && pos < strLength) |
| 1231 | result.append(toParse[pos]); |
| 1232 | if (pos > strLength) |
| 1233 | return corruptSgf(pos); |
| 1234 | |
| 1235 | return true; |
| 1236 | } |
| 1237 | |
| 1238 | GameData * SGFParser::initGame(const QString &toParse, const QString &fileName) |
| 1239 | { |
| 1240 | QString tmp=""; |
| 1241 | GameData *gameData = new GameData; |
| 1242 | |
| 1243 | if (!parseProperty(toParse, "CA", tmp)) //codec |
| 1244 | return NULL; |
| 1245 | if (!tmp.isEmpty()) |
| 1246 | gameData->codec = tmp; |
| 1247 | else |
| 1248 | gameData->codec = QString(); |
| 1249 | //probably should either be Latin1 or some default codec from somewhere FIXME |
| 1250 | |
| 1251 | // White player name |
| 1252 | if (!parseProperty(toParse, "PW", tmp)) |
| 1253 | return NULL; |
| 1254 | if (!tmp.isEmpty()) |
| 1255 | gameData->white_name = tmp; |
| 1256 | else |
| 1257 | gameData->white_name = "White"; |
| 1258 | |
| 1259 | // White player rank |
| 1260 | if (!parseProperty(toParse, "WR", tmp)) |
| 1261 | return NULL; |
| 1262 | if (!tmp.isEmpty()) |
| 1263 | gameData->white_rank = tmp; |
| 1264 | else |
| 1265 | gameData->white_rank = ""; |
| 1266 | |
| 1267 | // Black player name |
| 1268 | if (!parseProperty(toParse, "PB", tmp)) |
| 1269 | return NULL; |
| 1270 | if (!tmp.isEmpty()) |
| 1271 | gameData->black_name = tmp; |
| 1272 | else |
| 1273 | gameData->black_name = "Black"; |
| 1274 | |
| 1275 | // Black player rank |
| 1276 | if (!parseProperty(toParse, "BR", tmp)) |
| 1277 | return NULL; |
| 1278 | if (!tmp.isEmpty()) |
| 1279 | gameData->black_rank = tmp; |
| 1280 | else |
| 1281 | gameData->black_rank = ""; |
| 1282 | |
| 1283 | // Board size |
| 1284 | if (!parseProperty(toParse, "SZ", tmp)) |
| 1285 | return NULL; |
| 1286 | if (!tmp.isEmpty()) |
| 1287 | gameData->board_size = tmp.toInt(); |
| 1288 | else |
| 1289 | gameData->board_size = 19; |
| 1290 | |
| 1291 | // Komi |
| 1292 | if (!parseProperty(toParse, "KM", tmp)) |
| 1293 | return NULL; |
| 1294 | if (!tmp.isEmpty()) |
| 1295 | gameData->komi = tmp.toFloat(); |
| 1296 | else |
| 1297 | gameData->komi = 0.0; |
| 1298 | |
| 1299 | // Handicap |
| 1300 | if (!parseProperty(toParse, "HA", tmp)) |
| 1301 | return NULL; |
| 1302 | if (!tmp.isEmpty()) |
| 1303 | gameData->handicap = tmp.toInt(); |
| 1304 | else |
| 1305 | gameData->handicap = 0; |
| 1306 | |
| 1307 | // Result |
| 1308 | if (!parseProperty(toParse, "RE", tmp)) |
| 1309 | return NULL; |
| 1310 | if (!tmp.isEmpty()) |
| 1311 | gameData->result = tmp; |
| 1312 | else |
| 1313 | gameData->result = ""; |
| 1314 | |
| 1315 | // Date |
| 1316 | if (!parseProperty(toParse, "DT", tmp)) |
| 1317 | return NULL; |
| 1318 | if (!tmp.isEmpty()) |
| 1319 | gameData->date = tmp; |
| 1320 | else |
| 1321 | gameData->date = ""; |
| 1322 | |
| 1323 | // Place |
| 1324 | if (!parseProperty(toParse, "PC", tmp)) |
| 1325 | return NULL; |
| 1326 | if (!tmp.isEmpty()) |
| 1327 | gameData->place = tmp; |
| 1328 | else |
| 1329 | gameData->place = ""; |
| 1330 | |
| 1331 | // Copyright |
| 1332 | if (!parseProperty(toParse, "CP", tmp)) |
| 1333 | return NULL; |
| 1334 | if (!tmp.isEmpty()) |
| 1335 | gameData->copyright = tmp; |
| 1336 | else |
| 1337 | gameData->copyright = ""; |
| 1338 | |
| 1339 | // Game Name |
| 1340 | if (!parseProperty(toParse, "GN", tmp)) |
| 1341 | return NULL; |
| 1342 | if (!tmp.isEmpty()) |
| 1343 | gameData->gameName = tmp; |
| 1344 | else |
| 1345 | gameData->gameName = ""; |
| 1346 | |
| 1347 | // Comments style |
| 1348 | if (!parseProperty(toParse, "ST", tmp)) |
| 1349 | return NULL; |
| 1350 | if (!tmp.isEmpty()) |
| 1351 | gameData->style = tmp.toInt(); |
| 1352 | else |
| 1353 | gameData->style = 1; |
| 1354 | |
| 1355 | // Timelimit |
| 1356 | if (!parseProperty(toParse, "TM", tmp)) |
| 1357 | return NULL; |
| 1358 | if (!tmp.isEmpty()) |
| 1359 | { |
| 1360 | gameData->timelimit = tmp.toInt(); |
| 1361 | gameData->timeSystem = absolute; |
| 1362 | } |
| 1363 | else |
| 1364 | { |
| 1365 | gameData->timelimit = 0; |
| 1366 | gameData->timeSystem = none; |
| 1367 | } |
| 1368 | |
| 1369 | // Overtime == time system |
| 1370 | if (!parseProperty(toParse, "OT", tmp)) |
| 1371 | return NULL; |
| 1372 | if (!tmp.isEmpty()) |
| 1373 | { |
| 1374 | gameData->overtime = tmp; |
| 1375 | if (tmp.contains("byo-yomi")) |
| 1376 | { |
| 1377 | // type: OT[5x30 byo-yomi] |
| 1378 | gameData->timeSystem = byoyomi; |
| 1379 | int pos1, pos2; |
| 1380 | if ((pos1 = tmp.indexOf("x")) != -1) |
| 1381 | { |
| 1382 | pos2 = tmp.indexOf("byo"); |
| 1383 | QString time = tmp.mid(pos1+1, pos2-pos1-1); |
| 1384 | /*gameData->byoTime = time.toInt(); |
| 1385 | gameData->byoPeriods = tmp.left(pos1).toInt(); |
| 1386 | gameData->byoStones = 0;*/ |
| 1387 | //FIXME okay? |
| 1388 | gameData->periodtime = time.toInt(); |
| 1389 | gameData->stones_periods = tmp.left(pos1).toInt(); |
| 1390 | #ifdef FIXME |
| 1391 | qDebug(QString("byoyomi time system: %1 Periods at %2 seconds").arg(gameData->stones_periods).arg(gameData->periodtime).toLatin1().constData()); |
| 1392 | #endif //FIXME |
| 1393 | } |
| 1394 | } |
| 1395 | else if (tmp.contains(":")) |
| 1396 | { |
| 1397 | // type: OT[3:30] = byo-yomi?; |
| 1398 | int pos1; |
| 1399 | gameData->timeSystem = byoyomi; |
| 1400 | if ((pos1 = tmp.indexOf(":")) != -1) |
| 1401 | { |
| 1402 | QString time = tmp.left(pos1); |
| 1403 | int t = time.toInt()*60 + tmp.right(tmp.length() - pos1 - 1).toInt(); |
| 1404 | gameData->periodtime = 30; |
| 1405 | gameData->stones_periods = t/gameData->periodtime; |
| 1406 | //gameData->byoStones = 0; |
| 1407 | |
| 1408 | ////////////////////FIXME qDebug(QString("byoyomi time system: %1 Periods at %2 seconds").arg(gameData->byoPeriods).arg(gameData->byoTime)); |
| 1409 | } |
| 1410 | } |
| 1411 | else if (tmp.contains("Canadian")) |
| 1412 | { |
| 1413 | // type: OT[25/300 Canadian] |
| 1414 | gameData->timeSystem = canadian; |
| 1415 | int pos1, pos2; |
| 1416 | if ((pos1 = tmp.indexOf("/")) != -1) |
| 1417 | { |
| 1418 | pos2 = tmp.indexOf("Can"); |
| 1419 | QString time = tmp.mid(pos1+1, pos2-pos1-1); |
| 1420 | gameData->periodtime = time.toInt(); |
| 1421 | gameData->stones_periods = tmp.left(pos1).toInt(); |
| 1422 | |
| 1423 | /////////////////////// qDebug(QString("Canadian time system: %1 seconds at %2 stones").arg(gameData->byoTime).arg(gameData->byoStones)); |
| 1424 | } |
| 1425 | } |
| 1426 | |
| 1427 | // simple check |
| 1428 | if (gameData->stones_periods < 0) |
| 1429 | gameData->stones_periods = 0; |
| 1430 | if (gameData->periodtime <= 0) |
| 1431 | { |
| 1432 | gameData->periodtime = 0; |
| 1433 | // gameData->timeSystem = none; |
| 1434 | } |
| 1435 | } |
| 1436 | else |
| 1437 | { |
| 1438 | gameData->overtime = ""; |
| 1439 | // gameData->timeSystem = none; |
| 1440 | gameData->stones_periods = 0; |
| 1441 | } |
| 1442 | |
| 1443 | // Game number |
| 1444 | gameData->number = 0; |
| 1445 | |
| 1446 | gameData->fileName = fileName; |
| 1447 | |
| 1448 | // boardHandler->board->initGame(gameData, true); // Set sgf flag |
| 1449 | |
| 1450 | return gameData ; |
| 1451 | } |
| 1452 | |
| 1453 | /* |
| 1454 | * takes a game tree, and puts it in SGF format |
| 1455 | */ |
| 1456 | bool SGFParser::exportSGFtoClipB(QString *str, Tree *tree, GameData *gd) |
| 1457 | { |
| 1458 | Q_CHECK_PTR(tree); |
| 1459 | |
| 1460 | if (stream != NULL) |
| 1461 | delete stream; |
| 1462 | stream = new QTextStream(str, QIODevice::WriteOnly); |
| 1463 | |
| 1464 | bool res = writeStream(tree,gd); |
| 1465 | |
| 1466 | delete stream; |
| 1467 | stream = NULL; |
| 1468 | return res; |
| 1469 | } |
| 1470 | |
| 1471 | /* |
| 1472 | * Opens a file for saving SGF |
| 1473 | */ |
| 1474 | bool SGFParser::doWrite(const QString &fileName, Tree *tree, GameData *gameData) |
| 1475 | { |
| 1476 | Q_CHECK_PTR(tree); |
| 1477 | |
| 1478 | QFile file(fileName); |
| 1479 | |
| 1480 | if (!file.open(QIODevice::WriteOnly)) |
| 1481 | { |
| 1482 | QMessageBox::warning(0, PACKAGE, QObject::tr("Could not open file:") + " " + fileName); |
| 1483 | return false; |
| 1484 | } |
| 1485 | |
| 1486 | if (stream != NULL) |
| 1487 | delete stream; |
| 1488 | stream = new QTextStream(&file); |
| 1489 | |
| 1490 | bool res = writeStream(tree, gameData ); |
| 1491 | |
| 1492 | file.flush(); |
| 1493 | file.close(); |
| 1494 | delete stream; |
| 1495 | stream = NULL; |
| 1496 | return res; |
| 1497 | } |
| 1498 | |
| 1499 | /* |
| 1500 | * Writes the SGF code for whot 'tree' into the 'stream' |
| 1501 | */ |
| 1502 | bool SGFParser::writeStream(Tree *tree, GameData *gameData) |
| 1503 | { |
| 1504 | Q_CHECK_PTR(stream); |
| 1505 | if (!setCodec(gameData->codec)) |
| 1506 | { |
| 1507 | QMessageBox::critical(0, PACKAGE, QObject::tr("Invalid text encoding given. Please check preferences!")); |
| 1508 | delete stream; |
| 1509 | return false; |
| 1510 | } |
| 1511 | |
| 1512 | Move *root = tree->getRoot(); |
| 1513 | if (root == NULL) |
| 1514 | return false; |
| 1515 | |
| 1516 | // GameData *gameData = boardHandler->getGameData(); |
| 1517 | |
| 1518 | // Traverse the tree recursive in pre-order |
| 1519 | isRoot = true; |
| 1520 | traverse(root, gameData); |
| 1521 | |
| 1522 | return true; |
| 1523 | } |
| 1524 | |
| 1525 | /* |
| 1526 | * Writes the SGF header data from gameData into the 'stream' |
| 1527 | */ |
| 1528 | void SGFParser::writeGameHeader(GameData *gameData) |
| 1529 | { |
| 1530 | // Assemble data for root node |
| 1531 | *stream << ";GM[1]FF[4]" // We play Go, we use FF 4 |
| 1532 | << "AP[" << PACKAGE << ":" << VERSION << "]"; // Application name : Version |
| 1533 | if (gameData->style >= 0 && gameData->style <= 4) |
| 1534 | *stream << "ST[" << gameData->style << "]"; |
| 1535 | else |
| 1536 | *stream << "ST[1]"; // We show vars of current node |
| 1537 | if(gameData->codec != QString()) |
| 1538 | *stream << "CA[" << gameData->codec << "]"; |
| 1539 | if (gameData->gameName.isEmpty()) // Skip game name if empty |
| 1540 | *stream << endl; |
| 1541 | else |
| 1542 | *stream << "GN[" << gameData->gameName << "]" // Game Name |
| 1543 | << endl; |
| 1544 | *stream << "SZ[" << gameData->board_size << "]" // Board size |
| 1545 | << "HA[" << gameData->handicap << "]" // Handicap |
| 1546 | << "KM[" << gameData->komi << "]"; // Komi |
| 1547 | // << endl; |
| 1548 | |
| 1549 | if (gameData->timelimit != 0) |
| 1550 | *stream << "TM[" << gameData->timelimit << "]"; // Timelimit |
| 1551 | |
| 1552 | if (!gameData->overtime.isEmpty()) |
| 1553 | *stream << "OT[" << gameData->overtime << "]" << endl; // Overtime |
| 1554 | |
| 1555 | if (!gameData->white_name.isEmpty()) |
| 1556 | *stream << "PW[" << gameData->white_name << "]"; // White name |
| 1557 | |
| 1558 | if (!gameData->white_rank.isEmpty()) |
| 1559 | *stream << "WR[" << gameData->white_rank << "]"; // White rank |
| 1560 | |
| 1561 | if (!gameData->black_name.isEmpty()) |
| 1562 | *stream << "PB[" << gameData->black_name << "]"; // Black name |
| 1563 | |
| 1564 | if (!gameData->black_rank.isEmpty()) |
| 1565 | *stream << "BR[" << gameData->black_rank << "]"; // Black rank |
| 1566 | |
| 1567 | if (!gameData->result.isEmpty()) |
| 1568 | *stream << "RE[" << gameData->result << "]"; // Result |
| 1569 | |
| 1570 | if (!gameData->date.isEmpty()) |
| 1571 | *stream << "DT[" << gameData->date << "]"; // Date |
| 1572 | |
| 1573 | if (!gameData->place.isEmpty()) |
| 1574 | *stream << "PC[" << gameData->place << "]"; // Place |
| 1575 | |
| 1576 | if (!gameData->copyright.isEmpty()) |
| 1577 | *stream << "CP[" << gameData->copyright << "]"; // Copyright |
| 1578 | |
| 1579 | *stream << endl; |
| 1580 | } |
| 1581 | |
| 1582 | /* |
| 1583 | * traverses the tree from move 't', and stores SGF code into 'stream' |
| 1584 | */ |
| 1585 | void SGFParser::traverse(Move *t, GameData *gameData) |
| 1586 | { |
| 1587 | QString txt; |
| 1588 | *stream << "("; |
| 1589 | int col = -1, cnt = 6; |
| 1590 | |
| 1591 | |
| 1592 | do { |
| 1593 | if (isRoot) |
| 1594 | { |
| 1595 | writeGameHeader(gameData); |
| 1596 | txt = t->saveMove(isRoot).toUtf8(); //utf8 might only be necessary if gameData->codec is set, but should be okay either way |
| 1597 | *stream << txt; |
| 1598 | isRoot = false; |
| 1599 | } |
| 1600 | else |
| 1601 | { |
| 1602 | // do some formatting: add single B[]/W[] properties to one line, max: 10 |
| 1603 | // if more properties, e.g. B[]PL[]C] -> new line |
| 1604 | txt = t->saveMove(false).toUtf8(); |
| 1605 | if(txt == ";") //don't save empty nodes |
| 1606 | txt = ""; |
| 1607 | int cnt_old = cnt; |
| 1608 | cnt = txt.length(); |
| 1609 | if (col % 10 == 0 || (col == 1 && cnt != 6) || cnt_old != 6 || col == -1) |
| 1610 | { |
| 1611 | *stream << endl; |
| 1612 | col = 0; |
| 1613 | } |
| 1614 | *stream << txt; |
| 1615 | col++; |
| 1616 | } |
| 1617 | |
| 1618 | Move *tmp = t->son; |
| 1619 | if (tmp != NULL && tmp->brother != NULL) |
| 1620 | { |
| 1621 | do { |
| 1622 | *stream << endl; |
| 1623 | traverse(tmp, NULL); |
| 1624 | } while ((tmp = tmp->brother) != NULL); |
| 1625 | break; |
| 1626 | } |
| 1627 | } while ((t = t->son) != NULL); |
| 1628 | *stream << endl << ")"; |
| 1629 | } |