kodi
POUtils.h
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 
15 typedef 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 
22 enum
23 {
24  ISSOURCELANG=true
25 };
26 
27 // Struct to hold current position and text of the string field in the main PO entry.
28 struct CStrEntry
29 {
30  size_t Pos;
31  std::string Str;
32 };
33 
34 // Struct to collect all important data of the current processed entry.
35 struct 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 
48 {
49 public:
50  CPODocument();
51  ~CPODocument();
52 
58  bool LoadFile(const std::string &pofilename);
59 
71  bool GetNextEntry();
72 
76  int GetEntryType() const {return m_Entry.Type;}
77 
83  uint32_t GetEntryID() const {return m_Entry.xID;}
84 
91  void ParseEntry(bool bisSourceLang);
92 
96  const std::string& GetMsgctxt() const {return m_Entry.msgCtxt.Str;}
97 
101  const std::string& GetMsgid() const {return m_Entry.msgID.Str;}
102 
106  const std::string& GetMsgstr() const {return m_Entry.msgStr.Str;}
107 
112  const std::string& GetPlurMsgstr (size_t plural) const;
113 
114 protected:
115 
121  std::string UnescapeString(const std::string &strInput);
122 
129  bool FindLineStart(const std::string &strToFind, size_t &FoundPos);
130 
136  void GetString(CStrEntry &strEntry);
137 
144  bool ParseNumID();
145 
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 };
Definition: POUtils.h:47
Definition: POUtils.h:28
uint32_t GetEntryID() const
Parses the numeric ID from current entry. This function can only be called right after GetNextEntry()...
Definition: POUtils.h:83
Definition: POUtils.h:35
int GetEntryType() const
Gets the type of entry found with GetNextEntry.
Definition: POUtils.h:76
const std::string & GetMsgstr() const
Gets the msgstr string previously parsed by ParseEntry().
Definition: POUtils.h:106
const std::string & GetMsgid() const
Gets the msgid string previously parsed by ParseEntry().
Definition: POUtils.h:101
const std::string & GetMsgctxt() const
Gets the msgctxt string previously parsed by ParseEntry().
Definition: POUtils.h:96