summaryrefslogtreecommitdiffstats
path: root/binfilter/bf_sfx2/source/doc/timestamp.cxx
blob: 735a5af5bc0cc84b496849c23195d8e53cd57d3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include "timestamp.hxx"

#include <tools/debug.hxx>

#define TIMESTAMP_INVALID_DATETIME      ( DateTime ( Date ( 1, 1, 1601 ), Time ( 0, 0, 0 ) ) )  /// Invalid value for date and time to create invalid instance of TimeStamp.

/*******************************************************************************************************************
 *
 *  @short      default constructor
 *  @descr      Take default values for member initialization.
 *
 *  @ATTENTION  We use default constructor for member !!! And the default of "DateTime" is the current date and time.
 *
 ******************************************************************************************************************/
namespace binfilter {

TimeStamp::TimeStamp ()
        :   m_sModifiedByName   ()
        ,   m_aModifiedDateTime ( DateTime::SYSTEM )
{
}

/*******************************************************************************************************************
 *
 *  @short      overloaded constructor
 *  @descr      Take default value for date and time. Name can be set.
 *
 *  @ATTENTION  We use default constructor for member !!! And the default of "DateTime" is the current date and time.
 *
 ******************************************************************************************************************/

TimeStamp::TimeStamp ( const String& rName )
        :   m_sModifiedByName   ( rName )
        ,   m_aModifiedDateTime ( DateTime::SYSTEM )
{
    // Safe impossible cases
    DBG_ASSERT ( impl_debug_checkParameter ( rName ), "TimeStamp::TimeStamp(String)\nInvalid parameter detected!\n" ) ;

    // Make valid name.
    impl_adjustName ( m_sModifiedByName ) ;
}

/*******************************************************************************************************************
 *
 *  @short      overloaded constructor
 *  @descr      Take default value for name. Date and time can be set.
 *
 ******************************************************************************************************************/

TimeStamp::TimeStamp ( const DateTime& rDateTime )
        :   m_sModifiedByName   (           )
        ,   m_aModifiedDateTime ( rDateTime )
{
    // Safe impossible cases
    DBG_ASSERT ( impl_debug_checkParameter ( rDateTime ), "TimeStamp::TimeStamp(DateTime)\nInvalid parameter detected!\n" ) ;
}

/*******************************************************************************************************************
 *
 *  @short      overloaded constructor
 *  @descr      All member can be set.
 *
 ******************************************************************************************************************/

TimeStamp::TimeStamp ( const String& rName, const DateTime& rDateTime )
        :   m_sModifiedByName   ( rName     )
        ,   m_aModifiedDateTime ( rDateTime )
{
    // Safe impossible cases
    DBG_ASSERT ( impl_debug_checkParameter ( rName, rDateTime ), "TimeStamp::TimeStamp(String,DateTime)\nInvalid parameter detected!\n" ) ;

    // Make valid name.
    impl_adjustName ( m_sModifiedByName ) ;
}

//______________________________________________________________________________________________________________________________
//  operator methods
//______________________________________________________________________________________________________________________________

/*******************************************************************************************************************
 *
 *  @short      Set one instance to another.
 *
 ******************************************************************************************************************/

const TimeStamp& TimeStamp::operator= ( const TimeStamp& rCopy )
{
    // Safe impossible cases
    DBG_ASSERT ( impl_debug_checkParameter ( rCopy ), "TimeStamp::operator=()\nInvalid parameter detected!\n" ) ;

    // Save new values.
    m_sModifiedByName   =   rCopy.m_sModifiedByName     ;
    m_aModifiedDateTime =   rCopy.m_aModifiedDateTime   ;

    // Adjust name.
    impl_adjustName ( m_sModifiedByName ) ;

    // Return pointer to this instance.
    return *this ;
}

/*******************************************************************************************************************
 *
 *  @short      Compare two instances of this class.
 *  @return     TRUE on equal / FALSE on non equal
 *
 ******************************************************************************************************************/

BOOL TimeStamp::operator== ( const TimeStamp& rCompare ) const
{
    // Safe impossible cases
    DBG_ASSERT ( impl_debug_checkParameter ( rCompare ), "TimeStamp::operator==()\nInvalid parameter detected!\n" ) ;

    // Compare member of this two objects.
    // Safe result for better debug ! (to se value)
    BOOL bResult =  (
                        ( m_sModifiedByName     ==  rCompare.m_sModifiedByName      )   &&
                        ( m_aModifiedDateTime   ==  rCompare.m_aModifiedDateTime    )
                    ) ;

    return bResult ;
}

/*******************************************************************************************************************
 *
 *  @short      Compare two instances of this class.
 *  @return     TRUE on non equal / FALSE on equal
 *
 ******************************************************************************************************************/

int TimeStamp::operator!= ( const TimeStamp& rCompare ) const
{
    // Safe impossible cases
    DBG_ASSERT ( impl_debug_checkParameter ( rCompare ), "TimeStamp::operator!=()\nInvalid parameter detected!\n" ) ;

    // Use operator "==" !!!
    return ( ! operator== ( rCompare ) ) ;
}

//______________________________________________________________________________________________________________________________
//  other methods
//______________________________________________________________________________________________________________________________

/*******************************************************************************************************************
 *
 *  @short      Check state of object.
 *  @return     TRUE on OK / FALSE on error
 *
 ******************************************************************************************************************/

BOOL TimeStamp::IsValid () const
{
    // Timestamp is only valid, if member "m_aModifiedDateTime" not the default.
    // The name can have a defaultvalue!
    // And its better to safe this result in a local variable ... for better debug! (to see value)
    BOOL bResult = ( m_aModifiedDateTime != TIMESTAMP_INVALID_DATETIME ) ;
    return bResult && m_aModifiedDateTime.IsValidAndGregorian();
}


/*******************************************************************************************************************
 *
 *  @short      Load a timestamp from stream.
 *  @return     TRUE on OK / FALSE on error
 *
 ******************************************************************************************************************/

BOOL TimeStamp::Load ( SvStream& rStream )
{
    // Safe impossible cases
    DBG_ASSERT ( impl_debug_checkParameter ( rStream ), "TimeStamp::Load()\nInvalid parameter detected!\n" ) ;

    // Load name from stream.
    CharSet eConvertCharSet = rStream.GetStreamCharSet();
    DBG_ASSERT( !(eConvertCharSet == RTL_TEXTENCODING_DONTKNOW) , "TimeStamp::Load()\nCharSet of stream is unknown. Can't convert bytestring to UniCode!\n" );
    DBG_ASSERT( !(eConvertCharSet == ((rtl_TextEncoding)9))     , "TimeStamp::Load()\nCharSet SYSTEM is obsolete. Can't convert bytestring to UniCode!\n"   );
    m_sModifiedByName = rStream.ReadUniOrByteString( eConvertCharSet );

    // Skip name in stream.

    // Follow operation "USHORT nSkip = ..." is right !!!
    // This fix old bugs of StarOffice versions before 5.1 ...
    // These versions write SfxStamps. And this implementation use USHORT to!
    // Follow subtraction will produce an overflow, if length of string greater then max. value.
    // We must keep ALL bytes of written strings - it can be up to 64K !!! ... :-(
    USHORT nSkip = TIMESTAMP_MAXLENGTH - m_sModifiedByName.Len () ;

    rStream.SeekRel ( nSkip ) ;

    // Adjust name.
    impl_adjustName ( m_sModifiedByName ) ;

    // Load date and time from stream.
    sal_Int32 nDate ;
    sal_Int32 nTime ;

    rStream >> nDate >> nTime ;

    // Safe date and time in member.
    m_aModifiedDateTime = DateTime ( Date ( nDate ), Time ( nTime ) ) ;

    // There was errors ...
    if ( rStream.GetError () != SVSTREAM_OK )
    {
        // Reset object to default => "IsValid()" return now FALSE !!!
        SetInvalid () ;
        // And return with ERROR.
        return FALSE ;
    }

    // OK.
    return TRUE ;
}

/*******************************************************************************************************************
 *
 *  @short      Set name of timestamp.
 *
 ******************************************************************************************************************/

void TimeStamp::SetName ( const String& rName )
{
    // Safe impossible cases
    DBG_ASSERT ( impl_debug_checkParameter ( rName ), "TimeStamp::SetName()\nInvalid parameter detected!\n" ) ;

    // Set new value at member.
    m_sModifiedByName = rName ;

    // Adjust member.
    impl_adjustName ( m_sModifiedByName ) ;
}

/*******************************************************************************************************************
 *
 *  @short      Set date and time of timestamp.
 *
 ******************************************************************************************************************/

void TimeStamp::SetTime ( const DateTime& rDateTime )
{
    // Safe impossible cases
    DBG_ASSERT ( impl_debug_checkParameter ( rDateTime ), "TimeStamp::SetTime()\nInvalid parameter detected!\n" ) ;

    m_aModifiedDateTime = rDateTime ;
}

/*******************************************************************************************************************
 *
 *  @short      Get name of timestamp.
 *
 ******************************************************************************************************************/

const String& TimeStamp::GetName () const
{
    return m_sModifiedByName ;
}

/*******************************************************************************************************************
 *
 *  @short      Get date and time of timestamp.
 *
 ******************************************************************************************************************/

const DateTime& TimeStamp::GetTime () const
{
    return m_aModifiedDateTime ;
}

//______________________________________________________________________________________________________________________________
//  protected methods
//______________________________________________________________________________________________________________________________

//______________________________________________________________________________________________________________________________
//  private methods
//______________________________________________________________________________________________________________________________

/*******************************************************************************************************************
 *
 *  @short      Reset member of instance to invalid values. The method "IsValid()" return now FALSE !!!
 *
 ******************************************************************************************************************/

void TimeStamp::SetInvalid ()
{
    m_sModifiedByName   =   String()                    ;
    m_aModifiedDateTime =   TIMESTAMP_INVALID_DATETIME  ;
}

/*******************************************************************************************************************
 *
 *  @short      Adjust member "m_sModifiedByName".
 *
 ******************************************************************************************************************/

void TimeStamp::impl_adjustName ( String& rName )
{
    // If there to much letters ...
    if ( rName.Len () > TIMESTAMP_MAXLENGTH )
    {
        // .. delete it.
        rName.Erase ( TIMESTAMP_MAXLENGTH ) ;
    }
}

//______________________________________________________________________________________________________________________________
//  debug methods
//______________________________________________________________________________________________________________________________

#ifdef DBG_UTIL

/*******************************************************************************************************************
 *
 *  @descr      The follow methods "impl_debug_checkParameter()" checks parameter for other methods.
 *              The return value is used for a DEBUG_ASSERT in "other methods".
 *
 *              There are no check-methods for ALL public-methods ... only for different parameters!
 *
 ******************************************************************************************************************/

BOOL TimeStamp::impl_debug_checkParameter ( const String& rString ) const
{
    if ( &rString       ==  NULL    ) return FALSE ;    // NULL-Pointer as reference :-(
//  if ( rString.Len () <   1       ) return FALSE ;    // This is an invalid text for a name!

    // OK.
    return TRUE ;
}

BOOL TimeStamp::impl_debug_checkParameter ( const String& rString, const DateTime& rDateTime ) const
{
    if ( &rString       ==  NULL                        ) return FALSE ;    // NULL-Pointer as reference :-(
    if ( &rDateTime     ==  NULL                        ) return FALSE ;    // NULL-Pointer as reference :-(
//  if ( rString.Len () <   1                           ) return FALSE ;    // This is an invalid text for a name!
//  if ( rDateTime      ==  TIMESTAMP_INVALID_DATETIME  ) return FALSE ;    // This will set "IsValid()" to FALSE !!!

    // OK.
    return TRUE ;
}

BOOL TimeStamp::impl_debug_checkParameter ( const TimeStamp& rTimeStamp ) const
{
    if ( &rTimeStamp            ==  NULL    ) return FALSE ;    // NULL-Pointer as reference :-(
//  if ( rTimeStamp.IsValid ()  ==  FALSE   ) return FALSE ;    // Somewhere will set invalid values ??!!

    // OK.
    return TRUE ;
}

BOOL TimeStamp::impl_debug_checkParameter ( SvStream& rSvStream ) const
{
    if ( &rSvStream             ==  NULL        ) return FALSE ;    // NULL-Pointer as reference :-(
    if ( rSvStream.GetError ()  !=  SVSTREAM_OK ) return FALSE ;    // This stream has errors!

    // OK.
    return TRUE ;
}

BOOL TimeStamp::impl_debug_checkParameter ( const DateTime& rDateTime ) const
{
    if ( &rDateTime ==  NULL                        ) return FALSE ;    // NULL-Pointer as reference :-(
//  if ( rDateTime  ==  TIMESTAMP_INVALID_DATETIME  ) return FALSE ;    // This will set "IsValid()" to FALSE !!!

    // OK.
    return TRUE ;
}

#endif // DBG_UTIL

}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */