trailer.con commit trailer: add data structures and basic functions (9385b5d)
   1#include "cache.h"
   2/*
   3 * Copyright (c) 2013, 2014 Christian Couder <chriscool@tuxfamily.org>
   4 */
   5
   6enum action_where { WHERE_END, WHERE_AFTER, WHERE_BEFORE, WHERE_START };
   7enum action_if_exists { EXISTS_ADD_IF_DIFFERENT_NEIGHBOR, EXISTS_ADD_IF_DIFFERENT,
   8                        EXISTS_ADD, EXISTS_REPLACE, EXISTS_DO_NOTHING };
   9enum action_if_missing { MISSING_ADD, MISSING_DO_NOTHING };
  10
  11struct conf_info {
  12        char *name;
  13        char *key;
  14        char *command;
  15        enum action_where where;
  16        enum action_if_exists if_exists;
  17        enum action_if_missing if_missing;
  18};
  19
  20static struct conf_info default_conf_info;
  21
  22struct trailer_item {
  23        struct trailer_item *previous;
  24        struct trailer_item *next;
  25        const char *token;
  26        const char *value;
  27        struct conf_info conf;
  28};
  29
  30static struct trailer_item *first_conf_item;
  31
  32static char *separators = ":";
  33
  34static int after_or_end(enum action_where where)
  35{
  36        return (where == WHERE_AFTER) || (where == WHERE_END);
  37}
  38
  39/*
  40 * Return the length of the string not including any final
  41 * punctuation. E.g., the input "Signed-off-by:" would return
  42 * 13, stripping the trailing punctuation but retaining
  43 * internal punctuation.
  44 */
  45static size_t token_len_without_separator(const char *token, size_t len)
  46{
  47        while (len > 0 && !isalnum(token[len - 1]))
  48                len--;
  49        return len;
  50}
  51
  52static int same_token(struct trailer_item *a, struct trailer_item *b)
  53{
  54        size_t a_len = token_len_without_separator(a->token, strlen(a->token));
  55        size_t b_len = token_len_without_separator(b->token, strlen(b->token));
  56        size_t min_len = (a_len > b_len) ? b_len : a_len;
  57
  58        return !strncasecmp(a->token, b->token, min_len);
  59}
  60
  61static int same_value(struct trailer_item *a, struct trailer_item *b)
  62{
  63        return !strcasecmp(a->value, b->value);
  64}
  65
  66static int same_trailer(struct trailer_item *a, struct trailer_item *b)
  67{
  68        return same_token(a, b) && same_value(a, b);
  69}