implemented setting file for boolean values
Stefan Schuermans

Stefan Schuermans commited on 2011-12-29 14:01:24
Showing 5 changed files, with 148 additions and 0 deletions.

... ...
@@ -0,0 +1,46 @@
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 "Bool.h"
9
+#include "StringParser.h"
10
+
11
+namespace Blinker {
12
+
13
+/// constructor
14
+Bool::Bool():
15
+  m_bool(false)
16
+{
17
+}
18
+
19
+/**
20
+ * @brief parse from string format
21
+ * @param[in] str string format
22
+ * @return if parsing was successful
23
+ */
24
+bool Bool::fromStr(const std::string &str)
25
+{
26
+  StringParser parser(str);
27
+  bool boolVal;
28
+
29
+  if (!parser.boolVal(boolVal) ||
30
+      !parser.isDone())
31
+    return false;
32
+  m_bool = boolVal;
33
+  return true;
34
+}
35
+
36
+/**
37
+ * @brief convert to string format
38
+ * @return string format
39
+ */
40
+std::string Bool::toStr() const
41
+{
42
+  return m_bool ? "true" : "false";
43
+}
44
+
45
+} // namespace Blinker
46
+
... ...
@@ -0,0 +1,41 @@
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 BLINKER_BOOL_H
7
+#define BLINKER_BOOL_H
8
+
9
+#include <string>
10
+
11
+namespace Blinker {
12
+
13
+/// a bolean value
14
+class Bool
15
+{
16
+public:
17
+  /// constructor
18
+  Bool();
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_bool; ///< boolean value
36
+}; // class Bool
37
+
38
+} // namespace Blinker
39
+
40
+#endif // #ifndef BLINKER_BOOL_H
41
+
... ...
@@ -0,0 +1,20 @@
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 BLINKER_BOOLFILE_H
7
+#define BLINKER_BOOLFILE_H
8
+
9
+#include "Bool.h"
10
+#include "SettingFile_impl.h"
11
+
12
+namespace Blinker {
13
+
14
+/// setting file containting unsigned integer
15
+typedef SettingFile<Bool> BoolFile;
16
+
17
+} // namespace Blinker
18
+
19
+#endif // #ifndef BLINKER_BOOLFILE_H
20
+
... ...
@@ -47,6 +47,40 @@ bool StringParser::oneChrOf(const std::string &set, char &chr)
47 47
   return true;
48 48
 }
49 49
 
50
+/**
51
+ * @brief parse boolean value
52
+ * @param[out] boolVal boolean value parsed from string
53
+ * @return if parsing was successful
54
+ */
55
+bool StringParser::boolVal(bool &boolVal)
56
+{
57
+  if (m_it == m_str.end())
58
+    return false;
59
+  if (*m_it == '0') {
60
+    boolVal = false;
61
+    return true;
62
+  }
63
+  if (*m_it == '1') {
64
+    boolVal = true;
65
+    return true;
66
+  }
67
+  if (*m_it == 'f') {
68
+    boolVal = false;
69
+    const char *ptr = "false";
70
+    while (m_it != m_str.end() && *ptr && *m_it == *ptr)
71
+      ++m_it, ++ptr;
72
+    return !*ptr;
73
+  }
74
+  if (*m_it == 't') {
75
+    boolVal = true;
76
+    const char *ptr = "true";
77
+    while (m_it != m_str.end() && *ptr && *m_it == *ptr)
78
+      ++m_it, ++ptr;
79
+    return !*ptr;
80
+  }
81
+  return false;
82
+}
83
+
50 84
 /**
51 85
  * @brief parse unsigned number
52 86
  * @param[out] uint number parsed from string
... ...
@@ -36,6 +36,13 @@ public:
36 36
    */
37 37
   bool oneChrOf(const std::string &set, char &chr);
38 38
 
39
+  /**
40
+   * @brief parse boolean value
41
+   * @param[out] boolVal boolean value parsed from string
42
+   * @return if parsing was successful
43
+   */
44
+  bool boolVal(bool &boolVal);
45
+
39 46
   /**
40 47
    * @brief parse unsigned number
41 48
    * @param[out] uint number parsed from string
42 49