3
*************************************************************************
5
ArmageTron -- Just another Tron Lightcycle Game in 3D.
6
Copyright (C) 2000 Manuel Moos (manuel@moosnet.de)
8
**************************************************************************
10
This program is free software; you can redistribute it and/or
11
modify it under the terms of the GNU General Public License
12
as published by the Free Software Foundation; either version 2
13
of the License, or (at your option) any later version.
15
This program is distributed in the hope that it will be useful,
16
but WITHOUT ANY WARRANTY; without even the implied warranty of
17
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
GNU General Public License for more details.
20
You should have received a copy of the GNU General Public License
21
along with this program; if not, write to the Free Software
22
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24
***************************************************************************
29
#ifndef TRECORDER_H_INCLUDED
30
#include "tRecorder.h"
33
#include "tConfiguration.h"
34
#include "tDirectories.h"
35
#include "tRecorderInternal.h"
40
// *****************************************************************************************
44
// *****************************************************************************************
46
//! @return true if a recording is running
48
// *****************************************************************************************
50
bool tRecorderBase::IsRecording( void )
52
return tRecordingBlock::GetArchive();
55
// *****************************************************************************************
59
// *****************************************************************************************
61
//! @return true if a playback is running
63
// *****************************************************************************************
65
bool tRecorderBase::IsPlayingBack( void )
67
return tPlaybackBlock::GetArchive();
70
// *****************************************************************************************
74
// *****************************************************************************************
76
//! @return true if recording or playback are running
78
// *****************************************************************************************
80
bool tRecorderBase::IsRunning( void )
82
return IsRecording() || IsPlayingBack();
85
// *******************************************************************************************
89
// *******************************************************************************************
91
//! @param section the name of the section to record or play back
92
//! @return true on success
94
// *******************************************************************************************
96
bool tRecorder::Record( char const * section )
99
return tRecorderTemplate1< tRecordingBlock >::Archive( false, section );
102
// *******************************************************************************************
106
// *******************************************************************************************
108
//! @param section the name of the section to record or play back
109
//! @return true on success
111
// *******************************************************************************************
113
bool tRecorder::Playback( char const * section )
116
return tRecorderTemplate1< tPlaybackBlock >::Archive( false, section );
119
// *******************************************************************************************
123
// *******************************************************************************************
125
//! @param section the name of the section to record or play back
126
//! @return true on success
128
// *******************************************************************************************
130
bool tRecorder::PlaybackStrict( char const * section )
133
return tRecorderTemplate1< tPlaybackBlock >::Archive( true, section );
136
// *****************************************************************************************
137
// *****************************************************************************************
138
// *****************************************************************************************
139
// *****************************************************************************************
141
// *****************************************************************************************
145
// *****************************************************************************************
147
//! @param other the string to copy
149
// *****************************************************************************************
151
tLineString::tLineString( tString const & other )
156
// *****************************************************************************************
160
// *****************************************************************************************
163
// *****************************************************************************************
165
tLineString::tLineString( void )
169
// *****************************************************************************************
173
// *****************************************************************************************
176
// *****************************************************************************************
178
tLineString::~tLineString( void )
182
//! persistent string writing operator
183
std::ostream & operator << ( std::ostream & s, tLineString const & line )
185
// write magic character
188
// print string, encode newlines
189
for( int i=0; i<line.Len(); ++i)
196
else if ( c != '\0' )
203
//! persistent string reading operator
204
std::istream & operator >> ( std::istream & s, tLineString & line )
206
// read magic character
216
// std::cout << "Read: " << read << "\n";
220
// copy line, replacing "\n" with real newline
221
for(int i=0; i<read.Len()-1; ++i)
224
if ( c != '\\' || i+1 == read.Len() || ( read[i+1] != 'n' && read[i+1] != '\\' ) )
228
else if ( read[i+1] == '\\' )
233
else // if ( read[i+1] != 'n' )
243
// *****************************************************************************************
244
// *****************************************************************************************
245
// *****************************************************************************************
246
// *****************************************************************************************
248
// *****************************************************************************************
252
// *****************************************************************************************
254
//! @param section name of the section to start
255
//! @param recording recording to read block from
256
//! @return true on success
258
// *****************************************************************************************
260
bool tRecordingBlockBase::Initialize( char const * section, tRecording * recording )
262
// initialize recording pointer
263
recording_ = recording;
268
recording_->BeginSection( section );
274
// *****************************************************************************************
278
// *****************************************************************************************
280
//! @param section name of the section to start
281
//! @return true on success
283
// *****************************************************************************************
285
bool tRecordingBlockBase::Initialize( char const * section )
288
return Initialize( section, tRecording::currentRecording_ );
291
// *****************************************************************************************
295
// *****************************************************************************************
298
// *****************************************************************************************
300
void tRecordingBlockBase::Separator( void )
302
GetRecordingStream() << "\n";
306
// ******************************************************************************************
310
// ******************************************************************************************
312
//! @return the currently running recording
314
// ******************************************************************************************
316
tRecording * tRecordingBlockBase::GetArchive( void )
318
return tRecording::currentRecording_;
321
// *****************************************************************************************
323
// * tRecordingBlockBase
325
// *****************************************************************************************
328
// *****************************************************************************************
330
tRecordingBlockBase::tRecordingBlockBase( void )
331
: separate_( true ), recording_( NULL )
335
// *****************************************************************************************
337
// * ~tRecordingBlockBase
339
// *****************************************************************************************
342
// *****************************************************************************************
344
tRecordingBlockBase::~tRecordingBlockBase( void )
346
// make sure everything is logged, even if program crashes
348
GetRecordingStream().flush();
353
// *****************************************************************************************
355
// * GetRecordingStream
357
// *****************************************************************************************
359
//! @return the stream of the recording
361
// *****************************************************************************************
363
std::ostream & tRecordingBlockBase::GetRecordingStream() const
365
tASSERT( recording_ );
367
return recording_->DoGetStream();
370
// *******************************************************************************************
374
// *******************************************************************************************
377
// *******************************************************************************************
379
tRecordingBlock::tRecordingBlock( void )
383
// *******************************************************************************************
385
// * ~tRecordingBlock
387
// *******************************************************************************************
390
// *******************************************************************************************
392
tRecordingBlock::~tRecordingBlock( void )
396
// *****************************************************************************************
397
// *****************************************************************************************
398
// *****************************************************************************************
399
// *****************************************************************************************
401
// *****************************************************************************************
405
// *****************************************************************************************
407
//! @param section name of section to read
408
//! @param playback playback to read from
409
//! @return true on success
411
// *****************************************************************************************
413
bool tPlaybackBlockBase::Initialize( char const * section, tPlayback * playback )
415
// initialize playback pointer
416
playback_ = playback;
421
if( playback_->GetNextSection() != section )
431
// *****************************************************************************************
435
// *****************************************************************************************
437
//! @param section name of section to read
438
//! @return true on success
440
// *****************************************************************************************
442
bool tPlaybackBlockBase::Initialize( char const * section )
444
return Initialize( section, tPlayback::currentPlayback_ );
447
// *****************************************************************************************
451
// *****************************************************************************************
454
// *****************************************************************************************
456
void tPlaybackBlockBase::Separator( void ) const
459
// ******************************************************************************************
463
// ******************************************************************************************
465
//! @return the currently running playback
467
// ******************************************************************************************
469
tPlayback * tPlaybackBlockBase::GetArchive( void )
471
return tPlayback::currentPlayback_;
474
// *****************************************************************************************
476
// * tPlaybackBlockBase
478
// *****************************************************************************************
481
// *****************************************************************************************
483
tPlaybackBlockBase::tPlaybackBlockBase( void )
488
// *****************************************************************************************
490
// * ~tPlaybackBlockBase
492
// *****************************************************************************************
495
// *****************************************************************************************
497
tPlaybackBlockBase::~tPlaybackBlockBase( void )
499
// end current block and read next
501
playback_->AdvanceSection();
506
// *****************************************************************************************
508
// * GetPlaybackStream
510
// *****************************************************************************************
512
//! @return the stream of the playback
514
// *****************************************************************************************
516
std::istream & tPlaybackBlockBase::GetPlaybackStream() const
518
tASSERT( playback_ );
520
return playback_->DoGetStream();
523
// *******************************************************************************************
527
// *******************************************************************************************
530
// *******************************************************************************************
532
tPlaybackBlock::tPlaybackBlock( void )
536
// *******************************************************************************************
540
// *******************************************************************************************
543
// *******************************************************************************************
545
tPlaybackBlock::~tPlaybackBlock( void )
549
// *****************************************************************************************
550
// *****************************************************************************************
551
// *****************************************************************************************
552
// *****************************************************************************************
554
static int st_debugLevelRecording=0;
555
static tSettingItem<int>rdb( "RECORDING_DEBUGLEVEL",
556
st_debugLevelRecording );
558
// returns the playback debug level, archiving the result
559
static int st_GetDebugLevelPlayback()
561
// sync level with recording
562
int level = st_debugLevelRecording;
563
tRecorder::Playback( "DEBUGLEVEL", level );
564
tRecorder::Record( "DEBUGLEVEL", st_debugLevelRecording );
569
// *******************************************************************************************
571
// * GetDebugLevelPlayback
573
// *******************************************************************************************
577
// *******************************************************************************************
579
int tRecorderSyncBase::GetDebugLevelPlayback( void )
581
// get the playback level only once
582
static int level = st_GetDebugLevelPlayback();
587
// *******************************************************************************************
589
// * GetDebugLevelRecording
591
// *******************************************************************************************
595
// *******************************************************************************************
597
int tRecorderSyncBase::GetDebugLevelRecording( void )
599
return st_debugLevelRecording;
602
REAL st_GetDifference( REAL a, REAL b)
604
return fabs( a - b );
607
REAL st_GetDifference( int a, int b)
609
return fabs( REAL( a - b ) );
612
REAL st_GetDifference( unsigned int a, unsigned int b)
614
return fabs( REAL( a - b ) );
617
REAL st_GetDifference( unsigned long int a, unsigned long int b)
619
return fabs( REAL( a - b ) );
622
REAL st_GetDifference( tString const & a, tString const & b )
624
return ( a == b ) ? 0 : 1;
627
static char const * st_fileOpen = "FILE_OPEN";
628
static char const * st_fileRead = "FILE_READ";
630
// *******************************************************************************
634
// *******************************************************************************
636
//! @param searchPath the path to search for the file
637
//! @param fileName the name of the file to open
638
//! @return true if opening succeeded
640
// *******************************************************************************
642
bool tTextFileRecorder::Open( tPath const & searchPath, char const * fileName )
648
// try to read opening state from recording
649
if ( !tRecorder::Playback( st_fileOpen, result, eof_ ) )
651
// open the stream (if not in recording mode)
652
stream_ = tNEW( std::ifstream );
653
result = searchPath.Open( *stream_, fileName );
654
eof_ = !result || !stream_->good() || stream_->eof();
656
tRecorder::Record( st_fileOpen, result, eof_ );
661
// *******************************************************************************
663
// * tTextFileRecorder
665
// *******************************************************************************
667
//! @param searchPath the path to search for the file
668
//! @param fileName the name of the file to open
670
// *******************************************************************************
672
tTextFileRecorder::tTextFileRecorder( tPath const & searchPath, char const * fileName )
673
: stream_(NULL), eof_(false)
675
Open( searchPath, fileName );
678
// *******************************************************************************
680
// * tTextFileRecorder
682
// *******************************************************************************
685
// *******************************************************************************
687
tTextFileRecorder::tTextFileRecorder( void )
688
: stream_(NULL), eof_(false)
693
// *******************************************************************************
695
// * ~tTextFileRecorder
697
// *******************************************************************************
700
// *******************************************************************************
702
tTextFileRecorder::~tTextFileRecorder( void )
708
// *******************************************************************************
712
// *******************************************************************************
714
//! @return true if the end of file (or any other error that has the same effect) has been reached
716
// *******************************************************************************
718
bool tTextFileRecorder::EndOfFile( void ) const
723
// *******************************************************************************
727
// *******************************************************************************
729
//! @return the line read from the file or a recording thereof
731
// *******************************************************************************
733
std::string tTextFileRecorder::GetLine( void )
735
// try to read opening state from recording
737
if ( !tRecorder::Playback( st_fileRead, line, eof_ ) )
741
line.ReadLine( *stream_ );
743
eof_ = !stream_->good() || stream_->eof();
745
tRecorder::Record( st_fileRead, line, eof_ );
747
// convert and return read line
748
return std::string(line);