[Widelands-cvs] SF.net SVN: widelands: [2151] trunk/src
Status: Beta
Brought to you by:
sirver
From: <si...@us...> - 2007-03-24 02:16:11
|
Revision: 2151 http://svn.sourceforge.net/widelands/?rev=2151&view=rev Author: sigra Date: 2007-03-23 19:16:10 -0700 (Fri, 23 Mar 2007) Log Message: ----------- Add code for reading/writing a Coords that must be valid or null. Modified Paths: -------------- trunk/src/widelands_fileread.h trunk/src/widelands_filewrite.h Modified: trunk/src/widelands_fileread.h =================================================================== --- trunk/src/widelands_fileread.h 2007-03-24 02:13:06 UTC (rev 2150) +++ trunk/src/widelands_fileread.h 2007-03-24 02:16:10 UTC (rev 2151) @@ -29,12 +29,18 @@ struct Data_Error {}; struct Extent_Exceeded : public Data_Error {}; struct Width_Exceeded : public Extent_Exceeded { - Width_Exceeded(const Uint16 W, const X_Coordinate X) : w(W), x(X) {} + Width_Exceeded(const Pos pos, const Uint16 W, const X_Coordinate X) : + position(pos), w(W), x(X) + {} + Pos position; Uint16 w; X_Coordinate x; }; struct Height_Exceeded : public Extent_Exceeded { - Height_Exceeded(const Uint16 H, const Y_Coordinate Y) : h(H), y(Y) {} + Height_Exceeded(const Pos pos, const Uint16 H, const Y_Coordinate Y) : + position(pos), h(H), y(Y) + {} + Pos position; Uint16 h; Y_Coordinate y; }; @@ -48,14 +54,36 @@ */ Coords Coords32(const Extent extent); + /** + * Read Coords from the file. Use this when the result can only be a node + * coordinate or the special value indicating invalidity, as defined by + * Coords::isNull. Unless the read Coords is null, this will throw an + * exception if the width is <= the x coordinate or the height is <= the y + * coordinate. Both coordinates are read from the file before checking and + * possibly throwing, so in case such an exception is thrown, it is + * guaranteed that the whole coordinate pair has been read. + */ + Coords Coords32_allow_null(const Extent extent); + }; inline Coords WidelandsFileRead::Coords32(const Extent extent) { const Uint16 x = Little16(Deref16(Data(2))); const Uint16 y = Little16(Deref16(Data(2))); - if (extent.w <= x) throw Width_Exceeded (extent.w, x); - if (extent.h <= y) throw Height_Exceeded(extent.h, y); + if (extent.w <= x) throw Width_Exceeded (GetPos() - 4, extent.w, x); + if (extent.h <= y) throw Height_Exceeded(GetPrevPos(), extent.h, y); return Coords(x, y); } +inline Coords WidelandsFileRead::Coords32_allow_null(const Extent extent) { + const Uint16 x = Little16(Deref16(Data(2))); + const Uint16 y = Little16(Deref16(Data(2))); + const Coords result(x, y); + if (not result.isNull()) { + if (extent.w <= x) throw Width_Exceeded (GetPos() - 4, extent.w, x); + if (extent.h <= y) throw Height_Exceeded(GetPrevPos(), extent.h, y); + } + return Coords(x, y); +} + #endif Modified: trunk/src/widelands_filewrite.h =================================================================== --- trunk/src/widelands_filewrite.h 2007-03-24 02:13:06 UTC (rev 2150) +++ trunk/src/widelands_filewrite.h 2007-03-24 02:16:10 UTC (rev 2151) @@ -33,8 +33,8 @@ }; inline void WidelandsFileWrite::Coords32(const Coords c) { - assert(static_cast<const Uint16>(c.x) < 0x8000); - assert(static_cast<const Uint16>(c.y) < 0x8000); + assert(static_cast<const Uint16>(c.x) < 0x8000 or c.x == -1); + assert(static_cast<const Uint16>(c.y) < 0x8000 or c.y == -1); {const Uint16 x = Little16(c.x); Data(&x, 2);} {const Uint16 y = Little16(c.y); Data(&y, 2);} } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |