~strahinja/poe

ref: v1.4.2 poe/po.h -rw-r--r-- 3.9 KiB
955a2420Страхиња Радић Merge branch 'revert-to-1.4' 10 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* See the file LICENSE for copyright and license details. */

#include <stdint.h>
#include <stdlib.h>

#define MSGSTR_MAX	   4096
#define ALLOC_DELTA	   10
#define PO_DETECTION_LINES 24

#define LOAD_PARSE_ERR(e) (LOAD_ERR_PARSE_PREFIX + (e))

struct DrawState;

enum {
	PARSE_ERR_NONE	 = 0,
	PARSE_ERR_INVAL	 = 1,
	PARSE_ERR_ALLOC	 = 2,
	PARSE_ERR_SYNTAX = 3
};

enum {
	LOAD_ERR_NONE		= 0,
	LOAD_ERR_CANT_ALLOC	= 1,
	LOAD_ERR_CANT_OPEN_FILE = 2,
	LOAD_ERR_NOT_PO_FILE	= 3,
	LOAD_ERR_PARSE_PREFIX	= 100,
	LOAD_ERR_P_INVAL	= LOAD_PARSE_ERR(PARSE_ERR_INVAL),
	LOAD_ERR_P_ALLOC	= LOAD_PARSE_ERR(PARSE_ERR_ALLOC),
	LOAD_ERR_P_SYNTAX	= LOAD_PARSE_ERR(PARSE_ERR_SYNTAX)
};

enum {
	SAVE_ERR_NONE		= 0,
	SAVE_ERR_CANT_ALLOC	= 1,
	SAVE_ERR_CANT_OPEN_FILE = 2,
	SAVE_ERR_CANT_MOVE_FILE = 3
};

typedef enum {
	PS_NONE		= 0,
	PS_COMMENT_TRAN = 1,	  /* "translator-comments"   : #  */
	PS_COMMENT_EXTR = 1 << 1, /* "extracted-comments"    : #. */
	PS_COMMENT_FLAG = 1 << 2, /* "flag"                  : #, */
	PS_COMMENT_REFN = 1 << 3, /* "reference"             : #: */
	PS_COMMENT_OBSO = 1 << 4, /* obsolete (undocumented) : #~ */
	PS_MSGID	= 1 << 5,
	PS_MSGID_PLURAL = 1 << 6,
	PS_MSGSTR	= 1 << 7,
	PS_MSGSTR_ARRAY = 1 << 8
} ParseState;

/* Complete list at:
 *
 * https://www.gnu.org/software/gettext/manual/html_node/PO-Files.html
 *
 * - JS, awk and sh using gettext, seriously?
 *   "Let's just shove every programming and scripting language there is into
 *   flags!" /s
 *   Where are the flags for Pascal and Ada, by the way, hmm? [SR]
 *
 * - Including just the sane values, will strip the rest. */
typedef enum {
	FL_NONE	       = 0,
	FL_FUZZY       = 1,
	FL_C_FORMAT    = 1 << 1,
	FL_NO_C_FORMAT = 1 << 2
} Flags;

struct PoEntry {
	Flags flags;
	int obsolete;
	uint32_t* msgid;
	size_t msgid_size;
	size_t msgid_len;
	uint32_t* msgid_plural;
	size_t msgid_plural_size;
	size_t msgid_plural_len;
	uint32_t* plural_forms;
	uint32_t** msgstr;
	int msgstr_count;
	size_t* msgstr_size;
	size_t* msgstr_len;
	uint32_t** comments;
	size_t comment_lines;
	uint32_t** trans_comments;
	size_t trans_comment_lines;
	char** ref_comments;
	size_t ref_comment_lines;
};

char* format_flags(char* buffer, const size_t max, const struct PoEntry* entry);
void init_po_entry(struct PoEntry* entry);
void free_po_entry(struct PoEntry* entry);
struct PoEntry* set_flags(struct PoEntry* entry, const Flags flags);
struct PoEntry* set_msgid(struct PoEntry* entry, const char* msgid, int append);
struct PoEntry* set_msgid_plural(struct PoEntry* entry,
	const char* msgid_plural, int append);
struct PoEntry* set_plural_forms(struct PoEntry* entry,
	const char* plural_forms, int append);
struct PoEntry* set_msgstr(struct PoEntry* entry, const char* msgstr,
	int msgstr_index, int append, struct DrawState* state);
struct PoEntry* u32_set_msgid(struct PoEntry* entry, const uint32_t* msgid,
	size_t msgid_size, int append);
struct PoEntry* u32_set_msgid_plural(struct PoEntry* entry,
	const uint32_t* msgid_plural, size_t msgid_plural_size, int append);
struct PoEntry* u32_set_plural_forms(struct PoEntry* entry,
	const uint32_t* plural_forms, int append);
struct PoEntry* u32_set_msgstr(struct PoEntry* entry, const uint32_t* msgstr,
	int msgstr_index, int append, struct DrawState* state);
struct PoEntry* u32_add_comment(struct PoEntry* entry, const uint32_t* comment);
struct PoEntry* u32_add_trans_comment(struct PoEntry* entry,
	const uint32_t* trans_comment);
struct PoEntry* add_ref_comment(struct PoEntry* entry, const char* comment);
int load_file(struct DrawState* state, long* lineno, long* col);
int save_file(const struct PoEntry* entries, const size_t real_msgid_count,
	const char* filename, const int nplurals);
int parse_po_line(const char* line, struct DrawState* dstate,
	struct PoEntry** current_entry, ParseState* pstate, long* col,
	int* msgstr_index);
void update_statistics(const struct PoEntry* entries, struct DrawState* state,
	struct PoEntry* entry);