* Throw the actual exception objects, rather than pointers to them, i.e.:

- replace |throw new FooException()| with |throw FooException()|
  - replace |catch (FooException *ex)| with |catch (FooExceptio ex)|
  - update all uses of caught exceptions appropriately:
    . replace |ex->foo| with |ex.foo|
    . remove all attempts to delete the caught exception(s)
* Add 'const' to the 'char *' declarations for character encoding names



git-svn-id: https://zxing.googlecode.com/svn/trunk@1002 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
christian.brunschen 2009-06-27 13:22:03 +00:00
parent cca4b72bfa
commit f09b742ea2
28 changed files with 68 additions and 77 deletions

View file

@ -33,7 +33,7 @@ size_t GrayBytesMonochromeBitmapSource::getHeight() {
unsigned char GrayBytesMonochromeBitmapSource::getPixel(size_t x, size_t y) {
if (x >= width_ || y >= height_) {
throw new ReaderException("bitmap coordinate out of bounds");
throw ReaderException("bitmap coordinate out of bounds");
}
size_t index = y * bytesPerRow_ + x;
return bytes_[index];

View file

@ -95,7 +95,7 @@ estimateBlackPoint(BlackPointEstimationMethod method, int arg) {
}
} else if (method == BlackPointEstimationMethod_RowSampling) {
if (arg < 0 || arg >= (int)height) {
throw new IllegalArgumentException
throw IllegalArgumentException
("black point estimation argument out of range");
}
size_t y = static_cast<size_t> (arg);
@ -103,7 +103,7 @@ estimateBlackPoint(BlackPointEstimationMethod method, int arg) {
histogram[getPixel(x, y) >> LUMINANCE_SHIFT]++;
}
} else {
throw new IllegalArgumentException
throw IllegalArgumentException
("unknown black point estimation method");
}

View file

@ -72,7 +72,7 @@ namespace common {
}
bool BitArray::isRange(size_t start, size_t end, bool value) {
if (end < start) {
throw new IllegalArgumentException("end must be after start");
throw IllegalArgumentException("end must be after start");
}
if (end == start) {
return true;

View file

@ -73,15 +73,15 @@ namespace common {
size_t height,
size_t width) {
if (topI < 0 || leftJ < 0) {
throw new IllegalArgumentException("topI and leftJ must be nonnegative");
throw IllegalArgumentException("topI and leftJ must be nonnegative");
}
if (height < 1 || width < 1) {
throw new IllegalArgumentException("height and width must be at least 1");
throw IllegalArgumentException("height and width must be at least 1");
}
size_t maxJ = leftJ + width;
size_t maxI = topI + height;
if (maxI > dimension_ || maxJ > dimension_) {
throw new IllegalArgumentException
throw IllegalArgumentException
("topI + height and leftJ + width must be <= matrix dimension");
}
for (size_t j = leftJ; j < maxJ; j++) {

View file

@ -24,7 +24,7 @@
namespace common {
int BitSource::readBits(int numBits) {
if (numBits < 0 || numBits > 32) {
throw new IllegalArgumentException("cannot read <1 or >32 bits");
throw IllegalArgumentException("cannot read <1 or >32 bits");
}
int result = 0;

View file

@ -70,7 +70,7 @@ namespace common {
// for 1D formats, which are relatively lenient.
// We arbitrarily say "close" is "<= 1/16 of the total histogram buckets apart"
if (secondPeak - firstPeak <= numBuckets >> 4) {
throw new IllegalArgumentException
throw IllegalArgumentException
("Too little dynamic range in luminance");
}

View file

@ -77,7 +77,7 @@ namespace common {
if (x < -1 || x > width || y < -1 || y > height) {
ostringstream s;
s << "Transformed point out of bounds at " << x << "," << y;
throw new ReaderException(s.str().c_str());
throw ReaderException(s.str().c_str());
}
nudged = false;
if (x == -1) {
@ -103,7 +103,7 @@ namespace common {
if (x < -1 || x > width || y < -1 || y > height) {
ostringstream s;
s << "Transformed point out of bounds at " << x << "," << y;
throw new ReaderException(s.str().c_str());
throw ReaderException(s.str().c_str());
}
nudged = false;
if (x == -1) {

View file

@ -75,7 +75,7 @@ namespace reedsolomon {
cout << __FUNCTION__ << "\n";
#endif
if (degree < 0) {
throw new IllegalArgumentException("Degree must be non-negative");
throw IllegalArgumentException("Degree must be non-negative");
}
if (coefficient == 0) {
return zero_;
@ -97,14 +97,14 @@ namespace reedsolomon {
int GF256::log(int a) {
if (a == 0) {
throw new IllegalArgumentException("Cannot take the logarithm of 0");
throw IllegalArgumentException("Cannot take the logarithm of 0");
}
return log_[a];
}
int GF256::inverse(int a) {
if (a == 0) {
throw new IllegalArgumentException("Cannot calculate the inverse of 0");
throw IllegalArgumentException("Cannot calculate the inverse of 0");
}
return exp_[255 - log_[a]];
}

View file

@ -99,7 +99,7 @@ namespace reedsolomon {
GF256Poly *GF256Poly::addOrSubtract(GF256Poly *b) {
if (&field != &b->field) {
throw new IllegalArgumentException("Fields must be the same");
throw IllegalArgumentException("Fields must be the same");
}
if (isZero()) {
return b;
@ -131,7 +131,7 @@ namespace reedsolomon {
GF256Poly *GF256Poly::multiply(GF256Poly *b) {
if (&field != &b->field) {
throw new IllegalArgumentException("Fields must be the same");
throw IllegalArgumentException("Fields must be the same");
}
if (isZero() || b->isZero()) {
return field.getZero();
@ -172,7 +172,7 @@ namespace reedsolomon {
GF256Poly *GF256Poly::multiplyByMonomial(int degree,
int coefficient) {
if (degree < 0) {
throw new IllegalArgumentException("Degree must be non-negative");
throw IllegalArgumentException("Degree must be non-negative");
}
if (coefficient == 0) {
return field.getZero();

View file

@ -106,7 +106,7 @@ namespace reedsolomon {
// Divide rLastLast by rLast, with quotient q and remainder r
if (rLast->isZero()) {
// Oops, Euclidean algorithm already terminated?
throw new ReedSolomonException("r_{i-1} was zero");
throw ReedSolomonException("r_{i-1} was zero");
}
r = rLastLast;
Ref<GF256Poly> q(field.getZero());
@ -126,7 +126,7 @@ namespace reedsolomon {
int sigmaTildeAtZero = t->getCoefficient(0);
if (sigmaTildeAtZero == 0) {
throw new ReedSolomonException("sigmaTilde(0) was zero");
throw ReedSolomonException("sigmaTilde(0) was zero");
}
int inverse = field.inverse(sigmaTildeAtZero);
@ -165,7 +165,7 @@ namespace reedsolomon {
}
}
if (e != numErrors) {
throw new ReedSolomonException
throw ReedSolomonException
("Error locator degree does not match number of roots");
}
return result;

View file

@ -36,7 +36,7 @@ namespace qrcode {
bitMatrix_(bitMatrix), parsedVersion_(0), parsedFormatInfo_() {
int dimension = bitMatrix->getDimension();
if ((dimension < 21) || (dimension & 0x03) != 1) {
throw new ReaderException("Dimension must be 1 mod 4 and >= 21");
throw ReaderException("Dimension must be 1 mod 4 and >= 21");
}
}
@ -79,7 +79,7 @@ namespace qrcode {
if (parsedFormatInfo_ != 0) {
return parsedFormatInfo_;
}
throw new ReaderException("Could not decode format information");
throw ReaderException("Could not decode format information");
}
Version *BitMatrixParser::readVersion() {
@ -121,7 +121,7 @@ namespace qrcode {
if (parsedVersion_ != 0) {
return parsedVersion_;
}
throw new ReaderException("Could not decode version");
throw ReaderException("Could not decode version");
}
ArrayRef<unsigned char> BitMatrixParser::readCodewords() {
@ -177,7 +177,7 @@ namespace qrcode {
readingUp = !readingUp; // switch directions
}
if (resultOffset != version->getTotalCodewords()) {
throw new ReaderException("Did not read all codewords");
throw ReaderException("Did not read all codewords");
}
return result;
}

View file

@ -67,7 +67,7 @@ namespace qrcode {
break;
}
if (numCodewords != shorterBlocksTotalCodewords + 1) {
throw new IllegalArgumentException("Data block sizes differ by more than 1");
throw IllegalArgumentException("Data block sizes differ by more than 1");
}
longerBlocksStartAt--;
}
@ -96,7 +96,7 @@ namespace qrcode {
}
if ((size_t) rawCodewordsOffset != rawCodewords.size()) {
throw new IllegalArgumentException("rawCodewordsOffset != rawCodewords.length");
throw IllegalArgumentException("rawCodewordsOffset != rawCodewords.length");
}
return result;

View file

@ -30,7 +30,7 @@ namespace qrcode {
DataMask &DataMask::forReference(int reference) {
if (reference < 0 || reference > 7) {
throw new IllegalArgumentException("reference must be between 0 and 7");
throw IllegalArgumentException("reference must be between 0 and 7");
}
return *DATA_MASKS[reference];
}

View file

@ -27,18 +27,18 @@ namespace qrcode {
using namespace common;
using namespace std;
char DecodedBitStreamParser::ALPHANUMERIC_CHARS[] = {
const char DecodedBitStreamParser::ALPHANUMERIC_CHARS[] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
' ', '$', '%', '*', '+', '-', '.', '/', ':'
};
char *DecodedBitStreamParser::ASCII = "ASCII";
char *DecodedBitStreamParser::ISO88591 = "ISO-8859-1";
char *DecodedBitStreamParser::UTF8 = "UTF-8";
char *DecodedBitStreamParser::SHIFT_JIS = "SHIFT_JIS";
char *DecodedBitStreamParser::EUC_JP = "EUC-JP";
const char *DecodedBitStreamParser::ASCII = "ASCII";
const char *DecodedBitStreamParser::ISO88591 = "ISO-8859-1";
const char *DecodedBitStreamParser::UTF8 = "UTF-8";
const char *DecodedBitStreamParser::SHIFT_JIS = "SHIFT_JIS";
const char *DecodedBitStreamParser::EUC_JP = "EUC-JP";
void DecodedBitStreamParser::append(ostream &ost,
unsigned char *bufIn,
@ -59,7 +59,7 @@ namespace qrcode {
while (nFrom > 0) {
size_t oneway = iconv(cd, &fromPtr, &nFrom, &toPtr, &nTo);
if (oneway == (size_t)(-1)) {
throw new ReaderException("error converting characters");
throw ReaderException("error converting characters");
}
}
iconv_close(cd);
@ -106,7 +106,7 @@ namespace qrcode {
if (count << 3 > bits->available()) {
ostringstream s;
s << "Count too large: " << count;
throw new ReaderException(s.str().c_str());
throw ReaderException(s.str().c_str());
}
for (int i = 0; i < count; i++) {
readBytes[i] = (unsigned char) bits->readBits(8);
@ -116,7 +116,7 @@ namespace qrcode {
// upon decoding. I have seen ISO-8859-1 used as well as
// Shift_JIS -- without anything like an ECI designator to
// give a hint.
char *encoding = guessEncoding(readBytes, nBytes);
const char *encoding = guessEncoding(readBytes, nBytes);
append(result, readBytes, nBytes, encoding);
}
@ -133,7 +133,7 @@ namespace qrcode {
if (threeDigitsBits >= 1000) {
ostringstream s;
s << "Illegal value for 3-digit unit: " << threeDigitsBits;
throw new ReaderException(s.str().c_str());
throw ReaderException(s.str().c_str());
}
bytes[i++] = ALPHANUMERIC_CHARS[threeDigitsBits / 100];
bytes[i++] = ALPHANUMERIC_CHARS[(threeDigitsBits / 10) % 10];
@ -146,7 +146,7 @@ namespace qrcode {
if (twoDigitsBits >= 100) {
ostringstream s;
s << "Illegal value for 2-digit unit: " << twoDigitsBits;
throw new ReaderException(s.str().c_str());
throw ReaderException(s.str().c_str());
}
bytes[i++] = ALPHANUMERIC_CHARS[twoDigitsBits / 10];
bytes[i++] = ALPHANUMERIC_CHARS[twoDigitsBits % 10];
@ -156,7 +156,7 @@ namespace qrcode {
if (digitBits >= 10) {
ostringstream s;
s << "Illegal value for digit unit: " << digitBits;
throw new ReaderException(s.str().c_str());
throw ReaderException(s.str().c_str());
}
bytes[i++] = ALPHANUMERIC_CHARS[digitBits];
}
@ -182,7 +182,7 @@ namespace qrcode {
append(result, bytes, nBytes, ASCII);
}
char *
const char *
DecodedBitStreamParser::guessEncoding(unsigned char *bytes, int length) {
// Does it start with the UTF-8 byte order mark? then guess it's UTF-8
if (length > 3 && bytes[0] == (unsigned char) 0xEF &&
@ -262,7 +262,7 @@ namespace qrcode {
} else if (mode == &Mode::KANJI) {
decodeKanjiSegment(bits, result, count);
} else {
throw new ReaderException("Unsupported mode indicator");
throw ReaderException("Unsupported mode indicator");
}
}
} while (mode != &Mode::TERMINATOR);

View file

@ -38,13 +38,13 @@ namespace qrcode {
class DecodedBitStreamParser {
private:
static char ALPHANUMERIC_CHARS[];
static const char ALPHANUMERIC_CHARS[];
static char *ASCII;
static char *ISO88591;
static char *UTF8;
static char *SHIFT_JIS;
static char *EUC_JP;
static const char *ASCII;
static const char *ISO88591;
static const char *UTF8;
static const char *SHIFT_JIS;
static const char *EUC_JP;
static void decodeKanjiSegment(Ref<BitSource> bits,
ostringstream &result,
@ -58,7 +58,7 @@ namespace qrcode {
static void decodeNumericSegment(Ref<BitSource> bits,
ostringstream &result,
int count);
static char *guessEncoding(unsigned char *bytes, int length);
static const char *guessEncoding(unsigned char *bytes, int length);
static void append(ostream &ost, unsigned char *bufIn,
size_t nIn, const char *src);

View file

@ -45,9 +45,8 @@ namespace qrcode {
try {
rsDecoder_.decode(codewordInts, numECCodewords);
}
catch (ReedSolomonException *ex) {
ReaderException *rex = new ReaderException(ex->what());
delete ex;
catch (ReedSolomonException ex) {
ReaderException rex(ex.what());
throw rex;
}

View file

@ -41,7 +41,7 @@ namespace qrcode {
int ordinal() { return ordinal_; }
static ErrorCorrectionLevel& forBits(int bits) {
if (bits < 0 || bits >= N_LEVELS) {
throw new ReaderException("Ellegal error correction level bits");
throw ReaderException("Ellegal error correction level bits");
}
return *FOR_BITS[bits];
}

View file

@ -52,7 +52,7 @@ namespace qrcode {
default:
ostringstream s;
s << "Illegal mode bits: " << bits;
throw new ReaderException(s.str().c_str());
throw ReaderException(s.str().c_str());
}
}

View file

@ -63,14 +63,14 @@ namespace qrcode {
Version *Version::getProvisionalVersionForDimension(int dimension) {
if (dimension %4 != 1) {
throw new ReaderException("Dimension must be 1 mod 4");
throw ReaderException("Dimension must be 1 mod 4");
}
return Version::getVersionForNumber((dimension - 17) >> 2);
}
Version *Version::getVersionForNumber(int versionNumber) {
if (versionNumber < 1 || versionNumber > 40) {
throw new ReaderException("versionNumber must be between 1 and 40");
throw ReaderException("versionNumber must be between 1 and 40");
}
N_VERSIONS;
return VERSIONS[versionNumber - 1];

View file

@ -211,7 +211,7 @@ namespace qrcode {
return center;
}
throw new ReaderException("Could not find alignment pattern");
throw ReaderException("Could not find alignment pattern");
}
}

View file

@ -86,13 +86,12 @@ namespace qrcode {
estAlignmentY,
(float) i);
break;
} catch (ReaderException *re) {
} catch (ReaderException re) {
// try next round
delete re;
}
}
if (alignmentPattern == 0) {
throw new ReaderException("Could not find alignment pattern");
throw ReaderException("Could not find alignment pattern");
}
}
@ -173,7 +172,7 @@ namespace qrcode {
case 3:
ostringstream s;
s << "Bad dimension: " << dimension;
throw new ReaderException(s.str().c_str());
throw ReaderException(s.str().c_str());
}
return dimension;
}

View file

@ -318,7 +318,7 @@ namespace qrcode {
if (size < 3) {
// Couldn't find enough finder patterns
throw new ReaderException("Could not find three finder patterns");
throw ReaderException("Could not find three finder patterns");
}
if (size == 3) {

View file

@ -41,9 +41,8 @@ namespace common {
valarray<int> histogram(histogramRaw, 16);
BlackPointEstimator::estimate(histogram);
CPPUNIT_FAIL("Should have thrown an exception");
} catch (IllegalArgumentException* ie) {
} catch (IllegalArgumentException ie) {
// good
delete ie;
}
}
}

View file

@ -97,9 +97,8 @@ namespace reedsolomon {
checkQRRSDecode(received);
cout << "expected exception!\n";
CPPUNIT_FAIL("should not happen!");
} catch (ReedSolomonException *e) {
} catch (ReedSolomonException e) {
// expected
delete e;
} catch (...) {
CPPUNIT_FAIL("unexpected exception!");
}

View file

@ -40,9 +40,8 @@ namespace qrcode {
ErrorCorrectionLevel::forBits(4);
CPPUNIT_FAIL("should have thrown an exception");
}
catch (ReaderException *ex) {
catch (ReaderException ex) {
// expected
delete ex;
}
}
}

View file

@ -36,9 +36,8 @@ namespace qrcode {
Mode::forBits(0x10);
CPPUNIT_FAIL("should have thrown an exception");
}
catch (ReaderException *ex) {
catch (ReaderException ex) {
// expected
delete ex;
}
}

View file

@ -56,9 +56,8 @@ namespace qrcode {
try {
Version::getVersionForNumber(0);
CPPUNIT_FAIL("Should have thrown an exception");
} catch (ReaderException *re) {
} catch (ReaderException re) {
// good
delete re;
}
for (int i = 1; i <= 40; i++) {
checkVersion(Version::getVersionForNumber(i), i, 4*i + 17);

View file

@ -180,14 +180,12 @@ using namespace qrcode;
decoderResult = [TwoDDecoderResult resultWithText:resultString
points:points];
} catch (ReaderException *rex) {
} catch (ReaderException rex) {
NSLog(@"failed to decode, caught ReaderException '%s'",
rex->what());
delete rex;
} catch (IllegalArgumentException *iex) {
rex.what());
} catch (IllegalArgumentException iex) {
NSLog(@"failed to decode, caught IllegalArgumentException '%s'",
iex->what());
delete iex;
iex.what());
} catch (...) {
NSLog(@"Caught unknown exception!");
}