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.
This commit is contained in:
Jesse Beder
2008-07-20 05:02:01 +00:00
parent 2eab1e025e
commit 09d7ab365f
25 changed files with 297 additions and 251 deletions

View File

@@ -1,4 +1,4 @@
#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)
void run()
{
node[0] >> v.x;
node[1] >> v.y;
node[2] >> v.z;
}
std::ifstream fin("yaml-reader/tests/test.yaml");
struct Room {
std::string name;
Vec3 pos, size;
float height;
try {
YAML::Parser parser(fin);
if(!parser)
return;
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)
{
const YAML::Node& rooms = node["rooms"];
for(unsigned i=0;i<rooms.size();i++) {
Room room;
rooms[i] >> room;
level.rooms.push_back(room);
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;
}

79
yaml-reader/tests.cpp Normal file
View File

@@ -0,0 +1,79 @@
#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;
}
}

6
yaml-reader/tests.h Normal file
View File

@@ -0,0 +1,6 @@
#include <string>
namespace Test {
void RunAll();
bool Inout(const std::string& file);
}