1 : /* -*- mode: C -*-
2 : *
3 : * File: pdf-token.h
4 : * Date: Sat Jul 7 01:10:11 2007
5 : *
6 : * GNU PDF Library - PDF token objects
7 : *
8 : */
9 :
10 : /* Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc. */
11 :
12 : /* This program is free software: you can redistribute it and/or modify
13 : * it under the terms of the GNU General Public License as published by
14 : * the Free Software Foundation, either version 3 of the License, or
15 : * (at your option) any later version.
16 : *
17 : * This program is distributed in the hope that it will be useful,
18 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 : * GNU General Public License for more details.
21 : *
22 : * You should have received a copy of the GNU General Public License
23 : * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 : */
25 :
26 : /* pdf-token.{c,h} implement the token ADT, used to represent
27 : all types of tokens used by the token reader and writer:
28 : - Integer number
29 : - Real number
30 : - String
31 : - Name
32 : - Keyword
33 : - Comment
34 : - Valueless tokens:
35 : - PDF_TOKEN_DICT_START
36 : - PDF_TOKEN_DICT_END
37 : - PDF_TOKEN_ARRAY_START
38 : - PDF_TOKEN_ARRAY_END
39 : - PDF_TOKEN_PROC_START
40 : - PDF_TOKEN_PROC_END
41 : */
42 :
43 : #ifndef PDF_TOKEN_OBJ_H
44 : #define PDF_TOKEN_OBJ_H
45 :
46 : #include <config.h>
47 :
48 : #include <pdf-error.h>
49 : #include <pdf-fp.h>
50 : #include <pdf-types.h>
51 :
52 : /* BEGIN PUBLIC */
53 : /* pdf-token.h */
54 :
55 : enum pdf_token_type_e
56 : {
57 : PDF_TOKEN_INTEGER = 1,
58 : PDF_TOKEN_REAL = 2,
59 : PDF_TOKEN_STRING = 3,
60 : PDF_TOKEN_NAME = 4,
61 : PDF_TOKEN_KEYWORD = 5,
62 : PDF_TOKEN_COMMENT = 6,
63 : PDF_TOKEN_DICT_START = 7,
64 : PDF_TOKEN_DICT_END = 8,
65 : PDF_TOKEN_ARRAY_START = 9,
66 : PDF_TOKEN_ARRAY_END = 10,
67 : PDF_TOKEN_PROC_START = 11,
68 : PDF_TOKEN_PROC_END = 12,
69 : };
70 :
71 : struct pdf_token_s; /* opaque type */
72 : typedef struct pdf_token_s *pdf_token_t;
73 :
74 : /* Token creation */
75 : pdf_status_t pdf_token_integer_new (pdf_i32_t value, pdf_token_t *token);
76 : pdf_status_t pdf_token_real_new (pdf_real_t value, pdf_token_t *token);
77 : pdf_status_t pdf_token_string_new (const pdf_char_t *value, pdf_size_t size,
78 : pdf_token_t *token);
79 : pdf_status_t pdf_token_name_new (const pdf_char_t *value, pdf_size_t size,
80 : pdf_token_t *token);
81 : pdf_status_t pdf_token_keyword_new (const pdf_char_t *value, pdf_size_t size,
82 : pdf_token_t *token);
83 : pdf_status_t pdf_token_comment_new (const pdf_char_t *value, pdf_size_t size,
84 : pdf_token_t *token);
85 : pdf_status_t pdf_token_valueless_new (enum pdf_token_type_e type,
86 : pdf_token_t *token);
87 : pdf_status_t pdf_token_dup (const pdf_token_t token, pdf_token_t *new);
88 :
89 : /* Token destruction */
90 : pdf_status_t pdf_token_destroy (pdf_token_t token);
91 :
92 : /* Common functions */
93 : enum pdf_token_type_e pdf_token_get_type (const pdf_token_t token);
94 : pdf_bool_t pdf_token_equal_p (const pdf_token_t token1,
95 : const pdf_token_t token2);
96 :
97 : /* Managing tokens of numeric types */
98 : pdf_i32_t pdf_token_get_integer_value (const pdf_token_t token);
99 : pdf_real_t pdf_token_get_real_value (const pdf_token_t token);
100 :
101 : /* Managing strings */
102 : pdf_size_t pdf_token_get_string_size (const pdf_token_t token);
103 : const pdf_char_t *pdf_token_get_string_data (const pdf_token_t token);
104 :
105 : /* Managing names */
106 : pdf_size_t pdf_token_get_name_size (const pdf_token_t token);
107 : const pdf_char_t *pdf_token_get_name_data (const pdf_token_t token);
108 :
109 : /* Managing keywords */
110 : pdf_size_t pdf_token_get_keyword_size (const pdf_token_t token);
111 : const pdf_char_t *pdf_token_get_keyword_data (const pdf_token_t token);
112 :
113 : /* Managing comments */
114 : pdf_size_t pdf_token_get_comment_size (const pdf_token_t token);
115 : const pdf_char_t *pdf_token_get_comment_data (const pdf_token_t token);
116 :
117 : /* END PUBLIC */
118 :
119 :
120 : static INLINE int
121 : pdf_is_wspace_char (pdf_char_t ch)
122 : {
123 : /* ASCII codes for NUL, HT, LF, FF, CR, SP */
124 9772 : return (ch == 0 || ch == 9 || ch == 10 || ch == 12 || ch == 13 || ch == 32);
125 : }
126 :
127 : static INLINE int
128 : pdf_is_delim_char (pdf_char_t ch)
129 : {
130 : /* ASCII codes for '%', '(', ')', '/'; '<', '>', '[', ']'; '{', '}' */
131 7147 : return (ch == 37 || ch == 40 || ch == 41 || ch == 47
132 : || ch == 60 || ch == 62 || ch == 91 || ch == 93
133 : || ch == 123 || ch == 125);
134 : }
135 :
136 : static INLINE int
137 : pdf_is_eol_char (pdf_char_t ch)
138 : {
139 538 : return ch == 10 || ch == 13;
140 : }
141 :
142 : static INLINE int
143 : pdf_is_regular_char (pdf_char_t ch)
144 445 : {
145 7971 : return !pdf_is_wspace_char (ch) && !pdf_is_delim_char (ch);
146 : }
147 :
148 : /* According to the PDF reference, a PDF name object is an atomic
149 : symbol uniquely defined by a sequence of non-null characters. It has
150 : no internal structure.
151 :
152 : In the practice, PDF uses name objects in order to store
153 : information (such as font names). In that situation, it is
154 : recommended to code such information in UTF-8. Due to this
155 : stupidity we should store the entire byte sequence that conform the
156 : name.
157 :
158 : A pdf_token_buffer_s structure is used to store a name's value. */
159 :
160 :
161 : /* A PDF string is a sequence of bytes, in the range of 0-255. In
162 : particular it may contain NULL characters (code 0 in the ASCII
163 : CCS).
164 :
165 : Corollary: NEVER NEVER NEVER EVER use a PDF string as an input
166 : expecting null-terminated strings. You have been warned.
167 :
168 : A pdf_token_buffer_s structure is used to store a string's value. */
169 :
170 :
171 : /* pdf_token_buffer_s is an internal structure used for tokens with an
172 : * associated byte array. 'data' may or may not be null-terminated,
173 : * but size never includes the trailing null. */
174 : struct pdf_token_buffer_s
175 : {
176 : pdf_char_t *data;
177 : pdf_size_t size;
178 : };
179 :
180 :
181 : /* A `pdf_token_s' structure stores a PDF object. The object may be of
182 : any type (including NULL). */
183 :
184 : struct pdf_token_s
185 : {
186 : enum pdf_token_type_e type;
187 :
188 : union
189 : {
190 : struct pdf_token_buffer_s buffer;
191 : pdf_i32_t integer;
192 : pdf_real_t real;
193 :
194 : } value;
195 : };
196 :
197 : #endif /* PDF_TOKEN_OBJ_H */
198 :
199 : /* End of pdf-token.h */
|