aboutsummaryrefslogtreecommitdiff
path: root/src/stable/exceptionbase.cpp
blob: a43121152c52d0404a157a07c71bc2f1ff9679b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 * Copyright (C) 2007-2019 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#include "bu/exceptionbase.h"
#include <stdarg.h>
#include <string.h>
#include <stdio.h>

Bu::ExceptionBase::ExceptionBase( const char *lpFormat, ... ) throw() :
    nErrorCode( 0 ),
    sWhat( NULL )
{
    va_list ap;

    va_start(ap, lpFormat);
    setWhat( lpFormat, ap );
    va_end(ap);
}

Bu::ExceptionBase::ExceptionBase( int nCode, const char *lpFormat, ... ) throw() :
    nErrorCode( nCode ),
    sWhat( NULL )
{
    va_list ap;

    va_start(ap, lpFormat);
    setWhat( lpFormat, ap );
    va_end(ap);
}

Bu::ExceptionBase::ExceptionBase( int nCode ) throw() :
    nErrorCode( nCode ),
    sWhat( NULL )
{
}

Bu::ExceptionBase::ExceptionBase( const ExceptionBase &e ) throw () :
    std::exception( e ),
    nErrorCode( e.nErrorCode ),
    sWhat( NULL )
{
    setWhat( e.sWhat );
}

Bu::ExceptionBase::~ExceptionBase() throw()
{
    delete[] sWhat;
    sWhat = NULL;
}

void Bu::ExceptionBase::setWhat( const char *lpFormat, va_list &vargs )
{
    if( sWhat ) delete[] sWhat;
    int nSize;

    va_list vargs2;
    va_copy( vargs2, vargs );
    nSize = vsnprintf( NULL, 0, lpFormat, vargs2 );
    va_end( vargs2 );
    sWhat = new char[nSize+1];
    vsnprintf( sWhat, nSize+1, lpFormat, vargs );
}

void Bu::ExceptionBase::setWhat( const char *lpText )
{
    if( sWhat ) delete[] sWhat;
    int nSize;

    nSize = strlen( lpText );
    sWhat = new char[nSize+1];
    strcpy( sWhat, lpText );
}

const char *Bu::ExceptionBase::what() const throw()
{
    return sWhat;
}

int Bu::ExceptionBase::getErrorCode()
{
    return nErrorCode;
}

Bu::UnsupportedException::UnsupportedException() throw() :
    ExceptionBase( 0 )
{
    setWhat("An unsupperted operation was attempted.");
}