The Battle for Wesnoth  1.15.1+dev
po_message.hpp
Go to the documentation of this file.
1 // (C) Copyright 2015 - 2017 Christopher Beck
2 
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
5 
6 #ifndef SPIRIT_PO_PO_MESSAGE_HPP_INCLUDED
7 #define SPIRIT_PO_PO_MESSAGE_HPP_INCLUDED
8 
9 #include <boost/optional/optional.hpp>
10 #include <string>
11 #include <utility>
12 #include <vector>
13 
14 namespace spirit_po {
15 
16 typedef std::pair<std::string, std::vector<std::string>> plural_and_strings_type;
17 
18 struct po_message {
19  boost::optional<std::string> context;
20  std::string id;
21  plural_and_strings_type plural_and_strings;
22 
23  std::size_t line_no;
24 
25  // Get the 'id_plural', 'strings' fields from the pair.
26  // It is arranged as a pair here to allow for simpler parsing with spirit attributes.
27  std::string & id_plural() { return plural_and_strings.first; }
28  const std::string & id_plural() const { return plural_and_strings.first; }
29 
30  std::vector<std::string> & strings() { return plural_and_strings.second; }
31  const std::vector<std::string> & strings() const { return plural_and_strings.second; }
32 
33  // Check if message is plural. We do this for now by testing msgid_plural.size().
34  // Recommended to use this method in case we change it in the future.
35  bool is_plural() const { return (id_plural().size() != 0); }
36 };
37 
38 /***
39  * Debug printer
40  */
41 #ifdef SPIRIT_PO_DEBUG
42 inline std::string debug_string(const po_message & msg) {
43  std::string result = "{\n";
44  if (msg.context) {
45  result += " context: \"" + *msg.context + "\"\n";
46  }
47  result += " id: \"" + msg.id + "\"\n";
48  result += " id_plural: \"" + msg.id_plural() + "\"\n";
49  result += " strings: { ";
50  for (uint i = 0; i < msg.strings().size(); ++i) {
51  if (i) { result += ", "; }
52  result += '"' + msg.strings()[i] + '"';
53  }
54  result += " }\n";
55  result += "}";
56  return result;
57 }
58 #endif // SPIRIT_PO_DEBUG
59 
60 } // end namespace spirit_po
61 
62 #endif // SPIRIT_PO_PO_MESSAGE_HPP_INCLUDED
const std::string & id_plural() const
Definition: po_message.hpp:28
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:109
plural_and_strings_type plural_and_strings
Definition: po_message.hpp:21
std::string & id_plural()
Definition: po_message.hpp:27
boost::optional< std::string > context
Definition: po_message.hpp:19
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:86
std::vector< std::string > & strings()
Definition: po_message.hpp:30
std::pair< std::string, std::vector< std::string > > plural_and_strings_type
Definition: po_message.hpp:16
bool is_plural() const
Definition: po_message.hpp:35
std::size_t i
Definition: function.cpp:933
const std::vector< std::string > & strings() const
Definition: po_message.hpp:31
unsigned int uint
Definition: catalog.hpp:39