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-types.h>
49 : #include <pdf-base.h>
50 :
51 : /* BEGIN PUBLIC */
52 : /* pdf-token.h */
53 :
54 : enum pdf_token_type_e
55 : {
56 : PDF_TOKEN_INTEGER = 1,
57 : PDF_TOKEN_REAL = 2,
58 : PDF_TOKEN_STRING = 3,
59 : PDF_TOKEN_NAME = 4,
60 : PDF_TOKEN_KEYWORD = 5,
61 : PDF_TOKEN_COMMENT = 6,
62 : PDF_TOKEN_DICT_START = 7,
63 : PDF_TOKEN_DICT_END = 8,
64 : PDF_TOKEN_ARRAY_START = 9,
65 : PDF_TOKEN_ARRAY_END = 10,
66 : PDF_TOKEN_PROC_START = 11,
67 : PDF_TOKEN_PROC_END = 12,
68 : };
69 :
70 : struct pdf_token_s; /* opaque type */
71 : typedef struct pdf_token_s *pdf_token_t;
72 :
73 : /* Token creation */
74 : pdf_status_t pdf_token_integer_new (pdf_i32_t value, pdf_token_t *token);
75 : pdf_status_t pdf_token_real_new (pdf_real_t value, pdf_token_t *token);
76 : pdf_status_t pdf_token_string_new (const pdf_char_t *value, pdf_size_t size,
77 : pdf_token_t *token);
78 : pdf_status_t pdf_token_name_new (const pdf_char_t *value, pdf_size_t size,
79 : pdf_token_t *token);
80 : pdf_status_t pdf_token_keyword_new (const pdf_char_t *value, pdf_size_t size,
81 : pdf_token_t *token);
82 : pdf_status_t pdf_token_comment_new (const pdf_char_t *value, pdf_size_t size,
83 : pdf_token_t *token);
84 : pdf_status_t pdf_token_valueless_new (enum pdf_token_type_e type,
85 : pdf_token_t *token);
86 : pdf_status_t pdf_token_dup (const pdf_token_t token, pdf_token_t *new);
87 :
88 : /* Token destruction */
89 : pdf_status_t pdf_token_destroy (pdf_token_t token);
90 :
91 : /* Common functions */
92 : enum pdf_token_type_e pdf_token_get_type (const pdf_token_t token);
93 : pdf_bool_t pdf_token_equal_p (const pdf_token_t token1,
94 : const pdf_token_t token2);
95 :
96 : /* Managing tokens of numeric types */
97 : pdf_i32_t pdf_token_get_integer_value (const pdf_token_t token);
98 : pdf_real_t pdf_token_get_real_value (const pdf_token_t token);
99 :
100 : /* Managing strings */
101 : pdf_size_t pdf_token_get_string_size (const pdf_token_t token);
102 : const pdf_char_t *pdf_token_get_string_data (const pdf_token_t token);
103 :
104 : /* Managing names */
105 : pdf_size_t pdf_token_get_name_size (const pdf_token_t token);
106 : const pdf_char_t *pdf_token_get_name_data (const pdf_token_t token);
107 :
108 : /* Managing keywords */
109 : pdf_size_t pdf_token_get_keyword_size (const pdf_token_t token);
110 : const pdf_char_t *pdf_token_get_keyword_data (const pdf_token_t token);
111 :
112 : /* Managing comments */
113 : pdf_size_t pdf_token_get_comment_size (const pdf_token_t token);
114 : const pdf_char_t *pdf_token_get_comment_data (const pdf_token_t token);
115 :
116 : /* END PUBLIC */
117 :
118 :
119 : static INLINE int
120 : pdf_is_wspace_char (pdf_char_t ch)
121 : {
122 : /* ASCII codes for NUL, HT, LF, FF, CR, SP */
123 9323 : return (ch == 0 || ch == 9 || ch == 10 || ch == 12 || ch == 13 || ch == 32);
124 : }
125 :
126 : static INLINE int
127 : pdf_is_delim_char (pdf_char_t ch)
128 : {
129 : /* ASCII codes for '%', '(', ')', '/'; '<', '>', '[', ']'; '{', '}' */
130 6707 : return (ch == 37 || ch == 40 || ch == 41 || ch == 47
131 : || ch == 60 || ch == 62 || ch == 91 || ch == 93
132 : || ch == 123 || ch == 125);
133 : }
134 :
135 : static INLINE int
136 : pdf_is_eol_char (pdf_char_t ch)
137 : {
138 15 : return ch == 10 || ch == 13;
139 : }
140 :
141 : static INLINE int
142 : pdf_is_regular_char (pdf_char_t ch)
143 0 : {
144 6645 : return !pdf_is_wspace_char (ch) && !pdf_is_delim_char (ch);
145 : }
146 :
147 : /* According to the PDF reference, a PDF name object is an atomic
148 : symbol uniquely defined by a sequence of non-null characters. It has
149 : no internal structure.
150 :
151 : In the practice, PDF uses name objects in order to store
152 : information (such as font names). In that situation, it is
153 : recommended to code such information in UTF-8. Due to this
154 : stupidity we should store the entire byte sequence that conform the
155 : name.
156 :
157 : A pdf_token_buffer_s structure is used to store a name's value. */
158 :
159 :
160 : /* A PDF string is a sequence of bytes, in the range of 0-255. In
161 : particular it may contain NULL characters (code 0 in the ASCII
162 : CCS).
163 :
164 : Corollary: NEVER NEVER NEVER EVER use a PDF string as an input
165 : expecting null-terminated strings. You have been warned.
166 :
167 : A pdf_token_buffer_s structure is used to store a string's value. */
168 :
169 :
170 : /* pdf_token_buffer_s is an internal structure used for tokens with an
171 : * associated byte array. 'data' may or may not be null-terminated,
172 : * but size never includes the trailing null. */
173 : struct pdf_token_buffer_s
174 : {
175 : pdf_char_t *data;
176 : pdf_size_t size;
177 : };
178 :
179 :
180 : /* A `pdf_token_s' structure stores a PDF object. The object may be of
181 : any type (including NULL). */
182 :
183 : struct pdf_token_s
184 : {
185 : enum pdf_token_type_e type;
186 :
187 : union
188 : {
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 */
|