OSDN Git Service

Please enter the commit message for your changes. Lines starting
[eos/hostdependX86LINUX64.git] / util / X86LINUX64 / man / man3 / Tcl_GetEncodingFromObj.3
1 '\"
2 '\" Copyright (c) 1997-1998 Sun Microsystems, Inc.
3 '\"
4 '\" See the file "license.terms" for information on usage and redistribution
5 '\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
6 '\" 
7 .TH Tcl_GetEncoding 3 "8.1" Tcl "Tcl Library Procedures"
8 .\" The -*- nroff -*- definitions below are for supplemental macros used
9 .\" in Tcl/Tk manual entries.
10 .\"
11 .\" .AP type name in/out ?indent?
12 .\"     Start paragraph describing an argument to a library procedure.
13 .\"     type is type of argument (int, etc.), in/out is either "in", "out",
14 .\"     or "in/out" to describe whether procedure reads or modifies arg,
15 .\"     and indent is equivalent to second arg of .IP (shouldn't ever be
16 .\"     needed;  use .AS below instead)
17 .\"
18 .\" .AS ?type? ?name?
19 .\"     Give maximum sizes of arguments for setting tab stops.  Type and
20 .\"     name are examples of largest possible arguments that will be passed
21 .\"     to .AP later.  If args are omitted, default tab stops are used.
22 .\"
23 .\" .BS
24 .\"     Start box enclosure.  From here until next .BE, everything will be
25 .\"     enclosed in one large box.
26 .\"
27 .\" .BE
28 .\"     End of box enclosure.
29 .\"
30 .\" .CS
31 .\"     Begin code excerpt.
32 .\"
33 .\" .CE
34 .\"     End code excerpt.
35 .\"
36 .\" .VS ?version? ?br?
37 .\"     Begin vertical sidebar, for use in marking newly-changed parts
38 .\"     of man pages.  The first argument is ignored and used for recording
39 .\"     the version when the .VS was added, so that the sidebars can be
40 .\"     found and removed when they reach a certain age.  If another argument
41 .\"     is present, then a line break is forced before starting the sidebar.
42 .\"
43 .\" .VE
44 .\"     End of vertical sidebar.
45 .\"
46 .\" .DS
47 .\"     Begin an indented unfilled display.
48 .\"
49 .\" .DE
50 .\"     End of indented unfilled display.
51 .\"
52 .\" .SO ?manpage?
53 .\"     Start of list of standard options for a Tk widget. The manpage
54 .\"     argument defines where to look up the standard options; if
55 .\"     omitted, defaults to "options". The options follow on successive
56 .\"     lines, in three columns separated by tabs.
57 .\"
58 .\" .SE
59 .\"     End of list of standard options for a Tk widget.
60 .\"
61 .\" .OP cmdName dbName dbClass
62 .\"     Start of description of a specific option.  cmdName gives the
63 .\"     option's name as specified in the class command, dbName gives
64 .\"     the option's name in the option database, and dbClass gives
65 .\"     the option's class in the option database.
66 .\"
67 .\" .UL arg1 arg2
68 .\"     Print arg1 underlined, then print arg2 normally.
69 .\"
70 .\" .QW arg1 ?arg2?
71 .\"     Print arg1 in quotes, then arg2 normally (for trailing punctuation).
72 .\"
73 .\" .PQ arg1 ?arg2?
74 .\"     Print an open parenthesis, arg1 in quotes, then arg2 normally
75 .\"     (for trailing punctuation) and then a closing parenthesis.
76 .\"
77 .\"     # Set up traps and other miscellaneous stuff for Tcl/Tk man pages.
78 .if t .wh -1.3i ^B
79 .nr ^l \n(.l
80 .ad b
81 .\"     # Start an argument description
82 .de AP
83 .ie !"\\$4"" .TP \\$4
84 .el \{\
85 .   ie !"\\$2"" .TP \\n()Cu
86 .   el          .TP 15
87 .\}
88 .ta \\n()Au \\n()Bu
89 .ie !"\\$3"" \{\
90 \&\\$1 \\fI\\$2\\fP (\\$3)
91 .\".b
92 .\}
93 .el \{\
94 .br
95 .ie !"\\$2"" \{\
96 \&\\$1  \\fI\\$2\\fP
97 .\}
98 .el \{\
99 \&\\fI\\$1\\fP
100 .\}
101 .\}
102 ..
103 .\"     # define tabbing values for .AP
104 .de AS
105 .nr )A 10n
106 .if !"\\$1"" .nr )A \\w'\\$1'u+3n
107 .nr )B \\n()Au+15n
108 .\"
109 .if !"\\$2"" .nr )B \\w'\\$2'u+\\n()Au+3n
110 .nr )C \\n()Bu+\\w'(in/out)'u+2n
111 ..
112 .AS Tcl_Interp Tcl_CreateInterp in/out
113 .\"     # BS - start boxed text
114 .\"     # ^y = starting y location
115 .\"     # ^b = 1
116 .de BS
117 .br
118 .mk ^y
119 .nr ^b 1u
120 .if n .nf
121 .if n .ti 0
122 .if n \l'\\n(.lu\(ul'
123 .if n .fi
124 ..
125 .\"     # BE - end boxed text (draw box now)
126 .de BE
127 .nf
128 .ti 0
129 .mk ^t
130 .ie n \l'\\n(^lu\(ul'
131 .el \{\
132 .\"     Draw four-sided box normally, but don't draw top of
133 .\"     box if the box started on an earlier page.
134 .ie !\\n(^b-1 \{\
135 \h'-1.5n'\L'|\\n(^yu-1v'\l'\\n(^lu+3n\(ul'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul'
136 .\}
137 .el \}\
138 \h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul'
139 .\}
140 .\}
141 .fi
142 .br
143 .nr ^b 0
144 ..
145 .\"     # VS - start vertical sidebar
146 .\"     # ^Y = starting y location
147 .\"     # ^v = 1 (for troff;  for nroff this doesn't matter)
148 .de VS
149 .if !"\\$2"" .br
150 .mk ^Y
151 .ie n 'mc \s12\(br\s0
152 .el .nr ^v 1u
153 ..
154 .\"     # VE - end of vertical sidebar
155 .de VE
156 .ie n 'mc
157 .el \{\
158 .ev 2
159 .nf
160 .ti 0
161 .mk ^t
162 \h'|\\n(^lu+3n'\L'|\\n(^Yu-1v\(bv'\v'\\n(^tu+1v-\\n(^Yu'\h'-|\\n(^lu+3n'
163 .sp -1
164 .fi
165 .ev
166 .\}
167 .nr ^v 0
168 ..
169 .\"     # Special macro to handle page bottom:  finish off current
170 .\"     # box/sidebar if in box/sidebar mode, then invoked standard
171 .\"     # page bottom macro.
172 .de ^B
173 .ev 2
174 'ti 0
175 'nf
176 .mk ^t
177 .if \\n(^b \{\
178 .\"     Draw three-sided box if this is the box's first page,
179 .\"     draw two sides but no top otherwise.
180 .ie !\\n(^b-1 \h'-1.5n'\L'|\\n(^yu-1v'\l'\\n(^lu+3n\(ul'\L'\\n(^tu+1v-\\n(^yu'\h'|0u'\c
181 .el \h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\h'|0u'\c
182 .\}
183 .if \\n(^v \{\
184 .nr ^x \\n(^tu+1v-\\n(^Yu
185 \kx\h'-\\nxu'\h'|\\n(^lu+3n'\ky\L'-\\n(^xu'\v'\\n(^xu'\h'|0u'\c
186 .\}
187 .bp
188 'fi
189 .ev
190 .if \\n(^b \{\
191 .mk ^y
192 .nr ^b 2
193 .\}
194 .if \\n(^v \{\
195 .mk ^Y
196 .\}
197 ..
198 .\"     # DS - begin display
199 .de DS
200 .RS
201 .nf
202 .sp
203 ..
204 .\"     # DE - end display
205 .de DE
206 .fi
207 .RE
208 .sp
209 ..
210 .\"     # SO - start of list of standard options
211 .de SO
212 'ie '\\$1'' .ds So \\fBoptions\\fR
213 'el .ds So \\fB\\$1\\fR
214 .SH "STANDARD OPTIONS"
215 .LP
216 .nf
217 .ta 5.5c 11c
218 .ft B
219 ..
220 .\"     # SE - end of list of standard options
221 .de SE
222 .fi
223 .ft R
224 .LP
225 See the \\*(So manual entry for details on the standard options.
226 ..
227 .\"     # OP - start of full description for a single option
228 .de OP
229 .LP
230 .nf
231 .ta 4c
232 Command-Line Name:      \\fB\\$1\\fR
233 Database Name:  \\fB\\$2\\fR
234 Database Class: \\fB\\$3\\fR
235 .fi
236 .IP
237 ..
238 .\"     # CS - begin code excerpt
239 .de CS
240 .RS
241 .nf
242 .ta .25i .5i .75i 1i
243 ..
244 .\"     # CE - end code excerpt
245 .de CE
246 .fi
247 .RE
248 ..
249 .\"     # UL - underline word
250 .de UL
251 \\$1\l'|0\(ul'\\$2
252 ..
253 .\"     # QW - apply quotation marks to word
254 .de QW
255 .ie '\\*(lq'"' ``\\$1''\\$2
256 .\"" fix emacs highlighting
257 .el \\*(lq\\$1\\*(rq\\$2
258 ..
259 .\"     # PQ - apply parens and quotation marks to word
260 .de PQ
261 .ie '\\*(lq'"' (``\\$1''\\$2)\\$3
262 .\"" fix emacs highlighting
263 .el (\\*(lq\\$1\\*(rq\\$2)\\$3
264 ..
265 .\"     # QR - quoted range
266 .de QR
267 .ie '\\*(lq'"' ``\\$1''\\-``\\$2''\\$3
268 .\"" fix emacs highlighting
269 .el \\*(lq\\$1\\*(rq\\-\\*(lq\\$2\\*(rq\\$3
270 ..
271 .\"     # MT - "empty" string
272 .de MT
273 .QW ""
274 ..
275 .BS
276 .SH NAME
277 Tcl_GetEncoding, Tcl_FreeEncoding, Tcl_GetEncodingFromObj, Tcl_ExternalToUtfDString, Tcl_ExternalToUtf, Tcl_UtfToExternalDString, Tcl_UtfToExternal, Tcl_WinTCharToUtf, Tcl_WinUtfToTChar, Tcl_GetEncodingName, Tcl_SetSystemEncoding, Tcl_GetEncodingNameFromEnvironment, Tcl_GetEncodingNames, Tcl_CreateEncoding, Tcl_GetEncodingSearchPath, Tcl_SetEncodingSearchPath, Tcl_GetDefaultEncodingDir, Tcl_SetDefaultEncodingDir \- procedures for creating and using encodings
278 .SH SYNOPSIS
279 .nf
280 \fB#include <tcl.h>\fR
281 .sp
282 Tcl_Encoding
283 \fBTcl_GetEncoding\fR(\fIinterp, name\fR)
284 .sp
285 void
286 \fBTcl_FreeEncoding\fR(\fIencoding\fR)
287 .sp
288 int
289 \fBTcl_GetEncodingFromObj\fR(\fIinterp, objPtr, encodingPtr\fR)
290 .sp
291 char *
292 \fBTcl_ExternalToUtfDString\fR(\fIencoding, src, srcLen, dstPtr\fR)
293 .sp
294 char *
295 \fBTcl_UtfToExternalDString\fR(\fIencoding, src, srcLen, dstPtr\fR)
296 .sp
297 int
298 \fBTcl_ExternalToUtf\fR(\fIinterp, encoding, src, srcLen, flags, statePtr,
299                   dst, dstLen, srcReadPtr, dstWrotePtr, dstCharsPtr\fR)
300 .sp
301 int
302 \fBTcl_UtfToExternal\fR(\fIinterp, encoding, src, srcLen, flags, statePtr,
303                   dst, dstLen, srcReadPtr, dstWrotePtr, dstCharsPtr\fR)
304 .sp
305 char *
306 \fBTcl_WinTCharToUtf\fR(\fItsrc, srcLen, dstPtr\fR)
307 .sp
308 TCHAR *
309 \fBTcl_WinUtfToTChar\fR(\fIsrc, srcLen, dstPtr\fR)
310 .sp
311 const char *
312 \fBTcl_GetEncodingName\fR(\fIencoding\fR)
313 .sp
314 int
315 \fBTcl_SetSystemEncoding\fR(\fIinterp, name\fR)
316 .sp
317 const char *
318 \fBTcl_GetEncodingNameFromEnvironment\fR(\fIbufPtr\fR)
319 .sp
320 void
321 \fBTcl_GetEncodingNames\fR(\fIinterp\fR)
322 .sp
323 Tcl_Encoding
324 \fBTcl_CreateEncoding\fR(\fItypePtr\fR)
325 .sp
326 Tcl_Obj *
327 \fBTcl_GetEncodingSearchPath\fR()
328 .sp
329 int
330 \fBTcl_SetEncodingSearchPath\fR(\fIsearchPath\fR)
331 .sp
332 const char *
333 \fBTcl_GetDefaultEncodingDir\fR(\fIvoid\fR)
334 .sp
335 void
336 \fBTcl_SetDefaultEncodingDir\fR(\fIpath\fR)
337 .SH ARGUMENTS
338 .AS "const Tcl_EncodingType" *dstWrotePtr in/out
339 .AP Tcl_Interp *interp in
340 Interpreter to use for error reporting, or NULL if no error reporting is
341 desired.
342 .AP "const char" *name in
343 Name of encoding to load.
344 .AP Tcl_Encoding encoding in
345 The encoding to query, free, or use for converting text.  If \fIencoding\fR is 
346 NULL, the current system encoding is used.
347 .AP Tcl_Obj *objPtr in
348 Name of encoding to get token for.
349 .AP Tcl_Encoding *encodingPtr out
350 Points to storage where encoding token is to be written.
351 .AP "const char" *src in
352 For the \fBTcl_ExternalToUtf\fR functions, an array of bytes in the
353 specified encoding that are to be converted to UTF-8.  For the
354 \fBTcl_UtfToExternal\fR and \fBTcl_WinUtfToTChar\fR functions, an array of
355 UTF-8 characters to be converted to the specified encoding.  
356 .AP "const TCHAR" *tsrc in
357 An array of Windows TCHAR characters to convert to UTF-8.
358 .AP int srcLen in 
359 Length of \fIsrc\fR or \fItsrc\fR in bytes.  If the length is negative, the 
360 encoding-specific length of the string is used.
361 .AP Tcl_DString *dstPtr out
362 Pointer to an uninitialized or free \fBTcl_DString\fR in which the converted
363 result will be stored.
364 .AP int flags in
365 Various flag bits OR-ed together.  
366 \fBTCL_ENCODING_START\fR signifies that the
367 source buffer is the first block in a (potentially multi-block) input
368 stream, telling the conversion routine to reset to an initial state and
369 perform any initialization that needs to occur before the first byte is
370 converted. \fBTCL_ENCODING_END\fR signifies that the source buffer is the last
371 block in a (potentially multi-block) input stream, telling the conversion
372 routine to perform any finalization that needs to occur after the last
373 byte is converted and then to reset to an initial state.
374 \fBTCL_ENCODING_STOPONERROR\fR signifies that the conversion routine should
375 return immediately upon reading a source character that does not exist in
376 the target encoding; otherwise a default fallback character will
377 automatically be substituted.  
378 .AP Tcl_EncodingState *statePtr in/out
379 Used when converting a (generally long or indefinite length) byte stream
380 in a piece-by-piece fashion.  The conversion routine stores its current
381 state in \fI*statePtr\fR after \fIsrc\fR (the buffer containing the
382 current piece) has been converted; that state information must be passed
383 back when converting the next piece of the stream so the conversion
384 routine knows what state it was in when it left off at the end of the
385 last piece.  May be NULL, in which case the value specified for \fIflags\fR 
386 is ignored and the source buffer is assumed to contain the complete string to
387 convert.
388 .AP char *dst out
389 Buffer in which the converted result will be stored.  No more than
390 \fIdstLen\fR bytes will be stored in \fIdst\fR.
391 .AP int dstLen in
392 The maximum length of the output buffer \fIdst\fR in bytes.
393 .AP int *srcReadPtr out
394 Filled with the number of bytes from \fIsrc\fR that were actually
395 converted.  This may be less than the original source length if there was
396 a problem converting some source characters.  May be NULL.
397 .AP int *dstWrotePtr out
398 Filled with the number of bytes that were actually stored in the output
399 buffer as a result of the conversion.  May be NULL.
400 .AP int *dstCharsPtr out
401 Filled with the number of characters that correspond to the number of bytes
402 stored in the output buffer.  May be NULL.
403 .AP Tcl_DString *bufPtr out
404 Storage for the prescribed system encoding name.
405 .AP "const Tcl_EncodingType" *typePtr in
406 Structure that defines a new type of encoding.  
407 .AP Tcl_Obj *searchPath in
408 List of filesystem directories in which to search for encoding data files.
409 .AP "const char" *path in
410 A path to the location of the encoding file.  
411 .BE
412 .SH INTRODUCTION
413 .PP
414 These routines convert between Tcl's internal character representation,
415 UTF-8, and character representations used by various operating systems or
416 file systems, such as Unicode, ASCII, or Shift-JIS.  When operating on
417 strings, such as such as obtaining the names of files or displaying
418 characters using international fonts, the strings must be translated into
419 one or possibly multiple formats that the various system calls can use.  For
420 instance, on a Japanese Unix workstation, a user might obtain a filename
421 represented in the EUC-JP file encoding and then translate the characters to
422 the jisx0208 font encoding in order to display the filename in a Tk widget.
423 The purpose of the encoding package is to help bridge the translation gap.
424 UTF-8 provides an intermediate staging ground for all the various
425 encodings.  In the example above, text would be translated into UTF-8 from
426 whatever file encoding the operating system is using.  Then it would be
427 translated from UTF-8 into whatever font encoding the display routines
428 require.
429 .PP
430 Some basic encodings are compiled into Tcl.  Others can be defined by the
431 user or dynamically loaded from encoding files in a
432 platform-independent manner.
433 .SH DESCRIPTION
434 .PP
435 \fBTcl_GetEncoding\fR finds an encoding given its \fIname\fR.  The name may
436 refer to a built-in Tcl encoding, a user-defined encoding registered by
437 calling \fBTcl_CreateEncoding\fR, or a dynamically-loadable encoding
438 file.  The return value is a token that represents the encoding and can be
439 used in subsequent calls to procedures such as \fBTcl_GetEncodingName\fR,
440 \fBTcl_FreeEncoding\fR, and \fBTcl_UtfToExternal\fR.  If the name did not
441 refer to any known or loadable encoding, NULL is returned and an error
442 message is returned in \fIinterp\fR.
443 .PP
444 The encoding package maintains a database of all encodings currently in use.
445 The first time \fIname\fR is seen, \fBTcl_GetEncoding\fR returns an
446 encoding with a reference count of 1.  If the same \fIname\fR is requested
447 further times, then the reference count for that encoding is incremented
448 without the overhead of allocating a new encoding and all its associated
449 data structures.  
450 .PP
451 When an \fIencoding\fR is no longer needed, \fBTcl_FreeEncoding\fR
452 should be called to release it.  When an \fIencoding\fR is no longer in use
453 anywhere (i.e., it has been freed as many times as it has been gotten)
454 \fBTcl_FreeEncoding\fR will release all storage the encoding was using
455 and delete it from the database. 
456 .PP
457 \fBTcl_GetEncodingFromObj\fR treats the string representation of
458 \fIobjPtr\fR as an encoding name, and finds an encoding with that
459 name, just as \fBTcl_GetEncoding\fR does. When an encoding is found,
460 it is cached within the \fBobjPtr\fR value for future reference, the
461 \fBTcl_Encoding\fR token is written to the storage pointed to by
462 \fIencodingPtr\fR, and the value \fBTCL_OK\fR is returned. If no such
463 encoding is found, the value \fBTCL_ERROR\fR is returned, and no
464 writing to \fB*\fR\fIencodingPtr\fR takes place. Just as with
465 \fBTcl_GetEncoding\fR, the caller should call \fBTcl_FreeEncoding\fR
466 on the resulting encoding token when that token will no longer be
467 used.
468 .PP
469 \fBTcl_ExternalToUtfDString\fR converts a source buffer \fIsrc\fR from the
470 specified \fIencoding\fR into UTF-8.  The converted bytes are stored in 
471 \fIdstPtr\fR, which is then null-terminated.  The caller should eventually
472 call \fBTcl_DStringFree\fR to free any information stored in \fIdstPtr\fR.
473 When converting, if any of the characters in the source buffer cannot be
474 represented in the target encoding, a default fallback character will be
475 used.  The return value is a pointer to the value stored in the DString.
476 .PP
477 \fBTcl_ExternalToUtf\fR converts a source buffer \fIsrc\fR from the specified
478 \fIencoding\fR into UTF-8.  Up to \fIsrcLen\fR bytes are converted from the
479 source buffer and up to \fIdstLen\fR converted bytes are stored in \fIdst\fR.
480 In all cases, \fI*srcReadPtr\fR is filled with the number of bytes that were
481 successfully converted from \fIsrc\fR and \fI*dstWrotePtr\fR is filled with
482 the corresponding number of bytes that were stored in \fIdst\fR.  The return
483 value is one of the following:
484 .RS
485 .IP \fBTCL_OK\fR 29
486 All bytes of \fIsrc\fR were converted.
487 .IP \fBTCL_CONVERT_NOSPACE\fR 29
488 The destination buffer was not large enough for all of the converted data; as
489 many characters as could fit were converted though.
490 .IP \fBTCL_CONVERT_MULTIBYTE\fR 29
491 The last few bytes in the source buffer were the beginning of a multibyte
492 sequence, but more bytes were needed to complete this sequence.  A
493 subsequent call to the conversion routine should pass a buffer containing
494 the unconverted bytes that remained in \fIsrc\fR plus some further bytes
495 from the source stream to properly convert the formerly split-up multibyte
496 sequence.  
497 .IP \fBTCL_CONVERT_SYNTAX\fR 29
498 The source buffer contained an invalid character sequence.  This may occur
499 if the input stream has been damaged or if the input encoding method was
500 misidentified.
501 .IP \fBTCL_CONVERT_UNKNOWN\fR 29
502 The source buffer contained a character that could not be represented in
503 the target encoding and \fBTCL_ENCODING_STOPONERROR\fR was specified.  
504 .RE
505 .LP
506 \fBTcl_UtfToExternalDString\fR converts a source buffer \fIsrc\fR from UTF-8 
507 into the specified \fIencoding\fR.  The converted bytes are stored in
508 \fIdstPtr\fR, which is then terminated with the appropriate encoding-specific
509 null.  The caller should eventually call \fBTcl_DStringFree\fR to free any
510 information stored in \fIdstPtr\fR.  When converting, if any of the
511 characters in the source buffer cannot be represented in the target
512 encoding, a default fallback character will be used.  The return value is
513 a pointer to the value stored in the DString.
514 .PP
515 \fBTcl_UtfToExternal\fR converts a source buffer \fIsrc\fR from UTF-8 into
516 the specified \fIencoding\fR.  Up to \fIsrcLen\fR bytes are converted from
517 the source buffer and up to \fIdstLen\fR converted bytes are stored in
518 \fIdst\fR.  In all cases, \fI*srcReadPtr\fR is filled with the number of
519 bytes that were successfully converted from \fIsrc\fR and \fI*dstWrotePtr\fR
520 is filled with the corresponding number of bytes that were stored in
521 \fIdst\fR.  The return values are the same as the return values for
522 \fBTcl_ExternalToUtf\fR.
523 .PP
524 \fBTcl_WinUtfToTChar\fR and \fBTcl_WinTCharToUtf\fR are
525 Windows-only convenience
526 functions for converting between UTF-8 and Windows strings
527 based on the TCHAR type which is by convention
528 a Unicode character on Windows NT.
529 These functions are essentially wrappers around
530 \fBTcl_UtfToExternalDString\fR and
531 \fBTcl_ExternalToUtfDString\fR that convert to and from the
532 Unicode encoding.
533 .PP
534 \fBTcl_GetEncodingName\fR is roughly the inverse of \fBTcl_GetEncoding\fR.
535 Given an \fIencoding\fR, the return value is the \fIname\fR argument that
536 was used to create the encoding.  The string returned by 
537 \fBTcl_GetEncodingName\fR is only guaranteed to persist until the
538 \fIencoding\fR is deleted.  The caller must not modify this string.
539 .PP
540 \fBTcl_SetSystemEncoding\fR sets the default encoding that should be used
541 whenever the user passes a NULL value for the \fIencoding\fR argument to
542 any of the other encoding functions.  If \fIname\fR is NULL, the system
543 encoding is reset to the default system encoding, \fBbinary\fR.  If the
544 name did not refer to any known or loadable encoding, \fBTCL_ERROR\fR is
545 returned and an error message is left in \fIinterp\fR.  Otherwise, this
546 procedure increments the reference count of the new system encoding,
547 decrements the reference count of the old system encoding, and returns
548 \fBTCL_OK\fR.
549 .PP
550 \fBTcl_GetEncodingNameFromEnvironment\fR provides a means for the Tcl
551 library to report the encoding name it believes to be the correct one
552 to use as the system encoding, based on system calls and examination of
553 the environment suitable for the platform.  It accepts \fIbufPtr\fR,
554 a pointer to an uninitialized or freed \fBTcl_DString\fR and writes
555 the encoding name to it.  The \fBTcl_DStringValue\fR is returned.
556 .PP
557 \fBTcl_GetEncodingNames\fR sets the \fIinterp\fR result to a list
558 consisting of the names of all the encodings that are currently defined
559 or can be dynamically loaded, searching the encoding path specified by
560 \fBTcl_SetDefaultEncodingDir\fR.  This procedure does not ensure that the
561 dynamically-loadable encoding files contain valid data, but merely that they
562 exist.
563 .PP
564 \fBTcl_CreateEncoding\fR defines a new encoding and registers the C
565 procedures that are called back to convert between the encoding and
566 UTF-8.  Encodings created by \fBTcl_CreateEncoding\fR are thereafter
567 visible in the database used by \fBTcl_GetEncoding\fR.  Just as with the
568 \fBTcl_GetEncoding\fR procedure, the return value is a token that
569 represents the encoding and can be used in subsequent calls to other
570 encoding functions.  \fBTcl_CreateEncoding\fR returns an encoding with a
571 reference count of 1. If an encoding with the specified \fIname\fR
572 already exists, then its entry in the database is replaced with the new
573 encoding; the token for the old encoding will remain valid and continue
574 to behave as before, but users of the new token will now call the new
575 encoding procedures.  
576 .PP
577 The \fItypePtr\fR argument to \fBTcl_CreateEncoding\fR contains information 
578 about the name of the encoding and the procedures that will be called to
579 convert between this encoding and UTF-8.  It is defined as follows:
580 .PP
581 .CS
582 typedef struct Tcl_EncodingType {
583     const char *\fIencodingName\fR;
584     Tcl_EncodingConvertProc *\fItoUtfProc\fR;
585     Tcl_EncodingConvertProc *\fIfromUtfProc\fR;
586     Tcl_EncodingFreeProc *\fIfreeProc\fR;
587     ClientData \fIclientData\fR;
588     int \fInullSize\fR;
589 } \fBTcl_EncodingType\fR;  
590 .CE
591 .PP
592 The \fIencodingName\fR provides a string name for the encoding, by
593 which it can be referred in other procedures such as
594 \fBTcl_GetEncoding\fR.  The \fItoUtfProc\fR refers to a callback
595 procedure to invoke to convert text from this encoding into UTF-8.
596 The \fIfromUtfProc\fR refers to a callback procedure to invoke to
597 convert text from UTF-8 into this encoding.  The \fIfreeProc\fR refers
598 to a callback procedure to invoke when this encoding is deleted.  The
599 \fIfreeProc\fR field may be NULL.  The \fIclientData\fR contains an
600 arbitrary one-word value passed to \fItoUtfProc\fR, \fIfromUtfProc\fR,
601 and \fIfreeProc\fR whenever they are called.  Typically, this is a
602 pointer to a data structure containing encoding-specific information
603 that can be used by the callback procedures.  For instance, two very
604 similar encodings such as \fBascii\fR and \fBmacRoman\fR may use the
605 same callback procedure, but use different values of \fIclientData\fR
606 to control its behavior.  The \fInullSize\fR specifies the number of
607 zero bytes that signify end-of-string in this encoding.  It must be
608 \fB1\fR (for single-byte or multi-byte encodings like ASCII or
609 Shift-JIS) or \fB2\fR (for double-byte encodings like Unicode).
610 Constant-sized encodings with 3 or more bytes per character (such as
611 CNS11643) are not accepted.
612 .PP
613 The callback procedures \fItoUtfProc\fR and \fIfromUtfProc\fR should match the
614 type \fBTcl_EncodingConvertProc\fR:
615 .PP
616 .CS
617 typedef int \fBTcl_EncodingConvertProc\fR(
618         ClientData \fIclientData\fR,
619         const char *\fIsrc\fR, 
620         int \fIsrcLen\fR, 
621         int \fIflags\fR, 
622         Tcl_EncodingState *\fIstatePtr\fR,
623         char *\fIdst\fR, 
624         int \fIdstLen\fR, 
625         int *\fIsrcReadPtr\fR,
626         int *\fIdstWrotePtr\fR,
627         int *\fIdstCharsPtr\fR);
628 .CE
629 .PP
630 The \fItoUtfProc\fR and \fIfromUtfProc\fR procedures are called by the
631 \fBTcl_ExternalToUtf\fR or \fBTcl_UtfToExternal\fR family of functions to
632 perform the actual conversion.  The \fIclientData\fR parameter to these
633 procedures is the same as the \fIclientData\fR field specified to
634 \fBTcl_CreateEncoding\fR when the encoding was created.  The remaining
635 arguments to the callback procedures are the same as the arguments,
636 documented at the top, to \fBTcl_ExternalToUtf\fR or
637 \fBTcl_UtfToExternal\fR, with the following exceptions.  If the
638 \fIsrcLen\fR argument to one of those high-level functions is negative,
639 the value passed to the callback procedure will be the appropriate
640 encoding-specific string length of \fIsrc\fR.  If any of the \fIsrcReadPtr\fR, 
641 \fIdstWrotePtr\fR, or \fIdstCharsPtr\fR arguments to one of the high-level
642 functions is NULL, the corresponding value passed to the callback
643 procedure will be a non-NULL location.
644 .PP
645 The callback procedure \fIfreeProc\fR, if non-NULL, should match the type 
646 \fBTcl_EncodingFreeProc\fR:
647 .PP
648 .CS
649 typedef void \fBTcl_EncodingFreeProc\fR(
650         ClientData \fIclientData\fR);
651 .CE
652 .PP
653 This \fIfreeProc\fR function is called when the encoding is deleted.  The
654 \fIclientData\fR parameter is the same as the \fIclientData\fR field
655 specified to \fBTcl_CreateEncoding\fR when the encoding was created.  
656 .PP
657 \fBTcl_GetEncodingSearchPath\fR and \fBTcl_SetEncodingSearchPath\fR
658 are called to access and set the list of filesystem directories searched
659 for encoding data files.  
660 .PP
661 The value returned by \fBTcl_GetEncodingSearchPath\fR
662 is the value stored by the last successful call to
663 \fBTcl_SetEncodingSearchPath\fR.  If no calls to
664 \fBTcl_SetEncodingSearchPath\fR have occurred, Tcl will compute an initial
665 value based on the environment.  There is one encoding search path for the
666 entire process, shared by all threads in the process.
667 .PP
668 \fBTcl_SetEncodingSearchPath\fR stores \fIsearchPath\fR and returns
669 \fBTCL_OK\fR, unless \fIsearchPath\fR is not a valid Tcl list, which
670 causes \fBTCL_ERROR\fR to be returned.  The elements of \fIsearchPath\fR
671 are not verified as existing readable filesystem directories.  When
672 searching for encoding data files takes place, and non-existent or
673 non-readable filesystem directories on the \fIsearchPath\fR are silently
674 ignored.
675 .PP
676 \fBTcl_GetDefaultEncodingDir\fR and \fBTcl_SetDefaultEncodingDir\fR
677 are obsolete interfaces best replaced with calls to
678 \fBTcl_GetEncodingSearchPath\fR and \fBTcl_SetEncodingSearchPath\fR.
679 They are called to access and set the first element of the \fIsearchPath\fR
680 list.  Since Tcl searches \fIsearchPath\fR for encoding data files in
681 list order, these routines establish the
682 .QW default
683 directory in which to find encoding data files.
684 .SH "ENCODING FILES"
685 Space would prohibit precompiling into Tcl every possible encoding
686 algorithm, so many encodings are stored on disk as dynamically-loadable
687 encoding files.  This behavior also allows the user to create additional
688 encoding files that can be loaded using the same mechanism.  These
689 encoding files contain information about the tables and/or escape
690 sequences used to map between an external encoding and Unicode.  The
691 external encoding may consist of single-byte, multi-byte, or double-byte
692 characters.  
693 .PP
694 Each dynamically-loadable encoding is represented as a text file.  The
695 initial line of the file, beginning with a
696 .QW #
697 symbol, is a comment
698 that provides a human-readable description of the file.  The next line
699 identifies the type of encoding file.  It can be one of the following
700 letters:
701 .IP "[1] \fBS\fR"
702 A single-byte encoding, where one character is always one byte long in the
703 encoding.  An example is \fBiso8859-1\fR, used by many European languages.
704 .IP "[2] \fBD\fR"
705 A double-byte encoding, where one character is always two bytes long in the
706 encoding.  An example is \fBbig5\fR, used for Chinese text.
707 .IP "[3] \fBM\fR"
708 A multi-byte encoding, where one character may be either one or two bytes long.
709 Certain bytes are lead bytes, indicating that another byte must follow
710 and that together the two bytes represent one character.  Other bytes are not
711 lead bytes and represent themselves.  An example is \fBshiftjis\fR, used by
712 many Japanese computers.
713 .IP "[4] \fBE\fR"
714 An escape-sequence encoding, specifying that certain sequences of bytes
715 do not represent characters, but commands that describe how following bytes
716 should be interpreted.  
717 .PP
718 The rest of the lines in the file depend on the type.  
719 .PP
720 Cases [1], [2], and [3] are collectively referred to as table-based encoding
721 files.  The lines in a table-based encoding file are in the same
722 format as this example taken from the \fBshiftjis\fR encoding (this is not
723 the complete file):
724 .PP
725 .CS
726 # Encoding file: shiftjis, multi-byte
727 M
728 003F 0 40
729 00
730 0000000100020003000400050006000700080009000A000B000C000D000E000F
731 0010001100120013001400150016001700180019001A001B001C001D001E001F
732 0020002100220023002400250026002700280029002A002B002C002D002E002F
733 0030003100320033003400350036003700380039003A003B003C003D003E003F
734 0040004100420043004400450046004700480049004A004B004C004D004E004F
735 0050005100520053005400550056005700580059005A005B005C005D005E005F
736 0060006100620063006400650066006700680069006A006B006C006D006E006F
737 0070007100720073007400750076007700780079007A007B007C007D203E007F
738 0080000000000000000000000000000000000000000000000000000000000000
739 0000000000000000000000000000000000000000000000000000000000000000
740 0000FF61FF62FF63FF64FF65FF66FF67FF68FF69FF6AFF6BFF6CFF6DFF6EFF6F
741 FF70FF71FF72FF73FF74FF75FF76FF77FF78FF79FF7AFF7BFF7CFF7DFF7EFF7F
742 FF80FF81FF82FF83FF84FF85FF86FF87FF88FF89FF8AFF8BFF8CFF8DFF8EFF8F
743 FF90FF91FF92FF93FF94FF95FF96FF97FF98FF99FF9AFF9BFF9CFF9DFF9EFF9F
744 0000000000000000000000000000000000000000000000000000000000000000
745 0000000000000000000000000000000000000000000000000000000000000000
746 81
747 0000000000000000000000000000000000000000000000000000000000000000
748 0000000000000000000000000000000000000000000000000000000000000000
749 0000000000000000000000000000000000000000000000000000000000000000
750 0000000000000000000000000000000000000000000000000000000000000000
751 300030013002FF0CFF0E30FBFF1AFF1BFF1FFF01309B309C00B4FF4000A8FF3E
752 FFE3FF3F30FD30FE309D309E30034EDD30053006300730FC20152010FF0F005C
753 301C2016FF5C2026202520182019201C201DFF08FF0930143015FF3BFF3DFF5B
754 FF5D30083009300A300B300C300D300E300F30103011FF0B221200B100D70000
755 00F7FF1D2260FF1CFF1E22662267221E22342642264000B0203220332103FFE5
756 FF0400A200A3FF05FF03FF06FF0AFF2000A72606260525CB25CF25CE25C725C6
757 25A125A025B325B225BD25BC203B301221922190219121933013000000000000
758 000000000000000000000000000000002208220B2286228722822283222A2229
759 000000000000000000000000000000002227222800AC21D221D4220022030000
760 0000000000000000000000000000000000000000222022A52312220222072261
761 2252226A226B221A223D221D2235222B222C0000000000000000000000000000
762 212B2030266F266D266A2020202100B6000000000000000025EF000000000000
763 .CE
764 .PP
765 The third line of the file is three numbers.  The first number is the
766 fallback character (in base 16) to use when converting from UTF-8 to this
767 encoding.  The second number is a \fB1\fR if this file represents the
768 encoding for a symbol font, or \fB0\fR otherwise.  The last number (in base
769 10) is how many pages of data follow.  
770 .PP
771 Subsequent lines in the example above are pages that describe how to map
772 from the encoding into 2-byte Unicode.  The first line in a page identifies
773 the page number.  Following it are 256 double-byte numbers, arranged as 16
774 rows of 16 numbers.  Given a character in the encoding, the high byte of
775 that character is used to select which page, and the low byte of that
776 character is used as an index to select one of the double-byte numbers in
777 that page \- the value obtained being the corresponding Unicode character.
778 By examination of the example above, one can see that the characters 0x7E
779 and 0x8163 in \fBshiftjis\fR map to 203E and 2026 in Unicode, respectively.
780 .PP
781 Following the first page will be all the other pages, each in the same
782 format as the first: one number identifying the page followed by 256
783 double-byte Unicode characters.  If a character in the encoding maps to the
784 Unicode character 0000, it means that the character does not actually exist.
785 If all characters on a page would map to 0000, that page can be omitted.
786 .PP
787 Case [4] is the escape-sequence encoding file.  The lines in an this type of
788 file are in the same format as this example taken from the \fBiso2022-jp\fR
789 encoding:
790 .PP
791 .CS
792 .ta 1.5i
793 # Encoding file: iso2022-jp, escape-driven
794 E
795 init            {}
796 final           {}
797 iso8859-1       \ex1b(B
798 jis0201         \ex1b(J
799 jis0208         \ex1b$@
800 jis0208         \ex1b$B
801 jis0212         \ex1b$(D
802 gb2312          \ex1b$A
803 ksc5601         \ex1b$(C
804 .CE
805 .PP
806 In the file, the first column represents an option and the second column
807 is the associated value.  \fBinit\fR is a string to emit or expect before
808 the first character is converted, while \fBfinal\fR is a string to emit
809 or expect after the last character.  All other options are names of
810 table-based encodings; the associated value is the escape-sequence that
811 marks that encoding.  Tcl syntax is used for the values; in the above
812 example, for instance,
813 .QW \fB{}\fR
814 represents the empty string and
815 .QW \fB\ex1b\fR
816 represents character 27.
817 .PP
818 When \fBTcl_GetEncoding\fR encounters an encoding \fIname\fR that has not
819 been loaded, it attempts to load an encoding file called \fIname\fB.enc\fR
820 from the \fBencoding\fR subdirectory of each directory that Tcl searches
821 for its script library.  If the encoding file exists, but is
822 malformed, an error message will be left in \fIinterp\fR.
823 .SH KEYWORDS
824 utf, encoding, convert