-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathfield-cache.c
More file actions
121 lines (106 loc) · 3.16 KB
/
field-cache.c
File metadata and controls
121 lines (106 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include "platform.h"
#include "parser-private.h"
#include "field-cache.h"
/*
* The field cache 'string' interface has some simple rules:
* the "descriptor" part is assumed to be a static allocation,
* while the "value" is something that this interface will
* always allocate with 'strdup()', so you can generate it
* dynamically on the stack or whatever without having to
* worry about it.
*/
dc_status_t dc_field_add_string(dc_field_cache_t *cache, const char *desc, const char *value)
{
int i;
cache->initialized |= 1 << DC_FIELD_STRING;
for (i = 0; i < MAXSTRINGS; i++) {
dc_field_string_t *str = cache->strings+i;
if (str->desc)
continue;
str->value = strdup(value);
if (!str->value)
return DC_STATUS_NOMEMORY;
str->desc = desc;
return DC_STATUS_SUCCESS;
}
return DC_STATUS_INVALIDARGS;
}
dc_status_t dc_field_add_string_fmt(dc_field_cache_t *cache, const char *desc, const char *fmt, ...)
{
char buffer[256];
va_list ap;
/*
* We ignore the return value from vsnprintf, and we
* always NUL-terminate the destination buffer ourselves.
*
* That way we don't have to worry about random bad legacy
* implementations.
*/
va_start(ap, fmt);
buffer[sizeof(buffer)-1] = 0;
(void) vsnprintf(buffer, sizeof(buffer)-1, fmt, ap);
va_end(ap);
return dc_field_add_string(cache, desc, buffer);
}
dc_status_t dc_field_get_string(dc_field_cache_t *cache, unsigned idx, dc_field_string_t *value)
{
if (idx < MAXSTRINGS) {
dc_field_string_t *res = cache->strings+idx;
if (res->desc && res->value) {
*value = *res;
return DC_STATUS_SUCCESS;
}
}
return DC_STATUS_UNSUPPORTED;
}
/*
* Use this generic "pick fields from the field cache" helper
* after you've handled all the ones you do differently
*/
dc_status_t
dc_field_get(dc_field_cache_t *cache, dc_field_type_t type, unsigned int flags, void* value)
{
if (!value)
return DC_STATUS_INVALIDARGS;
if (!(cache->initialized & (1 << type)))
return DC_STATUS_UNSUPPORTED;
switch (type) {
case DC_FIELD_DIVETIME:
return DC_FIELD_VALUE(*cache, value, DIVETIME);
case DC_FIELD_MAXDEPTH:
return DC_FIELD_VALUE(*cache, value, MAXDEPTH);
case DC_FIELD_AVGDEPTH:
return DC_FIELD_VALUE(*cache, value, AVGDEPTH);
case DC_FIELD_GASMIX_COUNT:
return DC_FIELD_VALUE(*cache, value, GASMIX_COUNT);
case DC_FIELD_TANK_COUNT:
return DC_FIELD_VALUE(*cache, value, TANK_COUNT);
case DC_FIELD_GASMIX:
if (flags >= MAXGASES)
break;
return DC_FIELD_INDEX(*cache, value, GASMIX, flags);
case DC_FIELD_SALINITY:
return DC_FIELD_VALUE(*cache, value, SALINITY);
case DC_FIELD_ATMOSPHERIC:
return DC_FIELD_VALUE(*cache, value, ATMOSPHERIC);
case DC_FIELD_DIVEMODE:
return DC_FIELD_VALUE(*cache, value, DIVEMODE);
case DC_FIELD_TANK:
if (flags >= MAXGASES)
break;
dc_tank_t *tank = (dc_tank_t *) value;
tank->volume = cache->tanksize[flags];
tank->gasmix = flags;
tank->workpressure = cache->tankworkingpressure[flags];
tank->type = cache->tankinfo[flags];
return DC_STATUS_SUCCESS;
case DC_FIELD_STRING:
return dc_field_get_string(cache, flags, (dc_field_string_t *)value);
default:
break;
}
return DC_STATUS_UNSUPPORTED;
}