1 | #ifndef PS_PG_SPECMAP_HXX |
---|
2 | #define PS_PG_SPECMAP_HXX |
---|
3 | |
---|
4 | // ----------------------------------------- |
---|
5 | // mapping shortname <-> SpeciesID |
---|
6 | |
---|
7 | static Name2IDMap __NAME2ID_MAP; |
---|
8 | static ID2NameMap __ID2NAME_MAP; |
---|
9 | static bool __MAPS_INITIALIZED = false; |
---|
10 | |
---|
11 | static GB_ERROR PG_initSpeciesMaps(GBDATA *pb_main) { |
---|
12 | |
---|
13 | GB_transaction ta(pb_main); |
---|
14 | |
---|
15 | ps_assert(!__MAPS_INITIALIZED); |
---|
16 | |
---|
17 | // look for existing mapping in pb-db: |
---|
18 | GBDATA *pb_mapping = GB_entry(pb_main, "species_mapping"); |
---|
19 | if (!pb_mapping) { // error |
---|
20 | GB_export_error("No species mapping"); |
---|
21 | } |
---|
22 | else { |
---|
23 | // retrieve mapping from string |
---|
24 | const char *mapping = GB_read_char_pntr(pb_mapping); |
---|
25 | if (!mapping) return GB_export_error("Can't read mapping"); |
---|
26 | |
---|
27 | while (mapping[0]) { |
---|
28 | const char *comma = strchr(mapping, ','); if (!comma) break; |
---|
29 | const char *semicolon = strchr(comma, ';'); if (!semicolon) break; |
---|
30 | string name(mapping, comma-mapping); |
---|
31 | comma+=1; |
---|
32 | string idnum(comma, semicolon-comma); |
---|
33 | SpeciesID id = atoi(idnum.c_str()); |
---|
34 | |
---|
35 | __NAME2ID_MAP[name] = id; |
---|
36 | __ID2NAME_MAP[id] = name; |
---|
37 | |
---|
38 | mapping = semicolon+1; |
---|
39 | } |
---|
40 | } |
---|
41 | |
---|
42 | __MAPS_INITIALIZED = true; |
---|
43 | return NULp; |
---|
44 | } |
---|
45 | |
---|
46 | SpeciesID PG_SpeciesName2SpeciesID(const string& shortname) { |
---|
47 | ps_assert(__MAPS_INITIALIZED); // you didn't call PG_initSpeciesMaps |
---|
48 | return __NAME2ID_MAP[shortname]; |
---|
49 | } |
---|
50 | |
---|
51 | inline const string& PG_SpeciesID2SpeciesName(SpeciesID num) { |
---|
52 | ps_assert(__MAPS_INITIALIZED); // you didn't call PG_initSpeciesMaps |
---|
53 | return __ID2NAME_MAP[num]; |
---|
54 | } |
---|
55 | |
---|
56 | static int PG_NumberSpecies() { |
---|
57 | return __ID2NAME_MAP.size(); |
---|
58 | } |
---|
59 | |
---|
60 | #else |
---|
61 | #error ps_pg_specmap.hxx included twice |
---|
62 | #endif // PS_PG_SPECMAP_HXX |
---|