updated: SpaceToJSON (not finished yet)

git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@428 e52654a7-88a9-db11-a3e9-0013d4bc506e
master
Tomasz Sowa 11 years ago
parent 2af6c28a54
commit 2b88d011f6

@ -1,4 +1,6 @@
# DO NOT DELETE
jsontospaceparser.o: jsontospaceparser.h space.h ../utf8/utf8.h
space.o: space.h ../utf8/utf8.h
spaceparser.o: spaceparser.h space.h ../utf8/utf8.h
spacetojson.o: spacetojson.h space.h

@ -1 +1 @@
o = space.o spaceparser.o
o = jsontospaceparser.o space.o spaceparser.o spacetojson.o

@ -42,8 +42,21 @@ namespace PT
{
void SpaceToJSON::ChangeSpacesToTable(const wchar_t * space_name)
{
spaces_to_table.push_back(space_name);
}
bool SpaceToJSON::HasUnnamedSpace(Space & space) const
void SpaceToJSON::ChangeSpacesToTable(const std::wstring & space_name)
{
spaces_to_table.push_back(space_name);
}
bool SpaceToJSON::HasUnnamedSpace(Space & space)
{
for(size_t i=0 ; i<space.spaces.size() ; ++i)
if( space.spaces[i]->name.empty() )
@ -54,6 +67,56 @@ return false;
bool SpaceToJSON::HasSpace(Space & space, const std::wstring & name)
{
for(size_t i=0 ; i<space.spaces.size() ; ++i)
if( space.spaces[i]->name == name )
return true;
return false;
}
void SpaceToJSON::TreatAsNumeric(const wchar_t * name)
{
numeric.insert(name);
}
void SpaceToJSON::TreatAsNumeric(const std::wstring & name)
{
numeric.insert(name);
}
void SpaceToJSON::TreatAsBool(const wchar_t * name)
{
boolean.insert(name);
}
void SpaceToJSON::TreatAsBool(const std::wstring & name)
{
boolean.insert(name);
}
bool SpaceToJSON::IsNumeric(const std::wstring & name)
{
std::set<std::wstring>::iterator i = numeric.find(name);
return i != numeric.end();
}
bool SpaceToJSON::IsBool(const std::wstring & name)
{
std::set<std::wstring>::iterator i = boolean.find(name);
return i != boolean.end();
}

@ -38,6 +38,9 @@
#ifndef headerfile_picotools_space_spacetojson
#define headerfile_picotools_space_spacetojson
#include <string>
#include <vector>
#include <set>
#include "space.h"
@ -49,26 +52,44 @@ class SpaceToJSON
{
public:
void ChangeSpacesToTable(const wchar_t * space_name);
void ChangeSpacesToTable(const std::wstring & space_name);
void TreatAsNumeric(const wchar_t * name);
void TreatAsNumeric(const std::wstring & name);
void TreatAsBool(const wchar_t * name);
void TreatAsBool(const std::wstring & name);
template<class Stream>
void Serialize(Space & space, Stream & out, bool use_indents = false, int level = 0) const;
void Serialize(Space & space, Stream & out, bool use_indents = false, int level = 0, bool use_comma = false, bool skip_name = false);
template<class Stream>
void SerializeTableSingle(Space & space, Stream & out, bool use_indents, int level) const;
void SerializeTableSingle(Space & space, Stream & out, bool use_indents, int level, bool use_comma);
template<class Stream>
void SerializeTableMulti(Space & space, Stream & out, bool use_indents, int level) const;
void SerializeTableMulti(Space & space, Stream & out, bool use_indents, int level, bool use_comma);
template<class Stream, class StringType>
static void PrintToken(Stream & out, const StringType & str);
void PrintToken(Stream & out, const StringType & str, bool check_specials = false);
template<class Stream>
static void PrintLevel(Stream & out, bool use_indents, int level);
void PrintLevel(Stream & out, bool use_indents, int level);
private:
bool HasUnnamedSpace(Space & space) const;
bool HasUnnamedSpace(Space & space);
bool HasSpace(Space & space, const std::wstring & name);
bool IsNumeric(const std::wstring & name);
bool IsBool(const std::wstring & name);
std::vector<std::wstring> spaces_to_table;
std::vector<bool> space_used;
std::set<std::wstring> numeric, boolean;
};
@ -88,9 +109,10 @@ void SpaceToJSON::PrintLevel(Stream & out, bool use_indents, int level)
template<class Stream, class StringType>
void SpaceToJSON::PrintToken(Stream & out, const StringType & str)
void SpaceToJSON::PrintToken(Stream & out, const StringType & str, bool is_special)
{
out << '\"';
if( !is_special )
out << '\"';
for(size_t i=0 ; i<str.size() ; ++i)
{
@ -109,25 +131,38 @@ void SpaceToJSON::PrintToken(Stream & out, const StringType & str)
}
}
out << '\"';
if( !is_special )
out << '\"';
}
template<class Stream>
void SpaceToJSON::SerializeTableSingle(Space & space, Stream & out, bool use_indents, int level) const
void SpaceToJSON::SerializeTableSingle(Space & space, Stream & out, bool use_indents, int level, bool use_comma)
{
Space::TableSingle::const_iterator i;
size_t index = 0;
bool is_special;
if( use_comma && !space.table_single.empty() )
{
PrintLevel(out, use_indents, level);
out << L",\n";
}
for(i=space.table_single.begin() ; i != space.table_single.end() ; ++i, ++index)
{
is_special = IsNumeric(i->first) || IsBool(i->first);
PrintLevel(out, use_indents, level);
PrintToken(out, i->first);
out << L": ";
PrintToken(out, i->second);
PrintToken(out, i->second, is_special);
if( index + 1 < space.table_single.size() || !space.table.empty() )
if( index + 1 < space.table_single.size() )
out << ',';
out << '\n';
@ -137,14 +172,23 @@ size_t index = 0;
template<class Stream>
void SpaceToJSON::SerializeTableMulti(Space & space, Stream & out, bool use_indents, int level) const
void SpaceToJSON::SerializeTableMulti(Space & space, Stream & out, bool use_indents, int level, bool use_comma)
{
Space::Table::const_iterator i2;
size_t v;
size_t index = 0;
bool is_special;
if( use_comma && !space.table.empty() )
{
PrintLevel(out, use_indents, level);
out << L",\n";
}
for(i2 = space.table.begin() ; i2 != space.table.end() ; ++i2, ++index)
{
is_special = IsNumeric(i2->first) || IsBool(i2->first);
PrintLevel(out, use_indents, level);
PrintToken(out, i2->first);
out << L": ";
@ -157,7 +201,7 @@ size_t index = 0;
if( v > 0 )
PrintLevel(out, use_indents, level + i2->first.size() + 3);
PrintToken(out, i2->second[v]);
PrintToken(out, i2->second[v], is_special);
if( v + 1 < i2->second.size() )
out << L",\n";
@ -166,7 +210,7 @@ size_t index = 0;
if( i2->second.size() != 1 )
out << ']';
if( index + 1 < space.table.size() || !space.spaces.empty() )
if( index + 1 < space.table.size() )
out << ',';
out << '\n';
@ -177,49 +221,86 @@ size_t index = 0;
template<class Stream>
void SpaceToJSON::Serialize(Space & space, Stream & out, bool use_indents, int level) const
void SpaceToJSON::Serialize(Space & space, Stream & out, bool use_indents, int level, bool use_comma, bool skip_name)
{
// if( level > 0 )
// out << '\n';
if( use_comma )
{
PrintLevel(out, use_indents, level);
out << L",\n";
}
PrintLevel(out, use_indents, level);
if( !space.name.empty() )
if( !skip_name )
{
PrintToken(out, space.name);
out << L": ";
if( !space.name.empty() )
{
PrintToken(out, space.name);
out << L": ";
}
}
bool is_table = HasUnnamedSpace(space);
out << L"{\n";
if( is_table )
out << L"[\n";
else
out << L"{\n";
bool printed_something = false;
// !! what about if table or table.single has values
// and there is the table? (not supported yet)
SerializeTableSingle(space, out, use_indents, level, false);
SerializeTableMulti(space, out, use_indents, level, !space.table_single.empty());
SerializeTableSingle(space, out, use_indents, level);
SerializeTableMulti(space, out, use_indents, level);
if( !space.table_single.empty() || !space.table.empty() )
printed_something = true;
for(size_t i=0 ; i<space.spaces.size() ; ++i)
space_used.resize(space.spaces.size());
for(size_t i=0 ; i<space_used.size() ; ++i)
space_used[i] = false;
for(size_t z=0 ; z<spaces_to_table.size() ; ++z)
{
Serialize(*space.spaces[i], out, use_indents, level+1);
if( HasSpace(space, spaces_to_table[z]) )
{
if( printed_something )
{
out << L",\n";
}
PrintToken(out, spaces_to_table[z]);
out << L": [\n";
for(size_t i=0 ; i<space.spaces.size() ; ++i)
{
if( space.spaces[i]->name == spaces_to_table[z] )
{
space_used[i] = true;
Serialize(*space.spaces[i], out, use_indents, level+1, i>0, true);
}
}
out << L"]\n";
printed_something = true;
}
}
if( i + 1 < space.spaces.size() )
for(size_t i=0 ; i<space.spaces.size() ; ++i)
{
if( !space_used[i] )
{
PrintLevel(out, use_indents, level);
out << L",\n";
Serialize(*space.spaces[i], out, use_indents, level+1, printed_something || i>0);
if( i + 1 < space.spaces.size() )
{
PrintLevel(out, use_indents, level);
out << L",\n";
}
printed_something = true;
}
}
PrintLevel(out, use_indents, level);
if( is_table )
out << L"]\n";
else
out << L"}\n";
out << L"}\n";
}

Loading…
Cancel
Save