1 /*
2  * Copyright (C) 2004 Ivo Danihelka (ivo@danihelka.net)
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  */
9 #include "Shape.h"
10 
11 #include "LayoutException.h"
12 #include "minmax.h"
13 
14 //-----------------------------------------------------------------
15 /**
16  * Read shape in format:
17  * "XXX...\n"
18  * ".XXXXX\n"
19  * "XX...X\n"
20  *
21  * NOTE: rows does not need to have the same length
22  *
23  * @throws LayoutException when shape has bad format
24  */
Shape(const std::string & shape)25 Shape::Shape(const std::string &shape)
26 {
27     int x = 0;
28     int y = 0;
29     int max_x = -1;
30     int max_y = -1;
31 
32     for (unsigned int i = 0; i < shape.size(); ++i) {
33         switch (shape[i]) {
34             case '\n':
35                 ++y;
36                 x = 0;
37                 break;
38             case 'X':
39                 m_marks.push_back(V2(x, y));
40                 max_x = max(max_x, x);
41                 max_y = max(max_y, y);
42                 ++x;
43                 break;
44             case '.':
45                 ++x;
46                 break;
47             default:
48                 throw LayoutException(ExInfo("bad shape char")
49                         .addInfo("char", shape[i])
50                         .addInfo("shape", shape));
51         }
52     }
53 
54     m_w = max_x + 1;
55     m_h = max_y + 1;
56 }
57 
58 //-----------------------------------------------------------------
59 std::string
toString() const60 Shape::toString() const
61 {
62     std::string result;
63 
64     t_marks::const_iterator end = m_marks.end();
65     for (t_marks::const_iterator i = m_marks.begin(); i != end; ++i) {
66         result.append(i->toString());
67     }
68     return result;
69 }
70 
71 
72 
73 
74