AWK escaping and unescaping functions v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 25 May 2019 21:52:56 +0200
branchv_0
changeset 29 b3d1a671315b
parent 28 4fdbe30d8c58
child 30 5261dfd3b952
AWK escaping and unescaping functions
scripts/awk
src/AwkHandler.h
--- a/scripts/awk	Sat May 25 17:00:51 2019 +0200
+++ b/scripts/awk	Sat May 25 21:52:56 2019 +0200
@@ -60,6 +60,7 @@
 exit `cat "$logDir/awk-status.log"`;
 
 # Use e.g. GNU Screen with four windows to watch files:
+# tabs 2
 # watchFile() { while inotifywait "$1" &>/dev/null ; do clear; cat "$1" ; done; }
 # watchFile "/tmp/relpipe-tr-awk/awk-args.log"
 # watchFile "/tmp/relpipe-tr-awk/awk-stdin.log"
--- a/src/AwkHandler.h	Sat May 25 17:00:51 2019 +0200
+++ b/src/AwkHandler.h	Sat May 25 21:52:56 2019 +0200
@@ -324,18 +324,42 @@
 				awkScript << L"END {" << std::endl;
 				awkScript << currentRelationConfiguration->awkAfterRecords << std::endl;
 				awkScript << L"};" << std::endl;
-				awkScript << std::endl;
 
-				awkScript << L"function _escape(value) {" << std::endl;
-				// FIXME: escape function
-				awkScript << L"return value;" << std::endl;
-				awkScript << L"};" << std::endl;
-				awkScript << std::endl;
+				awkScript << LR"AWK(
+function _escape(value,    i) {
+	result = "";
+	split(value, chars, "");
+	for (i = 1; i <= length(chars); i++) {
+		ch = chars[i];
+		if (ch == "\\")      { ch = "\\\\"; }
+		else if (ch == "\t") { ch = "\\t"; }
+		else if (ch == "\n") { ch = "\\n"; }
+		result = result ch;
+	}
+	return result;
+};
+						
+function _unescape(value,    i) {
+	result = "";
+	split(value, chars, "");
+	for (i = 1; i <= length(chars); i++) {
+		ch = chars[i];
+		if (ch == "\\") {
+			ch = chars[++i];
+			if (ch == "\\")     { ch = "\\"; }
+			else if (ch == "t") { ch = "\t"; }
+			else if (ch == "n") { ch = "\n"; }
+			else {
+				printf("Unsupported escape sequence: %s\n", ch) > "/dev/stderr";
+				exit 70;
+			}
+		}
+		result = result ch;
+	}
+	return result;
+};
+)AWK";
 
-				awkScript << L"function _unescape(value) {" << std::endl;
-				// FIXME: unescape function
-				awkScript << L"return value;" << std::endl;
-				awkScript << L"};" << std::endl;
 				awkScript << std::endl;
 
 				awkScript << L"function _readVariables() {" << std::endl;