forked from RolandRosenfeld/lbdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvcquery.c
129 lines (108 loc) · 3.37 KB
/
vcquery.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
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
122
123
124
125
126
127
128
129
/*
* Copyright (C) 2005 Brendan Cully <[email protected]>
* Copyright (C) 2010 martin f krafft <[email protected]>
* Copyright (C) 2011-2018 Roland Rosenfeld <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,, USA.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vf_iface.h>
#define STRING 256
typedef struct {
char* filename;
} option_t;
int parse_opts(option_t* opts, int argc, char** argv);
int main(int argc, char** argv)
{
option_t opts;
VF_OBJECT_T* vfobj;
VF_PROP_T* prop;
char* propval;
char* fullname; /* TODO: initialise */
char* nickname;
const unsigned int namemap[5] = {3, 1, 2, 0, 4};
/* RFC 2426 defines N: field with 5 components:
* Family Name (3), Given Name (1), Additional Names (2),
* Honorific Prefixes (0), Honorific Suffixes (4)
*/
if (parse_opts(&opts, argc, argv))
return 1;
if (!vf_read_file(&vfobj, opts.filename)) {
fprintf(stderr, "Could not read VCF file %s\n", opts.filename);
return 1;
}
do {
fullname = 0;
/* First extract name */
if (vf_get_property(&prop, vfobj, VFGP_FIND, NULL, "FN", NULL))
if ((fullname = vf_get_prop_value_string(prop, 0)))
fullname = strdup(fullname);
/* No full name, lets try name */
if (!fullname
&& vf_get_property(&prop, vfobj, VFGP_FIND, NULL, "N", NULL)) {
char name[STRING+1] = { 0 };
size_t available = STRING;
int props = 0;
while (available > 0 && props < 5) {
if (propval = vf_get_prop_value_string(prop, namemap[props++])) {
strncat(name, propval, available);
available -= strlen(propval);
if (available > 0)
strncat(name, " ", available--);
}
}
if (available < STRING)
fullname = strdup(name);
}
/* TODO: fullname may actually still be NULL! */
nickname = NULL;
if (vf_get_property(&prop, vfobj, VFGP_FIND, NULL, "NICKNAME", NULL)) {
propval = vf_get_prop_value_string(prop, 0);
if (propval)
nickname = strdup(propval);
}
if (vf_get_property(&prop, vfobj, VFGP_FIND, NULL, "EMAIL", NULL)) {
do {
int props = 0;
while ((propval = vf_get_prop_value_string(prop, props++))) {
printf("%s\t%s", propval, fullname);
if (nickname) {
printf("\t%s", nickname);
} else {
putchar('\t');
}
putchar('\n');
}
} while (vf_get_next_property(&prop));
}
if (fullname)
free(fullname);
if (nickname)
free(nickname);
} while (vf_get_next_object(&vfobj));
return 0;
}
int parse_opts(option_t* opts, int argc, char** argv)
{
if (argc < 2) {
fprintf(stderr, "File name required\n");
return -1;
}
opts->filename = strdup(argv[1]);
return 0;
}