environment variables v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Tue, 07 May 2019 21:25:45 +0200
branchv_0
changeset 18 2ac59242d0cb
parent 17 54716ea44d52
child 19 e4558df9ba2d
environment variables
src/AwkHandler.h
--- a/src/AwkHandler.h	Tue May 07 17:03:07 2019 +0200
+++ b/src/AwkHandler.h	Tue May 07 21:25:45 2019 +0200
@@ -101,6 +101,21 @@
 		throw cli::RelpipeCLIException(L"Unable to do execvp().", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exceptions?
 	}
 
+	/* TODO: move to lib-cli when stable and used in other modules */
+	void setEnv(const char * name, const string_t& value) {
+		setenv(name, convertor.to_bytes(value).c_str(), true);
+	}
+
+	/* TODO: move to lib-cli when stable and used in other modules */
+	void setEnv(const char * name, std::string& value) {
+		setenv(name, value.c_str(), true);
+	}
+
+	/* TODO: move to lib-cli when stable and used in other modules */
+	void setEnv(const char * name, const integer_t& value) {
+		setenv(name, to_string(value).c_str(), true);
+	}
+
 	void addDefinition(std::vector<std::string>& awkCliArgs, DefinitionRecipe& d) {
 		awkCliArgs.push_back("-v");
 		awkCliArgs.push_back(convertor.to_bytes(a2v(d.name) + L"=" + d.value));
@@ -188,10 +203,25 @@
 				redirectFD(awkInputReaderFD, STDIN_FILENO);
 				redirectFD(awkOutputWriterFD, STDOUT_FILENO);
 
+				// Environment variables:
+				setEnv("relationName", name);
+				setEnv("inputAttributeCount", currentReaderMetadata.size());
+				setEnv("outputAttributeCount", currentWriterMetadata.size());
+				// TODO: better names? ENV vs. AWK variables?
+				for (int i = 0; i < currentReaderMetadata.size(); i++) {
+					setEnv((std::string("inputAttributeName") + std::to_string(i)).c_str(), currentReaderMetadata[i].getAttributeName());
+					setEnv("inputAttributeType" + i, currentReaderMetadata[i].getTypeName());
+				}
+				for (int i = 0; i < currentWriterMetadata.size(); i++) {
+					setEnv("outputAttributeName" + i, currentWriterMetadata[i].attributeName);
+					// setEnv("outputAttributeType" + i, currentWriterMetadata[i].typeId); // TODO: type?
+				}
+
 				// AWK script:
 				std::wstringstream awkScript;
 				awkScript << L"BEGIN {" << std::endl;
 				awkScript << L"FS=\"\\t\";" << std::endl;
+				awkScript << L"OFS=\"\\t\";" << std::endl;
 				awkScript << currentRelationConfiguration->awkBeforeRecords << std::endl;
 				awkScript << L"};" << std::endl;
 				awkScript << std::endl;