Index: common/vcard.c =================================================================== RCS file: /sources/gnokii/gnokii/common/vcard.c,v retrieving revision 1.34 diff -u -p -r1.34 vcard.c --- common/vcard.c 12 Jun 2008 18:26:53 -0000 1.34 +++ common/vcard.c 12 Jun 2008 22:21:13 -0000 @@ -63,6 +63,62 @@ static int gn_vcard_fprintf(FILE *f, con return current - buf; } +typedef struct { + char *str; + char *end; + unsigned int len; +} vcard_string; + +static void gn_vcard_append_printf(vcard_string *str, const char *fmt, ...) +{ + char buf[1024]; + va_list ap; + int len, lines, l; + + va_start(ap, fmt); + vsnprintf(buf, sizeof(buf), fmt, ap); + va_end(ap); + + /* Number of lines needed */ + lines = strlen(buf) / 76 + 1; + /* 3 characters for each line beyond the first one, + * plus the length of the buffer + * plus the line feed and the nul byte to finish it off */ + len = (lines - 1) * 3 + strlen (buf) + 3; + + /* The first malloc must have a nul byte at the end */ + if (str->str) + str->str = realloc(str->str, len + str->len); + else + str->str = realloc(str->str, len + 1); + if (str->end == NULL) + str->end = str->str; + else + str->end = str->str + str->len; + + for (l = 0; l < lines; l++) { + int to_copy; + + to_copy = GNOKII_MIN(76, strlen (buf) - 76 * l); + memcpy (str->end, buf + 76 * l, to_copy); + str->end = str->end + to_copy; + if (l != lines - 1) { + char *s = "\r\n "; + memcpy (str->end, s, 3); + str->end += 3; + } + } + + { + char *s = "\r\n"; + memcpy (str->end, s, 2); + str->end += 2; + str->end[0] = '\0'; + } + + str->len = str->end - str->str; +} + static int gn_phonebook2vcard_real(void *data, print_func func, gn_phonebook_entry *entry) { int i; @@ -185,6 +241,16 @@ GNOKII_API int gn_phonebook2vcard(FILE * return gn_phonebook2vcard_real((void *) f, (print_func) gn_vcard_fprintf, entry); } +GNOKII_API char * gn_phonebook2vcardstr(gn_phonebook_entry *entry) +{ + vcard_string str; + + str.str = NULL; + str.end = NULL; + str.len = 0; + gn_phonebook2vcard_real(&str, (print_func) gn_vcard_append_printf, entry); + return str.str; +} #define BEGINS(a) ( !strncmp(buf, a, strlen(a)) ) #define STRIP(a, b) strip_slashes(b, buf+strlen(a), line_len - strlen(a), GN_PHONEBOOK_NAME_MAX_LENGTH) Index: gnokii/gnokii-phonebook.c =================================================================== RCS file: /sources/gnokii/gnokii/gnokii/gnokii-phonebook.c,v retrieving revision 1.23 diff -u -p -r1.23 gnokii-phonebook.c --- gnokii/gnokii-phonebook.c 14 Apr 2008 21:17:27 -0000 1.23 +++ gnokii/gnokii-phonebook.c 12 Jun 2008 22:21:13 -0000 @@ -175,7 +175,13 @@ gn_error getphonebook(int argc, char *ar break; case 2: snprintf(location, sizeof(location), "%s%d", memory_type_string, entry.location); - gn_phonebook2vcard(stdout, &entry, location); + //gn_phonebook2vcard(stdout, &entry, location); + { + char *s; + s = gn_phonebook2vcardstr (&entry); + printf ("%s", s); + free (s); + } break; case 3: gn_phonebook2ldif(stdout, &entry); Index: include/gnokii.h.in =================================================================== RCS file: /sources/gnokii/gnokii/include/gnokii.h.in,v retrieving revision 1.7 diff -u -p -r1.7 gnokii.h.in --- include/gnokii.h.in 30 May 2008 15:33:00 -0000 1.7 +++ include/gnokii.h.in 12 Jun 2008 22:21:13 -0000 @@ -268,6 +268,7 @@ GNOKII_API gn_error gn_cfg_phone_load(co /* In/Out routines, file formats */ GNOKII_API int gn_phonebook2vcard(FILE *f, gn_phonebook_entry *entry, char *location); +GNOKII_API char * gn_phonebook2vcardstr(gn_phonebook_entry *entry); GNOKII_API int gn_vcard2phonebook(FILE *f, gn_phonebook_entry *entry); GNOKII_API int gn_phonebook2ldif(FILE *f, gn_phonebook_entry *entry);