src/resource_id.c
author Hernâni Marques <hernani@pep.foundation>
Wed, 27 Jun 2018 00:52:09 +0200
changeset 2771 b0c0d39f643d
parent 1898 7d6df20bf02c
child 3083 da1eb049cca1
permissions -rw-r--r--
typos (by reading for IETF)
     1 // This file is under GNU General Public License 3.0
     2 // see LICENSE.txt
     3 
     4 #include "pEp_internal.h"
     5 #include "resource_id.h"
     6 
     7 pEp_rid_list_t* new_rid_node(pEp_resource_id_type type, const char* resource) {
     8     pEp_rid_list_t* retval = (pEp_rid_list_t*)calloc(1, sizeof(pEp_rid_list_t));
     9     retval->rid_type = type;
    10     retval->rid = strdup(resource);
    11     return retval;
    12 }
    13 
    14 void free_rid_list(pEp_rid_list_t* list) {
    15     while (list) {
    16         pEp_rid_list_t* nextptr = list->next;
    17         free(list->rid);
    18         free(list);
    19         list = nextptr;
    20     }
    21 }
    22 
    23 const char* get_resource_ptr_noown(const char* uri) {
    24     char* uri_delim = strstr(uri, "://");
    25     if (!uri_delim)
    26         return uri;
    27     else
    28         return uri_delim + 3;
    29 }
    30 
    31 char* get_resource(char* uri) {
    32     const char* resource_ptr = get_resource_ptr_noown(uri);
    33     char* resource_str = NULL;
    34     if (resource_ptr)
    35         resource_str = strdup(resource_ptr);
    36     return resource_str;
    37 }
    38 
    39 bool is_file_uri(char* str) {
    40     return(!str ? false : strncmp(str, "file://", 7) == 0);
    41 }
    42 
    43 bool is_cid_uri(const char* str) {
    44     return(!str ? false : strncmp(str, "cid://", 6) == 0);
    45 }
    46 
    47 pEp_rid_list_t* parse_uri(const char* uri) {
    48     if (!uri)
    49         return NULL;
    50     pEp_resource_id_type type = (is_cid_uri(uri) ? PEP_RID_CID : PEP_RID_FILENAME);
    51     const char* resource = get_resource_ptr_noown(uri);
    52     return new_rid_node(type, resource);
    53 }
    54 
    55 char* build_uri(const char* uri_prefix, const char* resource) {
    56     if (!uri_prefix || !resource)
    57         return NULL;
    58     const char* delim = "://";
    59     const int delim_len = 3;
    60     int prefix_len = strlen(uri_prefix);
    61     int resource_len = strlen(resource);
    62     int retval_len = prefix_len + delim_len + resource_len;
    63 
    64     char* retval = calloc(1, retval_len + 1);
    65     strlcpy(retval, uri_prefix, retval_len + 1);
    66     strlcat(retval, delim, retval_len + 1);
    67     strlcat(retval, resource, retval_len + 1);
    68     
    69     return retval;
    70 }