~njansson/dolfin/hpc

« back to all changes in this revision

Viewing changes to src/kernel/common/utils.cpp

  • Committer: Johannes Ring
  • Date: 2008-03-05 22:43:06 UTC
  • Revision ID: johannr@simula.no-20080305224306-2npsdyhfdpl2esji
The BIG commit!

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Copyright (C) 2002-2005 Anders Logg.
2
 
// Licensed under the GNU LGPL Version 2.1.
3
 
//
4
 
// First added:  2002-11-29
5
 
// Last changed: 2005-12-21
6
 
 
7
 
#include <ctime>
8
 
#include <dolfin/dolfin_log.h>
9
 
#include <dolfin/utils.h>
10
 
 
11
 
using namespace dolfin;
12
 
 
13
 
//-----------------------------------------------------------------------------
14
 
bool dolfin::suffix(const char *string, const char *suffix)
15
 
{
16
 
  // Step to end of string
17
 
  unsigned int i = 0;
18
 
  for (; string[i]; i++);
19
 
 
20
 
  // Step to end of suffix
21
 
  unsigned int j = 0;
22
 
  for (; suffix[j]; j++);
23
 
 
24
 
  // String can not be shorter than suffix
25
 
  if ( i < j )
26
 
    return false;
27
 
  
28
 
  // Compare
29
 
  for (unsigned int k = i-j; k < i; k++)
30
 
    if ( string[k] != suffix[k-i+j] )
31
 
      return false;
32
 
  
33
 
  return true;
34
 
}
35
 
//-----------------------------------------------------------------------------
36
 
void dolfin::remove_newline(char *string)
37
 
{
38
 
  for (unsigned int i = 0; string[i]; i++)
39
 
    if ( string[i] == '\n' ){
40
 
      string[i] = '\0';
41
 
      return;
42
 
    }
43
 
}
44
 
//-----------------------------------------------------------------------------
45
 
int dolfin::length(const char *string)
46
 
{
47
 
  int n = 0;
48
 
  for (; string[n]; n++);
49
 
  return n;
50
 
}
51
 
//-----------------------------------------------------------------------------
52
 
void dolfin::delay(real seconds)
53
 
{
54
 
  if ( seconds < 0 )
55
 
  {
56
 
    warning("Delay must be positive.");
57
 
    return;
58
 
  }
59
 
  
60
 
  struct timespec req;
61
 
  req.tv_sec  = (int) floor(seconds);
62
 
  req.tv_nsec = (int) (1000000000.0 * (seconds - floor(seconds)));
63
 
  
64
 
  nanosleep(&req, 0);
65
 
}
66
 
//-----------------------------------------------------------------------------
67
 
std::string dolfin::date()
68
 
{
69
 
  // Get current date
70
 
  time_t t = time(0);
71
 
  std::string current_date(ctime(&t));
72
 
 
73
 
  // Remove trailing newline
74
 
  if ( current_date[current_date.length() - 1] == '\n' )
75
 
    current_date.erase(current_date.length() - 1);
76
 
  
77
 
  return current_date;
78
 
}
79
 
//-----------------------------------------------------------------------------