classes for format, size and position of Blinken frames
Stefan Schuermans

Stefan Schuermans commited on 2011-11-17 19:54:45
Showing 8 changed files, with 408 additions and 0 deletions.

... ...
@@ -0,0 +1,61 @@
1
+/* Blinker
2
+   Copyright 2011 Stefan Schuermans <stefan@blinkenarea.org>
3
+   Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html
4
+   a blinkenarea.org project */
5
+
6
+#include <sstream>
7
+#include <string>
8
+
9
+#include "Format.h"
10
+#include "StringParser.h"
11
+
12
+namespace Blinker {
13
+
14
+/// constructor
15
+Format::Format():
16
+  m_width(1),
17
+  m_height(1),
18
+  m_channels(1),
19
+  m_maxval(1)
20
+{
21
+}
22
+
23
+/**
24
+ * @brief parse from string format
25
+ * @param[in] str string format
26
+ * @return if parsing was successful
27
+ */
28
+bool Format::fromStr(const std::string &str)
29
+{
30
+  StringParser parser(str);
31
+  unsigned int width, height, channels, maxval1;
32
+
33
+  if (!parser.uintMin(1, width) ||
34
+      !parser.fixChr('x') ||
35
+      !parser.uintMin(1, height) ||
36
+      !parser.fixChr('-') ||
37
+      !parser.uintMin(1, channels) ||
38
+      !parser.fixChr('/') ||
39
+      !parser.uintMin(2, maxval1))
40
+    return false;
41
+  m_width = width;
42
+  m_height = height;
43
+  m_channels = channels;
44
+  m_maxval = maxval1 - 1;
45
+  return true;
46
+}
47
+
48
+/**
49
+ * @brief convert to string format
50
+ * @return string format
51
+ */
52
+std::string Format::toStr() const
53
+{
54
+  std::stringstream strm;
55
+  strm << m_width << "x" << m_height << "-"
56
+       << m_channels << "/" << (m_maxval + 1);
57
+  return strm.str();
58
+}
59
+
60
+} // namespace Blinker
61
+
... ...
@@ -0,0 +1,44 @@
1
+/* Blinker
2
+   Copyright 2011 Stefan Schuermans <stefan@blinkenarea.org>
3
+   Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html
4
+   a blinkenarea.org project */
5
+
6
+#ifndef FORMAT_H
7
+#define FORMAT_H
8
+
9
+#include <string>
10
+
11
+namespace Blinker {
12
+
13
+/// a Blinken frame format
14
+class Format
15
+{
16
+public:
17
+  /// constructor
18
+  Format();
19
+
20
+public:
21
+  /**
22
+   * @brief parse from string format
23
+   * @param[in] str string format
24
+   * @return if parsing was successful
25
+   */
26
+  bool fromStr(const std::string &str);
27
+
28
+  /**
29
+   * @brief convert to string format
30
+   * @return string format
31
+   */
32
+  std::string toStr() const;
33
+
34
+public:
35
+  unsigned int m_width;    ///< width of the frame in pixels
36
+  unsigned int m_height;   ///< height of the frame in pixels
37
+  unsigned int m_channels; ///< number of color channels per pixel
38
+  unsigned int m_maxval;   ///< maximum value for each channel
39
+}; // class Format
40
+
41
+} // namespace Blinker
42
+
43
+#endif // #ifndef FORMAT_H
44
+
... ...
@@ -0,0 +1,52 @@
1
+/* Blinker
2
+   Copyright 2011 Stefan Schuermans <stefan@blinkenarea.org>
3
+   Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html
4
+   a blinkenarea.org project */
5
+
6
+#include <sstream>
7
+#include <string>
8
+
9
+#include "Position.h"
10
+#include "StringParser.h"
11
+
12
+namespace Blinker {
13
+
14
+/// constructor
15
+Position::Position():
16
+  m_x(0),
17
+  m_y(0)
18
+{
19
+}
20
+
21
+/**
22
+ * @brief parse from string format
23
+ * @param[in] str string format
24
+ * @return if parsing was successful
25
+ */
26
+bool Position::fromStr(const std::string &str)
27
+{
28
+  StringParser parser(str);
29
+  unsigned int x, y;
30
+
31
+  if (!parser.uintMin(0, x) ||
32
+      !parser.fixChr(',') ||
33
+      !parser.uintMin(0, y))
34
+    return false;
35
+  m_x = x;
36
+  m_y = y;
37
+  return true;
38
+}
39
+
40
+/**
41
+ * @brief convert to string format
42
+ * @return string format
43
+ */
44
+std::string Position::toStr() const
45
+{
46
+  std::stringstream strm;
47
+  strm << m_x << "," << m_y;
48
+  return strm.str();
49
+}
50
+
51
+} // namespace Blinker
52
+
... ...
@@ -0,0 +1,42 @@
1
+/* Blinker
2
+   Copyright 2011 Stefan Schuermans <stefan@blinkenarea.org>
3
+   Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html
4
+   a blinkenarea.org project */
5
+
6
+#ifndef POSITION_H
7
+#define POSITION_H
8
+
9
+#include <string>
10
+
11
+namespace Blinker {
12
+
13
+/// a Blinken frame position
14
+class Position
15
+{
16
+public:
17
+  /// constructor
18
+  Position();
19
+
20
+public:
21
+  /**
22
+   * @brief parse from string format
23
+   * @param[in] str string format
24
+   * @return if parsing was successful
25
+   */
26
+  bool fromStr(const std::string &str);
27
+
28
+  /**
29
+   * @brief convert to string format
30
+   * @return string format
31
+   */
32
+  std::string toStr() const;
33
+
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)
37
+}; // class Position
38
+
39
+} // namespace Blinker
40
+
41
+#endif // #ifndef POSITION_H
42
+
... ...
@@ -0,0 +1,52 @@
1
+/* Blinker
2
+   Copyright 2011 Stefan Schuermans <stefan@blinkenarea.org>
3
+   Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html
4
+   a blinkenarea.org project */
5
+
6
+#include <sstream>
7
+#include <string>
8
+
9
+#include "Size.h"
10
+#include "StringParser.h"
11
+
12
+namespace Blinker {
13
+
14
+/// constructor
15
+Size::Size():
16
+  m_width(1),
17
+  m_height(1)
18
+{
19
+}
20
+
21
+/**
22
+ * @brief parse from string format
23
+ * @param[in] str string format
24
+ * @return if parsing was successful
25
+ */
26
+bool Size::fromStr(const std::string &str)
27
+{
28
+  StringParser parser(str);
29
+  unsigned int width, height;
30
+
31
+  if (!parser.uintMin(1, width) ||
32
+      !parser.fixChr('x') ||
33
+      !parser.uintMin(1, height))
34
+    return false;
35
+  m_width = width;
36
+  m_height = height;
37
+  return true;
38
+}
39
+
40
+/**
41
+ * @brief convert to string format
42
+ * @return string format
43
+ */
44
+std::string Size::toStr() const
45
+{
46
+  std::stringstream strm;
47
+  strm << m_width << "x" << m_height;
48
+  return strm.str();
49
+}
50
+
51
+} // namespace Blinker
52
+
... ...
@@ -0,0 +1,42 @@
1
+/* Blinker
2
+   Copyright 2011 Stefan Schuermans <stefan@blinkenarea.org>
3
+   Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html
4
+   a blinkenarea.org project */
5
+
6
+#ifndef SIZE_H
7
+#define SIZE_H
8
+
9
+#include <string>
10
+
11
+namespace Blinker {
12
+
13
+/// a Blinken frame size
14
+class Size
15
+{
16
+public:
17
+  /// constructor
18
+  Size();
19
+
20
+public:
21
+  /**
22
+   * @brief parse from string format
23
+   * @param[in] str string format
24
+   * @return if parsing was successful
25
+   */
26
+  bool fromStr(const std::string &str);
27
+
28
+  /**
29
+   * @brief convert to string format
30
+   * @return string format
31
+   */
32
+  std::string toStr() const;
33
+
34
+public:
35
+  unsigned int m_width;  ///< width of the frame in pixels
36
+  unsigned int m_height; ///< height of the frame in pixels
37
+}; // class Size
38
+
39
+} // namespace Blinker
40
+
41
+#endif // #ifndef SIZE_H
42
+
... ...
@@ -0,0 +1,62 @@
1
+/* Blinker
2
+   Copyright 2011 Stefan Schuermans <stefan@blinkenarea.org>
3
+   Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html
4
+   a blinkenarea.org project */
5
+
6
+#include <string>
7
+
8
+#include "StringParser.h"
9
+
10
+namespace Blinker {
11
+
12
+/**
13
+ * @brief constructor
14
+ * @param[in] str string to parse
15
+ */
16
+StringParser::StringParser(const std::string &str):
17
+  m_str(str),
18
+  m_it(str.begin())
19
+{
20
+}
21
+
22
+/**
23
+ * @brief parse fixed character
24
+ * @param[in] chr character to expect
25
+ * @return if expected character was found and processed
26
+ */
27
+bool StringParser::fixChr(char chr)
28
+{
29
+  if (m_it == m_str.end() || *m_it != chr)
30
+    return false;
31
+  ++m_it;
32
+  return true;
33
+}
34
+
35
+/**
36
+ * @brief parse unsigned number and check minimum
37
+ * @param[in] min minimum value to expect
38
+ * @param[out] uint number parsed from string
39
+ * @return if parsing was successful
40
+ */
41
+bool StringParser::uintMin(unsigned int min, unsigned int &uint)
42
+{
43
+  uint = 0;
44
+  while (m_it != m_str.end() && *m_it >= '0' && *m_it <= '9') {
45
+    uint *= 10;
46
+    uint += *m_it - '0';
47
+    ++m_it;
48
+  }
49
+  return uint >= min;
50
+}
51
+
52
+/**
53
+ * @brief check if parsing is done
54
+ * @return if parsing is done (i.e. has arrived at the end of the string)
55
+ */
56
+bool StringParser::isDone()
57
+{
58
+  return m_it == m_str.end();
59
+}
60
+
61
+} // namespace Blinker
62
+
... ...
@@ -0,0 +1,53 @@
1
+/* Blinker
2
+   Copyright 2011 Stefan Schuermans <stefan@blinkenarea.org>
3
+   Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html
4
+   a blinkenarea.org project */
5
+
6
+#ifndef STRINGPARSER_H
7
+#define STRINGPARSER_H
8
+
9
+#include <string>
10
+
11
+namespace Blinker {
12
+
13
+/// a simple string parser
14
+class StringParser
15
+{
16
+public:
17
+  /**
18
+   * @brief constructor
19
+   * @param[in] str string to parse
20
+   */
21
+  StringParser(const std::string &str);
22
+
23
+public:
24
+  /**
25
+   * @brief parse fixed character
26
+   * @param[in] chr character to expect
27
+   * @return if expected character was found and processed
28
+   */
29
+  bool fixChr(char chr);
30
+
31
+  /**
32
+   * @brief parse unsigned number and check minimum
33
+   * @param[in] min minimum value to expect
34
+   * @param[out] uint number parsed from string
35
+   * @return if parsing was successful
36
+   */
37
+  bool uintMin(unsigned int min, unsigned int &uint);
38
+
39
+  /**
40
+   * @brief check if parsing is done
41
+   * @return if parsing is done (i.e. has arrived at the end of the string)
42
+   */
43
+  bool isDone();
44
+
45
+protected:
46
+  std::string                 m_str; ///< string begin parsed
47
+  std::string::const_iterator m_it;  ///< current position of parsing
48
+}; // class StringParser
49
+
50
+} // namespace Blinker
51
+
52
+#endif // #ifndef STRINGPARSER_H
53
+
0 54