1 /* Handling strings that are given partially in the source encoding and 2 partially in Unicode. 3 Copyright (C) 2001-2018 Free Software Foundation, Inc. 4 5 This program is free software: you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation; either version 3 of the License, or 8 (at your option) any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program. If not, see <https://www.gnu.org/licenses/>. */ 17 18 #ifndef _XGETTEXT_MIXED_STRING_H 19 #define _XGETTEXT_MIXED_STRING_H 20 21 #include <stdbool.h> 22 #include <stddef.h> 23 24 #include "xg-encoding.h" 25 26 #ifdef __cplusplus 27 extern "C" { 28 #endif 29 30 31 /* A string that contains segments in the xgettext_current_source_encoding 32 and segments in UTF-8, in an alternating way. */ 33 34 enum segment_type 35 { 36 source_encoded, 37 utf8_encoded 38 }; 39 40 struct mixed_string_segment 41 { 42 /*enum segment_type*/ unsigned char type; 43 size_t length; 44 char contents[FLEXIBLE_ARRAY_MEMBER]; 45 }; 46 47 typedef struct mixed_string mixed_string_ty; 48 struct mixed_string 49 { 50 /* The alternating segments. */ 51 struct mixed_string_segment **segments; 52 size_t nsegments; 53 /* The lexical context. Used only for error message purposes. */ 54 lexical_context_ty lcontext; 55 const char *logical_file_name; 56 int line_number; 57 }; 58 59 /* Creates a mixed_string that contains just a string in the 60 xgettext_current_source_encoding. */ 61 extern mixed_string_ty * 62 mixed_string_alloc_simple (const char *string, 63 lexical_context_ty lcontext, 64 const char *logical_file_name, 65 int line_number); 66 67 /* Creates a mixed_string that contains just a UTF-8 string. */ 68 extern mixed_string_ty * 69 mixed_string_alloc_utf8 (const char *string, 70 lexical_context_ty lcontext, 71 const char *logical_file_name, 72 int line_number); 73 74 /* Creates a copy of a mixed_string. */ 75 extern mixed_string_ty * 76 mixed_string_clone (const mixed_string_ty *ms1); 77 78 /* Returns the contents of a mixed_string as an UTF-8 encoded string. 79 This may provoke an error if no source encoding has been specified 80 through --from-code. The result is freshly allocated. */ 81 extern char * 82 mixed_string_contents (const mixed_string_ty *ms); 83 84 /* Frees a mixed_string. */ 85 extern void 86 mixed_string_free (mixed_string_ty *ms); 87 88 /* Returns the contents of a mixed_string as an UTF-8 encoded string, 89 and frees the argument. */ 90 extern char * 91 mixed_string_contents_free1 (mixed_string_ty *ms); 92 93 /* Concatenates two mixed_strings. */ 94 extern mixed_string_ty * 95 mixed_string_concat (const mixed_string_ty *ms1, 96 const mixed_string_ty *ms2); 97 /* Concatenates two mixed_strings, and frees the first argument. */ 98 extern mixed_string_ty * 99 mixed_string_concat_free1 (mixed_string_ty *ms1, 100 const mixed_string_ty *ms2); 101 102 103 /* A string buffer type that allows appending bytes (in the 104 xgettext_current_source_encoding) or Unicode characters. 105 When done, it returns the entire string as a mixed_string. */ 106 107 struct mixed_string_buffer 108 { 109 /* The alternating segments that are already finished. */ 110 struct mixed_string_segment **segments; 111 size_t nsegments; 112 size_t nsegments_allocated; 113 /* The segment that is being accumulated. */ 114 int curr_type; /* An enum segment_type, or -1. */ 115 char *curr_buffer; 116 size_t curr_buflen; 117 size_t curr_allocated; 118 /* The first half of an UTF-16 surrogate character. */ 119 unsigned short utf16_surr; 120 /* The lexical context. Used only for error message purposes. */ 121 lexical_context_ty lcontext; 122 const char *logical_file_name; 123 int line_number; 124 }; 125 126 /* Initializes a mixed_string_buffer. */ 127 extern void 128 mixed_string_buffer_init (struct mixed_string_buffer *bp, 129 lexical_context_ty lcontext, 130 const char *logical_file_name, 131 int line_number); 132 133 /* Determines whether a mixed_string_buffer is still empty. */ 134 extern bool 135 mixed_string_buffer_is_empty (const struct mixed_string_buffer *bp); 136 137 /* Appends a character to a mixed_string_buffer. */ 138 extern void 139 mixed_string_buffer_append_char (struct mixed_string_buffer *bp, int c); 140 141 /* Appends a Unicode character to a mixed_string_buffer. */ 142 extern void 143 mixed_string_buffer_append_unicode (struct mixed_string_buffer *bp, 144 int c); 145 146 /* Frees the memory pointed to by a 'struct mixed_string_buffer' and 147 discards the accumulated string. */ 148 extern void 149 mixed_string_buffer_destroy (struct mixed_string_buffer *bp); 150 151 /* Frees the memory pointed to by a 'struct mixed_string_buffer' 152 and returns the accumulated string. */ 153 extern mixed_string_ty * 154 mixed_string_buffer_result (struct mixed_string_buffer *bp); 155 156 157 #ifdef __cplusplus 158 } 159 #endif 160 161 162 #endif /* _XGETTEXT_MIXED_STRING_H */ 163