1 /*---------------------------------------------------------------------------* 2 | PDFlib - A library for generating PDF on the fly | 3 +---------------------------------------------------------------------------+ 4 | Copyright (c) 1997-2006 Thomas Merz and PDFlib GmbH. All rights reserved. | 5 +---------------------------------------------------------------------------+ 6 | | 7 | This software is subject to the PDFlib license. It is NOT in the | 8 | public domain. Extended versions and commercial licenses are | 9 | available, please check http://www.pdflib.com. | 10 | | 11 *---------------------------------------------------------------------------*/ 12 13 /* $Id: pc_string.h,v 1.20.2.2 2007/03/27 11:28:02 york Exp $ 14 * 15 * The core string classes. 16 * 17 */ 18 19 #ifndef PC_STRING_H 20 #define PC_STRING_H 21 22 #include "pc_core.h" 23 24 /* there are two pdcore string classes. the structures "behind" 25 ** these classes are opaque. conceptually, pdc_bstr objects are 26 ** sequences of 'pdc_byte' (unsigned 8-bit entities), whereas 27 ** pdc_ustr objects are sequences of 'pdc_ucval' ("unicode value"; 28 ** unsigned 32-bit entities). 29 */ 30 #ifndef PDC_STRINGS_DEFINED 31 #define PDC_STRINGS_DEFINED 32 typedef struct pdc_bstr_s pdc_bstr; /* byte strings */ 33 typedef struct pdc_ustr_s pdc_ustr; /* unicode strings */ 34 #endif 35 36 37 /* TODO: naming conventions for "per module" init/cleanup */ 38 void pdc_init_strings(pdc_core *pdc); 39 void pdc_cleanup_strings(pdc_core *pdc); 40 41 42 /************************************************************************/ 43 /* */ 44 /* string object construction and deletion. */ 45 /* */ 46 /************************************************************************/ 47 48 /* convert raw memory into an (empty) string object. 49 */ 50 void pdc_bs_boot(pdc_core *pdc, pdc_bstr *s); 51 void pdc_us_boot(pdc_core *pdc, pdc_ustr *s); 52 53 /* release all resources allocated by a string object (if any). 54 */ 55 void pdc_bs_shutdown(pdc_bstr *s); 56 void pdc_us_shutdown(pdc_ustr *s); 57 58 /* allocate a new (empty) pdc_bstr object. 59 */ 60 pdc_bstr *pdc_bs_new(pdc_core *pdc); 61 62 /* allocate a new pdc_ustr object and initialize its contents with the 63 ** 'n' values from 'src'. if 'src' is null or 'n' is zero, 64 ** an empty string object is constructed. 65 */ 66 pdc_ustr *pdc_us_new(pdc_core *pdc, const pdc_ucval *src, size_t n); 67 68 /* TODO: more constructors for various "source" types, eg. 69 ** 70 pdc_ustr *pdc_us_new_utf16(pdc_core *pdc, const pdc_ushort *src, size_t n); 71 pdc_ustr *pdc_us_new_utf8(pdc_core *pdc, const pdc_byte *src, size_t n); 72 */ 73 74 /* return a copy of string 'src' ("copy constructor"). 75 */ 76 pdc_bstr *pdc_bs_dup(const pdc_bstr *src); 77 pdc_ustr *pdc_us_dup(const pdc_ustr *src); 78 79 /* delete a string object explicitly ("destructor"). 80 */ 81 void pdc_bs_delete(pdc_bstr *s); 82 void pdc_us_delete(pdc_ustr *s); 83 84 /************************************************************************/ 85 /* */ 86 /* "getters". */ 87 /* */ 88 /************************************************************************/ 89 90 /* get the length of a string object in bytes or unicode values, resp. 91 */ 92 size_t pdc_bs_length(const pdc_bstr *s); 93 size_t pdc_us_length(const pdc_ustr *s); 94 95 /* string component access (range checked). 96 */ 97 pdc_byte pdc_bs_get(const pdc_bstr *s, int idx); 98 pdc_ucval pdc_us_get(const pdc_ustr *s, int idx); 99 100 /* TODO: try to get rid of that. */ 101 const pdc_byte *pdc_bs_get_cptr(const pdc_bstr *s); 102 const pdc_byte *pdc_us_get_cptr(const pdc_ustr *s); 103 104 /************************************************************************/ 105 /* */ 106 /* "modifiers". */ 107 /* */ 108 /************************************************************************/ 109 110 /* copy 'src' to 'dst' ("assignment operator"). 111 */ 112 void pdc_bs_copy(pdc_bstr *dst, const pdc_bstr *src); 113 void pdc_us_copy(pdc_ustr *dst, const pdc_ustr *src); 114 115 /* copy part of 'src' to 'dst'. 116 */ 117 void pdc_bs_substr(pdc_bstr *dst, const pdc_bstr *src, 118 size_t pos, size_t len); 119 void pdc_us_substr(pdc_ustr *dst, const pdc_ustr *src, 120 size_t pos, size_t len); 121 122 /* insert 'src' into 'dst' at 'pos'. 123 */ 124 void pdc_bs_insert(pdc_bstr *dst, const pdc_bstr *src, size_t pos); 125 void pdc_us_insert(pdc_ustr *dst, const pdc_ustr *src, size_t pos); 126 127 /* append 'src' to 'dst'. 128 */ 129 void pdc_bs_concat(pdc_bstr *dst, const pdc_bstr *src); 130 void pdc_us_concat(pdc_ustr *dst, const pdc_ustr *src); 131 132 /* string component access (range checked). 133 */ 134 void pdc_bs_set(pdc_bstr *s, int idx, pdc_byte val); 135 void pdc_us_set(pdc_ustr *s, int idx, pdc_ucval val); 136 137 /* case conversion. 138 */ 139 void pdc_bs_tolower(pdc_bstr *s); 140 void pdc_bs_toupper(pdc_bstr *s); 141 142 /************************************************************************/ 143 /* */ 144 /* stream-like functions. */ 145 /* */ 146 /************************************************************************/ 147 148 /* append 'n' values from 'src' to 'dst'. if 'n' is zero, 149 ** or 'src' is null, 'dst' remains unchanged. 150 */ 151 void pdc_bs_write(pdc_bstr *dst, const pdc_byte *src, size_t n); 152 153 /* append the null terminated string 'src' to 'dst'. 154 */ 155 void pdc_bs_puts(pdc_bstr *dst, const pdc_byte *src); 156 157 /* append 'n' values from 'src' to 'dst'. if 'src' is null or 'n' 158 ** is zero, 'dst' remains unchanged. 159 */ 160 void pdc_us_write(pdc_ustr *dst, const pdc_ucval *src, size_t n); 161 162 void pdc_us_write_utf16(pdc_ustr *dst, const pdc_ushort *src, size_t n); 163 164 /* TODO: more writer functions for various "source" types, eg. 165 ** 166 void pdc_us_write_utf8(pdc_ustr *dst, const pdc_byte *src, size_t n); 167 */ 168 169 /* reset 's' to an empty stream object. 170 */ 171 void pdc_bs_rewrite(pdc_bstr *s); 172 void pdc_us_rewrite(pdc_ustr *s); 173 174 /* append a single byte (or unicode value, resp.) to a string object. 175 */ 176 void pdc_bs_putc(pdc_bstr *s, pdc_byte val); 177 void pdc_us_putc(pdc_ustr *s, pdc_ucval val); 178 179 /* TODO: stream-like read access. again, the read functions for pdc_ustr 180 ** objects will be available in several flavors in order to support 181 ** conversion to various "external" formats. 182 ** 183 void pdc_bs_reset(pdc_bstr *s); 184 void pdc_us_reset(pdc_ustr *s); 185 void pdc_bs_seek(pdc_bstr *s, size_t pos); 186 void pdc_us_seek(pdc_ustr *s, size_t pos); 187 size_t pdc_bs_tell(const pdc_bstr *s); 188 size_t pdc_us_tell(const pdc_ustr *s); 189 size_t pdc_bs_read(pdc_bstr *src, pdc_byte *dst, size_t n); 190 size_t pdc_us_read(pdc_ustr *src, pdc_ucval *dst, size_t n); 191 size_t pdc_us_read_utf16(pdc_ustr *src, pdc_ushort *dst, size_t n); 192 size_t pdc_us_read_utf8(pdc_ustr *src, pdc_byte *dst, size_t n); 193 */ 194 195 /************************************************************************/ 196 /* */ 197 /* other utilities. */ 198 /* */ 199 /************************************************************************/ 200 201 int pdc_bs_compare(const pdc_bstr *s1, const pdc_bstr *s2); 202 203 /************************************************************************/ 204 /* */ 205 /* PRIVATE SECTION */ 206 /* */ 207 /* the declarations below are strictly private to the implementation */ 208 /* module, and must not be used by any client modules! */ 209 /* */ 210 /************************************************************************/ 211 212 #define PDC_STR_INLINE_CAP 16 213 214 struct pdc_bstr_s 215 { 216 pdc_core * pdc; 217 218 pdc_byte buf0[PDC_STR_INLINE_CAP]; 219 pdc_byte * buf; 220 size_t len; 221 size_t cap; 222 }; 223 224 struct pdc_ustr_s 225 { 226 pdc_core * pdc; 227 228 pdc_ucval buf0[PDC_STR_INLINE_CAP]; 229 pdc_ucval * buf; 230 size_t len; 231 size_t cap; 232 }; 233 234 #if 0 235 /* string representation. 236 */ 237 typedef struct 238 { 239 pdc_byte * buf; /* contents */ 240 size_t cap; /* capacity (unit: pdc_byte) */ 241 size_t len; /* length (unit: pdc_byte) */ 242 int ref; /* reference count */ 243 } pdc_bs_rep; 244 245 typedef struct 246 { 247 pdc_ucval * buf; /* contents */ 248 size_t cap; /* capacity (unit: pdc_ucval) */ 249 size_t len; /* length (unit: pdc_ucval) */ 250 int ref; /* reference count */ 251 } pdc_us_rep; 252 253 254 struct pdc_bstr_s 255 { 256 pdc_core * pdc; 257 pdc_bs_rep *rep; 258 }; 259 260 struct pdc_ustr_s 261 { 262 pdc_core * pdc; 263 pdc_us_rep *rep; 264 }; 265 #endif 266 267 #endif /* PC_STRING_H */ 268