Code:
/* RECEIVE CONSOLE INPUT AND CREATE A MOCK REPRESENTATION MATRIX */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define EQL == /* Equal To */
#define NEQL != /* Not Equal To */
#define LOEQL <= /* Less Than or Equal To */
#define GOEQL >= /* Greater Than or Equal To */
typedef unsigned int uint;
typedef signed int sint;
#define e 2.718281828
#define ROWS 5
#define COLS 5
#define MAX_INPUT_LEN 254
void deallocate2D(double **array, int nrows) {
/* deallocate each row */
int i;
for(i = 0; i < nrows; i++) {
free(array[i]);
array[i]=NULL;
}
/* deallocate array of pointers */
free(array);
array=NULL;
}
void deallocate2C(char **array, int nrows) {
/* deallocate each row */
int i;
for(i = 0; i < nrows; i++) {
free(array[i]);
array[i]=NULL;
}
/* deallocate array of pointers */
free(array);
array=NULL;
}
void quitapp(uint rowcnt,double **mem2Dblock_a,char **mem2Dblock_b)
{
deallocate2D(mem2Dblock_a,rowcnt);
deallocate2C(mem2Dblock_b,rowcnt);
fcloseall();
fprintf(stderr,"\nCleaned up.\nExiting.\nBye!");
exit(0);
}
void restartapp(char *appexec,uint rowcnt,double **mem2Dblock_a,char **mem2Dblock_b)
{
deallocate2D(mem2Dblock_a,rowcnt);
deallocate2C(mem2Dblock_b,rowcnt);
fcloseall();
system(appexec);
exit(0);
}
int main(int argc,char *argv[])
{
static uint i=0,j=0,k=0,m=0,n=0,lastknown_rows=0,lastknown_cols=0,lastknown_dims=0,outputlen=0,ramgrowth=0;
size_t inputlen;
char **kylst=malloc(sizeof(char*)*1);
double **kbmtx=malloc(sizeof(double*)*1);
char buff[MAX_INPUT_LEN];
char *dimfilenm = "~dim.tmp";
char *kyfilenm = "~ky.lst";
char *kbfilenm = "~kb.bin";
char *outfilenm = "~out.tmp";
FILE *kbfile=NULL;
FILE *dimfile=NULL;
FILE *kyfile=NULL;
FILE *outfile=NULL;
if(argc==2)
{
if(((dimfile=fopen(dimfilenm, "r")) NEQL NULL) && (lastknown_dims=fscanf(dimfile,"%d %d",&lastknown_rows,&lastknown_cols))EQL 2)
{
m=lastknown_rows;
n=lastknown_cols;
printf("/* Found a valid DIM file showing %dx%d to be the last known dimensions */\n",m,n);
printf("/* Locate and try reading the stored KY list file */\n");
if((kyfile = fopen(kyfilenm, "r")) EQL NULL)
{
m=0;
n=0;
if((kyfile = fopen(kyfilenm, "w")) NEQL NULL)
{
printf("/* Created a new KY list file */\n");
//restartapp(argv[0],m,kbmtx,kylst);
if((kbfile = fopen(kbfilenm, "wb")) NEQL NULL)
{
printf("/* Created a new KB list file */\n");
//restartapp(argv[0],m,kbmtx,kylst);
printf("/* Please restart %s to proceed */\n",argv[0]);
fprintf(dimfile,"%d %d\n",m,n);
quitapp(m,kbmtx,kylst);
} else {
fprintf(stderr, "/* Error creating a new KB list file */\n");
quitapp(m,kbmtx,kylst);
}
} else { /* if((kyfile = fopen(kyfilenm, "w")) NEQL NULL) */
fprintf(stderr, "/* Error creating a new KY list file */\n");
quitapp(m,kbmtx,kylst);
}
fclose(kbfile);
} else { /* if((kyfile = fopen(kyfilenm, "r")) EQL NULL) */
if(m>0&&n>0)
{
m=lastknown_rows;
n=lastknown_cols;
printf("/* Resize 'char** kylst' to accommodate the last known rows : %d */\n",m);
kylst=(char**)realloc(kylst,(size_t)(sizeof(char*)*(m)));
if(kylst EQL NULL)quitapp(m,kbmtx,kylst);
k=0;
for(i=0;i<(m);i++)
{
k+=fscanf(kyfile,"%s",buff);
if(i > k) {i--; break;}
if((kylst[i] = (char *)malloc((size_t)((strlen(buff)+1)*sizeof(char)))) EQL NULL)quitapp(m,kbmtx,kylst);
strcpy(kylst[i],buff);
}
printf("/* Locate and try reading the stored KB data file */\n");
if((kbfile = fopen(kbfilenm, "rb")) NEQL NULL)
{
printf("/* Resize 'double **kbmtx' to accommodate the last known dimensions : %dx%d */\n",m,n);
kbmtx=(double**)realloc(kbmtx,sizeof(double*)*m);
if(kbmtx==NULL)quitapp(m,kbmtx,kylst);
printf("/* Initialize KB with zeros. */\n");
for(i = 0; i < m; i++)
{
kbmtx[i]=(double*)malloc(sizeof(double)*n);
if(kbmtx[i]==NULL) quitapp(m,kbmtx,kylst);
for(j = 0; j < n; j++)
{
kbmtx[i][j]=0.0;
printf("%lg\t",kbmtx[i][j]);
}
printf("\n");
}
printf("/* Load the binary file data into KB, row by row. */\n");
for (i = 0; i < m; i++)
{
if (fread(kbmtx[i], sizeof(double), n, kbfile) != n)
{
fprintf(stderr, "Error writing to file.");
quitapp(m,kbmtx,kylst);
}
}
printf("/* Close the binary file */\n");
fclose(kbfile);
printf("/* Now print KB to the screen. */\n");
for (i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
printf("%lg\t",kbmtx[i][j]);
}
printf("\n");
}
printf("/* Show KB RAM addresses on the screen. */\n");
for (i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
printf("%d\t",&kbmtx[i][j]);
}
printf("\n");
}
} else { /* if((kbfile = fopen(kbfilenm, "rb")) NEQL NULL) */
fprintf(stderr, "/* Cannot read the KB data file */\n");
quitapp(m,kbmtx,kylst);
}
}
fclose(kyfile);
}
fclose(dimfile);
puts("Type A New String and Press <ENTER> To Store in RAM and Binary File, ");
for(m=lastknown_rows,n=lastknown_cols;;m++,n++)
{
if((dimfile = fopen(dimfilenm, "w")) NEQL NULL)
{
gets(buff);
inputlen=(strlen(buff)+1);
if(inputlen<MAX_INPUT_LEN)
{
if(strcmp(buff,"Q!") EQL 0)
{
quitapp(m,kbmtx,kylst);
}
if((kyfile = fopen(kyfilenm, "a")) NEQL NULL)
{
kylst=(char**)realloc(kylst,(size_t)(sizeof(char*)*(m+1)));
if(kylst EQL NULL)quitapp(m,kbmtx,kylst);
kylst[m]=(char*)malloc((size_t)(sizeof(char)*inputlen));
if(kylst[m] EQL NULL)quitapp(m,kbmtx,kylst);
strcpy(kylst[m],buff);
if((kbmtx=(double **)realloc(kbmtx,(size_t)((m+1)*sizeof(double*)))) NEQL NULL)
{
if((kbmtx[m]=(double *)malloc((size_t)((n+1)*sizeof(double)))) EQL NULL)quitapp(m,kbmtx,kylst);
for(i=0;i<(m+1);i++)
{
for(j=0;j<(n+1);j++)
{
kbmtx[i][j]=0.0;
}
}
} else quitapp(m,kbmtx,kylst);
printf("Committing to KY list file\n");
fprintf(kyfile,"%s\n",kylst[m]);
printf("/* Opening the KB binary data file for writing */\n");
if((kbfile = fopen(kbfilenm, "wb")) NEQL NULL)
{
for(i=0;i<(m+1);i++)
{
if((fwrite(kbmtx[i],sizeof(double),(n+1),kbfile))!=(n+1))quitapp(m,kbmtx,kylst);
}
fclose(kbfile);
} else {
fprintf(stderr, "Error opening KB file for updates.");
quitapp(m,kbmtx,kylst);
}
fclose(kyfile);
fprintf(dimfile,"%d %d\n",m+1,n+1);
//lastknown_dims=flushall();
//printf("/* Written %d updates to DIM file */\n",lastknown_dims);
printf("/* Show KB RAM addresses on the screen. */\n");
for (i = 0; i < (m+1); i++)
{
for(j = 0; j < (n+1); j++)
{
printf("%lg\t",kbmtx[i][j]);
}
printf("\n");
}
puts("Type and Store Another String Or Type Q! and <ENTER> To Quit: ");
} else printf("/* Oops! Cannot open KY list file for updates */\n");
} else { /* if(buff<MAX_INPUT_LEN) */
printf("/* Oops! Your input is too large. Keep it below 254 characters */\n");
}
} else printf("/* Oops! Cannot open DIM file for updates */\n");
fclose(dimfile);
}
} else { /* if(((dimfile=fopen(dimfilenm, "r+")) NEQL NULL) && (lastknown_dims=fscanf(dimfile,"%d %d",&lastknown_rows,&lastknown_cols))EQL 2) */
fprintf(stderr, "/* Cannot read or verify integrity of the DIM file */\n");
if((dimfile = fopen(dimfilenm, "w")) NEQL NULL)
{
printf("/* Created a new DIM file */\n");
fprintf(dimfile,"%d %d\n",0,0);
printf("/* Please restart %s to proceed */\n",argv[0]);
quitapp(m,kbmtx,kylst);
} else {
fprintf(stderr, "/* Error creating a new DIM file */\n");
quitapp(m,kbmtx,kylst);
}
}
quitapp(m,kbmtx,kylst);
}
return(0);
}