Same code review request, to not learn anything bad. Again, Rexx ported to C. The code works, and creates a Metastock PRN file after importing individual HTML files.
The business end is in the large for-loop.
Please ignore the error texts, I know I'm reusing the date variable, I'm using DosForceDelete intentionally, and as far as I know the code works, albeit translation may have introduced an error.
Main data formats are £123,45, 123.456 or e.g. $1.2345. Data doesn't have to be checked. I don't know if I have processed Rexx' PARSEs correctly, and it's the first time I've used pointers this way without copying existing code.
/* Import.C */
#define INCL_DOSDATETIME
#define INCL_DOSFILEMGR
#include <ctype.h>
#include <io.h>
#include <os2.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFLEN 8192
#define INDEX1 "PSI20\0"
#define MAXRECORDS 768
#define LINELEN 128
typedef struct _RECORDS {char symbol[11];} RECORDS;
int main(void)
{
BOOL found=0;
char *all,sourcename[CCHMAXPATH],day,*data,date[LINELEN],targetname[LINELEN],dummy[2],high[16],low[16],month,open[16],*orgdata,*skipped,prefix[16],line[LINELEN],suffix[16],volume[16];
DATETIME dt;
FILE *source,*target;
RECORDS *records;
ULONG date2,today;
USHORT done=0,i,year,counter=0,counter2,counter3=0;
DosGetDateTime(&dt);
today=dt.year*10000+dt.month*100+dt.day;
if (dt.hours*60+dt.minutes<1105)
{
sprintf(sourcename,"%08u.001",today);
if (!access(sourcename,00))
{
printf("\Error\n");
return 1;
}
dt.day--;
if (!dt.day)
{
dt.day=31;
dt.month--;
if (!dt.month)
{
dt.month=12;
dt.year--;
}
if (dt.month==4||dt.month==6||dt.month==9||dt.month==11)
dt.day--;
else if (dt.month==2)
{
dt.day=28;
if (!dt.year%4&&dt.year%100)
dt.day++;
}
}
today=dt.year*10000+dt.month*100+dt.day;
sprintf(sourcename,"%08u.001",today);
if (-1==access(sourcename,00))
{
printf("\nError\n");
return 1;
}
}
if (NULL==(records=calloc(MAXRECORDS,sizeof(RECORDS))))
{
printf("\nError\n");
return 1;
}
sprintf(sourcename,"AANDELEN.LST");
if ((source=fopen(sourcename,"r"))==NULL)
{
printf("\nError\n",sourcename);
free(records);
return 1;
}
while (NULL!=fgets(line,LINELEN,source))
{
sscanf(line,"%10c",records[0].symbol);
records[0].symbol[10]=0;
while (strlen(records[0].symbol)&&isblank(records[0].symbol[strlen(records[0].symbol)-1]))
records[0].symbol[strlen(records[0].symbol)-1]=0;
if (strlen(records[0].symbol))
strcpy(records[++counter].symbol,records[0].symbol);
}
fclose(source);
if (!counter)
{
printf("\nError\n",sourcename);
free(records);
return 1;
}
for (i=1;i<=counter;i++)
if (!strcmp(records[i].symbol,INDEX1))
{
found=1;
i=counter;
}
if (!found)
{
printf("\nError\n",INDEX1,sourcename);
free(records);
return 1;
}
strcpy(sourcename,INDEX1);
if (-1==access(sourcename,00))
{
printf("\nError\n",sourcename);
free(records);
return 1;
}
if (NULL==(skipped=calloc(BUFLEN,sizeof(char))))
{
printf("\nError\n");
free(records);
return 1;
}
if (NULL==(all=calloc(BUFLEN,sizeof(char))))
{
printf("\nError\n");
free(records);
free(skipped);
return 1;
}
if (NULL==(data=calloc(BUFLEN,sizeof(char))))
{
printf("\nError\n");
free(all);
free(records);
free(skipped);
return 1;
}
orgdata=data;
sprintf(targetname,"%04u.PRN",dt.year);
if (-1==access(targetname,00))
found=0;
if ((target=fopen(targetname,"a"))==NULL)
{
printf("\nError\n",targetname);
free(all);
free(data);
free(records);
free(skipped);
return 1;
}
if (!found)
{
fprintf(target,"<TICKER>,<PER>,<DTYYYYMMDD>,<OPEN>,<HIGH>,<LOW>,<CLOSE>,<VOL>,<OI>\n");
printf("\nFile %s created.\n\n",targetname);
}
for (i=1;i<=counter;i++)
{
if ((source=fopen(records[i].symbol,"rb"))==NULL)
{
printf("\nError\n");
fclose(target);
free(all);
free(data);
free(records);
free(skipped);
return 1;
}
fread(all,sizeof(char),BUFLEN,source);
fclose(source);
sprintf(date,"id=\"datetimeLastvalue\">\0");
data=strstr(all,date);
data+=strlen(date);
day=(*data-'0')*10;
data++;
day+=*data-'0';
data+=2;
month=(*data-'0')*10;
data++;
month+=*data-'0';
data+=2;
year=(*data-'0')*1000;
data++;
year+=(*data-'0')*100;
data++;
year+=(*data-'0')*10;
data++;
year+=*data-'0';
date2=year*10000+month*100+day;
if (date2!=today)
printf("No data found: %s\n",records[i].symbol);
else
{
sprintf(date,"id=\"lastPriceint\">\0");
if (NULL!=(data=strstr(all,date)))
{
data+=strlen(date);
counter2=0;
while (isdigit(*data))
{
prefix[counter2++]=*data;
*data++;
}
prefix[counter2]=0;
counter2=0;
sprintf(date,"id=\"lastPricefract\">\0");
data=strstr(all,date);
data+=strlen(date);
while (isdigit(*data))
{
suffix[counter2++]=*data;
*data++;
}
suffix[counter2]=0;
counter2=0;
sprintf(date,"id=\"todayVolumevalue\">\0");
if (NULL!=(data=strstr(all,date)))
{
data+=strlen(date);
while (isdigit(*data)||*data=='.')
{
if (isdigit(*data))
volume[counter2++]=*data;
*data++;
}
volume[counter2]=0;
}
else
strcpy(volume,"0\0");
counter2=0;
sprintf(date,">Open<\0");
data=strstr(all,date);
data+=strlen(date);
sprintf(date,">\0");
data=strstr(data,date);
data+=strlen(date);
data=strstr(data,date);
data+=strlen(date);
if (*data=='$')
data++;
else if (*data=='&')
{
sprintf(date,";\0");
data=strstr(data,date);
data+=strlen(date);
}
while (isdigit(*data)||*data=='.'||*data==',')
{
if (isdigit(*data))
open[counter2++]=*data;
else if (*data==',')
open[counter2++]='.';
*data++;
}
open[counter2]=0;
counter2=0;
sprintf(date,"id=\"highPricevalue\">\0");
data=strstr(all,date);
data+=strlen(date);
while (isdigit(*data)||*data=='.'||*data==',')
{
if (isdigit(*data))
high[counter2++]=*data;
else if (*data==',')
high[counter2++]='.';
*data++;
}
high[counter2]=0;
counter2=0;
sprintf(date,"id=\"lowPricevalue\">\0");
data=strstr(all,date);
data+=strlen(date);
while (isdigit(*data)||*data=='.'||*data==',')
{
if (isdigit(*data))
low[counter2++]=*data;
else if (*data==',')
low[counter2++]='.';
*data++;
}
low[counter2]=0;
if (strlen(prefix)&&strlen(suffix))
{
done++;
fprintf(target,"%s,D,%8u,%s,%s,%s,%s.%s,%s\n",records[i].symbol,date2,open,high,low,prefix,suffix,volume);
}
else
{
counter3++;
if (!strlen(skipped))
strcpy(skipped,records[i].symbol);
else
{
strcat(skipped," \0");
strcat(skipped,records[i].symbol);
}
}
}
else
{
counter3++;
if (!strlen(skipped))
strcpy(skipped,records[i].symbol);
else
{
strcat(skipped," \0");
strcat(skipped,records[i].symbol);
}
}
}
DosForceDelete(records[i].symbol);
}
fclose(target);
free(all);
free(orgdata);
free(records);
free(skipped);
printf("Import: %u\n",done);
if (strlen(skipped))
printf("Skipped: %s (%u)\n",skipped,counter3);
return 0;
}