src/AwkHandler.h
branchv_0
changeset 30 5261dfd3b952
parent 29 b3d1a671315b
child 31 64d9244ee252
equal deleted inserted replaced
29:b3d1a671315b 30:5261dfd3b952
    36 #include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
    36 #include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
    37 #include <relpipe/reader/handlers/AttributeMetadata.h>
    37 #include <relpipe/reader/handlers/AttributeMetadata.h>
    38 
    38 
    39 #include <relpipe/writer/Factory.h>
    39 #include <relpipe/writer/Factory.h>
    40 
    40 
    41 #include <relpipe/cli/RelpipeCLIException.h>
       
    42 
       
    43 #include "Configuration.h"
    41 #include "Configuration.h"
    44 #include "AwkException.h"
    42 #include "AwkException.h"
    45 
    43 
    46 namespace relpipe {
    44 namespace relpipe {
    47 namespace tr {
    45 namespace tr {
   136 			closeOrThrow(awkInputWriterFD);
   134 			closeOrThrow(awkInputWriterFD);
   137 			int error1;
   135 			int error1;
   138 			int error2;
   136 			int error2;
   139 			__pid_t waitPID1 = wait(&error1);
   137 			__pid_t waitPID1 = wait(&error1);
   140 			__pid_t waitPID2 = wait(&error2);
   138 			__pid_t waitPID2 = wait(&error2);
   141 			if (error1 || error2) throw cli::RelpipeCLIException(L"The child process returned an error exit code.", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exceptions?
   139 			if (error1 || error2) throw AwkException(L"The child process returned an error exit code.");
   142 			awkInputWriterFD = -1;
   140 			awkInputWriterFD = -1;
   143 		}
   141 		}
   144 
   142 
   145 		currentAttributeIndex = 0;
   143 		currentAttributeIndex = 0;
   146 		currentReaderMetadata.clear();
   144 		currentReaderMetadata.clear();
   171 	}
   169 	}
   172 
   170 
   173 	string_t escapeAwkVariableName(const string_t& attributeName) {
   171 	string_t escapeAwkVariableName(const string_t& attributeName) {
   174 		// cat awkgram.y | awk -v FS='\\{"|",' -v ORS='|' '/static const struct token tokentab/, /\};/ { if (/^\{/) { print $2} }'
   172 		// cat awkgram.y | awk -v FS='\\{"|",' -v ORS='|' '/static const struct token tokentab/, /\};/ { if (/^\{/) { print $2} }'
   175 		// cat AwkHandler.h | awk -v FS=' |\\(' -v ORS='|' '/awkScript.*"function/ { print $4; }'
   173 		// cat AwkHandler.h | awk -v FS=' |\\(' -v ORS='|' '/awkScript.*"function/ { print $4; }'
   176 		std::wregex awkReservedNames(L"BEGIN|BEGINFILE|END|ENDFILE|adump|and|asort|asorti|atan2|bindtextdomain|break|case|close|compl|continue|cos|dcgettext|dcngettext|default|delete|do|else|eval|exit|exp|fflush|for|func|function|gensub|getline|gsub|if|in|include|index|int|intdiv0|isarray|length|load|log|lshift|match|mktime|namespace|next|nextfile|or|patsplit|print|printf|rand|return|rshift|sin|split|sprintf|sqrt|srand|stopme|strftime|strtonum|sub|substr|switch|system|systime|tolower|toupper|typeof|while|xor");
   174 		std::wregex awkReservedNames(L"FS|OFS|NR|NF|" L"BEGIN|BEGINFILE|END|ENDFILE|adump|and|asort|asorti|atan2|bindtextdomain|break|case|close|compl|continue|cos|dcgettext|dcngettext|default|delete|do|else|eval|exit|exp|fflush|for|func|function|gensub|getline|gsub|if|in|include|index|int|intdiv0|isarray|length|load|log|lshift|match|mktime|namespace|next|nextfile|or|patsplit|print|printf|rand|return|rshift|sin|split|sprintf|sqrt|srand|stopme|strftime|strtonum|sub|substr|switch|system|systime|tolower|toupper|typeof|while|xor");
   177 		std::wregex trReservedNames(L"_escape|_unescape|_readVariables|_writeVariables|record");
   175 		std::wregex trReservedNames(L"_escape|_unescape|_readVariables|_writeVariables|record");
   178 		std::wregex badCharacters(L"[^a-zA-Z0-9_]|^([0-9])");
   176 		std::wregex badCharacters(L"[^a-zA-Z0-9_]|^([0-9])");
   179 
   177 
   180 		const string_t& name = std::regex_replace(attributeName, badCharacters, L"_$1");
   178 		const string_t& name = std::regex_replace(attributeName, badCharacters, L"_$1");
   181 
   179