Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
df4413a
commit a32e279
Showing
10 changed files
with
510 additions
and
367 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
Copyright (C) 2011 - 2018 by Sytyi Nick <nsytyi@gmail.com> | ||
Part of the Battle for Wesnoth Project https://www.wesnoth.org/ | ||
This program is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; either version 2 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY. | ||
See the COPYING file for more details. | ||
*/ | ||
|
||
/** | ||
* @file | ||
* Implementation of key.hpp. | ||
*/ | ||
|
||
#include "serialization/schema/key.hpp" | ||
|
||
#include "config.hpp" | ||
|
||
namespace schema_validation | ||
{ | ||
|
||
class_key::class_key(const config& cfg) | ||
: name_(cfg["name"].str()) | ||
, type_(cfg["type"].str()) | ||
, default_() | ||
, mandatory_(false) | ||
, fuzzy_(name_.find_first_of("*?") != std::string::npos) | ||
{ | ||
if(cfg.has_attribute("mandatory")) { | ||
mandatory_ = cfg["mandatory"].to_bool(); | ||
} else { | ||
if(cfg.has_attribute("default")) { | ||
default_ = cfg["default"].str(); | ||
} | ||
} | ||
} | ||
|
||
void class_key::print(std::ostream& os, int level) const | ||
{ | ||
std::string s; | ||
for(int j = 0; j < level; j++) { | ||
s.append(" "); | ||
} | ||
|
||
os << s << "[key]\n" << s << " name=\"" << name_ << "\"\n" << s << " type=\"" << type_ << "\"\n"; | ||
|
||
if(is_mandatory()) { | ||
os << s << " mandatory=\"true\"\n"; | ||
} else { | ||
os << s << " default=" << default_ << "\n"; | ||
} | ||
|
||
// TODO: Other attributes | ||
|
||
os << s << "[/key]\n"; | ||
} | ||
|
||
} // namespace schema_validation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/* | ||
Copyright (C) 2011 - 2018 by Sytyi Nick <nsytyi@gmail.com> | ||
Part of the Battle for Wesnoth Project https://www.wesnoth.org/ | ||
This program is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; either version 2 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY. | ||
See the COPYING file for more details. | ||
*/ | ||
|
||
/** | ||
* @file | ||
* This file contains object "key", which is used to store | ||
* information about keys while annotation parsing. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <iosfwd> | ||
#include <string> | ||
|
||
class config; | ||
|
||
namespace schema_validation | ||
{ | ||
|
||
/** | ||
* class_key is used to save the information about one key. | ||
* Key has next info: name, type, default value or key is mandatory. | ||
*/ | ||
class class_key | ||
{ | ||
public: | ||
class_key() | ||
: name_("") | ||
, type_("") | ||
, default_("\"\"") | ||
, mandatory_(false) | ||
, fuzzy_(false) | ||
{ | ||
} | ||
|
||
class_key(const std::string& name, const std::string& type, const std::string& def = "\"\"") | ||
: name_(name) | ||
, type_(type) | ||
, default_(def) | ||
, mandatory_(def.empty()) | ||
, fuzzy_(name.find_first_of("*?") != std::string::npos) | ||
{ | ||
} | ||
|
||
class_key(const config&); | ||
|
||
const std::string& get_name() const | ||
{ | ||
return name_; | ||
} | ||
|
||
const std::string& get_type() const | ||
{ | ||
return type_; | ||
} | ||
|
||
const std::string& get_default() const | ||
{ | ||
return default_; | ||
} | ||
|
||
bool is_mandatory() const | ||
{ | ||
return mandatory_; | ||
} | ||
|
||
bool is_fuzzy() const { | ||
return fuzzy_; | ||
} | ||
|
||
void set_name(const std::string& name) | ||
{ | ||
name_ = name; | ||
} | ||
|
||
void set_type(const std::string& type) | ||
{ | ||
type_ = type; | ||
} | ||
|
||
void set_default(const std::string& def) | ||
{ | ||
default_ = def; | ||
if(def.empty()) { | ||
mandatory_ = true; | ||
} | ||
} | ||
|
||
void set_mandatory(bool mandatory) | ||
{ | ||
mandatory_ = mandatory; | ||
} | ||
|
||
void set_fuzzy(bool f) | ||
{ | ||
fuzzy_ = f; | ||
} | ||
|
||
/** is used to print key info | ||
* the format is next | ||
* [key] | ||
* name="name" | ||
* type="type" | ||
* default="default" | ||
* mandatory="true/false" | ||
* [/key] | ||
*/ | ||
void print(std::ostream& os, int level) const; | ||
|
||
/** Compares keys by name. Used in std::sort, i.e. */ | ||
bool operator<(const class_key& k) const | ||
{ | ||
return (get_name() < k.get_name()); | ||
} | ||
|
||
private: | ||
/** Name of key. */ | ||
std::string name_; | ||
|
||
/** Type of key. */ | ||
std::string type_; | ||
|
||
/** Default value. */ | ||
std::string default_; | ||
|
||
/** Shows, if key is a mandatory key. */ | ||
bool mandatory_; | ||
|
||
/** Whether the key is a fuzzy match. */ | ||
bool fuzzy_; | ||
}; | ||
} |
Oops, something went wrong.