src/AwkHandler.h
branchv_0
changeset 18 2ac59242d0cb
parent 17 54716ea44d52
child 19 e4558df9ba2d
equal deleted inserted replaced
17:54716ea44d52 18:2ac59242d0cb
    99 
    99 
   100 		delete[] a;
   100 		delete[] a;
   101 		throw cli::RelpipeCLIException(L"Unable to do execvp().", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exceptions?
   101 		throw cli::RelpipeCLIException(L"Unable to do execvp().", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exceptions?
   102 	}
   102 	}
   103 
   103 
       
   104 	/* TODO: move to lib-cli when stable and used in other modules */
       
   105 	void setEnv(const char * name, const string_t& value) {
       
   106 		setenv(name, convertor.to_bytes(value).c_str(), true);
       
   107 	}
       
   108 
       
   109 	/* TODO: move to lib-cli when stable and used in other modules */
       
   110 	void setEnv(const char * name, std::string& value) {
       
   111 		setenv(name, value.c_str(), true);
       
   112 	}
       
   113 
       
   114 	/* TODO: move to lib-cli when stable and used in other modules */
       
   115 	void setEnv(const char * name, const integer_t& value) {
       
   116 		setenv(name, to_string(value).c_str(), true);
       
   117 	}
       
   118 
   104 	void addDefinition(std::vector<std::string>& awkCliArgs, DefinitionRecipe& d) {
   119 	void addDefinition(std::vector<std::string>& awkCliArgs, DefinitionRecipe& d) {
   105 		awkCliArgs.push_back("-v");
   120 		awkCliArgs.push_back("-v");
   106 		awkCliArgs.push_back(convertor.to_bytes(a2v(d.name) + L"=" + d.value));
   121 		awkCliArgs.push_back(convertor.to_bytes(a2v(d.name) + L"=" + d.value));
   107 	}
   122 	}
   108 
   123 
   186 				closeOrThrow(awkOutputReaderFD);
   201 				closeOrThrow(awkOutputReaderFD);
   187 
   202 
   188 				redirectFD(awkInputReaderFD, STDIN_FILENO);
   203 				redirectFD(awkInputReaderFD, STDIN_FILENO);
   189 				redirectFD(awkOutputWriterFD, STDOUT_FILENO);
   204 				redirectFD(awkOutputWriterFD, STDOUT_FILENO);
   190 
   205 
       
   206 				// Environment variables:
       
   207 				setEnv("relationName", name);
       
   208 				setEnv("inputAttributeCount", currentReaderMetadata.size());
       
   209 				setEnv("outputAttributeCount", currentWriterMetadata.size());
       
   210 				// TODO: better names? ENV vs. AWK variables?
       
   211 				for (int i = 0; i < currentReaderMetadata.size(); i++) {
       
   212 					setEnv((std::string("inputAttributeName") + std::to_string(i)).c_str(), currentReaderMetadata[i].getAttributeName());
       
   213 					setEnv("inputAttributeType" + i, currentReaderMetadata[i].getTypeName());
       
   214 				}
       
   215 				for (int i = 0; i < currentWriterMetadata.size(); i++) {
       
   216 					setEnv("outputAttributeName" + i, currentWriterMetadata[i].attributeName);
       
   217 					// setEnv("outputAttributeType" + i, currentWriterMetadata[i].typeId); // TODO: type?
       
   218 				}
       
   219 
   191 				// AWK script:
   220 				// AWK script:
   192 				std::wstringstream awkScript;
   221 				std::wstringstream awkScript;
   193 				awkScript << L"BEGIN {" << std::endl;
   222 				awkScript << L"BEGIN {" << std::endl;
   194 				awkScript << L"FS=\"\\t\";" << std::endl;
   223 				awkScript << L"FS=\"\\t\";" << std::endl;
       
   224 				awkScript << L"OFS=\"\\t\";" << std::endl;
   195 				awkScript << currentRelationConfiguration->awkBeforeRecords << std::endl;
   225 				awkScript << currentRelationConfiguration->awkBeforeRecords << std::endl;
   196 				awkScript << L"};" << std::endl;
   226 				awkScript << L"};" << std::endl;
   197 				awkScript << std::endl;
   227 				awkScript << std::endl;
   198 
   228 
   199 				awkScript << L"END {" << std::endl;
   229 				awkScript << L"END {" << std::endl;