allow negative coordinates for positions
Stefan Schuermans

Stefan Schuermans commited on 2011-11-18 22:27:52
Showing 4 changed files, with 66 additions and 9 deletions.

... ...
@@ -26,11 +26,11 @@ Position::Position():
26 26
 bool Position::fromStr(const std::string &str)
27 27
 {
28 28
   StringParser parser(str);
29
-  unsigned int x, y;
29
+  int x, y;
30 30
 
31
-  if (!parser.uintMin(0, x) ||
31
+  if (!parser.sintNo(x) ||
32 32
       !parser.fixChr(',') ||
33
-      !parser.uintMin(0, y))
33
+      !parser.sintNo(y))
34 34
     return false;
35 35
   m_x = x;
36 36
   m_y = y;
... ...
@@ -32,8 +32,8 @@ public:
32 32
   std::string toStr() const;
33 33
 
34 34
 public:
35
-  unsigned int m_x; ///< X coordinate within the frame (in pixels)
36
-  unsigned int m_y; ///< Y ccordinate within the frame (in pixels)
35
+  int m_x; ///< X coordinate within a frame (in pixels)
36
+  int m_y; ///< Y ccordinate within a frame (in pixels)
37 37
 }; // class Position
38 38
 
39 39
 } // namespace Blinker
... ...
@@ -33,20 +33,63 @@ bool StringParser::fixChr(char chr)
33 33
 }
34 34
 
35 35
 /**
36
- * @brief parse unsigned number and check minimum
37
- * @param[in] min minimum value to expect
36
+ * @brief parse unsigned number
38 37
  * @param[out] uint number parsed from string
39 38
  * @return if parsing was successful
40 39
  */
41
-bool StringParser::uintMin(unsigned int min, unsigned int &uint)
40
+bool StringParser::uintNo(unsigned int &uint)
42 41
 {
42
+  bool ret = false;
43 43
   uint = 0;
44 44
   while (m_it != m_str.end() && *m_it >= '0' && *m_it <= '9') {
45 45
     uint *= 10;
46 46
     uint += *m_it - '0';
47 47
     ++m_it;
48
+    ret = true;
49
+  }
50
+  return ret;
51
+}
52
+
53
+/**
54
+ * @brief parse unsigned number and check minimum
55
+ * @param[in] min minimum value to expect
56
+ * @param[out] uint number parsed from string
57
+ * @return if parsing was successful
58
+ */
59
+bool StringParser::uintMin(unsigned int min, unsigned int &uint)
60
+{
61
+  return uintNo(uint) && uint >= min;
62
+}
63
+
64
+/**
65
+ * @brief parse signed number
66
+ * @param[out] sint number parsed from string
67
+ * @return if parsing was successful
68
+ */
69
+bool StringParser::sintNo(int &sint)
70
+{
71
+  int sign;
72
+  unsigned int uint;
73
+  bool ret;
74
+  sint = 0;
75
+  if (m_it != m_str.end()) {
76
+    switch (*m_it) {
77
+      case '+':
78
+        sign = 1;
79
+        ++m_it;
80
+        break;
81
+      case '-':
82
+        sign = -1;
83
+        ++m_it;
84
+        break;
85
+      default:
86
+        sign = 1;
87
+        break;
88
+    }
48 89
   }
49
-  return uint >= min;
90
+  ret = uintNo(uint);
91
+  sint = sign * uint;
92
+  return ret;
50 93
 }
51 94
 
52 95
 /**
... ...
@@ -28,6 +28,13 @@ public:
28 28
    */
29 29
   bool fixChr(char chr);
30 30
 
31
+  /**
32
+   * @brief parse unsigned number
33
+   * @param[out] uint number parsed from string
34
+   * @return if parsing was successful
35
+   */
36
+  bool uintNo(unsigned int &uint);
37
+
31 38
   /**
32 39
    * @brief parse unsigned number and check minimum
33 40
    * @param[in] min minimum value to expect
... ...
@@ -36,6 +43,13 @@ public:
36 43
    */
37 44
   bool uintMin(unsigned int min, unsigned int &uint);
38 45
 
46
+  /**
47
+   * @brief parse signed number
48
+   * @param[out] sint number parsed from string
49
+   * @return if parsing was successful
50
+   */
51
+  bool sintNo(int &sint);
52
+
39 53
   /**
40 54
    * @brief check if parsing is done
41 55
    * @return if parsing is done (i.e. has arrived at the end of the string)
42 56