00001
00002 #include <iostream>
00003
00004 #include "SDXCSVStreamWriter.h"
00005
00006 using namespace std;
00007 using namespace SDX::Formats;
00008
00009 CsvStreamWriter::CsvStreamWriter(){
00010
00011 }
00012
00013 void CsvStreamWriter::startDocument(){
00014 m_nodeStackSize = 0;
00015 m_rowHasChildren = false;
00016 m_lastUnnamedAttribute = "";
00017 }
00018
00019 void CsvStreamWriter::startNode(string){
00020 ++m_nodeStackSize;
00021
00022 if(m_nodeStackSize == 1)
00023 m_rowHasChildren = false;
00024 else if(m_nodeStackSize == 2)
00025 m_lastUnnamedAttribute = "";
00026 }
00027
00028 void CsvStreamWriter::writeAttribute(string name, string value){
00029 if(m_nodeStackSize == 2 && name.empty())
00030 m_lastUnnamedAttribute = value;
00031 }
00032
00033 void CsvStreamWriter::endNode(){
00034 if(m_nodeStackSize == 2){
00035 if(m_rowHasChildren)
00036 *m_oStream << ",";
00037
00038 bool needsQuotes = m_lastUnnamedAttribute.find_first_of(",\"") != string::npos;
00039 if(needsQuotes)
00040 *m_oStream << '"';
00041
00042 *m_oStream << m_lastUnnamedAttribute;
00043
00044 if(needsQuotes)
00045 *m_oStream << '"';
00046
00047 m_rowHasChildren = true;
00048 } else if(m_nodeStackSize == 1)
00049 *m_oStream << endl;
00050
00051 --m_nodeStackSize;
00052 }