mightymandel v16

GPU-based Mandelbrot set explorer

metadata.c
Go to the documentation of this file.
1 // mightymandel -- GPU-based Mandelbrot Set explorer
2 // Copyright (C) 2012,2013,2014,2015 Claude Heiland-Allen
3 // License GPL3+ http://www.gnu.org/licenses/gpl.html
4 
5 #include <stdbool.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 
10 #include "metadata.h"
11 
12 struct metadata_node {
15  char *key;
16  char *value;
17 };
18 
19 struct metadata {
22 };
23 
25  struct metadata *s = calloc(1, sizeof(struct metadata));
26  s->head.next = &s->tail;
27  s->tail.prev = &s->head;
28  return s;
29 }
30 
31 void metadata_delete(struct metadata *s) {
32  struct metadata_node *n = s->head.next;
33  while (n->next) {
34  n->prev->next = n->next;
35  n->next->prev = n->prev;
36  struct metadata_node *m = n->next;
37  free(n->key);
38  free(n->value);
39  free(n);
40  n = m;
41  }
42 }
43 
44 struct metadata_node *metadata_find(struct metadata *s, const char *key) {
45  struct metadata_node *n = s->head.next;
46  while (n->next) {
47  if (0 == strcmp(n->key, key)) {
48  return n;
49  }
50  n = n->next;
51  }
52  return 0;
53 }
54 
55 const char *metadata_lookup(struct metadata*s, const char *key) {
56  struct metadata_node *n = metadata_find(s, key);
57  if (n) {
58  return n->value;
59  }
60  return 0;
61 }
62 
63 void metadata_update(struct metadata *s, const char *key, const char *value) {
64  struct metadata_node *n = metadata_find(s, key);
65  if (n) {
66  free(n->value);
67  n->value = strdup(value);
68  } else {
69  n = calloc(1, sizeof(struct metadata_node));
70  n->key = strdup(key);
71  n->value = strdup(value);
72  n->next = &s->tail;
73  n->prev = s->tail.prev;
74  n->next->prev = n;
75  n->prev->next = n;
76  }
77 }
78 
79 void metadata_remove(struct metadata *s, const char *key) {
80  struct metadata_node *n = metadata_find(s, key);
81  if (! n) {
82  return;
83  }
84  n->prev->next = n->next;
85  n->next->prev = n->prev;
86  free(n->key);
87  free(n->value);
88  free(n);
89 }
90 
91 int metadata_strlen(const struct metadata *s, const char *prefix) {
92  int l = 0;
93  struct metadata_node *n = s->head.next;
94  while (n->next) {
95  l += strlen(prefix) + strlen(n->key) + 1 + strlen(n->value) + 1;
96  n = n->next;
97  }
98  return l;
99 }
100 
101 bool metadata_string(const struct metadata *s, const char *prefix, char *string, int length) {
102  char *str = string;
103  char *end = string + length;
104  const struct metadata_node *n = s->head.next;
105  while (n->next && str < end) {
106  str += snprintf(str, end - str, "%s%s %s\n", prefix, n->key, n->value);
107  n = n->next;
108  }
109  return str <= end;
110 }