dav/tags.c

Mon, 01 Jan 2018 19:53:36 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 01 Jan 2018 19:53:36 +0100
changeset 361
b6f2462ee055
child 363
e9ed8e130ccf
permissions
-rw-r--r--

adds xattr lib and tag support

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2018 Olaf Wintermann. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdio.h>
#include <stdlib.h>

#include <ucx/string.h>
#include <ucx/utils.h>

#include "libxattr.h"

#include "tags.h"

UcxList* parse_text_taglist(const char *buf, size_t length) {
    UcxList *tags = NULL;
    
    int line_start = 0;
    for(int i=0;i<length;i++) {
        if(buf[i] == '\n' || i == length-1) {
            sstr_t line = sstrtrim(sstrn((char*)buf + line_start, i - line_start));
            if(line.length > 0) {
                DavTag *tag = calloc(1, sizeof(DavTag));
                tag->name = sstrdup(line).ptr;
                tag->color = NULL;
                tags = ucx_list_append(tags, tag);
            }
            line_start = i+1;
        }
    }
    
    return tags;
}

UcxBuffer* create_text_taglist(UcxList *tags) {
    if(!tags) {
        return NULL;
    }
    
    UcxBuffer *buf = ucx_buffer_new(NULL, 128, UCX_BUFFER_AUTOEXTEND);
    UCX_FOREACH(elm, tags) {
        DavTag *tag = elm->data;
        ucx_bprintf(buf, "%s\n", tag->name);
    }
    return buf;
}


UcxList* parse_csv_taglist(const char *buf, size_t length) {
    UcxList *taglist = NULL;
    
    sstr_t str = sstrn((char*)buf, length);
    ssize_t count = 0;
    sstr_t *tags = sstrsplit(str, S(","), &count);
    for(int i=0;i<count;i++) {
        DavTag *tag = malloc(sizeof(DavTag));
        tag->name = sstrdup(sstrtrim(tags[i])).ptr;
        tag->color = NULL;
        taglist = ucx_list_append(taglist, tag);
        free(tags[i].ptr);
    }
    if(tags) {
        free(tags);
    }
    return taglist;
}

UcxBuffer* create_csv_taglist(UcxList *tags) {
    UcxBuffer *buf = ucx_buffer_new(NULL, 128, UCX_BUFFER_AUTOEXTEND);
    int insertsep = 0;
    UCX_FOREACH(elm, tags) {
        DavTag *tag = elm->data;
        if(insertsep) {
            ucx_buffer_putc(buf, ',');
        }
        ucx_buffer_puts(buf, tag->name);
        insertsep = 1;
    }
    return buf;
}


static DavTag* parse_xml_dav_tag(DavXmlNode *node) {
    char *name = NULL;
    
    DavXmlNode *c = node->children;
    while(c) {
        if(c->type == DAV_XML_ELEMENT) {
            char *value = dav_xml_getstring(c->children);
            if(value) {
                if(!strcmp(c->namespace, DAV_NS)) {
                    if(!strcmp(c->name, "name")) {
                        char *value = dav_xml_getstring(c->children);
                        if(value) {
                            name = value;
                        }
                    }
                    // TODO: color, ...
                }
            }
        }
        c = c->next;
    }
    
    DavTag *tag = NULL;
    if(name) {
        tag = malloc(sizeof(DavTag));
        tag->name = strdup(name);
        tag->color = NULL;
    }
    return tag;
} 

UcxList* parse_dav_xml_taglist(DavXmlNode *taglistnode) {
    UcxList *tags = NULL;
    
    DavXmlNode *node = taglistnode;
    while(node) {
        if(node->type == DAV_XML_ELEMENT) {
            if(!strcmp(node->namespace, DAV_NS) && !strcmp(node->name, "tag")) {
                DavTag *tag = parse_xml_dav_tag(node);
                if(tag) {
                    tags = ucx_list_append(tags, tag);
                }
            }
        } 
        node = node->next;
    }
    
    return tags;
}

DavXmlNode* create_xml_taglist(UcxList *tags) {
    DavXmlNode *tag1 = NULL;
    DavXmlNode *lasttag = NULL;
    UCX_FOREACH(elm, tags) {
        DavTag *tag = elm->data;
        
        DavXmlNode *tagelm = dav_xml_createnode(DAV_NS, "tag");
        DavXmlNode *tagname = dav_xml_createnode_with_text(DAV_NS, "name", tag->name);
        // TODO: color
        tagelm->children = tagname;
        
        if(lasttag) {
            lasttag->next = tagelm;
            tagelm->prev = lasttag;
        } else {
            tag1 = tagelm;
        }
        lasttag = tagelm;
    }
    return tag1;
}

mercurial