summaryrefslogtreecommitdiffstats
path: root/xbmc/utils/POUtils.h
diff options
context:
space:
mode:
Diffstat (limited to 'xbmc/utils/POUtils.h')
-rw-r--r--xbmc/utils/POUtils.h162
1 files changed, 162 insertions, 0 deletions
diff --git a/xbmc/utils/POUtils.h b/xbmc/utils/POUtils.h
new file mode 100644
index 0000000..1752b79
--- /dev/null
+++ b/xbmc/utils/POUtils.h
@@ -0,0 +1,162 @@
1/*
2 * Copyright (C) 2012-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9#pragma once
10
11#include <stdint.h>
12#include <string>
13#include <vector>
14
15typedef enum
16{
17 ID_FOUND = 0, // We have an entry with a numeric (previously XML) identification number.
18 MSGID_FOUND = 1, // We have a classic gettext entry with textual msgid. No numeric ID.
19 MSGID_PLURAL_FOUND = 2 // We have a classic gettext entry with textual msgid in plural form.
20} POIdType;
21
22enum
23{
24 ISSOURCELANG=true
25};
26
27// Struct to hold current position and text of the string field in the main PO entry.
28struct CStrEntry
29{
30 size_t Pos;
31 std::string Str;
32};
33
34// Struct to collect all important data of the current processed entry.
35struct CPOEntry
36{
37 int Type;
38 uint32_t xID;
39 size_t xIDPos;
40 std::string Content;
41 CStrEntry msgCtxt;
42 CStrEntry msgID;
43 CStrEntry msgStr;
44 std::vector<CStrEntry> msgStrPlural;
45};
46
47class CPODocument
48{
49public:
50 CPODocument();
51 ~CPODocument();
52
53 /*! \brief Tries to load a PO file into a temporary memory buffer.
54 * It also tries to parse the header of the PO file.
55 \param pofilename filename of the PO file to load.
56 \return true if the load was successful, unless return false
57 */
58 bool LoadFile(const std::string &pofilename);
59
60 /*! \brief Fast jumps to the next entry in PO buffer.
61 * Finds next entry started with "#: id:" or msgctx or msgid.
62 * to be as fast as possible this does not even get the id number
63 * just the type of the entry found. GetEntryID() has to be called
64 * for getting the id. After that ParseEntry() needs a call for
65 * actually getting the msg strings. The reason for this is to
66 * have calls and checks as fast as possible generally and specially
67 * for parsing weather tokens and to parse only the needed strings from
68 * the fallback language (missing from the gui language translation)
69 \return true if there was an entry found, false if reached the end of buffer
70 */
71 bool GetNextEntry();
72
73 /*! \brief Gets the type of entry found with GetNextEntry.
74 \return the type of entry: ID_FOUND || MSGID_FOUND || MSGID_PLURAL_FOUND
75 */
76 int GetEntryType() const {return m_Entry.Type;}
77
78 /*! \brief Parses the numeric ID from current entry.
79 * This function can only be called right after GetNextEntry()
80 * to make sure that we have a valid entry detected.
81 \return parsed ID number
82 */
83 uint32_t GetEntryID() const {return m_Entry.xID;}
84
85 /*! \brief Parses current entry.
86 * Reads msgid, msgstr, msgstr[x], msgctxt strings.
87 * Note that this function also back-converts the c++ style escape sequences.
88 * The function only parses the needed strings, considering if it is a source language file.
89 \param bisSourceLang if we parse a source English file.
90 */
91 void ParseEntry(bool bisSourceLang);
92
93 /*! \brief Gets the msgctxt string previously parsed by ParseEntry().
94 \return string* containing the msgctxt string, unescaped and linked together.
95 */
96 const std::string& GetMsgctxt() const {return m_Entry.msgCtxt.Str;}
97
98 /*! \brief Gets the msgid string previously parsed by ParseEntry().
99 \return string* containing the msgid string, unescaped and linked together.
100 */
101 const std::string& GetMsgid() const {return m_Entry.msgID.Str;}
102
103 /*! \brief Gets the msgstr string previously parsed by ParseEntry().
104 \return string* containing the msgstr string, unescaped and linked together.
105 */
106 const std::string& GetMsgstr() const {return m_Entry.msgStr.Str;}
107
108 /*! \brief Gets the msgstr[x] string previously parsed by ParseEntry().
109 \param plural the number of plural-form expected to get (0-6).
110 \return string* containing the msgstr string, unescaped and linked together.
111 */
112 const std::string& GetPlurMsgstr (size_t plural) const;
113
114protected:
115
116 /*! \brief Converts c++ style char escape sequences back to char.
117 * Supports: \a \v \n \t \r \" \0 \f \? \' \\
118 \param strInput string contains the string to be unescaped.
119 \return unescaped string.
120 */
121 std::string UnescapeString(const std::string &strInput);
122
123 /*! \brief Finds the position of line, starting with a given string in current entry.
124 * This function can only be called after GetNextEntry()
125 \param strToFind a string what we look for, at beginning of the lines.
126 \param FoundPos will get the position where we found the line starting with the string.
127 \return false if no line like that can be found in the entry (m_Entry)
128 */
129 bool FindLineStart(const std::string &strToFind, size_t &FoundPos);
130
131 /*! \brief Reads, and links together the quoted strings found with ParseEntry().
132 * This function can only be called after GetNextEntry() called.
133 \param strEntry.Str a string where we get the appended string lines.
134 \param strEntry.Pos the position in m_Entry.Content to start reading the string.
135 */
136 void GetString(CStrEntry &strEntry);
137
138 /*! \brief Parses the numeric id and checks if it is valid.
139 * This function can only be called after GetNextEntry()
140 * It checks m_Entry.Content at position m_Entry.xIDPos for the numeric id.
141 * The converted ID number goes into m_Entry.xID for public read out.
142 \return false, if parse and convert of the id number was unsuccessful.
143 */
144 bool ParseNumID();
145
146 /*! \brief If we have Windows or Mac line-end chars in PO file, convert them to Unix LFs
147 */
148 void ConvertLineEnds(const std::string &filename);
149
150 // Temporary string buffer to read file in.
151 std::string m_strBuffer;
152 // Size of the string buffer.
153 size_t m_POfilelength;
154
155 // Current cursor position in m_strBuffer.
156 size_t m_CursorPos;
157 // The next PO entry position in m_strBuffer.
158 size_t m_nextEntryPos;
159
160 // Variable to hold all data of currently processed entry.
161 CPOEntry m_Entry;
162};