CLAM-Development  1.4.0
Err.cxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 #include "Err.hxx"
23 #include <cstdio>
24 #include <cstring>
25 #include <new>
26 
27 namespace CLAM {
28 
29 /* invoking the default constructor */
30  Err::Err() throw()
31  {
32  mMsg = 0;
33  }
34 
35 /* invoking the overloaded constructor */
36  Err::Err(const char* msg) throw()
37  {
38  mMsg = new(std::nothrow) char[strlen(msg)+1];
39  if (!mMsg) return;
40  strncpy(mMsg,msg,strlen(msg)+1);
41  }
42 
43  Err::Err(const Err& orig) throw()
44  {
45  mMsg = new(std::nothrow) char[strlen(orig.what())+1];
46  if (!mMsg) return;
47  strncpy(mMsg,orig.what(),strlen(orig.what())+1);
48  }
49 
50  Err::~Err() throw()
51  {
52  if ( mMsg )
53  delete [] mMsg;
54  };
55 
56 /* specifying the header's member function */
57  void Err::Print(void) const throw()
58  {
59  fprintf(stderr,"CLAM Error: ");
60  if (mMsg)
61  fprintf(stderr,mMsg);
62  else
63  fprintf(stderr,"Unknown Error");
64  fprintf(stderr,"\n");
65  }
66 
67  void Err::Embed(const std::exception &e) throw()
68  {
69  Embed(e.what());
70  }
71 
72  void Err::Embed(const char* str) throw()
73  {
74  static const char* separation = "\n Nested error: ";
75  size_t msgLen = strlen(mMsg);
76  size_t sepLen = strlen(separation);
77  size_t strLen = strlen(str);
78  size_t len;
79  char* msg;
80  len = msgLen + sepLen + strLen + 1;
81  msg = new(std::nothrow) char[len];
82  if (!msg) return;
83  strcpy(msg,mMsg);
84  strcpy(msg+msgLen,separation);
85  strcpy(msg+msgLen+sepLen,str);
86  delete [] mMsg;
87  mMsg = msg;
88  }
89 
90 }
91