有一段C写的程序,现在想嵌入或者转换到DELPHI里,最好是是嵌入。请高手帮忙#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>#define MAXLENGTH 100
#define MAX_KEYVALUE 64int record_length=1115;
int columnNumber=0;
static char *temp_value;
static char *temp_key;
static char *temp_start_pos;
static char *temp_end_pos;struct keyent{
char *key;
int start_pos;
int end_pos;
};
struct record{
char *column;
char *value;
int start_pos;
int end_pos;
};
struct keyent keylist[MAX_KEYVALUE];
struct record *recordlist;static void rm_newline(char *s)
{
    int len;
    len = strlen(s);
    if(s[len - 1] == '\n') s[len - 1] = 0;
}static char *rm_headspace(char *s)
{
    while(*s){
        /* remove the spaces at the beginning */
        if(*s == ' ' || *s == '\t'){
            s ++;
        }else{
            return s;
        }
    }
    return s;
}static char *getKeyName(char *s)
{
char *pvalue;
static char keybuf[20];
int position; memset( keybuf, 0, sizeof(keybuf) );
     temp_value = s;
pvalue =strstr( temp_value, "=" );
position = pvalue - s;
strncpy( keybuf, temp_value, position );
temp_key = keybuf;
temp_value = pvalue+1;
return temp_value;
}static int addKeyTOList()
{
int i, position;
char *buf;
char *pvalue;
char startbuf[5], endbuf[5]; for(i = 0;i< MAX_KEYVALUE; i++ )
{
if(keylist[i].key == NULL)
{
if((buf = (char *)malloc(strlen(temp_key) + 1)) == NULL){
                printf("(config)Cannot malloc: %s\n", strerror(errno));
                return -1;
            }
strcpy( buf, temp_key );
keylist[i].key = buf;
//free(buf); if((buf = (char *)malloc(strlen(temp_value) + 1)) == NULL){
                printf("(config)Cannot malloc: %s\n", strerror(errno));
                return -1;
            }
strcpy( buf, temp_value );
pvalue = strstr( buf, "," );
position = pvalue - buf; memset( startbuf, 0, sizeof(startbuf) );
memset( endbuf, 0, sizeof(endbuf) );
strncpy( startbuf, buf, position );
strncpy( endbuf, pvalue+1, strlen(buf) - position );
keylist[i].start_pos = atoi(startbuf);
keylist[i].end_pos = atoi(endbuf);
//free(buf);
++columnNumber;
return i;
}
}
return -1;
}int initRecordList()
{
int i;

recordlist =(struct record *) malloc(sizeof( struct record)*columnNumber);
if( recordlist==NULL )
{
printf("malloc recordlist error:%s\n", strerror(errno));
return -1;
}
for(i = 0;i< columnNumber; i++ )
{
recordlist[i].column = (char *)calloc( strlen(keylist[i].key),1);
recordlist[i].value = (char *)calloc( keylist[i].end_pos - keylist[i].start_pos +1,1);
recordlist[i].start_pos = keylist[i].start_pos;
recordlist[i].end_pos = keylist[i].end_pos ;
strcpy(recordlist[i].column, keylist[i].key);
}
}int init(char *config)
{
FILE *fp;
char buf[MAXLENGTH];
char *pbuf; if((fp = fopen(config, "r"))==NULL){
printf("error to open configuration file: %s %s\n", config, strerror(errno));
return -1;
}
while(fgets(buf, sizeof(buf), fp)!=NULL)
{
rm_newline(buf);
pbuf = rm_headspace(buf); if(getKeyName(pbuf)!=NULL)
{
if( !strcmp(temp_key, "RECORD_LENGTH") )
record_length = atoi(temp_value);
else
if(addKeyTOList()<0) return -1;
}
}
fclose(fp); initRecordList();
return 0;
}int new_convert_file( char *f_from, char *f_to )
{
FILE *from, *to;
char *rbuf;
char *wbuf;
int n=0;
int i;
char *numberID[4];//1 - 4
char *orderNumber[14]; //7 - 20
char *custName[51]; //29 - 79
char *acctNumber[10]; //189 - 198
char *address[51]; //257 - 309
char *schedDate[9];//340 - 348
char *phoneNumber[11]; //309 - 319


if((from=fopen(f_from, "r"))==NULL){
printf("error to open file %s to read(%s).\n", f_from, strerror(errno));
return -1;
}
if((to=fopen(f_to, "w"))==NULL){
printf("error to open file %s to write(%s).\n", f_to, strerror(errno));
return -1;
} if((rbuf = (char *)malloc(record_length + 1)) == NULL){
printf("(rbuf)Cannot malloc: %s\n", strerror(errno));
        return -1;
    }
if((wbuf = (char *)malloc(record_length + 1)) == NULL){
printf("(wbuf)Cannot malloc: %s\n", strerror(errno));
        return -1;
    } while( n = fread(rbuf, 1, record_length, from) )
{
for(int i=0;i<n;i++)
{
if( rbuf[i]=='\0' )
rbuf[i]=' ';
}
for(int i=0;i< columnNumber; i++)
{
strncpy( recordlist[i].value, rbuf + recordlist[i].start_pos -1, recordlist[i].end_pos - recordlist[i].start_pos );
fprintf(to, "%s|", recordlist[i].value );
}
fseek(to, -1, SEEK_CUR);
fprintf(to, "\n");
}

fclose(from);
fclose(to);
return 0;
}
/**
int convert_file( char *f_from, char *f_to )
{
FILE *from, *to;
char rbuf[LENGTH+1];
char wbuf[LENGTH+1];
int n=0;
int i;
char numberID[4];//1 - 4
char orderNumber[14]; //7 - 20
char custName[51]; //29 - 79
char acctNumber[10]; //189 - 198
char address[51]; //257 - 309
char schedDate[9];//340 - 348
char phoneNumber[11]; //309 - 319


if((from=fopen(f_from, "r"))==NULL){
printf("error to open file %s to read(%s).\n", f_from, strerror(errno));
return -1;
}
if((to=fopen(f_to, "w"))==NULL){
printf("error to open file %s to read(%s).\n", f_to, strerror(errno));
return -1;
}
memset( rbuf, 0, sizeof(rbuf));
memset( numberID, 0, sizeof(numberID) );
memset( orderNumber, 0, sizeof(orderNumber) );
memset( custName, 0, sizeof(custName) );
memset( acctNumber, 0, sizeof(acctNumber) );
memset( schedDate, 0, sizeof(schedDate) );
memset( phoneNumber, 0, sizeof(phoneNumber) );
memset( address, 0, sizeof(address) ); while( n = fread(rbuf, 1, LENGTH, from) )
{
for(int i=0;i<n;i++)
{
if( rbuf[i]=='\0' )
rbuf[i]=' ';
}
strncpy( numberID, rbuf, 3 );
strncpy( orderNumber, rbuf + 6, 13 );
strncpy( custName, rbuf + 28, 50 );
strncpy( acctNumber, rbuf + 188, 9 );
strncpy( address, rbuf + 256, 50 );
strncpy( schedDate, rbuf + 339, 8 );
strncpy( phoneNumber, rbuf + 308, 10 ); fprintf(to, "%s|%s|%s|%s|%s|%s|%s\n", numberID, orderNumber, custName, acctNumber, schedDate, phoneNumber, address); memset( rbuf, 0, sizeof(rbuf));
memset( numberID, 0, sizeof(numberID) );
memset( orderNumber, 0, sizeof(orderNumber) );
memset( custName, 0, sizeof(custName) );
memset( acctNumber, 0, sizeof(acctNumber) );
memset( schedDate, 0, sizeof(schedDate) );
memset( phoneNumber, 0, sizeof(phoneNumber) );
}

fclose(from);
fclose(to);
return 0;
}
**/
int main(int argc, char **argv)
{
char filename_from[MAXLENGTH];
char filename_to[MAXLENGTH];
char configFile[MAXLENGTH];    if( argc<3 ){
     printf("missing some files.\n");
printf("Usage: %s config-filename source-filename [target-filename]\n", argv[0]);
     return -1;
}

strncpy( configFile, argv[1], strlen(argv[1]) );
configFile[strlen(argv[1])]=0;
strncpy( filename_from, argv[2], strlen(argv[2]) );
filename_from[strlen(argv[2])]=0;
sprintf(filename_to, "%s", "want.txt");
if( argv[3] ){
strncpy( filename_to, argv[3], strlen(argv[3]) );
filename_to[strlen(argv[3])]=0;
} if(init(configFile)<0)
return -1; new_convert_file( filename_from, filename_to );
    
return 0;
}谢谢