1//----------------------------------------------------------------------
2//  This software is part of the Haiku distribution and is covered
3//  by the MIT License.
4//---------------------------------------------------------------------
5/*!
6	\file Err.cpp
7	MIME sniffer Error class implementation
8*/
9
10#include <sniffer/Err.h>
11#include <new>
12#include <string.h>
13
14using namespace BPrivate::Storage::Sniffer;
15
16//------------------------------------------------------------------------------
17// Err
18//------------------------------------------------------------------------------
19
20Err::Err(const char *msg, const ssize_t pos)
21	: fMsg(NULL)
22	, fPos(-1)
23{
24	SetTo(msg, pos);
25}
26
27Err::Err(const std::string &msg, const ssize_t pos)
28	: fMsg(NULL)
29	, fPos(-1)
30{
31	SetTo(msg, pos);
32}
33
34Err::Err(const Err &ref)
35	: fMsg(NULL)
36	, fPos(-1)
37{
38	*this = ref;
39}
40
41Err::~Err() {
42	Unset();
43}
44
45Err&
46Err::operator=(const Err &ref) {
47	SetTo(ref.Msg(), ref.Pos());
48	return *this;
49}
50
51status_t
52Err::SetTo(const char *msg, const ssize_t pos) {
53	SetMsg(msg);
54	SetPos(pos);
55	return B_OK;
56}
57
58status_t
59Err::SetTo(const std::string &msg, const ssize_t pos) {
60	return SetTo(msg.c_str(), pos);
61}
62
63void
64Err::Unset() {
65	delete[] fMsg;
66	fMsg = NULL;
67	fPos = -1;
68}
69
70const char*
71Err::Msg() const {
72	return fMsg;
73}
74
75ssize_t
76Err::Pos() const {
77	return fPos;
78}
79
80void
81Err::SetMsg(const char *msg) {
82	if (fMsg) {
83		delete[] fMsg;
84		fMsg = NULL;
85	}
86	if (msg) {
87		fMsg = new(std::nothrow) char[strlen(msg)+1];
88		if (fMsg)
89			strcpy(fMsg, msg);
90	}
91}
92
93void
94Err::SetPos(ssize_t pos) {
95	fPos = pos;
96}
97
98
99
100