streamlet examples: QR: value-pattern option v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sun, 25 Apr 2021 19:26:43 +0200
branchv_0
changeset 90 59d12eee189f
parent 89 25a11859975b
child 91 cb1adcd17d0c
streamlet examples: QR: value-pattern option e.g. --option value-pattern "urn:uuid:.*" will look for QR codes containing URIs like „urn:uuid:c3e98a0d-97cf-4348-b46e-2bbb115ceea3“
streamlet-examples/qr-decode.cpp
--- a/streamlet-examples/qr-decode.cpp	Sun Apr 25 18:47:57 2021 +0200
+++ b/streamlet-examples/qr-decode.cpp	Sun Apr 25 19:26:43 2021 +0200
@@ -31,7 +31,11 @@
  *  - qr: first QR code found (if any)
  *  - qr_count: number of QR codes found
  *  - qr_xml: XML containing all QR cpdes found an additional metadata
- *	
+ * 
+ * Options:
+ *  - value-pattern: regular expression describing expected value of QR code;
+ *    if one or more options are given, the "qr" attribute will contain first value matching any of these patterns
+ * 
  */
 class QRStreamlet : public Streamlet {
 private:
@@ -110,6 +114,13 @@
 		return result;
 	}
 
+	bool matchesAny(const std::wstring& value, const std::vector<std::wregex>& valuePatterns) {
+		for (const std::wregex& pattern : valuePatterns) {
+			if (std::regex_match(value, pattern)) return true;
+		}
+		return false;
+	}
+
 protected:
 
 	std::vector<AttributeMetadata> getOutputAttributesMetadata() override {
@@ -137,11 +148,16 @@
 			validInput = false;
 		}
 
+		std::vector<Option> valuePatternOptions = getOptions(L"value-pattern");
+		std::vector<std::wregex> valuePatterns;
+		for (Option o : valuePatternOptions) valuePatterns.push_back(std::wregex(o.value));
+
 		for (Symbol s : symbols) {
-			// TODO: match against pattern (if any)
-			first = s.value;
-			hasSymbols = true;
-			break;
+			if (valuePatterns.size() == 0 || matchesAny(s.value, valuePatterns)) {
+				first = s.value;
+				hasSymbols = true;
+				break;
+			}
 		}
 
 		relpipe::xmlwriter::XMLWriter xmlWriter(xml);
@@ -156,7 +172,7 @@
 		for (Symbol s : symbols) {
 			xmlWriter.writeStartElement(L"symbol");
 			xmlWriter.writeTextElement(L"value",{}, s.value);
-			
+
 			// TODO: well-designed XML schema
 			// TODO: synchronize/share common XML parts with relpipe-in-qr
 			xmlWriter.writeStartElement(L"rectangular-box");