C til Pascal/Delphi
Nogen som har lyst til at lave en hurtig oversættelse af nedenstående#include <dos.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <mem.h>
#include <ctype.h>
#define MAX_FILE_LEN 32768U
#define MAX_ITEM 48
#define MAX_ITEM_SIZE 256
typedef struct {unsigned char id;
unsigned char reserved[3];
unsigned int version;
char fname[4];
unsigned int flen;
unsigned int idx1_len;
unsigned int idx2_len;} ProtocolHeader;
typedef struct {unsigned char par[MAX_ITEM_SIZE];} ItemPar;
char file[80];
char *EXT = ".TXT";
char *XEXT = ".BIN";
char *f99_file = "F099";
char *f98_file = "F098";
char *f97_file = "F097";
char *file_p;
FILE *file_handle;
FILE *f99_handle, *f98_handle, *f97_handle;
char filebuf[MAX_FILE_LEN];
/* MACROS */
/**********************************************************************
SKIP_WHITESPACE -- move a character pointer past whitespace char
***********************************************************************/
#define SKIP_WHITESPACE(char_ptr) \
while ((*(char_ptr) <= ' ') && (*(char_ptr) != '\0')) \
(char_ptr)++;
/**********************************************************************
SKIP_DIGIT -- move a character pointer past digit char
***********************************************************************/
#define SKIP_DIGIT(char_ptr) \
while ((*(char_ptr) >= '0') && (*(char_ptr) <= '9')) \
(char_ptr)++;
unsigned int AscInt(char *asc_ptr, int count)
{
unsigned int num = 0;
while (count--) {
num = num*10 + (*asc_ptr - '0');
asc_ptr++;
}
return num;
}
void Toupper(char *str)
{
while (*str) {
*str = toupper(*str);
str++;
}
}
int GetNumber(char *num_str, unsigned int *num)
{
unsigned int n;
char *s = num_str;
SKIP_DIGIT(num_str);
n = (unsigned int)(num_str - s);
if (n && n <= 5) {
*num = AscInt(s,n);
return 1;
}
return -1;
}
char *GetNum(char *num_str, unsigned int *num)
{
unsigned int n;
char *s = num_str;
SKIP_DIGIT(num_str);
n = (unsigned int)(num_str - s);
if (n && n <= 5) {
*num = AscInt(s,n);
return num_str;
}
return NULL;
}
int Convert(char *buf, unsigned int len)
{
unsigned int grp_num, par, i, i_size, par_size;
ProtocolHeader header;
ItemPar item[MAX_ITEM];
unsigned char temp[MAX_ITEM*MAX_ITEM_SIZE+sizeof(ProtocolHeader)];
char *temp_p;
char *buf_copy = buf;
if (!len) return -1;
*(buf+len) = '\0';
memset(&header,0,sizeof(ProtocolHeader));
/* first line - number of groups */
while (*buf && (*buf < '0' || *buf > '9')) {
if (*buf++ == '\n') return -1;
}
if ((buf = GetNum(buf,&grp_num)) == NULL) return -1;
while (*buf != '\n' && *buf++);
/* F99 */
memset(item,0,sizeof(item));
i = 0;
i_size = 0;
if ((buf = strstr(buf,"F99N")) == NULL) return -1;
while (1) {
if ((buf = strchr(buf,'(')) == NULL) return -1;
if ((buf = GetNum(++buf,&par_size)) == NULL) return -1;
if ((buf = strchr(buf,'\n')) == NULL) return -1;
SKIP_WHITESPACE(buf);
if (par_size > 2) {
while (*buf && *buf != ' ' && *buf != '\n' && *buf != '\r') {
item[i].par[i_size++] = *buf++;
par_size--;
}
i_size += par_size;
}
else {
if ((buf = GetNum(buf,&par)) == NULL) return -1;
if (par_size == 1) item[i].par[i_size++] = par;
else {
item[i].par[i_size++] = par;
item[i].par[i_size++] = (par & 0xff00) >> 8;
}
}
if ((buf = strstr(buf,"F99")) == NULL) {
break;
}
}
memcpy(header.fname,f99_file,4);
i++;
header.flen = i * i_size;
header.idx1_len = i;
header.idx2_len = i_size;
memcpy(temp,&header,sizeof(ProtocolHeader));
memcpy(temp+sizeof(ProtocolHeader),&item[0].par[0],i_size);
fwrite(temp,header.flen+sizeof(ProtocolHeader),1,f99_handle);
/* F98 */
if ((buf = strstr(buf_copy,"F98")) == NULL) {
return -2;
}
memset(item,0,sizeof(item));
i_size = 0;
while (1) {
if ((buf = strchr(buf,'(')) == NULL) return -2;
if ((buf = GetNum(++buf,&par_size)) == NULL) return -2;
if ((buf = strchr(buf,'\n')) == NULL) return -2;
for (i=0;i<grp_num;i++) {
SKIP_WHITESPACE(buf);
while ((*buf < '0' || *buf > '9') && *buf++) {
if (*buf == '\n') break;
}
if ((buf = GetNum(buf,&par)) == NULL) return -2;
if (par_size == 1) item[i].par[i_size] = par;
else {
item[i].par[i_size] = par;
item[i].par[i_size+1] = (par & 0xff00) >> 8;
}
}
i_size += par_size;
if ((buf = strstr(buf,"F98")) == NULL) {
break;
}
}
memcpy(header.fname,f98_file,4);
header.flen = grp_num * i_size;
header.idx1_len = grp_num;
header.idx2_len = i_size;
temp_p = temp;
memcpy(temp_p,&header,sizeof(ProtocolHeader));
temp_p += sizeof(ProtocolHeader);
for (i=0;i<grp_num;i++) {
memcpy(temp_p,&item[i].par[0],i_size);
temp_p += i_size;
}
fwrite(temp,header.flen+sizeof(ProtocolHeader),1,f98_handle);
/* F97 */
if ((buf = strstr(buf_copy,"F97")) == NULL) {
return -3;
}
memset(item,0,sizeof(item));
i_size = 0;
while (1) {
if ((buf = strchr(buf,'G')) == NULL) break;
while ((*buf < '0' || *buf > '9') && *buf++);
if ((buf = GetNum(buf,&i)) == NULL) return -3;
if (i-- > grp_num) break;
if ((buf = strchr(buf,'(')) == NULL) return -3;
if ((buf = GetNum(++buf,&par_size)) == NULL) return -3;
if ((buf = strchr(buf,'\n')) == NULL) return -3;
i_size = 0;
while (i_size < 40) {
SKIP_WHITESPACE(buf);
while ((*buf < '0' || *buf > '9') && *buf++) {
if (*buf == '\n') break;
}
if ((buf = GetNum(buf,&par)) == NULL) return -3;
item[i].par[i_size++] = par;
if ((buf = strchr(buf,'-')) == NULL) return -3;
buf++;
if ((buf = GetNum(buf,&par)) == NULL) return -3;
item[i].par[i_size++] = par;
if (*buf == '\n') break;
}
if ((buf = strstr(buf,"F97")) == NULL) {
break;
}
}
i_size = 40;
memcpy(header.fname,f97_file,4);
header.flen = grp_num * i_size;
header.idx1_len = grp_num;
header.idx2_len = i_size;
temp_p = temp;
memcpy(temp_p,&header,sizeof(ProtocolHeader));
temp_p += sizeof(ProtocolHeader);
for (i=0;i<grp_num;i++) {
memcpy(temp_p,&item[i].par[0],i_size);
temp_p += i_size;
}
fwrite(temp,header.flen+sizeof(ProtocolHeader),1,f97_handle);
return 1;
}
void main(int argc, char * argv[])
{
int res;
if (argc != 2) {
printf("A file name must be specified\n");
return;
}
strcpy(file,argv[1]);
if (strchr(file,'.') == NULL) strcat(file,EXT);
Toupper(file);
if ((file_handle = fopen(file,"rt")) == NULL) {
printf("Cannot open file %s\n",file);
return;
}
strcpy(file,f99_file);
strcat(file,XEXT);
if ((f99_handle = fopen(file,"wb")) == NULL) {
printf("Cannot create or open file %s\n",file);
return;
}
strcpy(file,f98_file);
strcat(file,XEXT);
if ((f98_handle = fopen(file,"wb")) == NULL) {
printf("Cannot create or open file %s\n",file);
return;
}
strcpy(file,f97_file);
strcat(file,XEXT);
if ((f97_handle = fopen(file,"wb")) == NULL) {
printf("Cannot create or open file %s\n",file);
return;
}
if ((res = Convert(filebuf,fread(filebuf,1,MAX_FILE_LEN,file_handle))) < 0) {
if (res == -1) file_p = f99_file;
else if (res == -2) file_p = f98_file;
else file_p = f97_file;
strcpy(file,argv[1]);
if (strchr(file,'.') == NULL) strcat(file,EXT);
printf("\n\rSyntax error inside file %s (%s)",file,file_p);
}
fclose(f97_handle);
fclose(f98_handle);
fclose(f99_handle);
fclose(file_handle);
}