OpenCores
URL https://opencores.org/ocsvn/w11/w11/trunk

Subversion Repositories w11

[/] [w11/] [tags/] [w11a_V0.7/] [tools/] [src/] [librtools/] [Rtools.cpp] - Blame information for rev 27

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 wfjm
// $Id: Rtools.cpp 606 2014-11-24 07:08:51Z mueller $
2 10 wfjm
//
3 27 wfjm
// Copyright 2011-2014 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
4 10 wfjm
//
5
// This program is free software; you may redistribute and/or modify it under
6
// the terms of the GNU General Public License as published by the Free
7
// Software Foundation, either version 2, or at your option any later version.
8
//
9
// This program is distributed in the hope that it will be useful, but
10
// WITHOUT ANY WARRANTY, without even the implied warranty of MERCHANTABILITY
11
// or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12
// for complete details.
13
// 
14
// Revision History: 
15
// Date         Rev Version  Comment
16 27 wfjm
// 2014-11-23   606   1.0.4  add TimeOfDayAsDouble()
17
// 2014-11-08   602   1.0.5  add (int) cast in snprintf to match %d type
18
// 2014-08-22   584   1.0.4  use nullptr
19 21 wfjm
// 2013-05-04   516   1.0.3  add CreateBackupFile()
20 19 wfjm
// 2013-02-13   481   1.0.2  remove Throw(Logic|Runtime)(); use Rexception
21 11 wfjm
// 2011-04-10   376   1.0.1  add ThrowLogic(), ThrowRuntime()
22 10 wfjm
// 2011-03-12   368   1.0    Initial version
23
// ---------------------------------------------------------------------------
24
 
25
/*!
26
  \file
27 27 wfjm
  \version $Id: Rtools.cpp 606 2014-11-24 07:08:51Z mueller $
28 10 wfjm
  \brief   Implemenation of Rtools .
29
*/
30
 
31 19 wfjm
#include <stdlib.h>
32 21 wfjm
#include <stdio.h>
33
#include <sys/types.h>
34
#include <sys/stat.h>
35 27 wfjm
#include <sys/time.h>
36 21 wfjm
#include <unistd.h>
37 10 wfjm
 
38 21 wfjm
#include <iostream>
39
#include <vector>
40
 
41 19 wfjm
#include "Rexception.hpp"
42 10 wfjm
 
43 19 wfjm
#include "Rtools.hpp"
44
 
45 10 wfjm
using namespace std;
46
 
47
/*!
48 19 wfjm
  \namespace Retro::Rtools
49 10 wfjm
  \brief FIXME_docs
50
*/
51
 
