resource_id.vala 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Copyright (c) 2012-2025 Daniele Bartolini et al.
  3. * SPDX-License-Identifier: GPL-3.0-or-later
  4. */
  5. namespace Crown
  6. {
  7. namespace ResourceId
  8. {
  9. /// Returns the extension of @a path or null if the path has no extension.
  10. public string? extension(string path)
  11. {
  12. string bn = GLib.Path.get_basename(path);
  13. int ld = bn.last_index_of_char('.');
  14. return (ld == -1 || bn.substring(ld) == bn) ? null : bn.substring(ld + 1);
  15. }
  16. /// Returns the type of the resource @a path or null if the path has not type.
  17. public string? type(string path)
  18. {
  19. return ResourceId.extension(path);
  20. }
  21. /// Returns the name of the resource @a path or null if the path is not a resource path.
  22. public string? name(string path)
  23. {
  24. string? type = ResourceId.type(path);
  25. return type == null ? null : path.substring(0, path.last_index_of_char('.'));
  26. }
  27. /// Returns the parent folder of the resource located at @a path.
  28. public string parent_folder(string path)
  29. {
  30. int ls = path.last_index_of_char('/');
  31. if (ls == -1)
  32. return "";
  33. return path.substring(0, ls);
  34. }
  35. public string path(string type, string name)
  36. {
  37. return type == "" ? name : name + "." + type;
  38. }
  39. /// Converts a regular path into a resource path.
  40. public string normalize(string path)
  41. {
  42. return path.replace("\\", "/");
  43. }
  44. } /* namespace ResourceId */
  45. } /* namespace Crown */