Switch to unified view

a/src/utils/smallut.cpp b/src/utils/smallut.cpp
1
#ifndef lint
1
#ifndef lint
2
static char rcsid[] = "@(#$Id: smallut.cpp,v 1.14 2006-01-23 13:32:28 dockes Exp $ (C) 2004 J.F.Dockes";
2
static char rcsid[] = "@(#$Id: smallut.cpp,v 1.15 2006-01-26 12:29:20 dockes Exp $ (C) 2004 J.F.Dockes";
3
#endif
3
#endif
4
/*
4
/*
5
 *   This program is free software; you can redistribute it and/or modify
5
 *   This program is free software; you can redistribute it and/or modify
6
 *   it under the terms of the GNU General Public License as published by
6
 *   it under the terms of the GNU General Public License as published by
7
 *   the Free Software Foundation; either version 2 of the License, or
7
 *   the Free Software Foundation; either version 2 of the License, or
...
...
16
 *   along with this program; if not, write to the
16
 *   along with this program; if not, write to the
17
 *   Free Software Foundation, Inc.,
17
 *   Free Software Foundation, Inc.,
18
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
 */
19
 */
20
#ifndef TEST_SMALLUT
20
#ifndef TEST_SMALLUT
21
#include <string>
21
#include <stdio.h>
22
#include <stdlib.h>
23
#include <time.h>
22
#include <ctype.h>
24
#include <ctype.h>
23
#include <unistd.h>
25
#include <unistd.h>
24
#include <errno.h>
26
#include <errno.h>
25
27
#include <sys/time.h>
26
#include <sys/types.h>
28
#include <sys/types.h>
27
#include <sys/stat.h>
29
#include <sys/stat.h>
30
31
#include <string>
28
32
29
#include "smallut.h"
33
#include "smallut.h"
30
#include "debuglog.h"
34
#include "debuglog.h"
31
#include "pathut.h"
35
#include "pathut.h"
32
36
...
...
344
    }
348
    }
345
    }
349
    }
346
    return out;
350
    return out;
347
}
351
}
348
352
353
////////////////////
354
// Internal redefinition of system time interface to help with dependancies
355
struct m_timespec {
356
  time_t tv_sec;
357
  long   tv_nsec;
358
};
359
360
#ifndef CLOCK_REALTIME
361
#define CLOCK_REALTIME 1
362
#endif
363
364
#define MILLIS(TV) ( (long)(((TV).tv_sec - m_secs) * 1000 + \
365
  ((TV).tv_nsec - m_nsecs) / 1000000))
366
367
#define MICROS(TV) ( (long)(((TV).tv_sec - m_secs) * 1000000 + \
368
  ((TV).tv_nsec - m_nsecs) / 1000))
369
370
371
// We use gettimeofday instead of clock_gettime for now and get only
372
// uS resolution, because clock_gettime is more configuration trouble
373
// than it's worth
374
static void gettime(int, struct m_timespec *ts)
375
{
376
  struct timeval tv;
377
  gettimeofday(&tv, 0);
378
  ts->tv_sec = tv.tv_sec;
379
  ts->tv_nsec = tv.tv_usec * 1000;
380
}
381
///// End system interface
382
383
static m_timespec frozen_tv;
384
void Chrono::refnow()
385
{
386
  gettime(CLOCK_REALTIME, &frozen_tv);
387
}
388
389
Chrono::Chrono()
390
{
391
  restart();
392
}
393
394
// Reset and return value before rest in milliseconds
395
long Chrono::restart()
396
{
397
  struct m_timespec tv;
398
  gettime(CLOCK_REALTIME, &tv);
399
  long ret = MILLIS(tv);
400
  m_secs = tv.tv_sec;
401
  m_nsecs = tv.tv_nsec;
402
  return ret;
403
}
404
405
// Get current timer value, milliseconds
406
long Chrono::millis(int frozen)
407
{
408
  if (frozen) {
409
    return MILLIS(frozen_tv);
410
  } else {
411
    struct m_timespec tv;
412
    gettime(CLOCK_REALTIME, &tv);
413
    return MILLIS(tv);
414
  }
415
}
416
417
//
418
long Chrono::micros(int frozen)
419
{
420
  if (frozen) {
421
    return MICROS(frozen_tv);
422
  } else {
423
    struct m_timespec tv;
424
    gettime(CLOCK_REALTIME, &tv);
425
    return MICROS(tv);
426
  }
427
}
428
429
float Chrono::secs(int frozen)
430
{
431
  struct m_timespec tv;
432
  gettime(CLOCK_REALTIME, &tv);
433
  float secs = (float)(frozen?frozen_tv.tv_sec:tv.tv_sec - m_secs);
434
  float nsecs = (float)(frozen?frozen_tv.tv_nsec:tv.tv_nsec - m_nsecs); 
435
  //fprintf(stderr, "secs %.2f nsecs %.2f\n", secs, nsecs);
436
  return secs + nsecs * 1e-9;
437
}
438
349
#else
439
#else
350
440
351
#include <string>
441
#include <string>
352
#include "smallut.h"
442
#include "smallut.h"
353
443