Aeon Engine c550894
AeonGames Open Source Game Engine
Loading...
Searching...
No Matches
CodeFieldValuePrinter.hpp
1/*
2Copyright (C) 2016-2022,2025,2026 Rodrigo Jose Hernandez Cordoba
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#ifdef _MSC_VER
18#pragma warning( push )
19#pragma warning( disable : PROTOBUF_WARNINGS )
20#endif
22#include <google/protobuf/text_format.h>
23#ifdef _MSC_VER
24#pragma warning( pop )
25#endif
26#include <sstream>
27namespace AeonGames
28{
29
31 class CodeFieldValuePrinter : public google::protobuf::TextFormat::FastFieldValuePrinter
32 {
33 public:
34 CodeFieldValuePrinter() : google::protobuf::TextFormat::FastFieldValuePrinter()
35 {
36 };
38 void PrintString ( const std::string & val, google::protobuf::TextFormat::BaseTextGenerator* base_text_generator ) const override
39 {
40 // Format multi-line strings by breaking them after each \n
41 // First, escape the string normally (quotes, backslashes, etc.)
42 std::string escaped;
43 escaped.reserve ( val.size() * 2 );
44 for ( char c : val )
45 {
46 switch ( c )
47 {
48 case '\\':
49 escaped += "\\\\";
50 break;
51 case '\"':
52 escaped += "\\\"";
53 break;
54 case '\n':
55 escaped += "\\n";
56 break;
57 case '\r':
58 escaped += "\\r";
59 break;
60 case '\t':
61 escaped += "\\t";
62 break;
63 default:
64 if ( c >= 32 && c < 127 )
65 {
66 escaped += c;
67 }
68 else
69 {
70 // Print as octal escape
71 char buf[5];
72 snprintf ( buf, sizeof ( buf ), "\\%03o", static_cast<unsigned char> ( c ) );
73 escaped += buf;
74 }
75 break;
76 }
77 }
78
79 // Now split the escaped string at each \n occurrence
80 std::ostringstream output;
81 output << '"';
82
83 size_t pos = 0;
84 size_t found;
85 while ( ( found = escaped.find ( "\\n", pos ) ) != std::string::npos )
86 {
87 // Print up to and including the \n
88 output << escaped.substr ( pos, found - pos + 2 );
89 // Check if there's more content after this \n
90 if ( found + 2 < escaped.size() )
91 {
92 output << "\"\n\"";
93 }
94 pos = found + 2;
95 }
96
97 // Print remaining content
98 if ( pos < escaped.size() )
99 {
100 output << escaped.substr ( pos );
101 }
102
103 output << '"';
104 base_text_generator->PrintString ( output.str() );
105 }
106 };
107}
Provides the DLL_PROTOBUF export/import macro for protobuf wrapper classes.
void PrintString(const std::string &val, google::protobuf::TextFormat::BaseTextGenerator *base_text_generator) const override
Print a string value with code-style formatting and escaping.
<- This is here just for the literals
Definition AABB.hpp:31