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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
// This file is part of The New Aspell
// Copyright (C) 2001 by Kevin Atkinson under the GNU LGPL license
// version 2.0 or 2.1. You should have received a copy of the LGPL
// license along with this library if you did not you can find
// it at http://www.gnu.org/.
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
//#include "iostream.hpp"
#include "posib_err.hpp"
#include "gettext.h"
namespace acommon {
String & String::operator= (const PosibErr<String> & s)
{
operator=(s.data);
return *this;
}
struct StrSize {
const char * str;
unsigned int size;
StrSize() : str(0), size(0) {}
void operator= (ParmString s) {str = s; size = s.size();}
};
PosibErrBase & PosibErrBase::set(const ErrorInfo * inf,
ParmString p1, ParmString p2,
ParmString p3, ParmString p4)
{
const char * s0 = inf->mesg ? _(inf->mesg) : "";
const char * s;
ParmString p[4] = {p1,p2,p3,p4};
StrSize m[10];
unsigned int i = 0;
while (i != 4 && p[i] != 0)
++i;
assert(i == inf->num_parms || i == inf->num_parms + 1);
i = 0;
while (true) {
s = s0 + strcspn(s0, "%");
m[i].str = s0;
m[i].size = s - s0;
if (*s == '\0') break;
++i;
s = strchr(s, ':') + 1;
unsigned int ip = *s - '0' - 1;
assert(0 <= ip && ip < inf->num_parms);
m[i] = p[ip];
++i;
s0 = s+1;
}
if (!p[inf->num_parms].empty()) {
m[++i] = " ";
m[++i] = p[inf->num_parms];
}
unsigned int size = 0;
for (i = 0; m[i].str != 0; ++i)
size += m[i].size;
char * str = (char *)malloc(size + 1);
s0 = str;
for (i = 0; m[i].str != 0; str+=m[i].size, ++i)
strncpy(str, m[i].str, m[i].size);
*str = '\0';
Error * e = new Error;
e->err = inf;
e->mesg = s0;
err_ = new ErrPtr(e);
return *this;
}
PosibErrBase & PosibErrBase::with_file(ParmString fn, int line_num)
{
assert(err_ != 0);
assert(err_->refcount == 1);
char * m = const_cast<char *>(err_->err->mesg);
unsigned int orig_len = strlen(m);
unsigned int new_len = fn.size() + (line_num ? 10 : 0) + 2 + orig_len + 1;
char * s = (char *)malloc(new_len);
if (line_num)
snprintf(s, new_len, "%s:%d: %s", fn.str(), line_num, m);
else
snprintf(s, new_len, "%s: %s", fn.str(), m);
free(m);
const_cast<Error *>(err_->err)->mesg = s;
return *this;
}
#ifndef NDEBUG
void PosibErrBase::handle_err() const {
assert (err_);
assert (!err_->handled);
fputs(_("Unhandled Error: "), stderr);
fputs(err_->err->mesg, stderr);
fputs("\n", stderr);
abort();
}
#endif
Error * PosibErrBase::release() {
assert (err_);
assert (err_->refcount <= 1);
--err_->refcount;
Error * tmp;
if (err_->refcount == 0) {
tmp = const_cast<Error *>(err_->err);
delete err_;
} else {
tmp = new Error(*err_->err);
}
err_ = 0;
return tmp;
}
void PosibErrBase::del() {
if (!err_) return;
delete const_cast<Error *>(err_->err);
delete err_;
}
}
|