Marsyas  0.5.0-beta1
/Users/jleben/code/marsyas/src/marsyas/marojson.cpp
Go to the documentation of this file.
00001 /*
00002 ** Copyright (C) 1998-2005 George Tzanetakis <gtzan@cs.uvic.ca>
00003  ** 2010 Paul Reimer <pdreimer@engr.uvic.ca>
00004 **
00005 ** This program is free software; you can redistribute it and/or modify
00006 ** it under the terms of the GNU General Public License as published by
00007 ** the Free Software Foundation; either version 2 of the License, or
00008 ** (at your option) any later version.
00009 **
00010 ** This program is distributed in the hope that it will be useful,
00011 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 ** GNU General Public License for more details.
00014 **
00015 ** You should have received a copy of the GNU General Public License
00016 ** along with this program; if not, write to the Free Software
00017 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00018 */
00019 
00020 
00021 #include <marsyas/marojson.h>
00022 #include <sstream>
00023 #include <iostream>
00024 
00025 //using namespace std;
00026 using namespace Marsyas;
00027 
00028 marojson::marojson() : marostring()
00029 {
00030   this->prettyPrint = true;
00031 }
00032 
00033 marojson::~marojson()
00034 {
00035   clear();
00036 }
00037 
00038 void
00039 marojson::begin_marsystem(bool isComposite, std::string type, std::string name)
00040 {
00041   result_ << "'" << name << "':{";
00042 
00043   result_ << "'isComposite':";
00044   if(isComposite)
00045     result_ << "true";
00046   else
00047     result_ << "false";
00048 
00049   result_ << ",";
00050 
00051   result_ << "'type':'" << type << "',";
00052 }
00053 
00054 void
00055 marojson::end_marsystem(bool isComposite, std::string type, std::string name)
00056 {
00057   (void) isComposite;
00058   (void) type;
00059   (void) name;
00060   result_ << "},";
00061 }
00062 
00063 void
00064 marojson::begin_controls(int num_controls)
00065 {
00066   if (num_controls > 0)
00067     result_ << "'controls':[";
00068 }
00069 
00070 void
00071 marojson::begin_control(std::string type, std::string name, std::string value, bool has_state)
00072 {
00073   (void) has_state;
00074   result_ << "{";
00075   result_ << "'type':'" << type << "',";
00076   result_ << "'name':'" << name << "',";
00077 
00078   result_ << "'value':";
00079 
00080   if (type == "mrs_realvec" || type == "mrs_string")
00081     result_ << "'";
00082 
00083   if (value=="")
00084     result_ << "MARSYAS_EMPTYSTRING";
00085   else
00086     result_ << value;
00087 
00088   if (type == "mrs_realvec" || type == "mrs_string")
00089     result_ << "'";
00090 
00091   result_ << "',";
00092 }
00093 void
00094 marojson::begin_control_links_in(int num_links)
00095 {
00096   if (num_links > 0)
00097     result_ << "'linksTo':[";
00098 }
00099 void
00100 marojson::put_control_link_in(std::string abspath, std::string type, std::string name)
00101 {
00102   result_ << "{";
00103   result_ << "'abspath':'" << abspath << "',";
00104   result_ << "'type':'" << type << "',";
00105   result_ << "'name':'" << name << "',";
00106   result_ << "},";
00107 }
00108 
00109 void
00110 marojson::end_control_links_in(int num_links)
00111 {
00112   if (num_links > 0)
00113     result_ << "],";
00114 }
00115 
00116 void
00117 marojson::begin_control_links_out(int num_links)
00118 {
00119   if (num_links > 0)
00120     result_ << "'linksFrom':[";
00121 }
00122 void
00123 marojson::put_control_link_out(std::string abspath, std::string type, std::string name)
00124 {
00125   result_ << "{";
00126   result_ << "'abspath':'" << abspath << "',";
00127   result_ << "'type':'" << type << "',";
00128   result_ << "'name':'" << name << "',";
00129   result_ << "},";
00130 }
00131 void
00132 marojson::end_control_links_out(int num_links)
00133 {
00134   if (num_links > 0)
00135     result_ << "],";
00136 }
00137 
00138 void
00139 marojson::end_control(std::string type, std::string name, std::string value, bool has_state)
00140 {
00141   (void) type;
00142   (void) name;
00143   (void) value;
00144   (void) has_state;
00145   result_ << "},";
00146 }
00147 void
00148 marojson::end_controls(int num_links)
00149 {
00150   if (num_links > 0)
00151     result_ << "],";
00152 }
00153 
00154 void
00155 marojson::begin_children(int num_children)
00156 {
00157   if (num_children > 0)
00158     result_ << "'components':[";
00159 }
00160 
00161 void
00162 marojson::end_children(int num_children)
00163 {
00164   if (num_children > 0)
00165     result_ << "],";
00166 }
00167 
00168 std::string
00169 marojson::str()
00170 {
00171   std::string json = result_.str();
00172   replace_all(json, ",}", "}");
00173   replace_all(json, ",]", "]");
00174 
00175   if (prettyPrint)
00176   {
00177     replace_all(json, ":", ": ");
00178 
00179 //      smart_indent(json, "{[", "]}");
00180 
00181     replace_all(json, "{", "{\r\n");
00182     replace_all(json, "}", "\r\n}");
00183     replace_all(json, "[", "[\r\n");
00184     replace_all(json, "]", "\r\n]");
00185     replace_all(json, ",", ",\r\n");
00186   }
00187 
00188   return "{" + json + "}";
00189 }
00190 
00191 std::string&
00192 marojson::replace_between(std::string& str,
00193                           const std::string &oldsubstr, const std::string &newsubstr,
00194                           std::string::size_type from_pos, std::string::size_type to_pos)
00195 {
00196   std::string::size_type position;
00197 
00198   position = str.find(oldsubstr, from_pos);
00199   while (position != std::string::npos && position < to_pos)
00200   {
00201     str.replace(position, oldsubstr.size(), newsubstr);
00202     position = str.find(oldsubstr, position + newsubstr.size());
00203   }
00204 
00205   return str;
00206 }
00207 
00208 std::string&
00209 marojson::replace_all(std::string& str, const std::string &oldsubstr, const std::string &newsubstr)
00210 {
00211   return replace_between(str, oldsubstr, newsubstr, 0, str.size() - 1);
00212 }
00213 
00214 std::string&
00215 marojson::smart_indent(std::string& str, const std::string &start, const std::string &end)
00216 {
00217   (void) end;
00218   std::string::size_type position;
00219 
00220   position = str.find(start);
00221   while (position != std::string::npos)
00222   {
00223     str.replace(position, start.size(), start);
00224     position = str.find(start, position + start.size());
00225   }
00226 
00227   return str;
00228 }