52 19 wfjm
// all method definitions in namespace Retro
53
namespace Retro {
54 21 wfjm
namespace Rtools {
55 19 wfjm
 
56 10 wfjm
//------------------------------------------+-----------------------------------
57
//! FIXME_docs
58
 
59 21 wfjm
std::string Flags2String(uint32_t flags, const RflagName* fnam, char delim)
60 10 wfjm
{
61 27 wfjm
  if (fnam == nullptr)
62
    throw Rexception("Rtools::Flags2String()","Bad args: fnam==nullptr");
63 10 wfjm
 
64
  string rval;
65
  while (fnam->mask) {
66
    if (flags & fnam->mask) {
67
      if (!rval.empty()) rval += delim;
68
      rval += fnam->name;
69
    }
70
    fnam++;
71
  }
72
  return rval;
73
}
74
 
75 21 wfjm
//------------------------------------------+-----------------------------------
76
//! FIXME_docs
77 22 wfjm
 
78 21 wfjm
bool String2Long(const std::string& str, long& res, RerrMsg& emsg, int base)
79
{
80
  char* endptr;
81
  res = ::strtol(str.c_str(), &endptr, base);
82
  if (*endptr == 0) return true;
83
 
84
  emsg.Init("Rtools::String2Long",
85
            string("conversion error in '") + str +"'");
86
  res = 0;
87
  return false;
88
}
89
 
90
//------------------------------------------+-----------------------------------
91
//! FIXME_docs
92 22 wfjm
 
93 21 wfjm
bool String2Long(const std::string& str, unsigned long& res,
94
                 RerrMsg& emsg, int base)
95
{
96
  char* endptr;
97
  res = ::strtoul(str.c_str(), &endptr, base);
98
  if (*endptr == 0) return true;
99
 
100
  emsg.Init("Rtools::String2Long",
101
            string("conversion error in '") + str +"'");
102
  res = 0;
103
  return false;
104
}
105
 
106
//------------------------------------------+-----------------------------------
107
//! FIXME_docs
108
 
109
bool CreateBackupFile(const std::string& fname, size_t nbackup, RerrMsg& emsg)
110
{
111
  if (nbackup == 0) return true;
112
 
113
  size_t dotpos = fname.find_last_of('.');
114
  string fbase = fname.substr(0,dotpos);
115
  string fext  = fname.substr(dotpos);
116
 
117
  if (nbackup > 99) {
118
    emsg.Init("Rtools::CreateBackupFile",
119
              "only up to 99 backup levels supported");
120
    return false;
121
  }
122
 
123
  vector<string> fnames;
124
  fnames.push_back(fname);
125
  for (size_t i=1; i<=nbackup; i++) {
126
    char fnum[4];
127 27 wfjm
    snprintf(fnum, 4, "%d", (int)i);
128 21 wfjm
    fnames.push_back(fbase + "_" + fnum + fext);
129
  }
130
 
131
  for (size_t i=nbackup; i>0; i--) {
132
    string fnam_new = fnames[i];
133
    string fnam_old = fnames[i-1];
134
 
135
    struct stat sbuf;
136
    int irc = ::stat(fnam_old.c_str(), &sbuf);
137
    if (irc < 0) {
138
      if (errno == ENOENT) continue;
139
      emsg.InitErrno("Rtools::CreateBackupFile",
140
                     string("stat() for '") + fnam_old + "'failed: ", errno);
141
      return false;
142
    }
143
    if (S_ISREG(sbuf.st_mode) == 0) {
144
      emsg.Init("Rtools::CreateBackupFile",
145
                "backups only supported for regular files");
146
      return false;
147
    }
148
    // here we know old file exists and is a regular file
149
    irc = ::rename(fnam_old.c_str(), fnam_new.c_str());
150
    if (irc < 0) {
151
      emsg.InitErrno("Rtools::CreateBackupFile",
152
                     string("rename() for '") + fnam_old + "' -> '" +
153
                     fnam_new + "'failed: ", errno);
154
      return false;
155
    }
156
  }
157
 
158
  return true;
159
}
160
 
161
//------------------------------------------+-----------------------------------
162
//! FIXME_docs
163
 
164
bool CreateBackupFile(const RparseUrl& purl, RerrMsg& emsg)
165
{
166
  string bck;
167
  if (!purl.FindOpt("app") && purl.FindOpt("bck", bck)) {
168
    unsigned long nbck;
169
    if (!Rtools::String2Long(bck, nbck, emsg)) return false;
170
    if (nbck > 0) {
171
      if (!Rtools::CreateBackupFile(purl.Path(), nbck, emsg)) return false;
172
    }
173
  }
174
  return true;
175
}
176 27 wfjm
 
177
//------------------------------------------+-----------------------------------
178
//! Returns the time-of-day as \c double value
179
/*!
180
  Calls \c gettimeofday() and returns the current time as a \c double.
181
  This is convenient for calculations with time values.
182
 
183
  \returns time is seconds as \a double with micro second resolution.
184
  \throws Rexception in case \c gettimeofday() fails.
185
 */
186
 
187
double TimeOfDayAsDouble()
188
{
189
  struct timeval tval;
190
  int irc = ::gettimeofday(&tval, 0);
191
  if (irc < 0) {
192
    throw Rexception("Rtools::TimeOfDayAsDouble()",
193
                     "gettimeofday failed with ", errno);
194
  }
195 21 wfjm
 
196 27 wfjm
  return double(tval.tv_sec) + 1.e-6*double(tval.tv_usec);
197
}
198
 
199 21 wfjm
} // end namespace Rtools
200 19 wfjm
} // end namespace Retro

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.