gamelog.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //---------------------------------------------------------------------------
  2. //
  3. // gamelog.cpp - This file contains the class functions for GameLog
  4. //
  5. //---------------------------------------------------------------------------//
  6. // Copyright (C) Microsoft Corporation. All rights reserved. //
  7. //===========================================================================//
  8. //---------------------------------------------------------------------------
  9. // Include files
  10. #ifndef FILE_H
  11. #include <file.h>
  12. #endif
  13. #ifndef GAMELOG_H
  14. #include "gamelog.h"
  15. #endif
  16. #ifndef HEAP_H
  17. #include <heap.h>
  18. #endif
  19. #include <malloc.h>
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <gameos.hpp>
  24. bool isSetup = false;
  25. //*****************************************************************************
  26. GameLogPtr GameLog::files[MAX_GAMELOGS] = {NULL, NULL, NULL, NULL, NULL, NULL};
  27. void* GameLog::operator new (size_t mySize) {
  28. void* result = NULL;
  29. result = gos_Malloc(mySize);
  30. return(result);
  31. }
  32. //---------------------------------------------------------------------------
  33. void GameLog::operator delete (void* us) {
  34. gos_Free(us);
  35. }
  36. //---------------------------------------------------------------------------
  37. void GameLog::dump (void) {
  38. //----------------
  39. // Dump to file...
  40. for (long i = 0; i < numLines; i++)
  41. filePtr->writeString(lines[i]);
  42. numLines = 0;
  43. }
  44. //---------------------------------------------------------------------------
  45. void GameLog::close (void) {
  46. if (filePtr && inUse) {
  47. dump();
  48. char s[512];
  49. sprintf(s, "\nNum Total Lines = %d\n", totalLines);
  50. filePtr->writeString(s);
  51. filePtr->close();
  52. inUse = false;
  53. numLines = 0;
  54. totalLines = 0;
  55. }
  56. }
  57. //---------------------------------------------------------------------------
  58. void GameLog::destroy (void) {
  59. close();
  60. }
  61. //---------------------------------------------------------------------------
  62. long GameLog::open (char* fileName) {
  63. numLines = 0;
  64. totalLines = 0;
  65. if (filePtr->create(fileName) != NO_ERR)
  66. return(-1);
  67. inUse = true;
  68. return(0);
  69. }
  70. //---------------------------------------------------------------------------
  71. void GameLog::write (char* s) {
  72. static char buffer[MAX_GAMELOG_LINELEN];
  73. if (numLines == MAX_GAMELOG_LINES)
  74. dump();
  75. if (strlen(s) > (MAX_GAMELOG_LINELEN - 1))
  76. s[MAX_GAMELOG_LINELEN - 1] = NULL;
  77. sprintf(buffer, "%s\n", s);
  78. strncpy(lines[numLines], buffer, MAX_GAMELOG_LINELEN - 1);
  79. numLines++;
  80. totalLines++;
  81. }
  82. //---------------------------------------------------------------------------
  83. GameLog* GameLog::getNewFile (void) {
  84. if (!isSetup)
  85. setup();
  86. long fileHandle = -1;
  87. for (long i = 0; i < MAX_GAMELOGS; i++)
  88. if (!files[i]->inUse) {
  89. fileHandle = i;
  90. break;
  91. }
  92. return(files[i]);
  93. }
  94. //---------------------------------------------------------------------------
  95. void GameLog::setup (void) {
  96. if (isSetup)
  97. return;
  98. isSetup = true;
  99. for (long i = 0; i < MAX_GAMELOGS; i++) {
  100. files[i] = new GameLog;
  101. files[i]->init();
  102. files[i]->handle = i;
  103. files[i]->inUse = false;
  104. files[i]->filePtr = new File;
  105. gosASSERT(files[i]->filePtr != NULL);
  106. }
  107. }
  108. //---------------------------------------------------------------------------
  109. void GameLog::cleanup (void) {
  110. if (!isSetup)
  111. return;
  112. for (long i = 0; i < MAX_GAMELOGS; i++)
  113. {
  114. if (files[i] && files[i]->inUse)
  115. {
  116. files[i]->close();
  117. files[i]->filePtr->close();
  118. delete files[i]->filePtr;
  119. files[i]->filePtr = NULL;
  120. delete files[i];
  121. files[i] = NULL;
  122. }
  123. }
  124. }