Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

Shape.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004 Ivo Danihelka (ivo@danihelka.net)
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  */
00009 #include "Shape.h"
00010 
00011 #include "LayoutException.h"
00012 #include "minmax.h"
00013 
00014 //-----------------------------------------------------------------
00015 /**
00016  * Read shape in format:
00017  * "XXX...\n"
00018  * ".XXXXX\n"
00019  * "XX...X\n"
00020  *
00021  * NOTE: rows does not need to have the same length
00022  *
00023  * @throws LayoutException when shape has bad format
00024  */
00025 Shape::Shape(const std::string &shape)
00026 {
00027     int x = 0;
00028     int y = 0;
00029     int max_x = -1;
00030     int max_y = -1;
00031 
00032     for (unsigned int i = 0; i < shape.size(); ++i) {
00033         switch (shape[i]) {
00034             case '\n':
00035                 ++y;
00036                 x = 0;
00037                 break;
00038             case 'X':
00039                 m_marks.push_back(V2(x, y));
00040                 max_x = max(max_x, x);
00041                 max_y = max(max_y, y);
00042                 ++x;
00043                 break;
00044             case '.':
00045                 ++x;
00046                 break;
00047             default:
00048                 throw LayoutException(ExInfo("bad shape char")
00049                         .addInfo("char", shape[i])
00050                         .addInfo("shape", shape));
00051         }
00052     }
00053 
00054     m_w = max_x + 1;
00055     m_h = max_y + 1;
00056 }
00057 
00058 //-----------------------------------------------------------------
00059 std::string
00060 Shape::toString() const
00061 {
00062     std::string result;
00063 
00064     t_marks::const_iterator end = m_marks.end();
00065     for (t_marks::const_iterator i = m_marks.begin(); i != end; ++i) {
00066         result.append(i->toString());
00067     }
00068     return result;
00069 }
00070 
00071 
00072 
00073 

Generated on Wed Jun 1 09:54:31 2005 for Fish Fillets - Next Generation by  doxygen 1.4.2