72 行
1.7 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2014 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://www.wtfpl.net/ for more details.
  9. //
  10. #include <lol/engine-internal.h>
  11. namespace lol
  12. {
  13. /*
  14. * Safe enum helpers
  15. */
  16. map<int64_t, String> BuildEnumMap(String const &str, char const **custom)
  17. {
  18. return BuildEnumMap(str.C(), custom);
  19. }
  20. map<int64_t, String> BuildEnumMap(char const *str, char const **custom)
  21. {
  22. map<int64_t, String> ret;
  23. char const *parser = str;
  24. int64_t next_value = 0;
  25. int64_t cur_idx = 0;
  26. for (;;)
  27. {
  28. /* Find name */
  29. while (*parser == ' ' || *parser == ',')
  30. ++parser;
  31. if (!*parser)
  32. break;
  33. /* Parse name */
  34. char const *name = parser;
  35. while (*parser && *parser != ' ' && *parser != ',' && *parser != '=')
  36. ++parser;
  37. char const *name_end = parser;
  38. /* Find the value (if any) */
  39. uint64_t current_value = next_value;
  40. while (*parser == ' ' || *parser == '=')
  41. ++parser;
  42. if (*parser && *parser != ',')
  43. {
  44. #if defined _WIN32
  45. current_value = _strtoi64(parser, nullptr, 0);
  46. #else
  47. current_value = strtoll(parser, nullptr, 0);
  48. #endif
  49. while (*parser && *parser != ' ' && *parser != ',')
  50. ++parser;
  51. }
  52. /* Store in the map */
  53. ret[current_value] = (!custom) ? (String(name, (int)(name_end - name))) : (String(custom[cur_idx]));
  54. next_value = current_value + 1;
  55. cur_idx++;
  56. }
  57. return ret;
  58. }
  59. } /* namespace lol */