20130319
[gdash.git] / src / fileops / bdcffhelper.hpp
blobdf86c9c4a75b6b2faa69b3fed74f11a79dbd9974
1 /*
2 * Copyright (c) 2007-2013, Czirkos Zoltan http://code.google.com/p/gdash/
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #ifndef _GD_BDCFF_HELPER
18 #define _GD_BDCFF_HELPER
20 #include "config.h"
22 #include <string>
23 #include <list>
24 #include <sstream>
26 #define BDCFF_VERSION "0.5"
28 /**
29 * Functor which checks if a string has another string as its prefix.
30 * eg, it will return true for "SlimePermeability=0.1" begins with "SlimePermeability"
31 * The check is case-insensitive!
33 class HasAttrib {
34 std::string attrib;
35 public:
36 explicit HasAttrib(const std::string &attrib_);
37 bool operator()(const std::string &str) const;
41 /// A class which splits a BDCFF line read
42 /// into two parts - an attribute name and parameters.
43 /// For example, "SlimePermeability=0.1" is split into
44 /// "SlimePermeability" (attrib) and "0.1" param.
45 class AttribParam {
46 public:
47 std::string attrib;
48 std::string param;
49 explicit AttribParam(const std::string &str, char separator='=');
52 typedef std::list<std::string> BdcffSection;
53 typedef BdcffSection::iterator BdcffSectionIterator;
54 typedef BdcffSection::const_iterator BdcffSectionConstIterator;
56 /**
57 * A structure, which stores the lines of text in a bdcff file; and has a list
58 * of these strings for each section.
60 * The main format of the bdcff file.
61 * Contains: bdcff section; highscore for caveset, map codes, caveset properties, and caves data.
62 * Caves data contains: highscore for cave, cave properties, maybe map, maybe objects, maybe replays, maybe demo (=legacy replay).
64 struct BdcffFile {
65 struct CaveInfo {
66 BdcffSection highscore;
67 BdcffSection properties;
68 BdcffSection map;
69 BdcffSection objects;
70 std::list<BdcffSection> replays;
71 BdcffSection demo;
74 BdcffSection bdcff;
75 BdcffSection highscore;
76 BdcffSection mapcodes;
77 BdcffSection caveset_properties;
78 std::list<CaveInfo> caves;
82 /** A class which helps outputting BDCFF lines like "Point=x y z".
84 * It stores a name (Point), and can be fed with parameters
85 * using a standard operator<<.
86 * If conversion is ready, the str() member function can be
87 * used to get the BDCFF output.
89 class BdcffFormat {
90 private:
91 std::ostringstream os; ///< for conversion
92 std::string name; ///< name of parameter, eg. Size
93 bool firstparam; ///< used internally do determine if a space is needed
95 public:
96 explicit BdcffFormat(const std::string &f="");
97 template <typename T> BdcffFormat &operator<<(const T &param);
98 void start_new(const std::string &f);
99 std::string str() const;
100 /** cast to string - return the string. */
101 operator std::string() const {
102 return str();
107 * @brief Feed next output parameter to the formatter.
108 * @param param The variable to write.
109 * @return Itself, for linking << a << b << c.
111 template <typename T>
112 BdcffFormat &BdcffFormat::operator<<(const T &param) {
113 /* if this is not the first parameter, add a space */
114 if (!firstparam)
115 os << ' ';
116 else
117 firstparam=false;
118 os << param;
119 return *this;
122 #endif