//===============================================================
// Projekt   : Undervisningsmateriale
// Enhed     : Sourcefil
// Fil       : logfile.cpp
// Forfatter : BA
//
// Beskrivelse:
//
// Versionshistorie:
//   05.04.02 / BA - Oprindelig version
//   01.05.02 / BA - LogData copy-constructor og operator= 
//     medtaget. Diverse fejl i class Log rettede.
//   23.10.02 / BA - Mange ændringer, en helt ny version.
//   31.03.03 / BA - et par smårettelser
//===============================================================

#include <cstdio>
#include <fstream>
#include <windows.h>
#include "rttarget.h"
#include "logfile.hpp"
using namespace std;

//---------------------------------------------------------------
// class LogData

LogData::LogData()
{
RTCMOSReadTime(&m_tid);
m_haendelse = new char[1];
strcpy(m_haendelse, "");
}


LogData::LogData(char *haendelse)
{
RTCMOSReadTime(&m_tid);
m_haendelse = new char[strlen(haendelse) + 1];
strcpy(m_haendelse, haendelse);
}


LogData::LogData(const LogData &ld)
{
m_haendelse = new char[strlen(ld.m_haendelse) + 1];
strcpy(m_haendelse, ld.m_haendelse);
m_tid = ld.m_tid;
}


LogData::~LogData()
{
delete[] m_haendelse;
}


LogData &LogData::operator=(const LogData &ld)
{
if (this != &ld)
  {
  delete[] m_haendelse;
  m_haendelse = new char[strlen(ld.m_haendelse) + 1];
  strcpy(m_haendelse, ld.m_haendelse);
  m_tid = ld.m_tid;
  }
return *this;
}


char *LogData::haendelse(char *hStr)
{
strcpy(hStr, m_haendelse);
return hStr;
}

int LogData::hStrLaengde()
{
return strlen(m_haendelse) + 1;
}

char *LogData::tid(char *tStr)
{
sprintf(tStr, "%2.2d.%2.2d.%4.4d-%2.2d:%2.2d:%2.2d", m_tid.wDay, m_tid.wMonth,
  m_tid.wYear, m_tid.wHour, m_tid.wMinute, m_tid.wSecond);
return tStr;
}

int LogData::tStrLaengde()
{
char s[50];
sprintf(s, "%2.2d.%2.2d.%4.4d-%2.2d:%2.2d:%2.2d", m_tid.wDay, m_tid.wMonth,
  m_tid.wYear, m_tid.wHour, m_tid.wMinute, m_tid.wSecond);
return strlen(s) + 1; 
}

ostream &operator<<(ostream &udfil, const LogData &ld)
{
udfil 
  << ld.m_haendelse << endl 
  << ld.m_tid.wYear << endl
  << ld.m_tid.wMonth << endl
  << ld.m_tid.wDayOfWeek << endl 
  << ld.m_tid.wDay << endl
  << ld.m_tid.wHour << endl
  << ld.m_tid.wMinute << endl 
  << ld.m_tid.wSecond << endl
  << ld.m_tid.wMilliseconds << endl;
return udfil;
}


istream &operator>>(istream &indfil, LogData &ld)
{
const int n = 100;
char hStr[n];
indfil.getline(hStr, n - 1);
delete[] ld.m_haendelse;
ld.m_haendelse = new char[strlen(hStr) + 1];
strcpy(ld.m_haendelse, hStr);
indfil 
  >> ld.m_tid.wYear
  >> ld.m_tid.wMonth
  >> ld.m_tid.wDayOfWeek 
  >> ld.m_tid.wDay 
  >> ld.m_tid.wHour 
  >> ld.m_tid.wMinute 
  >> ld.m_tid.wSecond 
  >> ld.m_tid.wMilliseconds;
indfil.getline(hStr, n);
return indfil;
}

//---------------------------------------------------------------
// class Log

Log::Log(char *filnavn)
{
m_filnavn = new char[strlen(filnavn) + 1];
strcpy(m_filnavn, filnavn);

int antal = 0;
LogData temp;
ofstream ostr(m_filnavn, ios::app);
ostr.close();
ifstream istr(m_filnavn);
while (!istr.eof())
  {
  istr >> temp;
  antal++;
  }
istr.close();
m_ldAntal = antal - 1;
m_logObjSem = RTKCreateSemaphore(ST_RESOURCE, 1, 
  "logObjSem");
}


Log::~Log()
{
delete[] m_filnavn;
RTKDeleteSemaphore(&m_logObjSem);
}


void Log::laes(LogData ld[])
{
RTKWait(m_logObjSem);
ifstream istr(m_filnavn);
for (int i = 0; i < m_ldAntal; i++)
  istr >> ld[i]; 
istr.close();
RTKSignal(m_logObjSem);
}


void Log::skriv(const LogData &ld)
{
RTKWait(m_logObjSem);
ofstream ostr(m_filnavn, ios::app);
ostr << ld;
ostr.close();
m_ldAntal++;
RTKSignal(m_logObjSem);
}


int Log::ldAntal()
{
return m_ldAntal;
}


void Log::nulstil()
{
RTKWait(m_logObjSem);
ofstream ostr(m_filnavn, ios::out);
ostr.close();
m_ldAntal = 0;
RTKSignal(m_logObjSem);
}

//===============================================================
