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
|
/*
* Copyright (C) 2005 Brendan Cully <brendan@kublai.com>
*
* 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.
*
* $Id: vcquery.c,v 1.4 2006-11-12 12:08:21 roland Exp $
*/
#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;
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;
}
while (vf_get_next_object(&vfobj)) {
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
&& (propval = vf_get_prop_value_string(prop, props++))) {
strncat(name, propval, available);
available -= strlen(propval);
if (available > 0)
strncat(name, " ", available--);
}
if (available < STRING)
fullname = strdup(name);
}
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\n", propval, fullname);
} while (vf_get_next_property(&prop));
}
if (fullname)
free(fullname);
}
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;
}
|