// Contact converter // by Peter Blockley // Converts contacts from Yahoo csv format to individual Vcards #include #include //go to next row of input file int next_row(FILE *input_stream) { char ch=NULL; while ((ch!='\n')&&!feof(input_stream)) { ch=fgetc(input_stream); }; return feof(input_stream); }; //get next column of data int next_col(FILE *input_stream, char *data) { int pointer=0; char ch; while ((ch!=',')&&!feof(input_stream)) { ch=fgetc(input_stream); }; fgetc(input_stream); while ((ch!='"')&&!feof(input_stream)) { ch=fgetc(input_stream); *(data+pointer)=ch; pointer++; }; *(data+pointer-1)=NULL; return feof(input_stream); }; int create_vcards(FILE *input_stream) { //creates Vcards char temp[100]; int index; char name[100]; char home_phone[100]; char mobile_phone[100]; char work_phone[100]; char email1[100]; char email2[100]; char email3[100]; char category[100]; int vcard_index=1; FILE *output_stream; while (!next_row(input_stream)) { //get first name, if end of file terminate if (next_col(input_stream, name)) return 0; next_col(input_stream, temp); //get surname next_col(input_stream, temp); //if surname not NULL, concatenate to first name if (temp[0]!=NULL) { strcat(name, " "); strcat(name, temp); }; //get data for(index=0;index<28;index++) next_col(input_stream, work_phone); for(index=0;index<6;index++) next_col(input_stream, home_phone); for(index=0;index<3;index++) next_col(input_stream, mobile_phone); for(index=0;index<13;index++) next_col(input_stream, category); for(index=0;index<2;index++) next_col(input_stream, email1); for(index=0;index<2;index++) next_col(input_stream, email2); for(index=0;index<2;index++) next_col(input_stream, email3); //if contact contains information output vcard to file //if (strcmpi(category, "Parents Friends")&&strcmpi(category, "Services Contacts")&&strcmpi(category, "Commercial")&&strcmpi(category, "Family Contacts")&&!((work_phone[0]==NULL)&&(home_phone[0]==NULL)&&(mobile_phone[0]==NULL)&&(email1[0]==NULL))) if (!((work_phone[0]==NULL)&&(home_phone[0]==NULL)&&(mobile_phone[0]==NULL)&&(email1[0]==NULL))) { sprintf(temp, "vcard%d.vcf", vcard_index++); output_stream = fopen(temp, "w"); fprintf(output_stream, "BEGIN:VCARD\n"); fprintf(output_stream, "VERSION:2.1\n"); fprintf(output_stream, "N:%s;;;;\n", name); fprintf(output_stream, "FN:%s\n", name); fprintf(output_stream, "TEL;HOME;VOICE:%s\n",home_phone); fprintf(output_stream, "TEL;CELL:%s\n",mobile_phone); fprintf(output_stream, "TEL;WORK;VOICE:%s\n",work_phone); fprintf(output_stream, "EMAIL;INTERNET:%s\n",email1); fprintf(output_stream, "EMAIL;HOME:%s\n",email2); fprintf(output_stream, "EMAIL;WORK:%s\n",email3); fprintf(output_stream, "END:VCARD\n"); fclose(output_stream); }; }; return 0; } int main(int argc, char* argv[]) { FILE *input_stream; //display help if (argc==1) { printf("Contact converter - Peter Blockley 2003\n"); printf("Syntax: convert file_name.csv\n"); return 0; }; //open input file input_stream = fopen(argv[1], "r"); if (input_stream==NULL) { printf("error: invalid file name\n"); return 1; }; //create vcards create_vcards(input_stream); //close input file fclose(input_stream); return 0; };