mirror of
				https://github.com/davidgiven/fluxengine.git
				synced 2025-10-31 11:17:01 -07:00 
			
		
		
		
	Preparse ConfigProto objects.
This commit is contained in:
		| @@ -3,15 +3,11 @@ | ||||
| #include "lib/flags.h" | ||||
| #include <fmt/format.h> | ||||
|  | ||||
| extern const std::map<std::string, std::string> formats; | ||||
| extern const std::map<std::string, const ConfigProto*> formats; | ||||
|  | ||||
| static ConfigProto findConfig(std::string name) | ||||
| static const ConfigProto& findConfig(std::string name) | ||||
| { | ||||
|     const auto data = formats.at(name); | ||||
|     ConfigProto config; | ||||
|     if (!config.ParseFromString(data)) | ||||
|         Error() << "bad config name: " + name; | ||||
|     return config; | ||||
|     return *formats.at(name); | ||||
| } | ||||
|  | ||||
| int main(int argc, const char* argv[]) | ||||
|   | ||||
| @@ -1,18 +1,19 @@ | ||||
| #!/bin/sh | ||||
| echo "#include <string>" | ||||
| echo "#include <map>" | ||||
| echo "class ConfigProto;" | ||||
|  | ||||
| word=$1 | ||||
| shift | ||||
|  | ||||
| for a in "$@"; do | ||||
| 	echo "extern std::string ${word}_${a}_pb();" | ||||
| 	echo "extern const ConfigProto ${word}_${a}_pb;" | ||||
| done | ||||
|  | ||||
| echo "extern const std::map<std::string, std::string> ${word};" | ||||
| echo "const std::map<std::string, std::string> ${word} = {" | ||||
| echo "extern const std::map<std::string, const ConfigProto*> ${word};" | ||||
| echo "const std::map<std::string, const ConfigProto*> ${word} = {" | ||||
| for a in "$@"; do | ||||
| 	echo "    { \"${a}\", ${word}_${a}_pb() }," | ||||
| 	echo "    { \"${a}\", &${word}_${a}_pb }," | ||||
| done | ||||
| echo "};" | ||||
|  | ||||
|   | ||||
| @@ -12,100 +12,100 @@ | ||||
|  | ||||
| static uint32_t readu8(std::string::iterator& it, std::string::iterator end) | ||||
| { | ||||
| 	int len; | ||||
|     int len; | ||||
|     uint32_t c = *it++; | ||||
|     if (c < 0x80) | ||||
|     { | ||||
|         /* Do nothing! */ | ||||
| 		len = 0; | ||||
|         len = 0; | ||||
|     } | ||||
|     else if (c < 0xc0) | ||||
|     { | ||||
|         /* Invalid character */ | ||||
|         c = -1; | ||||
| 		len = 0; | ||||
|         len = 0; | ||||
|     } | ||||
|     else if (c < 0xe0) | ||||
|     { | ||||
|         /* One trailing byte */ | ||||
|         c &= 0x1f; | ||||
| 		len = 1; | ||||
|         len = 1; | ||||
|     } | ||||
|     else if (c < 0xf0) | ||||
|     { | ||||
|         /* Two trailing bytes */ | ||||
|         c &= 0x0f; | ||||
| 		len = 2; | ||||
|         len = 2; | ||||
|     } | ||||
|     else if (c < 0xf8) | ||||
|     { | ||||
|         /* Three trailing bytes */ | ||||
|         c &= 0x07; | ||||
| 		len = 3; | ||||
|         len = 3; | ||||
|     } | ||||
|     else if (c < 0xfc) | ||||
|     { | ||||
|         /* Four trailing bytes */ | ||||
|         c &= 0x03; | ||||
| 		len = 4; | ||||
|         len = 4; | ||||
|     } | ||||
|     else | ||||
|     { | ||||
|         /* Five trailing bytes */ | ||||
|         c &= 0x01; | ||||
| 		len = 5; | ||||
|         len = 5; | ||||
|     } | ||||
|  | ||||
| 	while (len) | ||||
| 	{ | ||||
| 		if (it == end) | ||||
| 			break; | ||||
|     while (len) | ||||
|     { | ||||
|         if (it == end) | ||||
|             break; | ||||
|  | ||||
| 		uint8_t d = *it++; | ||||
| 		c <<= 6; | ||||
| 		c += d & 0x3f; | ||||
| 	} | ||||
|         uint8_t d = *it++; | ||||
|         c <<= 6; | ||||
|         c += d & 0x3f; | ||||
|     } | ||||
|     return c; | ||||
| } | ||||
|  | ||||
| int main(int argc, const char* argv[]) | ||||
| { | ||||
| 	PROTO message; | ||||
|     PROTO message; | ||||
|  | ||||
| 	std::ifstream input(argv[1]); | ||||
| 	if (!input) | ||||
| 	{ | ||||
| 		perror("couldn't open input file"); | ||||
| 		exit(1); | ||||
| 	} | ||||
|     std::ifstream input(argv[1]); | ||||
|     if (!input) | ||||
|     { | ||||
|         perror("couldn't open input file"); | ||||
|         exit(1); | ||||
|     } | ||||
|  | ||||
| 	std::stringstream ss; | ||||
| 	std::string s; | ||||
| 	while (std::getline(input, s, '\n')) | ||||
| 	{ | ||||
| 		if (s == "<<<") | ||||
| 		{ | ||||
| 			while (std::getline(input, s, '\n')) | ||||
| 			{ | ||||
| 				if (s == ">>>") | ||||
| 					break; | ||||
|     std::stringstream ss; | ||||
|     std::string s; | ||||
|     while (std::getline(input, s, '\n')) | ||||
|     { | ||||
|         if (s == "<<<") | ||||
|         { | ||||
|             while (std::getline(input, s, '\n')) | ||||
|             { | ||||
|                 if (s == ">>>") | ||||
|                     break; | ||||
|  | ||||
| 				ss << '"'; | ||||
| 				auto it = s.begin(); | ||||
| 				for (;;) | ||||
| 				{ | ||||
| 					uint32_t u = readu8(it, s.end()); | ||||
| 					if (!u) | ||||
| 						break; | ||||
|                 ss << '"'; | ||||
|                 auto it = s.begin(); | ||||
|                 for (;;) | ||||
|                 { | ||||
|                     uint32_t u = readu8(it, s.end()); | ||||
|                     if (!u) | ||||
|                         break; | ||||
|  | ||||
| 					ss << fmt::format("\\u{:04x}", u); | ||||
| 				} | ||||
| 				ss << "\\n\"\n"; | ||||
| 			} | ||||
| 		} | ||||
| 		else | ||||
| 			ss << s << '\n'; | ||||
| 	} | ||||
|                     ss << fmt::format("\\u{:04x}", u); | ||||
|                 } | ||||
|                 ss << "\\n\"\n"; | ||||
|             } | ||||
|         } | ||||
|         else | ||||
|             ss << s << '\n'; | ||||
|     } | ||||
|  | ||||
|     if (!google::protobuf::TextFormat::ParseFromString(ss.str(), &message)) | ||||
|     { | ||||
| @@ -113,34 +113,39 @@ int main(int argc, const char* argv[]) | ||||
|         exit(1); | ||||
|     } | ||||
|  | ||||
| 	std::ofstream output(argv[2]); | ||||
| 	if (!output) | ||||
| 	{ | ||||
| 		perror("couldn't open output file"); | ||||
| 		exit(1); | ||||
| 	} | ||||
|     std::ofstream output(argv[2]); | ||||
|     if (!output) | ||||
|     { | ||||
|         perror("couldn't open output file"); | ||||
|         exit(1); | ||||
|     } | ||||
|  | ||||
|     auto data = message.SerializeAsString(); | ||||
|  | ||||
| 	output << "#include <string>\n" | ||||
| 	       << "static const unsigned char data[] = {\n"; | ||||
|     output << "#include \"lib/proto.h\"\n" | ||||
|            << "#include <string_view>\n" | ||||
|            << "static const uint8_t rawData[] = {"; | ||||
|  | ||||
| 	int count = 0; | ||||
| 	for (char c : data) | ||||
| 	{ | ||||
|     int count = 0; | ||||
|     for (char c : data) | ||||
|     { | ||||
|         if (count == 0) | ||||
|             output << "\n\t"; | ||||
|         else | ||||
|             output << ' '; | ||||
|         output << fmt::format("0x{:02x},", (unsigned char)c); | ||||
|  | ||||
|         count = (count+1) & 7; | ||||
| 	} | ||||
|         count = (count + 1) & 7; | ||||
|     } | ||||
|  | ||||
| 	output << "};\n" | ||||
| 		   << fmt::format("extern std::string {}();\n", argv[3]) | ||||
| 		   << fmt::format("std::string {}()\n", argv[3]) | ||||
| 		   << "{ return std::string((const char*)data, sizeof(data)); }\n"; | ||||
|     output << "\n};\n"; | ||||
|     output << "extern const std::string_view " << argv[3] << "_data;\n"; | ||||
|     output << "const std::string_view " << argv[3] | ||||
|            << "_data = std::string_view((const char*)rawData, " << data.size() | ||||
|            << ");\n"; | ||||
|     output << "extern const ConfigProto " << argv[3] << ";\n"; | ||||
|     output << "const ConfigProto " << argv[3] << " = parseConfigBytes(" | ||||
|            << argv[3] << "_data);\n"; | ||||
|  | ||||
|     return 0; | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user