1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 // http://code.google.com/p/protobuf/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 //     * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 //     * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 // Author: kenton@google.com (Kenton Varda)
32 //  Based on original Protocol Buffers design by
33 //  Sanjay Ghemawat, Jeff Dean, and others.
34 
35 // Copyright (c) 2008-2013, Dave Benson.  All rights reserved.
36 //
37 // Redistribution and use in source and binary forms, with or without
38 // modification, are permitted provided that the following conditions are
39 // met:
40 //
41 //     * Redistributions of source code must retain the above copyright
42 // notice, this list of conditions and the following disclaimer.
43 //
44 //     * Redistributions in binary form must reproduce the above
45 // copyright notice, this list of conditions and the following disclaimer
46 // in the documentation and/or other materials provided with the
47 // distribution.
48 //
49 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
50 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
51 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
52 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
53 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
54 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
55 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
57 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
59 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60 
61 // Modified to implement C code by Dave Benson.
62 
63 #ifndef GOOGLE_PROTOBUF_COMPILER_C_HELPERS_H__
64 #define GOOGLE_PROTOBUF_COMPILER_C_HELPERS_H__
65 
66 #include <string>
67 #include <vector>
68 #include <sstream>
69 #include <google/protobuf/descriptor.h>
70 #include <protobuf-c/protobuf-c.pb.h>
71 #include <google/protobuf/io/printer.h>
72 
73 namespace google {
74 namespace protobuf {
75 namespace compiler {
76 namespace c {
77 
78 // --- Borrowed from stubs. ---
SimpleItoa(T n)79 template <typename T> std::string SimpleItoa(T n) {
80   std::stringstream stream;
81   stream << n;
82   return stream.str();
83 }
84 
85 std::string SimpleFtoa(float f);
86 std::string SimpleDtoa(double f);
87 void SplitStringUsing(const std::string &str, const char *delim, std::vector<std::string> *out);
88 std::string CEscape(const std::string& src);
89 std::string StringReplace(const std::string& s, const std::string& oldsub, const std::string& newsub, bool replace_all);
HasSuffixString(const std::string & str,const std::string & suffix)90 inline bool HasSuffixString(const std::string& str, const std::string& suffix) { return str.size() >= suffix.size() && str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0; }
StripSuffixString(const std::string & str,const std::string & suffix)91 inline std::string StripSuffixString(const std::string& str, const std::string& suffix) { if (HasSuffixString(str, suffix)) { return str.substr(0, str.size() - suffix.size()); } else { return str; } }
92 char* FastHexToBuffer(int i, char* buffer);
93 
94 
95 // Get the (unqualified) name that should be used for this field in C code.
96 // The name is coerced to lower-case to emulate proto1 behavior.  People
97 // should be using lowercase-with-underscores style for proto field names
98 // anyway, so normally this just returns field->name().
99 std::string FieldName(const FieldDescriptor* field);
100 
101 // Get macro string for deprecated field
102 std::string FieldDeprecated(const FieldDescriptor* field);
103 
104 // Returns the scope where the field was defined (for extensions, this is
105 // different from the message type to which the field applies).
FieldScope(const FieldDescriptor * field)106 inline const Descriptor* FieldScope(const FieldDescriptor* field) {
107   return field->is_extension() ?
108     field->extension_scope() : field->containing_type();
109 }
110 
111 // convert a CamelCase class name into an all uppercase affair
112 // with underscores separating words, e.g. MyClass becomes MY_CLASS.
113 std::string CamelToUpper(const std::string &class_name);
114 std::string CamelToLower(const std::string &class_name);
115 
116 // lowercased, underscored name to camel case
117 std::string ToCamel(const std::string &name);
118 
119 // lowercase the string
120 std::string ToLower(const std::string &class_name);
121 std::string ToUpper(const std::string &class_name);
122 
123 // full_name() to lowercase with underscores
124 std::string FullNameToLower(const std::string &full_name, const FileDescriptor *file);
125 std::string FullNameToUpper(const std::string &full_name, const FileDescriptor *file);
126 
127 // full_name() to c-typename (with underscores for packages, otherwise camel case)
128 std::string FullNameToC(const std::string &class_name, const FileDescriptor *file);
129 
130 // Splits, indents, formats, and prints comment lines
131 void PrintComment (io::Printer* printer, std::string comment);
132 
133 // make a string of spaces as long as input
134 std::string ConvertToSpaces(const std::string &input);
135 
136 // Strips ".proto" or ".protodevel" from the end of a filename.
137 std::string StripProto(const std::string& filename);
138 
139 // Get the C++ type name for a primitive type (e.g. "double", "::google::protobuf::int32", etc.).
140 // Note:  non-built-in type names will be qualified, meaning they will start
141 // with a ::.  If you are using the type as a template parameter, you will
142 // need to insure there is a space between the < and the ::, because the
143 // ridiculous C++ standard defines "<:" to be a synonym for "[".
144 const char* PrimitiveTypeName(FieldDescriptor::CppType type);
145 
146 // Get the declared type name in CamelCase format, as is used e.g. for the
147 // methods of WireFormat.  For example, TYPE_INT32 becomes "Int32".
148 const char* DeclaredTypeMethodName(FieldDescriptor::Type type);
149 
150 // Convert a file name into a valid identifier.
151 std::string FilenameIdentifier(const std::string& filename);
152 
153 // Return the name of the BuildDescriptors() function for a given file.
154 std::string GlobalBuildDescriptorsName(const std::string& filename);
155 
156 // return 'required', 'optional', or 'repeated'
157 std::string GetLabelName(FieldDescriptor::Label label);
158 
159 
160 // write IntRanges entries for a bunch of sorted values.
161 // returns the number of ranges there are to bsearch.
162 unsigned WriteIntRanges(io::Printer* printer, int n_values, const int *values, const std::string &name);
163 
164 struct NameIndex
165 {
166   unsigned index;
167   const char *name;
168 };
169 int compare_name_indices_by_name(const void*, const void*);
170 
171 // Return the syntax version of the file containing the field.
172 // This wrapper is needed to be able to compile against protobuf2.
FieldSyntax(const FieldDescriptor * field)173 inline int FieldSyntax(const FieldDescriptor* field) {
174 #ifdef HAVE_PROTO3
175   return field->file()->syntax() == FileDescriptor::SYNTAX_PROTO3 ? 3 : 2;
176 #else
177   return 2;
178 #endif
179 }
180 
181 }  // namespace c
182 }  // namespace compiler
183 }  // namespace protobuf
184 
185 }  // namespace google
186 #endif  // GOOGLE_PROTOBUF_COMPILER_C_HELPERS_H__
187