Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3107_LearningALPS/HeuristicLab.ExtLibs/HeuristicLab.ProtobufCS/2.4.1/ProtobufCS/src/ProtocolBuffers/GeneratedMessageLite.cs

Last change on this file was 8295, checked in by abeham, 12 years ago

#1897:

  • Removed protocol buffers 0.9.1
  • Added protocol buffers 2.4.1
  • Updated proto processing command
File size: 7.0 KB
Line 
1#region Copyright notice and license
2
3// Protocol Buffers - Google's data interchange format
4// Copyright 2008 Google Inc.  All rights reserved.
5// http://github.com/jskeet/dotnet-protobufs/
6// Original C++/Java/Python code:
7// http://code.google.com/p/protobuf/
8//
9// Redistribution and use in source and binary forms, with or without
10// modification, are permitted provided that the following conditions are
11// met:
12//
13//     * Redistributions of source code must retain the above copyright
14// notice, this list of conditions and the following disclaimer.
15//     * Redistributions in binary form must reproduce the above
16// copyright notice, this list of conditions and the following disclaimer
17// in the documentation and/or other materials provided with the
18// distribution.
19//     * Neither the name of Google Inc. nor the names of its
20// contributors may be used to endorse or promote products derived from
21// this software without specific prior written permission.
22//
23// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
35#endregion
36
37using System;
38using System.Collections.Generic;
39using System.Globalization;
40using System.IO;
41using System.Text;
42
43namespace Google.ProtocolBuffers
44{
45    /// <summary>
46    /// All generated protocol message classes extend this class. It implements
47    /// most of the IMessage interface using reflection. Users
48    /// can ignore this class as an implementation detail.
49    /// </summary>
50    public abstract partial class GeneratedMessageLite<TMessage, TBuilder> : AbstractMessageLite<TMessage, TBuilder>
51        where TMessage : GeneratedMessageLite<TMessage, TBuilder>
52        where TBuilder : GeneratedBuilderLite<TMessage, TBuilder>
53    {
54        protected abstract TMessage ThisMessage { get; }
55
56        public override sealed string ToString()
57        {
58            using (StringWriter wtr = new StringWriter())
59            {
60                PrintTo(wtr);
61                return wtr.ToString();
62            }
63        }
64
65        /// <summary>
66        /// PrintTo() helper methods for Lite Runtime
67        /// </summary>
68        protected static void PrintField<T>(string name, IList<T> value, TextWriter writer)
69        {
70            foreach (T item in value)
71            {
72                PrintField(name, true, (object) item, writer);
73            }
74        }
75
76        /// <summary>
77        /// PrintTo() helper methods for Lite Runtime
78        /// </summary>
79        protected static void PrintField(string name, bool hasValue, object value, TextWriter writer)
80        {
81            if (!hasValue)
82            {
83                return;
84            }
85            if (value is IMessageLite)
86            {
87                writer.WriteLine("{0} {{", name);
88                ((IMessageLite) value).PrintTo(writer);
89                writer.WriteLine("}");
90            }
91            else if (value is ByteString || value is String)
92            {
93                writer.Write("{0}: \"", name);
94                if (value is String)
95                {
96                    EscapeBytes(Encoding.UTF8.GetBytes((string) value), writer);
97                }
98                else
99                {
100                    EscapeBytes(((ByteString) value), writer);
101                }
102                writer.WriteLine("\"");
103            }
104            else if (value is bool)
105            {
106                writer.WriteLine("{0}: {1}", name, (bool) value ? "true" : "false");
107            }
108            else if (value is IEnumLite)
109            {
110                writer.WriteLine("{0}: {1}", name, ((IEnumLite) value).Name);
111            }
112            else
113            {
114                writer.WriteLine("{0}: {1}", name, ((IConvertible) value).ToString(CultureInfo.InvariantCulture));
115            }
116        }
117
118        /// <summary>
119        /// COPIED from TextFormat
120        /// Escapes bytes in the format used in protocol buffer text format, which
121        /// is the same as the format used for C string literals.  All bytes
122        /// that are not printable 7-bit ASCII characters are escaped, as well as
123        /// backslash, single-quote, and double-quote characters.  Characters for
124        /// which no defined short-hand escape sequence is defined will be escaped
125        /// using 3-digit octal sequences.
126        /// The returned value is guaranteed to be entirely ASCII.
127        /// </summary>
128        private static void EscapeBytes(IEnumerable<byte> input, TextWriter writer)
129        {
130            foreach (byte b in input)
131            {
132                switch (b)
133                {
134                        // C# does not use \a or \v
135                    case 0x07:
136                        writer.Write("\\a");
137                        break;
138                    case (byte) '\b':
139                        writer.Write("\\b");
140                        break;
141                    case (byte) '\f':
142                        writer.Write("\\f");
143                        break;
144                    case (byte) '\n':
145                        writer.Write("\\n");
146                        break;
147                    case (byte) '\r':
148                        writer.Write("\\r");
149                        break;
150                    case (byte) '\t':
151                        writer.Write("\\t");
152                        break;
153                    case 0x0b:
154                        writer.Write("\\v");
155                        break;
156                    case (byte) '\\':
157                        writer.Write("\\\\");
158                        break;
159                    case (byte) '\'':
160                        writer.Write("\\\'");
161                        break;
162                    case (byte) '"':
163                        writer.Write("\\\"");
164                        break;
165                    default:
166                        if (b >= 0x20 && b < 128)
167                        {
168                            writer.Write((char) b);
169                        }
170                        else
171                        {
172                            writer.Write('\\');
173                            writer.Write((char) ('0' + ((b >> 6) & 3)));
174                            writer.Write((char) ('0' + ((b >> 3) & 7)));
175                            writer.Write((char) ('0' + (b & 7)));
176                        }
177                        break;
178                }
179            }
180        }
181    }
182}
Note: See TracBrowser for help on using the repository browser.