* Try to load a new game from disk. * @param filename Name of the file to load. * @return Game was loaded successfully. * @todo In what state is the game left when loading fails? * @todo String normalization code is duplicated in Micropolis::saveCityAs(). * Extract to a sub-function. * @bug Function fails if \c lastDot<lastSlash (ie with \c "x.y/bla" ) */
| 511 | * @bug Function fails if \c lastDot<lastSlash (ie with \c "x.y/bla" ) |
| 512 | */ |
| 513 | bool Micropolis::loadCity(const char *filename) |
| 514 | { |
| 515 | if (loadFile(filename)) { |
| 516 | |
| 517 | cityFileName = filename; |
| 518 | |
| 519 | unsigned int lastSlash = cityFileName.find_last_of('/'); |
| 520 | unsigned int pos = (lastSlash == std::string::npos) ? 0 : lastSlash + 1; |
| 521 | |
| 522 | unsigned int lastDot = cityFileName.find_last_of('.'); |
| 523 | unsigned int last = |
| 524 | (lastDot == std::string::npos) ? cityFileName.length() : lastDot; |
| 525 | |
| 526 | std::string newCityName = cityFileName.substr(pos, last - pos); |
| 527 | setCityName(newCityName); |
| 528 | |
| 529 | didLoadCity(); |
| 530 | |
| 531 | return true; |
| 532 | |
| 533 | } else { |
| 534 | |
| 535 | didntLoadCity((filename && *filename) ? filename : "(null)"); |
| 536 | |
| 537 | return false; |
| 538 | |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | /** Report to the frontend that the game was successfully loaded. */ |
| 543 | void Micropolis::didLoadCity() |
no test coverage detected