ModUtility.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * CModHandler.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "ModUtility.h"
  12. #include <vstd/StringUtils.h>
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. std::string ModUtility::normalizeIdentifier(const std::string & scope, const std::string & remoteScope, const std::string & identifier)
  15. {
  16. auto p = vstd::splitStringToPair(identifier, ':');
  17. if(p.first.empty())
  18. p.first = scope;
  19. if(p.first == remoteScope)
  20. p.first.clear();
  21. return p.first.empty() ? p.second : p.first + ":" + p.second;
  22. }
  23. void ModUtility::parseIdentifier(const std::string & fullIdentifier, std::string & scope, std::string & type, std::string & identifier)
  24. {
  25. auto p = vstd::splitStringToPair(fullIdentifier, ':');
  26. scope = p.first;
  27. auto p2 = vstd::splitStringToPair(p.second, '.');
  28. if(!p2.first.empty())
  29. {
  30. type = p2.first;
  31. identifier = p2.second;
  32. }
  33. else
  34. {
  35. type = p.second;
  36. identifier.clear();
  37. }
  38. }
  39. std::string ModUtility::makeFullIdentifier(const std::string & scope, const std::string & type, const std::string & identifier)
  40. {
  41. if(type.empty())
  42. logGlobal->error("Full identifier (%s %s) requires type name", scope, identifier);
  43. std::string actualScope = scope;
  44. std::string actualName = identifier;
  45. //ignore scope if identifier is scoped
  46. auto scopeAndName = vstd::splitStringToPair(identifier, ':');
  47. if(!scopeAndName.first.empty())
  48. {
  49. actualScope = scopeAndName.first;
  50. actualName = scopeAndName.second;
  51. }
  52. if(actualScope.empty())
  53. {
  54. return actualName.empty() ? type : type + "." + actualName;
  55. }
  56. else
  57. {
  58. return actualName.empty() ? actualScope+ ":" + type : actualScope + ":" + type + "." + actualName;
  59. }
  60. }
  61. VCMI_LIB_NAMESPACE_END