1 /* stringpool.h
2 
3    Copyright 2009 Taco Hoekwater <taco@luatex.org>
4 
5    This file is part of LuaTeX.
6 
7    LuaTeX is free software; you can redistribute it and/or modify it under
8    the terms of the GNU General Public License as published by the Free
9    Software Foundation; either version 2 of the License, or (at your
10    option) any later version.
11 
12    LuaTeX is distributed in the hope that it will be useful, but WITHOUT
13    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15    License for more details.
16 
17    You should have received a copy of the GNU General Public License along
18    with LuaTeX; if not, see <http://www.gnu.org/licenses/>. */
19 
20 
21 #ifndef STRINGPOOL_H
22 #  define STRINGPOOL_H
23 
24 /* Both lua and tex strings can contains null, but C strings cannot, so: */
25 
26 typedef struct {
27     unsigned char *s;
28     size_t l;
29 } lstring;
30 
31 typedef struct {
32     const char *s;
33     size_t l;
34 } const_lstring;
35 
36 extern lstring *string_pool;
37 
38 extern str_number str_ptr;
39 extern str_number init_str_ptr;
40 
41 #  define STRING_OFFSET 0x200000
42 #  define STRING_OFFSET_BITS 21
43 
44 #  define get_nullstr() STRING_OFFSET
45 
46 #  define biggest_char 1114111
47 #  define number_chars 1114112
48 #  define special_char 1114113  /* |biggest_char+2| */
49 
50 /*
51   Several of the elementary string operations are performed using
52   macros instead of procedures, because many of the
53   operations are done quite frequently and we want to avoid the
54   overhead of procedure calls. For example, here is
55   a simple macro that computes the length of a string.
56 */
57 
58 #  define str_length(a) string_pool[(a)-STRING_OFFSET].l
59 #  define str_string(a) string_pool[(a)-STRING_OFFSET].s
60 #  define str_lstring(a) string_pool[(a)-STRING_OFFSET]
61 
62 /* Strings are created by appending character codes to |str_pool|.
63    The |append_char| macro, defined here, does not check to see if the
64    value of |pool_ptr| has gotten too high; this test is supposed to be
65    made before |append_char| is used. There is also a |flush_char|
66    macro, which erases the last character appended.
67 
68    To test if there is room to append |l| more characters to |str_pool|,
69    we shall write |str_room(l)|, which aborts \TeX\ and gives an
70    apologetic error message if there isn't enough room.
71 */
72 
73 /* The length of the current string is called |cur_length|: */
74 
75 extern unsigned char *cur_string;
76 extern unsigned cur_length;
77 extern unsigned cur_string_size;
78 extern unsigned pool_size;
79 
80 #  define EXTRA_STRING 500
81 
82 /* put |ASCII_code| \# at the end of |str_pool| */
83 #  define append_char(A) do {                                           \
84         if (cur_string==NULL) reset_cur_string();                       \
85         else str_room(1);                                               \
86         cur_string[cur_length++]=(unsigned char)(A);                    \
87     } while (0)
88 
89 #  define str_room(wsize) do {                                          \
90         unsigned nsize;                                                 \
91         if ((cur_length+wsize) > cur_string_size) {                     \
92             nsize = cur_string_size + cur_string_size / 5 + EXTRA_STRING; \
93             if (nsize < (unsigned)(wsize)) {                            \
94                 nsize = wsize + EXTRA_STRING;                           \
95             }                                                           \
96             cur_string = (unsigned char *) xreallocarray(cur_string, unsigned char, nsize); \
97             memset (cur_string+cur_length,0,nsize-cur_length); \
98             cur_string_size = nsize;                                    \
99         }                                                               \
100     } while (0)
101 
102 #  define flush_char() --cur_length     /* forget the last character in the pool */
103 
104 extern str_number make_string(void);
105 extern boolean str_eq_buf(str_number s, int k);
106 extern boolean str_eq_str(str_number s, str_number t);
107 extern boolean str_eq_cstr(str_number, const char *, size_t);
108 extern boolean get_strings_started(void);
109 extern void reset_cur_string(void);
110 
111 #  define save_cur_string() (cur_length>0 ? make_string() : 0)
112 
113 #  define restore_cur_string(u) if (u!=0) {                   \
114         unsigned l = (unsigned)str_length(u);		      \
115 	xfree(cur_string);                                  \
116         reset_cur_string();                                 \
117         str_room(l);                                        \
118         memcpy(cur_string, str_string(u),l);                \
119         cur_length = l;                                     \
120         flush_str(u);                                       \
121         u=0;                                                \
122     }
123 
124 
125 extern str_number search_string(str_number search);
126 extern int pool_to_unichar(unsigned char *t);
127 
128 extern str_number maketexstring(const char *);
129 extern str_number maketexlstring(const char *, size_t);
130 extern void append_string(const unsigned char *s, unsigned l);
131 
132 extern char *makecstring(int);
133 extern char *makeclstring(int, size_t *);
134 
135 extern int dump_string_pool(void);
136 extern int undump_string_pool(void);
137 
138 extern void init_string_pool_array(unsigned s);
139 extern void flush_str(str_number s);
140 
141 #endif
142