Savarese Software Research Corporation
PropertiesToString.cc
Go to the documentation of this file.
00001 /*
00002  * Copyright 2006-2009 Savarese Software Research Corporation
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     https://www.savarese.com/software/ApacheLicense-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include <ssrc/wispers/utility/PropertiesToString.h>
00018 
00019 #include <cstring>
00020 #include <cctype>
00021 
00022 namespace {
00023   const char * const lua_escape_chars = "\a\b\t\n\v\f\r\"\\";
00024   const char lua_escaped_chars[] = {
00025     'a', 'b', 't', 'n', 'v', 'f', 'r', '"', '\\'
00026   };
00027 
00028   enum { Assignment, OpenQuotedKey, CloseQuotedKey,
00029          OpenArray, CloseArray, ValueKey, NumTokens };
00030 
00031   const char * const OutputToken[2][NumTokens] = {
00032     { "=", "[\"",  "\"]=", "{", "}", "_value=" },
00033     { ":", "\"", "\":", "[", "]", "_value:" }
00034   };
00035 }
00036 
00037 __BEGIN_NS_SSRC_WSPR_UTILITY
00038 
00039 void escape_lua(string & result,
00040                 const char *text, const unsigned int text_size)
00041 {
00042   const char * const end = text + text_size;
00043   const char *pos;
00044   result.reserve(2*text_size);
00045 
00046   while(text < end && (pos = std::strpbrk(text, lua_escape_chars)) != 0) {
00047     result.append(text, (pos - text)).append(1, '\\')
00048       .append(1, lua_escaped_chars[std::strchr(lua_escape_chars, *pos) - lua_escape_chars]);
00049     text = pos + 1;
00050   }
00051 
00052   if(text < end) {
00053     result.append(text, (end - text));
00054   }
00055 }
00056 
00057 void PropertiesToString::operator()(const primitive_property_vector & v) const {
00058   _out << _token[OpenArray];
00059 
00060   for(primitive_property_vector::const_iterator && it = v.begin(),
00061         && end = v.end(); it != end; )
00062   {
00063     boost::apply_visitor(*this, *it);
00064     ++it;
00065     if(it != end) {
00066       _out << ",";
00067     }
00068   }
00069 
00070   _out << _token[CloseArray];
00071 }
00072 
00073 void PropertiesToString::operator()(const property_vector & v) const {
00074   _out << _token[OpenArray];
00075 
00076   for(property_vector::const_iterator && it = v.begin(), && end = v.end();
00077       it != end; )
00078   {
00079     PropertiesToString::operator()(*it);
00080     ++it;
00081     if(it != end) {
00082       _out << ",";
00083     }
00084   }
00085 
00086   _out << _token[CloseArray];
00087 }
00088 
00089 void PropertiesToString::enter(const std::string & key,
00090                                const Properties *props,
00091                                const bool is_last_child) const
00092 {
00093   const property_type *v = props->value();
00094 
00095   // Always quote keys because 1. required by JSON and 2. keys may contain
00096   // characters comprising invalid identifiers.
00097   _out << _token[OpenQuotedKey] << key << _token[CloseQuotedKey];
00098 
00099   if(props->is_leaf()) {
00100     if(v != 0) {
00101       boost::apply_visitor(*this, *v);
00102     }
00103   } else {
00104     _out << "{";
00105     if(v != 0) {
00106       _out << _token[ValueKey];
00107       boost::apply_visitor(*this, *v);
00108       _out << ",";
00109     }
00110   }
00111 }
00112 
00113 PropertiesToString::PropertiesToString(std::ostream & out,
00114                                        const ToFormat to_format) :
00115   _token(OutputToken[to_format]), _out(out)
00116 { }
00117 
00118 __END_NS_SSRC_WSPR_UTILITY

Savarese Software Research Corporation
Copyright © 2006-2011 Savarese Software Research Corporation. All rights reserved.