Added support for emitting and represeting null

This commit is contained in:
jbeder
2009-07-30 04:42:27 +00:00
parent a2bd317397
commit c043b9c64b
10 changed files with 576 additions and 514 deletions

View File

@@ -4,6 +4,7 @@
#define CONVERSION_H_62B23520_7C8E_11DE_8A39_0800200C9A66 #define CONVERSION_H_62B23520_7C8E_11DE_8A39_0800200C9A66
#include "null.h"
#include <string> #include <string>
#include <sstream> #include <sstream>
@@ -37,6 +38,9 @@ namespace YAML
template <> template <>
bool Converter<bool>::Convert(const std::string& input, bool& output); bool Converter<bool>::Convert(const std::string& input, bool& output);
template <>
bool Converter<_Null>::Convert(const std::string& input, _Null& output);
template <> template <>
bool Converter<std::wstring>::Convert(const std::string& input, std::wstring& output); bool Converter<std::wstring>::Convert(const std::string& input, std::wstring& output);
} }

View File

@@ -6,6 +6,7 @@
#include "emittermanip.h" #include "emittermanip.h"
#include "ostream.h" #include "ostream.h"
#include "null.h"
#include <memory> #include <memory>
#include <string> #include <string>
@@ -51,6 +52,7 @@ namespace YAML
Emitter& Write(const _Alias& alias); Emitter& Write(const _Alias& alias);
Emitter& Write(const _Anchor& anchor); Emitter& Write(const _Anchor& anchor);
Emitter& Write(const _Comment& comment); Emitter& Write(const _Comment& comment);
Emitter& Write(const _Null& null);
private: private:
enum ATOMIC_TYPE { AT_SCALAR, AT_SEQ, AT_BLOCK_SEQ, AT_FLOW_SEQ, AT_MAP, AT_BLOCK_MAP, AT_FLOW_MAP }; enum ATOMIC_TYPE { AT_SCALAR, AT_SEQ, AT_BLOCK_SEQ, AT_FLOW_SEQ, AT_MAP, AT_BLOCK_MAP, AT_FLOW_MAP };

13
include/null.h Normal file
View File

@@ -0,0 +1,13 @@
#pragma once
#ifndef NULL_H_62B23520_7C8E_11DE_8A39_0800200C9A66
#define NULL_H_62B23520_7C8E_11DE_8A39_0800200C9A66
namespace YAML
{
struct _Null {};
extern _Null Null;
}
#endif // NULL_H_62B23520_7C8E_11DE_8A39_0800200C9A66

View File

@@ -83,6 +83,12 @@ namespace YAML
return false; return false;
} }
template <>
bool Converter<_Null>::Convert(const std::string& input, _Null& /*output*/)
{
return input.empty() || input == "~" || input == "null" || input == "Null" || input == "NULL";
}
template <> template <>
bool Converter<std::wstring>::Convert(const std::string& input, std::wstring& output) bool Converter<std::wstring>::Convert(const std::string& input, std::wstring& output)
{ {

View File

@@ -666,4 +666,16 @@ namespace YAML
Utils::WriteComment(m_stream, comment.content, m_pState->GetPostCommentIndent()); Utils::WriteComment(m_stream, comment.content, m_pState->GetPostCommentIndent());
return *this; return *this;
} }
Emitter& Emitter::Write(const _Null& /*null*/)
{
if(!good())
return *this;
PreAtomicWrite();
EmitSeparationIfNecessary();
m_stream << "~";
PostAtomicWrite();
return *this;
}
} }

View File

@@ -10,6 +10,7 @@
#include "aliascontent.h" #include "aliascontent.h"
#include "iterpriv.h" #include "iterpriv.h"
#include "emitter.h" #include "emitter.h"
#include <stdexcept>
namespace YAML namespace YAML
{ {
@@ -85,6 +86,9 @@ namespace YAML
m_pContent = new Map; m_pContent = new Map;
break; break;
default: default:
// std::stringstream str;
// str << TokenNames[pScanner->peek().type];
// throw std::runtime_error(str.str());
break; break;
} }

6
src/null.cpp Normal file
View File

@@ -0,0 +1,6 @@
#include "null.h"
namespace YAML
{
_Null Null;
}

View File

@@ -425,6 +425,19 @@ namespace Test
desiredOutput = "-\n key 1: value 1\n key 2: [a, b, c]\n-\n ? [1, 2]\n :\n a: b"; desiredOutput = "-\n key 1: value 1\n key 2: [a, b, c]\n-\n ? [1, 2]\n :\n a: b";
} }
void Null(YAML::Emitter& out, std::string& desiredOutput)
{
out << YAML::BeginSeq;
out << YAML::Null;
out << YAML::BeginMap;
out << YAML::Key << "null value" << YAML::Value << YAML::Null;
out << YAML::Key << YAML::Null << YAML::Value << "null key";
out << YAML::EndMap;
out << YAML::EndSeq;
desiredOutput = "- ~\n-\n null value: ~\n ~: null key";
}
//////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////
// incorrect emitting // incorrect emitting

View File

@@ -351,6 +351,7 @@ namespace Test
RunEmitterTest(&Emitter::Indentation, "indentation", passed); RunEmitterTest(&Emitter::Indentation, "indentation", passed);
RunEmitterTest(&Emitter::SimpleGlobalSettings, "simple global settings", passed); RunEmitterTest(&Emitter::SimpleGlobalSettings, "simple global settings", passed);
RunEmitterTest(&Emitter::ComplexGlobalSettings, "complex global settings", passed); RunEmitterTest(&Emitter::ComplexGlobalSettings, "complex global settings", passed);
RunEmitterTest(&Emitter::Null, "null", passed);
RunEmitterErrorTest(&Emitter::ExtraEndSeq, "extra EndSeq", passed); RunEmitterErrorTest(&Emitter::ExtraEndSeq, "extra EndSeq", passed);
RunEmitterErrorTest(&Emitter::ExtraEndMap, "extra EndMap", passed); RunEmitterErrorTest(&Emitter::ExtraEndMap, "extra EndMap", passed);

View File

@@ -68,6 +68,7 @@ namespace Test {
void Indentation(YAML::Emitter& out, std::string& desiredOutput); void Indentation(YAML::Emitter& out, std::string& desiredOutput);
void SimpleGlobalSettings(YAML::Emitter& out, std::string& desiredOutput); void SimpleGlobalSettings(YAML::Emitter& out, std::string& desiredOutput);
void ComplexGlobalSettings(YAML::Emitter& out, std::string& desiredOutput); void ComplexGlobalSettings(YAML::Emitter& out, std::string& desiredOutput);
void Null(YAML::Emitter& out, std::string& desiredOutput);
// incorrect emitting // incorrect emitting
void ExtraEndSeq(YAML::Emitter& out, std::string& desiredError); void ExtraEndSeq(YAML::Emitter& out, std::string& desiredError);