123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- //---------------------------------------------------------------------------
- //
- // gamelog.cpp - This file contains the class functions for GameLog
- //
- //---------------------------------------------------------------------------//
- // Copyright (C) Microsoft Corporation. All rights reserved. //
- //===========================================================================//
- //---------------------------------------------------------------------------
- // Include files
- #ifndef FILE_H
- #include <file.h>
- #endif
- #ifndef GAMELOG_H
- #include "gamelog.h"
- #endif
- #ifndef HEAP_H
- #include <heap.h>
- #endif
- #include <malloc.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <gameos.hpp>
- bool isSetup = false;
- //*****************************************************************************
- GameLogPtr GameLog::files[MAX_GAMELOGS] = {NULL, NULL, NULL, NULL, NULL, NULL};
- void* GameLog::operator new (size_t mySize) {
- void* result = NULL;
-
- result = gos_Malloc(mySize);
-
- return(result);
- }
- //---------------------------------------------------------------------------
- void GameLog::operator delete (void* us) {
- gos_Free(us);
- }
- //---------------------------------------------------------------------------
- void GameLog::dump (void) {
- //----------------
- // Dump to file...
- for (long i = 0; i < numLines; i++)
- filePtr->writeString(lines[i]);
- numLines = 0;
- }
- //---------------------------------------------------------------------------
- void GameLog::close (void) {
- if (filePtr && inUse) {
- dump();
- char s[512];
- sprintf(s, "\nNum Total Lines = %d\n", totalLines);
- filePtr->writeString(s);
- filePtr->close();
- inUse = false;
- numLines = 0;
- totalLines = 0;
- }
- }
- //---------------------------------------------------------------------------
- void GameLog::destroy (void) {
- close();
- }
- //---------------------------------------------------------------------------
- long GameLog::open (char* fileName) {
- numLines = 0;
- totalLines = 0;
- if (filePtr->create(fileName) != NO_ERR)
- return(-1);
- inUse = true;
- return(0);
- }
- //---------------------------------------------------------------------------
- void GameLog::write (char* s) {
- static char buffer[MAX_GAMELOG_LINELEN];
-
- if (numLines == MAX_GAMELOG_LINES)
- dump();
-
- if (strlen(s) > (MAX_GAMELOG_LINELEN - 1))
- s[MAX_GAMELOG_LINELEN - 1] = NULL;
- sprintf(buffer, "%s\n", s);
- strncpy(lines[numLines], buffer, MAX_GAMELOG_LINELEN - 1);
- numLines++;
- totalLines++;
- }
- //---------------------------------------------------------------------------
- GameLog* GameLog::getNewFile (void) {
- if (!isSetup)
- setup();
- long fileHandle = -1;
- for (long i = 0; i < MAX_GAMELOGS; i++)
- if (!files[i]->inUse) {
- fileHandle = i;
- break;
- }
- return(files[i]);
- }
- //---------------------------------------------------------------------------
- void GameLog::setup (void) {
- if (isSetup)
- return;
- isSetup = true;
- for (long i = 0; i < MAX_GAMELOGS; i++) {
- files[i] = new GameLog;
- files[i]->init();
- files[i]->handle = i;
- files[i]->inUse = false;
- files[i]->filePtr = new File;
- gosASSERT(files[i]->filePtr != NULL);
- }
- }
- //---------------------------------------------------------------------------
- void GameLog::cleanup (void) {
- if (!isSetup)
- return;
- for (long i = 0; i < MAX_GAMELOGS; i++)
- {
- if (files[i] && files[i]->inUse)
- {
- files[i]->close();
- files[i]->filePtr->close();
- delete files[i]->filePtr;
- files[i]->filePtr = NULL;
- delete files[i];
- files[i] = NULL;
- }
- }
- }
|