| 1 | /* |
| 2 | * LegacyClonk |
| 3 | * |
| 4 | * Copyright (c) 1998-2000, Matthes Bender (RedWolf Design) |
| 5 | * Copyright (c) 2017-2021, The LegacyClonk Team and contributors |
| 6 | * |
| 7 | * Distributed under the terms of the ISC license; see accompanying file |
| 8 | * "COPYING" for details. |
| 9 | * |
| 10 | * "Clonk" is a registered trademark of Matthes Bender, used with permission. |
| 11 | * See accompanying file "TRADEMARK" for details. |
| 12 | * |
| 13 | * To redistribute this file separately, substitute the full license texts |
| 14 | * for the above references. |
| 15 | */ |
| 16 | |
| 17 | /* 32-bit value to identify object definitions */ |
| 18 | |
| 19 | #include <C4Include.h> |
| 20 | #include <C4Id.h> |
| 21 | |
| 22 | #include <format> |
| 23 | |
| 24 | static char C4IdTextBuffer[5]; |
| 25 | |
| 26 | const char *C4IdText(C4ID id) |
| 27 | { |
| 28 | GetC4IdText(id, sBuf: C4IdTextBuffer); |
| 29 | return C4IdTextBuffer; |
| 30 | } |
| 31 | |
| 32 | void GetC4IdText(C4ID id, char *sBuf) |
| 33 | { |
| 34 | // Invalid parameters |
| 35 | if (!sBuf) return; |
| 36 | // No id |
| 37 | if (id == C4ID_None) { SCopy(szSource: "NONE" , sTarget: sBuf); return; } |
| 38 | // Numerical id |
| 39 | if (Inside(ival: static_cast<int>(id), lbound: 0, rbound: 9999)) |
| 40 | { |
| 41 | std::format_to_n(out: sBuf, n: 4, fmt: "{:04}" , args: static_cast<unsigned int>(id)); |
| 42 | } |
| 43 | // Literal id |
| 44 | else |
| 45 | { |
| 46 | sBuf[0] = static_cast<char>((id & 0x000000FF) >> 0); |
| 47 | sBuf[1] = static_cast<char>((id & 0x0000FF00) >> 8); |
| 48 | sBuf[2] = static_cast<char>((id & 0x00FF0000) >> 16); |
| 49 | sBuf[3] = static_cast<char>((id & 0xFF000000) >> 24); |
| 50 | } |
| 51 | |
| 52 | sBuf[4] = 0; |
| 53 | } |
| 54 | |
| 55 | bool LooksLikeID(C4ID id) |
| 56 | { |
| 57 | // don't allow 0000, since this may indicate error |
| 58 | if (Inside(ival: static_cast<int>(id), lbound: 1, rbound: 9999)) return true; |
| 59 | for (int cnt = 0; cnt < 4; cnt++) |
| 60 | { |
| 61 | char b = static_cast<char>(id & 0xFF); |
| 62 | if (!(Inside(ival: b, lbound: 'A', rbound: 'Z') || Inside(ival: b, lbound: '0', rbound: '9') || (b == '_'))) return false; |
| 63 | id >>= 8; |
| 64 | } |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | // make sure that C4ID values are consistent |
| 69 | |
| 70 | static_assert("NONE"_id == 0); |
| 71 | static_assert("CLNK"_id == 0x4B4E4C43); |
| 72 | static_assert("1337"_id == 1337); |
| 73 | |