// Utility for telephone CSV-database convertation in
// Pocket PhonesDatabase format (for PocketPC)
// Written by ppcsoft.narod.ru (c) 2001-2003.
// Starting date 28.10.2001, version date 15.09.2003
#include <stdio.h>
#include <windows.h>
#define DBFRECORDSIZE 511
#define DBFFIELDS 5
#define DBNAMELEN 23
unsigned char sRecSize;
char cSepSymbol;
char* GetField(char *startfield)
{
char *nextfield,*field=startfield;
while ((*field&0xe0) && (*field!=cSepSymbol)) field++;
*field=0;
nextfield=field+1;
// trim all right spaces
for (--field; field>=startfield && isspace(*field); field--);
*++field = '\0';
return nextfield;
}
char *PutField(char *rec,char *field)
{
strcpy(rec, field);
sRecSize+=strlen(field)+1;
rec+=strlen(field);
*rec=0;
rec++;
return rec;
}
main(int arvc, char ** argv)
{
int sPhone;
char *sPhonep=(char *)&sPhone;
char sDBName[DBNAMELEN];
char cVersion=11;
char tPhone[8];
char cPhone[8];
char oRec[DBFRECORDSIZE];
char *pField,*pNextField;
char pRec[DBFRECORDSIZE];
char *pRecp;
FILE *filein, *fileout;
long numRecs;
int i,iSeparators;
unsigned char cPhoneDigits;
printf(
"====================================================\r\n"
" Utility for telephone CSV-database convertation in\r\n"
" Pocket PhonesDatabase format (for PocketPC).\r\n"
" Version: CSV.15.09.2003\r\n"
" Written by ppcsoft.narod.ru (c) 2001-2003.\r\n"
"====================================================\r\n\n"
);
if(arvc<5)
{
printf("Usage: convert database.csv phones.db dbname phonedigits separator\r\n"
"Sample: convert mycity.csv phones.db MY_CITY_2003 6 ;\r\n");
return -1;
}
if((filein = fopen(argv[1], "rb")) == NULL)
{
printf("Can't find - '%s'\n", argv[1]);
return -2;
}
if((fileout = fopen(argv[2], "w+b")) == NULL)
{
printf("Can't create - '%s'\n", argv[2]);
return -3;
}
if (arvc>5) cSepSymbol=argv[5][0]; else cSepSymbol=';';
//
cPhoneDigits=atoi(argv[4]);
// First 4 bytes in db - records count
fwrite(&numRecs, sizeof(numRecs), 1, fileout);
strset(sDBName,0);
// Write DB version
fwrite(&cVersion, sizeof(cVersion), 1, fileout);
// Write Phone Digits
fwrite(&cPhoneDigits, sizeof(cPhoneDigits), 1, fileout);
// Copy dbname
memset(sDBName,0,sizeof(sDBName));
strcpy(sDBName,argv[3]);
// Write DB Name
fwrite(&sDBName, sizeof(sDBName), 1, fileout);
// Start read file
numRecs=0;
while (fgets(oRec,DBFRECORDSIZE,filein)!=NULL)
{
//check input line
iSeparators=0;
for(i=0;i<strlen(oRec);i++)
{
if (oRec[i]==cSepSymbol) iSeparators++;
}
if (iSeparators==(DBFFIELDS-1)) {
// valid input record
AnsiUpper(oRec);
numRecs++;
if(numRecs % 1000 ==0)
{
printf ("\r#%i ", numRecs);
}
sRecSize=4;
pRecp=pRec;
memset(pRecp,0,DBFRECORDSIZE);
pRecp+=sRecSize;
memset(cPhone,0,sizeof(cPhone));
memset(pRec,0,DBFRECORDSIZE);
pField=GetField(oRec);
strcpy(tPhone,oRec);
i=-1;
while (++i<strlen(tPhone))
{
if (iswdigit(tPhone[i])) strncat(cPhone,tPhone+i,1);
}
sPhone=atoi(cPhone);
for(i=1;i<DBFFIELDS;i++)
{
pNextField=GetField(pField);
pRecp=PutField(pRecp,pField);
pField=pNextField;
}
pRec[0]=sRecSize;
pRec[1]=*sPhonep;
pRec[2]=*(sPhonep+1);
pRec[3]=*(sPhonep+2);
fwrite(pRec, sRecSize, 1, fileout);
fflush(fileout);
}
};
printf ("\r#%i ", numRecs);
// add final bytes #FFFF
pRec[0]=(char)0xFF;
pRec[1]=(char)0xFF;
fwrite(pRec, 2, 1, fileout);
// Move to start position
fseek(fileout, 0,SEEK_SET);
// First 4 bytes in db - records count
fwrite(&numRecs, sizeof(numRecs), 1, fileout);
fflush(fileout);
fclose(filein);
fclose(fileout);
printf("\r\n\nAll done.");
return 0;
}
|