Commit 09d7ab36 authored by Jesse Beder's avatar Jesse Beder
Browse files

Replaced the queue of Token pointers with values.

We were getting memory leaks (as told by the CRT detectors, which I also added), and there's really no reason (as long as we're careful) to use pointers there.
parent 2eab1e02
......@@ -206,7 +206,7 @@
>
</File>
<File
RelativePath=".\yaml-reader\test.yaml"
RelativePath=".\yaml-reader\tests\test.yaml"
>
</File>
</Filter>
......
#include "parser.h"
#include "yaml.h"
#include "tests.h"
#include <fstream>
#include <iostream>
......@@ -9,85 +9,29 @@
#pragma comment(lib, "yamlcpp.lib")
#endif
struct Vec3 {
float x, y, z;
friend std::ostream& operator << (std::ostream& out, const Vec3& v) {
out << v.x << " " << v.y << " " << v.z;
return out;
}
};
void operator >> (const YAML::Node& node, Vec3& v)
{
node[0] >> v.x;
node[1] >> v.y;
node[2] >> v.z;
}
struct Room {
std::string name;
Vec3 pos, size;
float height;
friend std::ostream& operator << (std::ostream& out, const Room& room) {
out << "Name: " << room.name << std::endl;
out << "Pos: " << room.pos << std::endl;
out << "Size: " << room.size << std::endl;
out << "Height: " << room.height << std::endl;
return out;
}
};
void operator >> (const YAML::Node& node, Room& room)
{
node["name"] >> room.name;
node["pos"] >> room.pos;
node["size"] >> room.size;
node["height"] >> room.height;
}
struct Level {
std::vector <Room> rooms;
friend std::ostream& operator << (std::ostream& out, const Level& level) {
for(unsigned i=0;i<level.rooms.size();i++) {
out << level.rooms[i];
out << "---------------------------------------\n";
}
return out;
}
};
void operator >> (const YAML::Node& node, Level& level)
void run()
{
const YAML::Node& rooms = node["rooms"];
for(unsigned i=0;i<rooms.size();i++) {
Room room;
rooms[i] >> room;
level.rooms.push_back(room);
std::ifstream fin("yaml-reader/tests/test.yaml");
try {
YAML::Parser parser(fin);
if(!parser)
return;
YAML::Node doc;
parser.GetNextDocument(doc);
std::cout << doc;
} catch(YAML::Exception&) {
std::cout << "Error parsing the yaml!\n";
}
}
int main()
{
YAML::Test::RunAll();
//std::ifstream fin("test.yaml");
//try {
// YAML::Parser parser(fin);
// if(!parser)
// return 0;
// YAML::Node doc;
// parser.GetNextDocument(doc);
// std::cout << doc;
//} catch(YAML::Exception&) {
// std::cout << "Error parsing the yaml!\n";
//}
{
_CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF);
Test::RunAll();
run();
getchar();
return 0;
}
#include "yaml.h"
#include "tests.h"
#include "parser.h"
#include <fstream>
#include <sstream>
#include <vector>
#include <iostream>
namespace Test
{
// runs all the tests on all data we have
void RunAll()
{
std::vector <std::string> files;
files.push_back("yaml-reader/tests/simple.yaml");
files.push_back("yaml-reader/tests/mixed.yaml");
files.push_back("yaml-reader/tests/scalars.yaml");
files.push_back("yaml-reader/tests/directives.yaml");
bool passed = true;
for(unsigned i=0;i<files.size();i++) {
if(!Inout(files[i])) {
std::cout << "Inout test failed on " << files[i] << std::endl;
passed = false;
}
}
if(passed)
std::cout << "All tests passed!\n";
}
// loads the given YAML file, outputs it, and then loads the outputted file,
// outputs again, and makes sure that the two outputs are the same
bool Inout(const std::string& file)
{
std::ifstream fin(file.c_str());
try {
// read and output
YAML::Parser parser(fin);
if(!parser)
return false;
YAML::Node doc;
parser.GetNextDocument(doc);
std::stringstream out;
out << doc;
// and save
std::string firstTry = out.str();
// and now again
parser.Load(out);
if(!parser)
return false;
parser.GetNextDocument(doc);
std::stringstream out2;
out2 << doc;
// and save
std::string secondTry = out2.str();
// now compare
if(firstTry == secondTry)
return true;
std::ofstream fout("tests/out.yaml");
fout << "---\n";
fout << firstTry << std::endl;
fout << "---\n";
fout << secondTry << std::endl;
} catch(YAML::ParserException& e) {
std::cout << file << " (line " << e.line + 1 << ", col " << e.column + 1 << "): " << e.msg << std::endl;
return false;
}
return true;
}
}
#include <string>
namespace Test {
void RunAll();
bool Inout(const std::string& file);
}
......@@ -230,10 +230,18 @@
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
RelativePath=".\include\crt.h"
>
</File>
<File
RelativePath=".\include\exceptions.h"
>
</File>
<File
RelativePath=".\include\yaml.h"
>
</File>
<Filter
Name="Parser"
>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment