File: transcode_ext.cpp

package info (click to toggle)
falconpl 0.9.6.9-git20120606-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 46,176 kB
  • sloc: cpp: 181,389; ansic: 109,025; yacc: 2,310; xml: 1,218; sh: 403; objc: 245; makefile: 82; sql: 20
file content (165 lines) | stat: -rw-r--r-- 5,053 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
   FALCON - The Falcon Programming Language
   FILE: transcode_ext.cpp

   Transcoder api for rtl.
   -------------------------------------------------------------------
   Author: Giancarlo Niccolai
   Begin: lun ott 2 2006

   -------------------------------------------------------------------
   (C) Copyright 2004: the FALCON developers (see list in AUTHORS file)

   See LICENSE file for licensing details.
*/

/*#
   @beginmodule core
*/

/** \file
   Transcoder api for rtl.
*/

#include <falcon/vm.h>
#include <falcon/stream.h>
#include <falcon/transcoding.h>
#include <falcon/coreobject.h>
#include <falcon/stdstreams.h>
#include <falcon/membuf.h>
#include "core_module.h"

namespace Falcon {
namespace core {

/*#
   @funset core_transcoding_functions Transcoding functions
   @brief Functions needed to transcode texts into various character sets.

   Transcoding functions turns binary strings encoded in a format into
   Falcon strings, or conversely, they turn Falcon strings into binary
   encoded buffers. Used in combination with binary stream reads and write,
   this function allows full internationalization of script input and output.

   However, if the target stream is known to support safe reads and writes and
   to provide immediate access to the needed data, the @a Stream.setEncoding method
   is more efficient, as it doesn't need a temporary buffer to store the binary read
   data, or the binary data that has to be written.

   @beginset core_transcoding_functions

*/

/*#
   @function getSystemEncoding
   @brief Returns the "official" system encoding, if it matches with one known by Falcon.
   @return The system encoding name.

   This function will return the name under which Falcon knows the default
   system encoding. Using returned value, the program is able to create encoders
   that should be able to parse the data provided by system functions as directory
   scanning, or that is probably used as the main encoding for system related text
   files (i.e. configuration files).
*/

FALCON_FUNC  getSystemEncoding ( ::Falcon::VMachine *vm )
{
   CoreString *res = new CoreString;
   GetSystemEncoding( *res );
   vm->retval( res );
}

/*#
   @function transcodeTo
   @brief Returns a binary buffer containing an encoded representation of a Falcon string.
   @param string Falcon string to be encoded.
   @param encoding Name of the encoding (as a string).
   @return On success, the transcoded string.
   @raise ParamError if the encoding is not known.

   In case the encoding name is not known, the function will raise a ParamError.
*/
FALCON_FUNC  transcodeTo ( ::Falcon::VMachine *vm )
{
   Item *i_source = vm->param( 0 );
   Item *i_encoding = vm->param( 1 );

   if ( i_source == 0 || ( ! i_source->isString() && ! i_source->isMemBuf() ) ||
        i_encoding == 0 || ! i_encoding->isString() )
   {
      throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).origin( e_orig_runtime ).extra("S|M,[M]") );
   }

   CoreString *res = new CoreString;
   String *source;
   String dummy;

   if ( i_source->isMemBuf() )
   {
      source = &dummy;
      // using 0 as allocated, the buffer is considered static.
      dummy.adopt( (char *) i_source->asMemBuf()->data(), i_source->asMemBuf()->size(), 0 );
   }
   else
   {
      source = i_source->asString();
   }

   if ( ! TranscodeString( *source, *(i_encoding->asString()), *res ) )
   {
      throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).origin( e_orig_runtime ) );
   }

   vm->retval( res );
}

/*#
   @function transcodeFrom
   @brief Returns a Falcon string created by parsing the given one as a binary sequence of bytes.
   @param string Falcon string or MemBuf to be encoded.
   @param encoding Name of the encoding (as a string).
   @return On success, the transcoded string.
   @raise ParamError if the encoding is not known.

   In case the encoding name is not known, the function will raise a ParamError.
   The transcoding may also fail if the source data is not a valid sequence under the
   given encoding, and cannot be decoded.
*/
FALCON_FUNC  transcodeFrom ( ::Falcon::VMachine *vm )
{
   Item *i_source = vm->param( 0 );
   Item *i_encoding = vm->param( 1 );

   if ( i_source == 0 || ( ! i_source->isString() && !i_source->isMemBuf() ) ||
        i_encoding == 0 || ! i_encoding->isString() )
   {
      throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).origin( e_orig_runtime ).extra( "S|M,S" ) );
   }

   CoreString *res = new CoreString;
   String *source;
   String dummy;

   if ( i_source->isMemBuf() )
   {
      source = &dummy;
      // using 0 as allocated, the buffer is considered static.
      dummy.adopt( (char *) i_source->asMemBuf()->data(), i_source->asMemBuf()->size(), 0 );
   }
   else
   {
      source = i_source->asString();
   }

   if ( ! TranscodeFromString( *source, *(i_encoding->asString()), *res ) )
   {
      throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).origin( e_orig_runtime ) );
   }

   vm->retval( res );
}

}
}

/* end of transcode_ext.cpp */