| 1 | /* |
| 2 | * Copyright (c) 2002 by Michael J. Roberts. All Rights Reserved. |
| 3 | * |
| 4 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 5 | * on using and copying this software. |
| 6 | */ |
| 7 | /* |
| 8 | Name |
| 9 | gameinfl.cpp - game info local character set extensions |
| 10 | Function |
| 11 | Defines functions for operating on GameInfo records in the local |
| 12 | character set. We implement these in a module separate from gameinfo.cpp |
| 13 | so that callers who don't need the character set translations won't have |
| 14 | to link in the character mapper module and everything it brings in. |
| 15 | Notes |
| 16 | |
| 17 | Modified |
| 18 | 07/16/02 MJRoberts - Creation |
| 19 | */ |
| 20 | |
| 21 | #include <string.h> |
| 22 | #include <stdlib.h> |
| 23 | |
| 24 | #include "os.h" |
| 25 | #include "t3std.h" |
| 26 | #include "utf8.h" |
| 27 | #include "vmerr.h" |
| 28 | #include "charmap.h" |
| 29 | #include "resload.h" |
| 30 | |
| 31 | #include "resfind.h" |
| 32 | #include "gameinfo.h" |
| 33 | |
| 34 | /* |
| 35 | * initialize with the default character mapper |
| 36 | */ |
| 37 | CTadsGameInfoLocal::CTadsGameInfoLocal(const char *argv0) |
| 38 | { |
| 39 | char charset_name[32]; |
| 40 | |
| 41 | /* get the default display character set */ |
| 42 | os_get_charmap(charset_name, OS_CHARMAP_DISPLAY); |
| 43 | |
| 44 | /* initialize with the selected character set */ |
| 45 | init(argv0, charset_name); |
| 46 | } |
| 47 | |
| 48 | /* |
| 49 | * initialize with the named character mapper |
| 50 | */ |
| 51 | CTadsGameInfoLocal::CTadsGameInfoLocal(const char *argv0, |
| 52 | const char *charset_name) |
| 53 | { |
| 54 | /* initialize with the selected character set */ |
| 55 | init(argv0, charset_name); |
| 56 | } |
| 57 | |
| 58 | /* |
| 59 | * initialize with the given character mapper |
| 60 | */ |
| 61 | CTadsGameInfoLocal::CTadsGameInfoLocal(class CCharmapToLocal *charmap) |
| 62 | { |
| 63 | /* remember the character set */ |
| 64 | local_mapper_ = charmap; |
| 65 | } |
| 66 | |
| 67 | /* |
| 68 | * initialize with a named character set |
| 69 | */ |
| 70 | void CTadsGameInfoLocal::init(const char *argv0, const char *charset_name) |
| 71 | { |
| 72 | char res_path[OSFNMAX]; |
| 73 | CResLoader *res_loader; |
| 74 | |
| 75 | /* |
| 76 | * Initialize the T3 error context, in case the caller hasn't already |
| 77 | * done so; this is necessary so that we can call the resource loader. |
| 78 | * (Note that it's harmless to re-initialize the error subsystem if |
| 79 | * it's already been initialized; the subsystem will just count the |
| 80 | * new reference and otherwise ignore the request.) |
| 81 | */ |
| 82 | err_init(512); |
| 83 | |
| 84 | /* set up a resource loader */ |
| 85 | os_get_special_path(res_path, sizeof(res_path), argv0, OS_GSP_T3_RES); |
| 86 | res_loader = new CResLoader(res_path); |
| 87 | |
| 88 | /* create a character mapper for the given character set */ |
| 89 | local_mapper_ = CCharmapToLocal::load(res_loader, charset_name); |
| 90 | |
| 91 | /* if that failed, use a plain ascii mapper (which never fails) */ |
| 92 | if (local_mapper_ == 0) |
| 93 | local_mapper_ = CCharmapToLocal::load(res_loader, "us-ascii"); |
| 94 | |
| 95 | /* done with the resource loader - delete it */ |
| 96 | delete res_loader; |
| 97 | |
| 98 | /* we're done with the error subsystem, so release our reference */ |
| 99 | err_terminate(); |
| 100 | } |
| 101 | |
| 102 | /* |
| 103 | * Delete |
| 104 | */ |
| 105 | CTadsGameInfoLocal::~CTadsGameInfoLocal() |
| 106 | { |
| 107 | /* |
| 108 | * delete the value list explicitly (we must call this in our subclass |
| 109 | * destructor, even though the base class destructor will also call |
| 110 | * it, because we override free_value() and thus need to make sure the |
| 111 | * free_value() invocations happen in the context of the subclass |
| 112 | * destructor) |
| 113 | */ |
| 114 | free_value_list(); |
| 115 | |
| 116 | /* release our reference on the local mapper */ |
| 117 | local_mapper_->release_ref(); |
| 118 | } |
| 119 | |
| 120 | /* |
| 121 | * Store a value string. We'll allocate a buffer and translate the string |
| 122 | * to the local character set. |
| 123 | */ |
| 124 | const char *CTadsGameInfoLocal::store_value(const char *val, size_t len) |
| 125 | { |
| 126 | char *new_str; |
| 127 | size_t new_len; |
| 128 | utf8_ptr p; |
| 129 | |
| 130 | /* set up a utf-8 pointer to the string */ |
| 131 | p.set((char *)val); |
| 132 | |
| 133 | /* get the length it takes to map the name */ |
| 134 | new_len = local_mapper_->map_utf8z(0, 0, p); |
| 135 | |
| 136 | /* add space for a null terminator */ |
| 137 | ++new_len; |
| 138 | |
| 139 | /* allocate space for the string */ |
| 140 | new_str = (char *)osmalloc(new_len); |
| 141 | |
| 142 | /* map the string */ |
| 143 | local_mapper_->map_utf8z(new_str, new_len, p); |
| 144 | |
| 145 | /* return the new string */ |
| 146 | return new_str; |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * Free a value previously stored |
| 151 | */ |
| 152 | void CTadsGameInfoLocal::free_value(const char *val) |
| 153 | { |
| 154 | /* we allocated the string with osmalloc, so free it with osfree */ |
| 155 | osfree((char *)val); |
| 156 | } |
| 157 |