• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 *
6 *   Copyright (C) 2005-2012, International Business Machines
7 *   Corporation and others.  All Rights Reserved.
8 *
9 *******************************************************************************
10 *   file name:  writesrc.h
11 *   encoding:   UTF-8
12 *   tab size:   8 (not used)
13 *   indentation:4
14 *
15 *   created on: 2005apr23
16 *   created by: Markus W. Scherer
17 *
18 *   Helper functions for writing source code for data.
19 */
20 
21 #ifndef __WRITESRC_H__
22 #define __WRITESRC_H__
23 
24 #include <stdio.h>
25 #include "unicode/utypes.h"
26 #include "unicode/ucpmap.h"
27 #include "unicode/ucptrie.h"
28 #include "unicode/umutablecptrie.h"
29 #include "unicode/uset.h"
30 #include "utrie2.h"
31 
32 /**
33  * An input to some of the functions in this file specifying whether to write data
34  * as C/C++ code initializers or as TOML.
35  */
36 typedef enum UTargetSyntax {
37     UPRV_TARGET_SYNTAX_CCODE = 0,
38     UPRV_TARGET_SYNTAX_TOML = 1,
39 } UTargetSyntax;
40 
41 /**
42  * Creates a source text file and writes a header comment with the ICU copyright.
43  * Writes a C/Java-style comment with the generator name.
44  */
45 U_CAPI FILE * U_EXPORT2
46 usrc_create(const char *path, const char *filename, int32_t copyrightYear, const char *generator);
47 
48 /**
49  * Creates a source text file and writes a header comment with the ICU copyright.
50  * Writes the comment with # lines, as used in scripts and text data.
51  */
52 U_CAPI FILE * U_EXPORT2
53 usrc_createTextData(const char *path, const char *filename, int32_t copyrightYear, const char *generator);
54 
55 /**
56  * Writes the ICU copyright to a file stream, with configurable year and comment style.
57  */
58 U_CAPI void U_EXPORT2
59 usrc_writeCopyrightHeader(FILE *f, const char *prefix, int32_t copyrightYear);
60 
61 /**
62  * Writes information about the file being machine-generated.
63  */
64 U_CAPI void U_EXPORT2
65 usrc_writeFileNameGeneratedBy(
66         FILE *f,
67         const char *prefix,
68         const char *filename,
69         const char *generator);
70 
71 /**
72  * Writes the contents of an array of 8/16/32/64-bit words.
73  * The prefix and postfix are optional (can be NULL) and are written first/last.
74  * The prefix may contain a %ld or similar field for the array length.
75  * The {} and declaration etc. need to be included in prefix/postfix or
76  * printed before and after the array contents.
77  */
78 U_CAPI void U_EXPORT2
79 usrc_writeArray(FILE *f,
80                 const char *prefix,
81                 const void *p, int32_t width, int32_t length,
82                 const char *indent,
83                 const char *postfix);
84 
85 /**
86  * Calls usrc_writeArray() for the index and data arrays of a frozen UTrie2.
87  * Only the index array is written for a 16-bit UTrie2. In this case, dataPrefix
88  * is ignored and can be NULL.
89  */
90 U_CAPI void U_EXPORT2
91 usrc_writeUTrie2Arrays(FILE *f,
92                        const char *indexPrefix, const char *dataPrefix,
93                        const UTrie2 *pTrie,
94                        const char *postfix);
95 
96 /**
97  * Writes the UTrie2 struct values.
98  * The {} and declaration etc. need to be included in prefix/postfix or
99  * printed before and after the array contents.
100  */
101 U_CAPI void U_EXPORT2
102 usrc_writeUTrie2Struct(FILE *f,
103                        const char *prefix,
104                        const UTrie2 *pTrie,
105                        const char *indexName, const char *dataName,
106                        const char *postfix);
107 
108 /**
109  * Calls usrc_writeArray() for the index and data arrays of a UCPTrie.
110  */
111 U_CAPI void U_EXPORT2
112 usrc_writeUCPTrieArrays(FILE *f,
113                         const char *indexPrefix, const char *dataPrefix,
114                         const UCPTrie *pTrie,
115                         const char *postfix,
116                         UTargetSyntax syntax);
117 
118 /**
119  * Writes the UCPTrie struct values.
120  * The {} and declaration etc. need to be included in prefix/postfix or
121  * printed before and after the array contents.
122  */
123 U_CAPI void U_EXPORT2
124 usrc_writeUCPTrieStruct(FILE *f,
125                         const char *prefix,
126                         const UCPTrie *pTrie,
127                         const char *indexName, const char *dataName,
128                         const char *postfix,
129                         UTargetSyntax syntax);
130 
131 /**
132  * Writes the UCPTrie arrays and struct values.
133  */
134 U_CAPI void U_EXPORT2
135 usrc_writeUCPTrie(FILE *f, const char *name, const UCPTrie *pTrie, UTargetSyntax syntax);
136 
137 /**
138  * Writes the UnicodeSet range and string lists.
139  */
140 U_CAPI void U_EXPORT2
141 usrc_writeUnicodeSet(
142     FILE *f,
143     const USet *pSet,
144     UTargetSyntax syntax);
145 
146 #ifdef __cplusplus
147 
148 U_NAMESPACE_BEGIN
149 
150 class U_TOOLUTIL_API ValueNameGetter {
151 public:
152     virtual ~ValueNameGetter();
153     virtual const char *getName(uint32_t value) = 0;
154 };
155 
156 U_NAMESPACE_END
157 
158 /**
159  * Writes the UCPMap ranges list.
160  *
161  * The "valueNameGetter" argument is optional; ignored if nullptr.
162  * If present, it will be used to look up value name strings.
163  */
164 U_CAPI void U_EXPORT2
165 usrc_writeUCPMap(
166     FILE *f,
167     const UCPMap *pMap,
168     icu::ValueNameGetter *valueNameGetter,
169     UTargetSyntax syntax);
170 
171 #endif  // __cplusplus
172 
173 /**
174  * Writes the contents of an array of mostly invariant characters.
175  * Characters 0..0x1f are printed as numbers,
176  * others as characters with single quotes: '%c'.
177  *
178  * The prefix and postfix are optional (can be NULL) and are written first/last.
179  * The prefix may contain a %ld or similar field for the array length.
180  * The {} and declaration etc. need to be included in prefix/postfix or
181  * printed before and after the array contents.
182  */
183 U_CAPI void U_EXPORT2
184 usrc_writeArrayOfMostlyInvChars(FILE *f,
185                                 const char *prefix,
186                                 const char *p, int32_t length,
187                                 const char *postfix);
188 
189 /**
190  * Writes a syntactically valid Unicode string in all ASCII, escaping quotes
191  * and non-ASCII characters.
192  */
193 U_CAPI void U_EXPORT2
194 usrc_writeStringAsASCII(FILE *f,
195                         const UChar* ptr, int32_t length,
196                         UTargetSyntax syntax);
197 
198 #endif
199