// Utility for telephone dbf-database
convertation in
// Pocket PhonesDatabase format (for
PocketPC)
// Written by ppcsoft.narod.ru (c)
2001-2002.
// Starting date 28.10.2001, version date
18.06.2002, Serov city
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#define DBFRECORDSTART 0x283
#define DBFRECORDSIZE 248
#define DBFFIELDS 6
#define DBNAMELEN 23
unsigned char sRecSize;
// выделение поля из dbf структуры
void GetField(char *field, unsigned char
fieldsize)
{
signed char i=fieldsize;
while ((i>=0) &&
(field[i]==0x20)) i--;
field[i+1]=0;
}
// сохранение поля
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 sOwner[50];
char sOwnerInfo[70];
char sPRE[50];
char sStreet[50];
char sHome[7];
char sDoor[7];
// разобранные поля
char
*sData[DBFFIELDS]={tPhone,sStreet,sHome,sDoor,sOwner,sPRE};
char oRec[DBFRECORDSIZE];
char pRec[DBFRECORDSIZE];
char *pRecp;
FILE *filein, *fileout;
int readRecs;
long numRecs;
// длина полей
dbf структуры
unsigned char
oFileds[DBFFIELDS]={8,20,5,4,20,20};
// смещение начала полей dbf структуры
unsigned char
iFileds[DBFFIELDS]={0,9,29,33,63,83};
int i, addr;
// число цифр в телефоне
unsigned char cPhoneDigits;
printf(
"Utility for telephone dbf-database convertation
in\r\n"
"Pocket PhonesDatabase format (for
PocketPC)\r\n"
"Version: SPRAW.DBF\r\n"
"Written by ppcsoft.narod.ru (c)
2001-2002.\r\n"
"Starting date
28.10.2001-18.06.2002\r\n\n");
if(arvc<5)
{
printf("Usage: convert
database.in database.out dbname phonedigits\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;
}
//
cPhoneDigits=atoi(argv[4]);
// Move to start position
fseek(filein, DBFRECORDSTART,SEEK_SET);
// 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;
do {
readRecs=fread( oRec, DBFRECORDSIZE, 1,
filein );
if (readRecs==1)
{
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);
GetField(oRec, oFileds[0]-1);
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++)
{
addr=iFileds[i];
GetField(oRec+addr,
oFileds[i]-1);
OemToAnsi(oRec+addr,sData[i]);
AnsiUpper(sData[i]);
}
strset(sOwnerInfo,0);
if (strlen(sOwner)) {
strcpy(sOwnerInfo,sOwner);
}
if (strlen(sPRE)) {
if
(strlen(sOwnerInfo)) strcat(sOwnerInfo,", ");
strcat(sOwnerInfo,sPRE);
}
pRecp=PutField(pRecp,sOwnerInfo);
pRecp=PutField(pRecp,sStreet);
pRecp=PutField(pRecp,sHome);
pRecp=PutField(pRecp,sDoor);
pRec[0]=sRecSize;
pRec[1]=*sPhonep;
pRec[2]=*(sPhonep+1);
pRec[3]=*(sPhonep+2);
fwrite( pRec, sRecSize, 1,
fileout);
fflush(fileout);
}
} while (readRecs==1);
// add final bytes #FFFF
pRec[0]=0xFF;
pRec[1]=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;
}
|