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

Subversion Repositories w11

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

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

Line No. Rev Author Line
1 22 wfjm
// $Id: Rtools.cpp 521 2013-05-20 22:16:45Z mueller $
2 10 wfjm
//
3 19 wfjm
// Copyright 2011-2013 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 21 wfjm
// 2013-05-04   516   1.0.3  add CreateBackupFile()
17 19 wfjm
// 2013-02-13   481   1.0.2  remove Throw(Logic|Runtime)(); use Rexception
18 11 wfjm
// 2011-04-10   376   1.0.1  add ThrowLogic(), ThrowRuntime()
19 10 wfjm
// 2011-03-12   368   1.0    Initial version
20
// ---------------------------------------------------------------------------
21
 
22
/*!
23
  \file
24 22 wfjm
  \version $Id: Rtools.cpp 521 2013-05-20 22:16:45Z mueller $
25 10 wfjm
  \brief   Implemenation of Rtools .
26
*/
27
 
28 19 wfjm
#include <stdlib.h>
29 21 wfjm
#include <stdio.h>
30
#include <sys/types.h>
31
#include <sys/stat.h>
32
#include <unistd.h>
33 10 wfjm
 
34 21 wfjm
#include <iostream>
35
#include <vector>
36
 
37 19 wfjm
#include "Rexception.hpp"
38 10 wfjm
 
39 19 wfjm
#include "Rtools.hpp"
40
 
41 10 wfjm
using namespace std;
42
 
43
/*!
44 19 wfjm
  \namespace Retro::Rtools
45 10 wfjm
  \brief FIXME_docs
46
*/
47
 
48 19 wfjm
// all method definitions in namespace Retro
49
namespace Retro {
50 21 wfjm
namespace Rtools {
51 19 wfjm
 
52 10 wfjm
//------------------------------------------+-----------------------------------
53
//! FIXME_docs
54
 
55 21 wfjm
std::string Flags2String(uint32_t flags, const RflagName* fnam, char delim)
56 10 wfjm
{
57
  if (fnam == 0)
58 19 wfjm
    throw Rexception("Rtools::Flags2String()","Bad args: fnam==NULL");
59 10 wfjm
 
60
  string rval;
61
  while (fnam->mask) {
62
    if (flags & fnam->mask) {
63
      if (!rval.empty()) rval += delim;
64
      rval += fnam->name;
65
    }
66
    fnam++;
67
  }
68
  return rval;
69
}
70
 
71 21 wfjm
//------------------------------------------+-----------------------------------
72
//! FIXME_docs
73 22 wfjm
 
74 21 wfjm
bool String2Long(const std::string& str, long& res, RerrMsg& emsg, int base)
75
{
76
  char* endptr;
77
  res = ::strtol(str.c_str(), &endptr, base);
78
  if (*endptr == 0) return true;
79
 
80
  emsg.Init("Rtools::String2Long",
81
            string("conversion error in '") + str +"'");
82
  res = 0;
83
  return false;
84
}
85
 
86
//------------------------------------------+-----------------------------------
87
//! FIXME_docs
88 22 wfjm
 
89 21 wfjm
bool String2Long(const std::string& str, unsigned long& res,
90
                 RerrMsg& emsg, int base)
91
{
92
  char* endptr;
93
  res = ::strtoul(str.c_str(), &endptr, base);
94
  if (*endptr == 0) return true;
95
 
96
  emsg.Init("Rtools::String2Long",
97
            string("conversion error in '") + str +"'");
98
  res = 0;
99
  return false;
100
}
101
 
102
//------------------------------------------+-----------------------------------
103
//! FIXME_docs
104
 
105
bool CreateBackupFile(const std::string& fname, size_t nbackup, RerrMsg& emsg)
106
{
107
  if (nbackup == 0) return true;
108
 
109
  size_t dotpos = fname.find_last_of('.');
110
  string fbase = fname.substr(0,dotpos);
111
  string fext  = fname.substr(dotpos);
112
 
113
  if (nbackup > 99) {
114
    emsg.Init("Rtools::CreateBackupFile",
115
              "only up to 99 backup levels supported");
116
    return false;
117
  }
118
 
119
  vector<string> fnames;
120
  fnames.push_back(fname);
121
  for (size_t i=1; i<=nbackup; i++) {
122
    char fnum[4];
123
    snprintf(fnum, 4, "%d", i);
124
    fnames.push_back(fbase + "_" + fnum + fext);
125
  }
126
 
127
  for (size_t i=nbackup; i>0; i--) {
128
    string fnam_new = fnames[i];
129
    string fnam_old = fnames[i-1];
130
 
131
    struct stat sbuf;
132
    int irc = ::stat(fnam_old.c_str(), &sbuf);
133
    if (irc < 0) {
134
      if (errno == ENOENT) continue;
135
      emsg.InitErrno("Rtools::CreateBackupFile",
136
                     string("stat() for '") + fnam_old + "'failed: ", errno);
137
      return false;
138
    }
139
    if (S_ISREG(sbuf.st_mode) == 0) {
140
      emsg.Init("Rtools::CreateBackupFile",
141
                "backups only supported for regular files");
142
      return false;
143
    }
144
    // here we know old file exists and is a regular file
145
    irc = ::rename(fnam_old.c_str(), fnam_new.c_str());
146
    if (irc < 0) {
147
      emsg.InitErrno("Rtools::CreateBackupFile",
148
                     string("rename() for '") + fnam_old + "' -> '" +
149
                     fnam_new + "'failed: ", errno);
150
      return false;
151
    }
152
  }
153
 
154
  return true;
155
}
156
 
157
//------------------------------------------+-----------------------------------
158
//! FIXME_docs
159
 
160
bool CreateBackupFile(const RparseUrl& purl, RerrMsg& emsg)
161
{
162
  string bck;
163
  if (!purl.FindOpt("app") && purl.FindOpt("bck", bck)) {
164
    unsigned long nbck;
165
    if (!Rtools::String2Long(bck, nbck, emsg)) return false;
166
    if (nbck > 0) {
167
      if (!Rtools::CreateBackupFile(purl.Path(), nbck, emsg)) return false;
168
    }
169
  }
170
  return true;
171
}
172
 
173
} // end namespace Rtools
174 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.