From 61cf765863f937ced05365b7f03bebc4a381e28f Mon Sep 17 00:00:00 2001 From: Jeff Davidson Date: Thu, 13 Apr 2023 22:27:18 -0700 Subject: [PATCH] Add support for coded ipuz files. This is an unofficial extension documented at https://crosswordnexus.com/ipuz/coded/, but it's as simple as a custom type on an otherwise normal crossword. If/when ipuz officially supports the format, we would just need to add the official kind. Generalize kind parsing by looping in reverse from most to least specific until we find a type that we know how to handle. --- puz/formats/ipuz/load_ipuz.cpp | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/puz/formats/ipuz/load_ipuz.cpp b/puz/formats/ipuz/load_ipuz.cpp index ad546c8c..9a54a0ad 100644 --- a/puz/formats/ipuz/load_ipuz.cpp +++ b/puz/formats/ipuz/load_ipuz.cpp @@ -176,17 +176,29 @@ bool ipuzParser::DoLoadPuzzle(Puzzle * puz, json::Value * root) { json::Map * doc = root->AsMap(); try { - // Check kind - string_t kind = doc->PopArray(puzT("kind"))->at(0)->AsString(); - if (kind.substr(kind.size() - 2) == puzT("#1")) - kind = kind.substr(0, kind.size() - 2); - if (kind == puzT("http://ipuz.org/crossword") || - kind == puzT("http://ipuz.org/crossword/crypticcrossword")) { - // Regular crossword - } else if (kind == puzT("http://ipuz.org/crossword/diagramless")) { - // Diagramless crossword - puz->GetGrid().SetType(TYPE_DIAGRAMLESS); - } else { + // Check kind from most to least specific until we find a match + json::Array * kinds = doc->PopArray(puzT("kind")); + std::vector::iterator i = kinds->end(); + bool known_type = false; + while (i != kinds->begin() && !known_type) { + string_t kind = (*(--i))->AsString(); + if (kind.substr(kind.size() - 2) == puzT("#1")) + kind = kind.substr(0, kind.size() - 2); + if (kind == puzT("http://ipuz.org/crossword") || + kind == puzT("http://ipuz.org/crossword/crypticcrossword")) { + // Regular crossword + known_type = true; + } else if (kind == puzT("http://ipuz.org/crossword/diagramless")) { + // Diagramless crossword + known_type = true; + puz->GetGrid().SetType(TYPE_DIAGRAMLESS); + } else if (kind == puzT("http://crosswordnexus.com/ipuz/coded")) { + // Coded crossword + known_type = true; + puz->GetGrid().SetType(TYPE_CODED); + } + } + if (!known_type) { throw LoadError("Unsupported ipuz kind"); } }