-/* Determine coding system from coding format */
-
-/* #### not correct for all values of `fmt'! */
-static Lisp_Object
-external_data_format_to_coding_system (enum external_data_format fmt)
-{
- switch (fmt)
- {
- case FORMAT_FILENAME:
- case FORMAT_TERMINAL:
- if (EQ (Vfile_name_coding_system, Qnil) ||
- EQ (Vfile_name_coding_system, Qbinary))
- return Qnil;
- else
- return Fget_coding_system (Vfile_name_coding_system);
-#ifdef MULE
- case FORMAT_CTEXT:
- return Fget_coding_system (Qctext);
-#endif
- default:
- return Qnil;
- }
-}
-
-Extbyte *
-convert_to_external_format (CONST Bufbyte *ptr,
- Bytecount len,
- Extcount *len_out,
- enum external_data_format fmt)
-{
- Lisp_Object coding_system = external_data_format_to_coding_system (fmt);
-
- if (!conversion_out_dynarr)
- conversion_out_dynarr = Dynarr_new (Extbyte);
- else
- Dynarr_reset (conversion_out_dynarr);
-
- if (NILP (coding_system))
- {
- CONST Bufbyte *end = ptr + len;
-
- for (; ptr < end;)
- {
- Bufbyte c =
- (BYTE_ASCII_P (*ptr)) ? *ptr :
- (*ptr == LEADING_BYTE_CONTROL_1) ? (*(ptr+1) - 0x20) :
- (*ptr == LEADING_BYTE_LATIN_ISO8859_1) ? (*(ptr+1)) :
- '~';
-
- Dynarr_add (conversion_out_dynarr, (Extbyte) c);
- INC_CHARPTR (ptr);
- }
-
-#ifdef ERROR_CHECK_BUFPOS
- assert (ptr == end);
-#endif
- }
- else
- {
- Lisp_Object instream, outstream, da_outstream;
- Lstream *istr, *ostr;
- struct gcpro gcpro1, gcpro2, gcpro3;
- char tempbuf[1024]; /* some random amount */
-
- instream = make_fixed_buffer_input_stream ((unsigned char *) ptr, len);
- da_outstream = make_dynarr_output_stream
- ((unsigned_char_dynarr *) conversion_out_dynarr);
- outstream =
- make_encoding_output_stream (XLSTREAM (da_outstream), coding_system);
- istr = XLSTREAM (instream);
- ostr = XLSTREAM (outstream);
- GCPRO3 (instream, outstream, da_outstream);
- while (1)
- {
- ssize_t size_in_bytes = Lstream_read (istr, tempbuf, sizeof (tempbuf));
- if (!size_in_bytes)
- break;
- Lstream_write (ostr, tempbuf, size_in_bytes);
- }
- Lstream_close (istr);
- Lstream_close (ostr);
- UNGCPRO;
- Lstream_delete (istr);
- Lstream_delete (ostr);
- Lstream_delete (XLSTREAM (da_outstream));
- }
-
- *len_out = Dynarr_length (conversion_out_dynarr);
- Dynarr_add (conversion_out_dynarr, 0); /* remember to zero-terminate! */
- return Dynarr_atp (conversion_out_dynarr, 0);
-}
-
-Bufbyte *
-convert_from_external_format (CONST Extbyte *ptr,
- Extcount len,
- Bytecount *len_out,
- enum external_data_format fmt)
-{
- Lisp_Object coding_system = external_data_format_to_coding_system (fmt);
-
- if (!conversion_in_dynarr)
- conversion_in_dynarr = Dynarr_new (Bufbyte);
- else
- Dynarr_reset (conversion_in_dynarr);
-
- if (NILP (coding_system))
- {
- CONST Extbyte *end = ptr + len;
- for (; ptr < end; ptr++)
- {
- Extbyte c = *ptr;
- DECODE_ADD_BINARY_CHAR (c, conversion_in_dynarr);
- }
- }
- else
- {
- Lisp_Object instream, outstream, da_outstream;
- Lstream *istr, *ostr;
- struct gcpro gcpro1, gcpro2, gcpro3;
- char tempbuf[1024]; /* some random amount */
-
- instream = make_fixed_buffer_input_stream ((unsigned char *) ptr, len);
- da_outstream = make_dynarr_output_stream
- ((unsigned_char_dynarr *) conversion_in_dynarr);
- outstream =
- make_decoding_output_stream (XLSTREAM (da_outstream), coding_system);
- istr = XLSTREAM (instream);
- ostr = XLSTREAM (outstream);
- GCPRO3 (instream, outstream, da_outstream);
- while (1)
- {
- ssize_t size_in_bytes = Lstream_read (istr, tempbuf, sizeof (tempbuf));
- if (!size_in_bytes)
- break;
- Lstream_write (ostr, tempbuf, size_in_bytes);
- }
- Lstream_close (istr);
- Lstream_close (ostr);
- UNGCPRO;
- Lstream_delete (istr);
- Lstream_delete (ostr);
- Lstream_delete (XLSTREAM (da_outstream));
- }
-
- *len_out = Dynarr_length (conversion_in_dynarr);
- Dynarr_add (conversion_in_dynarr, 0); /* remember to zero-terminate! */
- return Dynarr_atp (conversion_in_dynarr, 0);
-}
-
-\f