From 140334f858c7c02f3187b668dd2b79dbc2580efa Mon Sep 17 00:00:00 2001 From: Stefano Ceccherini Date: Sat, 12 May 2007 08:46:56 +0000 Subject: [PATCH] Added error checking using exceptions git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@21112 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/private/interface/PictureDataWriter.h | 10 +- src/kits/interface/PictureDataWriter.cpp | 324 ++++++++++++------ 2 files changed, 222 insertions(+), 112 deletions(-) diff --git a/headers/private/interface/PictureDataWriter.h b/headers/private/interface/PictureDataWriter.h index 2bad8beb4e..778ff60399 100644 --- a/headers/private/interface/PictureDataWriter.h +++ b/headers/private/interface/PictureDataWriter.h @@ -64,11 +64,11 @@ public: protected: - status_t WriteData(const void *data, size_t size); - template status_t Write(const T &data) { return WriteData(&data, sizeof(data)); } - - status_t BeginOp(const int16 &op); - status_t EndOp(); + // throw a status_t on error + void BeginOp(const int16 &op); + void EndOp(); + void WriteData(const void *data, size_t size); + template void Write(const T &data) { WriteData(&data, sizeof(data)); } private: BPositionIO *fData; diff --git a/src/kits/interface/PictureDataWriter.cpp b/src/kits/interface/PictureDataWriter.cpp index 0627bf5121..ee5cf63315 100644 --- a/src/kits/interface/PictureDataWriter.cpp +++ b/src/kits/interface/PictureDataWriter.cpp @@ -32,6 +32,8 @@ PictureDataWriter::PictureDataWriter(BPositionIO *data) status_t PictureDataWriter::SetTo(BPositionIO *data) { + if (data == NULL) + return B_BAD_VALUE; fData = data; return B_OK; } @@ -40,9 +42,13 @@ PictureDataWriter::SetTo(BPositionIO *data) status_t PictureDataWriter::WriteSetOrigin(const BPoint &point) { - BeginOp(B_PIC_SET_ORIGIN); - Write(point); - EndOp(); + try { + BeginOp(B_PIC_SET_ORIGIN); + Write(point); + EndOp(); + } catch (status_t &status) { + return status; + } return B_OK; } @@ -50,13 +56,17 @@ PictureDataWriter::WriteSetOrigin(const BPoint &point) status_t PictureDataWriter::WriteInvertRect(const BRect &rect) { - WriteSetDrawingMode(B_OP_INVERT); - - BeginOp(B_PIC_FILL_RECT); - Write(rect); - EndOp(); + try { + WriteSetDrawingMode(B_OP_INVERT); + + BeginOp(B_PIC_FILL_RECT); + Write(rect); + EndOp(); - WriteSetDrawingMode(B_OP_COPY); + WriteSetDrawingMode(B_OP_COPY); + } catch (status_t &status) { + return status; + } return B_OK; } @@ -64,9 +74,13 @@ PictureDataWriter::WriteInvertRect(const BRect &rect) status_t PictureDataWriter::WriteSetDrawingMode(const drawing_mode &mode) { - BeginOp(B_PIC_SET_DRAWING_MODE); - Write((int16)mode); - EndOp(); + try { + BeginOp(B_PIC_SET_DRAWING_MODE); + Write((int16)mode); + EndOp(); + } catch (status_t &status) { + return status; + } return B_OK; } @@ -74,9 +88,13 @@ PictureDataWriter::WriteSetDrawingMode(const drawing_mode &mode) status_t PictureDataWriter::WriteSetPenSize(const float &penSize) { - BeginOp(B_PIC_SET_PEN_SIZE); - Write(penSize); - EndOp(); + try { + BeginOp(B_PIC_SET_PEN_SIZE); + Write(penSize); + EndOp(); + } catch (status_t &status) { + return status; + } return B_OK; } @@ -84,11 +102,15 @@ PictureDataWriter::WriteSetPenSize(const float &penSize) status_t PictureDataWriter::WriteSetLineMode(const cap_mode &cap, const join_mode &join, const float &miterLimit) { - BeginOp(B_PIC_SET_LINE_MODE); - Write((int16)cap); - Write((int16)join); - Write(miterLimit); - EndOp(); + try { + BeginOp(B_PIC_SET_LINE_MODE); + Write((int16)cap); + Write((int16)join); + Write(miterLimit); + EndOp(); + } catch (status_t &status) { + return status; + } return B_OK; } @@ -96,9 +118,13 @@ PictureDataWriter::WriteSetLineMode(const cap_mode &cap, const join_mode &join, status_t PictureDataWriter::WriteSetScale(const float &scale) { - BeginOp(B_PIC_SET_SCALE); - Write(scale); - EndOp(); + try { + BeginOp(B_PIC_SET_SCALE); + Write(scale); + EndOp(); + } catch (status_t &status) { + return status; + } return B_OK; } @@ -106,9 +132,13 @@ PictureDataWriter::WriteSetScale(const float &scale) status_t PictureDataWriter::WriteSetHighColor(const rgb_color &color) { - BeginOp(B_PIC_SET_FORE_COLOR); - Write(color); - EndOp(); + try { + BeginOp(B_PIC_SET_FORE_COLOR); + Write(color); + EndOp(); + } catch (status_t &status) { + return status; + } return B_OK; } @@ -116,9 +146,13 @@ PictureDataWriter::WriteSetHighColor(const rgb_color &color) status_t PictureDataWriter::WriteSetLowColor(const rgb_color &color) { - BeginOp(B_PIC_SET_BACK_COLOR); - Write(color); - EndOp(); + try { + BeginOp(B_PIC_SET_BACK_COLOR); + Write(color); + EndOp(); + } catch (status_t &status) { + return status; + } return B_OK; } @@ -126,9 +160,13 @@ PictureDataWriter::WriteSetLowColor(const rgb_color &color) status_t PictureDataWriter::WriteDrawRect(const BRect &rect, const bool &fill) { - BeginOp(fill ? B_PIC_FILL_RECT : B_PIC_STROKE_RECT); - Write(rect); - EndOp(); + try { + BeginOp(fill ? B_PIC_FILL_RECT : B_PIC_STROKE_RECT); + Write(rect); + EndOp(); + } catch (status_t &status) { + return status; + } return B_OK; } @@ -136,10 +174,14 @@ PictureDataWriter::WriteDrawRect(const BRect &rect, const bool &fill) status_t PictureDataWriter::WriteDrawRoundRect(const BRect &rect, const BPoint &radius, const bool &fill) { - BeginOp(fill ? B_PIC_FILL_ROUND_RECT : B_PIC_STROKE_ROUND_RECT); - Write(rect); - Write(radius); - EndOp(); + try { + BeginOp(fill ? B_PIC_FILL_ROUND_RECT : B_PIC_STROKE_ROUND_RECT); + Write(rect); + Write(radius); + EndOp(); + } catch (status_t &status) { + return status; + } return B_OK; } @@ -147,9 +189,13 @@ PictureDataWriter::WriteDrawRoundRect(const BRect &rect, const BPoint &radius, c status_t PictureDataWriter::WriteDrawEllipse(const BRect &rect, const bool &fill) { - BeginOp(fill ? B_PIC_FILL_ELLIPSE : B_PIC_STROKE_ELLIPSE); - Write(rect); - EndOp(); + try { + BeginOp(fill ? B_PIC_FILL_ELLIPSE : B_PIC_STROKE_ELLIPSE); + Write(rect); + EndOp(); + } catch (status_t &status) { + return status; + } return B_OK; } @@ -158,12 +204,16 @@ status_t PictureDataWriter::WriteDrawArc(const BPoint ¢er, const BPoint &radius, const float &startTheta, const float &arcTheta, const bool &fill) { - BeginOp(fill ? B_PIC_FILL_ARC : B_PIC_STROKE_ARC); - Write(center); - Write(radius); - Write(startTheta); - Write(arcTheta); - EndOp(); + try { + BeginOp(fill ? B_PIC_FILL_ARC : B_PIC_STROKE_ARC); + Write(center); + Write(radius); + Write(startTheta); + Write(arcTheta); + EndOp(); + } catch (status_t &status) { + return status; + } return B_OK; } @@ -171,10 +221,14 @@ PictureDataWriter::WriteDrawArc(const BPoint ¢er, const BPoint &radius, status_t PictureDataWriter::WriteStrokeLine(const BPoint &start, const BPoint &end) { - BeginOp(B_PIC_STROKE_LINE); - Write(start); - Write(end); - EndOp(); + try { + BeginOp(B_PIC_STROKE_LINE); + Write(start); + Write(end); + EndOp(); + } catch (status_t &status) { + return status; + } return B_OK; } @@ -183,17 +237,21 @@ status_t PictureDataWriter::WriteDrawString(const BPoint &where, const char *string, const int32 &length, const escapement_delta &escapement) { - BeginOp(B_PIC_SET_PEN_LOCATION); - Write(where); - EndOp(); - - BeginOp(B_PIC_DRAW_STRING); - Write(escapement.space); - Write(escapement.nonspace); - //WriteData(string, length + 1); // TODO: is string 0 terminated? why is length given? - WriteData(string, length); - Write(0); - EndOp(); + try { + BeginOp(B_PIC_SET_PEN_LOCATION); + Write(where); + EndOp(); + + BeginOp(B_PIC_DRAW_STRING); + Write(escapement.space); + Write(escapement.nonspace); + //WriteData(string, length + 1); // TODO: is string 0 terminated? why is length given? + WriteData(string, length); + Write(0); + EndOp(); + } catch (status_t &status) { + return status; + } return B_OK; } @@ -203,12 +261,16 @@ status_t PictureDataWriter::WriteDrawShape(const int32 &opCount, const void *opList, const int32 &ptCount, const void *ptList, const bool &fill) { - BeginOp(fill ? B_PIC_FILL_SHAPE : B_PIC_STROKE_SHAPE); - Write(opCount); - Write(ptCount); - WriteData(opList, opCount * sizeof(uint32)); - WriteData(ptList, ptCount * sizeof(BPoint)); - EndOp(); + try { + BeginOp(fill ? B_PIC_FILL_SHAPE : B_PIC_STROKE_SHAPE); + Write(opCount); + Write(ptCount); + WriteData(opList, opCount * sizeof(uint32)); + WriteData(ptList, ptCount * sizeof(BPoint)); + EndOp(); + } catch (status_t &status) { + return status; + } return B_OK; } @@ -221,16 +283,20 @@ PictureDataWriter::WriteDrawBitmap(const BRect &srcRect, const BRect &dstRect, c { if (length != height * bytesPerRow) debugger("PictureDataWriter::WriteDrawBitmap: invalid length"); - BeginOp(B_PIC_DRAW_PIXELS); - Write(srcRect); - Write(dstRect); - Write(width); - Write(height); - Write(bytesPerRow); - Write(colorSpace); - Write(flags); - WriteData(data, length); - EndOp(); + try { + BeginOp(B_PIC_DRAW_PIXELS); + Write(srcRect); + Write(dstRect); + Write(width); + Write(height); + Write(bytesPerRow); + Write(colorSpace); + Write(flags); + WriteData(data, length); + EndOp(); + } catch (status_t &status) { + return status; + } return B_OK; } @@ -258,9 +324,13 @@ PictureDataWriter::WriteSetFontStyle(const font_style &style) status_t PictureDataWriter::WriteSetFontSpacing(const int32 &spacing) { - BeginOp(B_PIC_SET_FONT_SPACING); - Write(spacing); - EndOp(); + try { + BeginOp(B_PIC_SET_FONT_SPACING); + Write(spacing); + EndOp(); + } catch (status_t &status) { + return status; + } return B_OK; } @@ -268,9 +338,13 @@ PictureDataWriter::WriteSetFontSpacing(const int32 &spacing) status_t PictureDataWriter::WriteSetFontSize(const float &size) { - BeginOp(B_PIC_SET_FONT_SIZE); - Write(size); - EndOp(); + try { + BeginOp(B_PIC_SET_FONT_SIZE); + Write(size); + EndOp(); + } catch (status_t &status) { + return status; + } return B_OK; } @@ -278,9 +352,13 @@ PictureDataWriter::WriteSetFontSize(const float &size) status_t PictureDataWriter::WriteSetFontRotation(const float &rotation) { - BeginOp(B_PIC_SET_FONT_ROTATE); - Write(rotation); - EndOp(); + try { + BeginOp(B_PIC_SET_FONT_ROTATE); + Write(rotation); + EndOp(); + } catch (status_t &status) { + return status; + } return B_OK; } @@ -288,9 +366,13 @@ PictureDataWriter::WriteSetFontRotation(const float &rotation) status_t PictureDataWriter::WriteSetFontEncoding(const int32 &encoding) { - BeginOp(B_PIC_SET_FONT_ENCODING); - Write(encoding); - EndOp(); + try { + BeginOp(B_PIC_SET_FONT_ENCODING); + Write(encoding); + EndOp(); + } catch (status_t &status) { + return status; + } return B_OK; } @@ -298,9 +380,13 @@ PictureDataWriter::WriteSetFontEncoding(const int32 &encoding) status_t PictureDataWriter::WriteSetFontFlags(const int32 &flags) { - BeginOp(B_PIC_SET_FONT_FLAGS); - Write(flags); - EndOp(); + try { + BeginOp(B_PIC_SET_FONT_FLAGS); + Write(flags); + EndOp(); + } catch (status_t &status) { + return status; + } return B_OK; } @@ -308,9 +394,13 @@ PictureDataWriter::WriteSetFontFlags(const int32 &flags) status_t PictureDataWriter::WriteSetFontShear(const int32 &shear) { - BeginOp(B_PIC_SET_FONT_SHEAR); - Write(shear); - EndOp(); + try { + BeginOp(B_PIC_SET_FONT_SHEAR); + Write(shear); + EndOp(); + } catch (status_t &status) { + return status; + } return B_OK; } @@ -318,9 +408,13 @@ PictureDataWriter::WriteSetFontShear(const int32 &shear) status_t PictureDataWriter::WriteSetFontFace(const int32 &face) { - BeginOp(B_PIC_SET_FONT_FACE); - Write(face); - EndOp(); + try { + BeginOp(B_PIC_SET_FONT_FACE); + Write(face); + EndOp(); + } catch (status_t &status) { + return status; + } return B_OK; } @@ -328,8 +422,12 @@ PictureDataWriter::WriteSetFontFace(const int32 &face) status_t PictureDataWriter::WritePushState() { - BeginOp(B_PIC_PUSH_STATE); - EndOp(); + try { + BeginOp(B_PIC_PUSH_STATE); + EndOp(); + } catch (status_t &status) { + return status; + } return B_OK; } @@ -337,29 +435,38 @@ PictureDataWriter::WritePushState() status_t PictureDataWriter::WritePopState() { - BeginOp(B_PIC_POP_STATE); - EndOp(); + try { + BeginOp(B_PIC_POP_STATE); + EndOp(); + } catch (status_t &status) { + return status; + } return B_OK; } // private -status_t +void PictureDataWriter::BeginOp(const int16 &op) { + if (fData == NULL) + throw B_NO_INIT; + fStack.push(fData->Position()); fData->Write(&op, sizeof(op)); // Init the size of the opcode block to 0 size_t size = 0; fData->Write(&size, sizeof(size)); - return B_OK; } -status_t +void PictureDataWriter::EndOp() { + if (fData == NULL) + throw B_NO_INIT; + off_t curPos = fData->Position(); off_t stackPos = fStack.top(); fStack.pop(); @@ -374,12 +481,15 @@ PictureDataWriter::EndOp() fData->Seek(stackPos + sizeof(int16), SEEK_SET); fData->Write(&size, sizeof(size)); fData->Seek(curPos, SEEK_SET); - return B_OK; } -status_t +void PictureDataWriter::WriteData(const void *data, size_t size) { - return fData->Write(data, size); + ssize_t result = fData->Write(data, size); + if (result < 0) + throw (status_t)result; + if (result != size) + throw B_IO_ERROR; }