// Implementation of FileAccess
#include <stdio.h>
#include <string.h>
#include <time.h>
#ifndef GLOBAL_DECL
#include "global.h" // Global data types and variables
#endif
fileaccess::fileaccess(char* filename, char* type, config* setts) // constructor
{
settings = setts;
// Initialise semaphore with value of 1
file_sema = CreateSemaphore( 0, 1, 1, 0 );
fptr = fopen( filename, type );
if (fptr == NULL)
error = true;
else
error = false;
last_was_break = false;
last_was_space = false;
ignored_section = false;
always_flush = false;
}
fileaccess::~fileaccess()
{
if (!error)
fclose( fptr );
}
void fileaccess::write ( char* string )
{
int n;
if (!ignored_section)
{
if (!stricmp(string, "<BR>"))
{
if (!last_was_break)
for ( n=0; n<(signed)strlen( string ); n++ )
fputc ( string[n], fptr );
last_was_break = true;
}
else
{
for ( n=0; n<(signed)strlen( string ); n++ )
fputc ( string[n], fptr );
last_was_break = false;
}
if (always_flush)
fflush(fptr);
}
}
void fileaccess::write ( char* string, int width )
{
int n, offset;
offset = (signed)strlen( string ) - width;
for ( n=0; n<width; n++ )
{
if (offset<0)
fputc ( ' ', fptr );
else
fputc ( string[offset], fptr );
offset++;
}
if (always_flush)
fflush(fptr);
}
void fileaccess::write ( char character )
{
if (!ignored_section)
{
fputc ( character, fptr );
last_was_break = false;
if (always_flush)
fflush(fptr);
}
}
char fileaccess::read()
{
char read_char;
bool proceed = true;
if ((settings != NULL) && (settings -> cancel))
proceed = false;
if ((!feof(fptr)) && (proceed))
{
do
{
read_char = fgetc ( fptr );
if ((read_char == 10) || (read_char == 9))
read_char = ' ';
} while ( ((read_char == ' ') && (last_was_space)) || (read_char == 13));
if (read_char == ' ')
last_was_space = true;
else
last_was_space = false;
if (read_char != EOF)
return read_char;
else
return NULL;
}
else
return NULL;
}
void fileaccess::writetime()
{
struct tm* datetime;
time_t lTime;
char szTime[100];
char temp[6];
// Date and Time
time (&lTime);
datetime = localtime (&lTime);
itoa(DAY, temp, 10);
if (temp[1] == NULL) { temp[2] = NULL; temp[1] = temp[0]; temp[0] = '0'; }
strcpy(szTime, temp); strcat(szTime, "/");
itoa(MONTH, temp, 10);
if (temp[1] == NULL) { temp[2] = NULL; temp[1] = temp[0]; temp[0] = '0'; }
strcat(szTime, temp); strcat(szTime, "/");
strcat(szTime, itoa(YEAR + 2000, temp, 10)); strcat(szTime, " - ");
itoa(HOUR, temp, 10);
if (temp[1] == NULL) { temp[2] = NULL; temp[1] = temp[0]; temp[0] = '0'; }
strcat(szTime, temp); strcat(szTime, ":");
itoa(MIN, temp, 10);
if (temp[1] == NULL) { temp[2] = NULL; temp[1] = temp[0]; temp[0] = '0'; }
strcat(szTime, temp); strcat(szTime, ":");
itoa(SEC, temp, 10);
if (temp[1] == NULL) { temp[2] = NULL; temp[1] = temp[0]; temp[0] = '0'; }
strcat(szTime, temp);
write( szTime );
}
void fileaccess::writeheaders(char* url, char* description)
{
// Write HTML headers
write ( "<!-- " );
write ( url );
write ( " -->\n" );
write ( "<HTML>\n" );
write ( "<BODY>\n<TITLE>" );
write ( description );
write ( "</TITLE>\n<FONT SIZE=\"" );
write ( settings -> font_size );
write ( "\"><B>" );
write ( description );
write ( "\n<BR><BR>PAGE START</B><BR><BR>\n" );
}
void fileaccess::writefooterstart()
{
write ( "\n</FONT><FONT SIZE=\"" );
write ( settings -> font_size );
write ( "\">\n<BR><BR><B>END OF PAGE<BR><BR>" );
write ( "\n<A HREF=\"javascript:history.back(1)\">Go back to previous page</A><BR>" );
}
void fileaccess::writefooterend()
{
write ( "<A HREF=\"mailto:adam.hatherly@stud.umist.ac.uk?subject=" );
write ( "Page or link error\">Report errors or dead links</A>\n" );
write ( "</FONT></B>\n" );
write ( "</BODY>\n" );
write ( "</HTML>" );
}
void fileaccess::writeerrorfooters()
{
// Write error HTML footers
writefooterstart();
writefooterend();
}
void fileaccess::writefooters(char* url)
{
// Write normal HTML footers
writefooterstart();
write ( "\n<A HREF=\"" );
write ( url );
write ( "\">Go to the actual page on the web</A><BR>\n" );
writefooterend();
}
void fileaccess::alwaysflush()
{
always_flush = true;
}
void fileaccess::lock()
{
WaitForSingleObject(file_sema, INFINITE);
}
void fileaccess::unlock()
{
ReleaseSemaphore(file_sema, 1, 0);
}
syntax highlighted by Code2HTML, v. 0.8.11