src/resource_id.c
author Krista Bennett <krista@pep-project.org>
Wed, 21 Jun 2017 13:20:50 +0200
branchENGINE-9
changeset 1880 e2c2680bba23
child 1881 facb0b5b0bce
permissions -rw-r--r--
ENGINE-9: forgot to add resource_id.*
krista@1880
     1
// This file is under GNU General Public License 3.0
krista@1880
     2
// see LICENSE.txt
krista@1880
     3
krista@1880
     4
#include "pEp_internal.h"
krista@1880
     5
#include "resource_id.h"
krista@1880
     6
krista@1880
     7
pEp_rid_list_t* new_rid_node(pEp_resource_id_type type, const char* resource) {
krista@1880
     8
    pEp_rid_list_t* retval = (pEp_rid_list_t*)calloc(1, sizeof(pEp_rid_list_t));
krista@1880
     9
    retval->rid_type = type;
krista@1880
    10
    retval->rid = strdup(resource);
krista@1880
    11
    return retval;
krista@1880
    12
}
krista@1880
    13
krista@1880
    14
void free_rid_list(pEp_rid_list_t* list) {
krista@1880
    15
    while (list) {
krista@1880
    16
        pEp_rid_list_t* nextptr = list->next;
krista@1880
    17
        free(list->rid);
krista@1880
    18
        free(list);
krista@1880
    19
        list = nextptr;
krista@1880
    20
    }
krista@1880
    21
}
krista@1880
    22
krista@1880
    23
const char* get_resource_ptr_noown(const char* uri) {
krista@1880
    24
    char* uri_delim = strstr(uri, "://");
krista@1880
    25
    if (!uri_delim)
krista@1880
    26
        return uri;
krista@1880
    27
    else
krista@1880
    28
        return uri + 3;
krista@1880
    29
}
krista@1880
    30
krista@1880
    31
char* get_resource(char* uri) {
krista@1880
    32
    const char* resource_ptr = get_resource_ptr_noown(uri);
krista@1880
    33
    char* resource_str = NULL;
krista@1880
    34
    if (resource_ptr)
krista@1880
    35
        resource_str = strdup(resource_ptr);
krista@1880
    36
    return resource_str;
krista@1880
    37
}
krista@1880
    38
krista@1880
    39
bool is_file_uri(char* str) {
krista@1880
    40
    return(!str ? false : strncmp(str, "file://", 7) == 0);
krista@1880
    41
}
krista@1880
    42
krista@1880
    43
bool is_cid_uri(const char* str) {
krista@1880
    44
    return(!str ? false : strncmp(str, "cid://", 6) == 0);
krista@1880
    45
}
krista@1880
    46
krista@1880
    47
pEp_rid_list_t* parse_uri(const char* uri) {
krista@1880
    48
    if (!uri)
krista@1880
    49
        return NULL;
krista@1880
    50
    pEp_resource_id_type type = (is_cid_uri(uri) ? PEP_RID_CID : PEP_RID_FILENAME);
krista@1880
    51
    const char* resource = get_resource_ptr_noown(uri);
krista@1880
    52
    return new_rid_node(type, resource);
krista@1880
    53
}