| 1 | // Copyright (C) 2011 by Ben Cressey. |
| 2 | // |
| 3 | // This file is part of Gargoyle. |
| 4 | // |
| 5 | // Gargoyle is free software; you can redistribute it and/or modify |
| 6 | // it under the terms of the GNU General Public License as published by |
| 7 | // the Free Software Foundation; either version 2 of the License, or |
| 8 | // (at your option) any later version. |
| 9 | // |
| 10 | // Gargoyle is distributed in the hope that it will be useful, |
| 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | // GNU General Public License for more details. |
| 14 | // |
| 15 | // You should have received a copy of the GNU General Public License |
| 16 | // along with Gargoyle; if not, write to the Free Software |
| 17 | // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | |
| 19 | #include <string> |
| 20 | |
| 21 | #include "glk.h" |
| 22 | #include "garglk.h" |
| 23 | |
| 24 | #ifdef BABEL_HANDLER |
| 25 | |
| 26 | #include <cstdlib> |
| 27 | #include <memory> |
| 28 | #include <new> |
| 29 | #include <vector> |
| 30 | |
| 31 | #include "babel_handler.h" |
| 32 | #include "format.h" |
| 33 | #include "ifiction.h" |
| 34 | #include "treaty.h" |
| 35 | |
| 36 | void gli_initialize_babel(const std::string &filename) |
| 37 | { |
| 38 | auto ctx = garglk::unique(get_babel_ctx(), release_babel_ctx); |
| 39 | if (babel_init_ctx(const_cast<char *>(filename.c_str()), ctx.get()) != nullptr) { |
| 40 | int metaSize = babel_treaty_ctx(GET_STORY_FILE_METADATA_EXTENT_SEL, nullptr, 0, ctx.get()); |
| 41 | if (metaSize > 0) { |
| 42 | try { |
| 43 | std::vector<char> metadata(metaSize); |
| 44 | if (babel_treaty_ctx(GET_STORY_FILE_METADATA_SEL, metadata.data(), metadata.size(), ctx.get()) > 0) { |
| 45 | auto get_metadata = [&metadata](const std::string &key) { |
| 46 | return garglk::unique(ifiction_get_tag(metadata.data(), const_cast<char *>("bibliographic"), const_cast<char *>(key.c_str()), nullptr), [](void *p) { std::free(p); }); |
| 47 | }; |
| 48 | auto story_title = get_metadata("title"); |
| 49 | auto story_author = get_metadata("author"); |
| 50 | if (story_title != nullptr && story_author != nullptr) { |
| 51 | std::string title; |
| 52 | title = Format("{} - {}", story_title.get(), story_author.get()); |
| 53 | garglk_set_story_title(title.c_str()); |
| 54 | } |
| 55 | } |
| 56 | } catch (const std::bad_alloc &) { |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | babel_release_ctx(ctx.get()); |
| 61 | } |
| 62 | |
| 63 | #else |
| 64 | |
| 65 | void gli_initialize_babel(const std::string &) |
| 66 | { |
| 67 | } |
| 68 | |
| 69 | #endif // BABEL_HANDLER |