-
Notifications
You must be signed in to change notification settings - Fork 7
/
custom_check.c
92 lines (77 loc) · 2.45 KB
/
custom_check.c
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
/*
Copyright 2014-2017 Thumbtack, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#define _DEFAULT_SOURCE
#define _POSIX_C_SOURCE 200809L /* for strdup */
#include "custom_check.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "logger.h"
extern custom_check_t custom_check_create(char *query, char *operator,
char *expected_result)
{
custom_check_t check = malloc(sizeof(struct custom_check));
if (check) {
CUSTOM_CHECK_QUERY(check) = strdup(query);
CUSTOM_CHECK_RESULT(check) = strdup(expected_result);
CUSTOM_CHECK_OPERATOR(check) = strdup(operator);
if (! (CUSTOM_CHECK_QUERY(check) &&
CUSTOM_CHECK_RESULT(check) &&
CUSTOM_CHECK_OPERATOR(check))) {
logger_write(LOG_CRIT, "%m");
}
} else {
logger_write(LOG_CRIT, "%m");
}
return check;
}
static void set_check_parameter(char **param, const char *value)
{
*param = realloc(*param, strlen(value)+1);
if (*param) {
strcpy(*param, value);
}
}
extern void custom_check_set_query(custom_check_t check, char *query)
{
if (check) {
set_check_parameter(&CUSTOM_CHECK_QUERY(check), query);
}
}
extern void custom_check_set_result(custom_check_t check, char *expected_result)
{
if (check) {
set_check_parameter(&CUSTOM_CHECK_RESULT(check), expected_result);
}
}
extern void custom_check_set_operator(custom_check_t check, char *operator)
{
if (check) {
set_check_parameter(&CUSTOM_CHECK_OPERATOR(check), operator);
}
}
extern void custom_check_destroy(custom_check_t check)
{
if (check) {
if (CUSTOM_CHECK_QUERY(check)) {
free(CUSTOM_CHECK_QUERY(check));
}
if (CUSTOM_CHECK_RESULT(check)) {
free(CUSTOM_CHECK_RESULT(check));
}
if (CUSTOM_CHECK_OPERATOR(check)) {
free(CUSTOM_CHECK_OPERATOR(check));
}
free(check);
}
}