Merge road stop removal cases in BuildRoadToolbarWindow
[openttd/fttd.git] / src / base_media.cpp
blob2e61e7e353ef21b67399634ec64cf95f8482d5d6
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
8 /** @file base_media.cpp Generic functions for replacing base data (graphics, sounds). */
10 #include "stdafx.h"
12 #include <set>
14 #include "debug.h"
15 #include "base_media_base.h"
17 /** Set of isocodes. */
18 typedef std::set <const char *, StringCompare> StringSet;
20 /** Keep a set of all isocodes seen, instead of duplicating each one. */
21 static StringSet isocodes;
23 /** Get the reference pointer for an isocode. */
24 static const char *register_isocode (const char *isocode)
26 std::pair <StringSet::iterator, StringSet::iterator> pair =
27 isocodes.equal_range (isocode);
29 if (pair.first != pair.second) return *pair.first;
31 /* Not found. */
32 const char *p = xstrdup (isocode);
33 isocodes.insert (pair.first, p);
34 return p;
37 /**
38 * Add a description of this set for a given language.
39 * @param isocode The isocode of the language.
40 * @param desc The description for that language.
42 void BaseSetDesc::add_desc (const char *isocode, const char *desc)
44 this->description[register_isocode(isocode)].reset (xstrdup (desc));
47 /**
48 * Get the description of this set for the given ISO code.
49 * It falls back to the first two characters of the ISO code in case
50 * no match could be made with the full ISO code. If even then the
51 * matching fails the default is returned.
52 * @param isocode The isocode to search for.
53 * @return The description.
55 const char *BaseSetDesc::get_desc (const char *isocode) const
57 assert (isocode != NULL);
59 /* First the full ISO code */
60 StringMap::const_iterator iter = this->description.find (isocode);
61 if (iter != this->description.end()) return iter->second.get();
63 /* Then the first two characters */
64 const char alt [3] = { isocode[0], isocode[1], '\0' };
65 iter = this->description.lower_bound (alt);
66 if (iter != this->description.end() && strncmp (iter->first, isocode, 2) == 0) {
67 return iter->second.get();
70 /* Then fall back */
71 return this->get_default_desc();
74 /**
75 * Try to read a single piece of metadata from an ini file.
76 * @param metadata The metadata group to search in.
77 * @param name The name of the item to read.
78 * @param type The type of the set for debugging output.
79 * @param filename The name of the filename for debugging output.
80 * @return The associated item, or NULL if it doesn't exist.
82 const IniItem *BaseSetDesc::fetch_metadata (const IniGroup *metadata,
83 const char *name, const char *type, const char *filename)
85 const IniItem *item = metadata->find (name);
86 if (item == NULL || StrEmpty (item->value)) {
87 DEBUG (grf, 0, "Base %sset detail loading: %s field missing in %s.",
88 type, name, filename);
89 return NULL;
91 return item;