You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.2 KiB

4 years ago
4 years ago
4 years ago
  1. /** @file */
  2. /** @brief File description for doxygen missing. FIXME */
  3. // This file is under GNU General Public License 3.0
  4. // see LICENSE.txt
  5. #include "pEp_internal.h"
  6. #include "resource_id.h"
  7. pEp_rid_list_t* new_rid_node(pEp_resource_id_type type, const char* resource) {
  8. assert(resource);
  9. if (!resource)
  10. return NULL;
  11. pEp_rid_list_t* retval = (pEp_rid_list_t*)calloc(1, sizeof(pEp_rid_list_t));
  12. assert(retval);
  13. if (!retval)
  14. return NULL;
  15. retval->rid_type = type;
  16. retval->rid = strdup(resource);
  17. assert(retval->rid);
  18. if (!retval->rid) {
  19. free(retval);
  20. return NULL;
  21. }
  22. return retval;
  23. }
  24. void free_rid_list(pEp_rid_list_t* list) {
  25. while (list) {
  26. pEp_rid_list_t* nextptr = list->next;
  27. free(list->rid);
  28. free(list);
  29. list = nextptr;
  30. }
  31. }
  32. const char* get_resource_ptr_noown(const char* uri) {
  33. char* uri_delim = strstr(uri, "://");
  34. if (!uri_delim)
  35. return uri;
  36. else
  37. return uri_delim + 3;
  38. }
  39. char* get_resource(char* uri) {
  40. const char* resource_ptr = get_resource_ptr_noown(uri);
  41. char* resource_str = NULL;
  42. if (resource_ptr)
  43. resource_str = strdup(resource_ptr);
  44. return resource_str;
  45. }
  46. bool is_file_uri(char* str) {
  47. return(!str ? false : strncmp(str, "file://", 7) == 0);
  48. }
  49. bool is_cid_uri(const char* str) {
  50. return(!str ? false : strncmp(str, "cid://", 6) == 0);
  51. }
  52. pEp_rid_list_t* parse_uri(const char* uri) {
  53. if (!uri)
  54. return NULL;
  55. pEp_resource_id_type type = (is_cid_uri(uri) ? PEP_RID_CID : PEP_RID_FILENAME);
  56. const char* resource = get_resource_ptr_noown(uri);
  57. return new_rid_node(type, resource);
  58. }
  59. char* build_uri(const char* uri_prefix, const char* resource) {
  60. if (!uri_prefix || !resource)
  61. return NULL;
  62. const char* delim = "://";
  63. const int delim_len = 3;
  64. int prefix_len = strlen(uri_prefix);
  65. int resource_len = strlen(resource);
  66. int retval_len = prefix_len + delim_len + resource_len;
  67. char* retval = calloc(1, retval_len + 1);
  68. strlcpy(retval, uri_prefix, retval_len + 1);
  69. strlcat(retval, delim, retval_len + 1);
  70. strlcat(retval, resource, retval_len + 1);
  71. return retval;
  72. }