OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [native/] [jni/] [java-nio/] [gnu_java_nio_charset_iconv_IconvEncoder.c] - Blame information for rev 774

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 774 jeremybenn
/* gnu_java_nio_charset_iconv_IconvEncoder.c --
2
   Copyright (C) 2005 Free Software Foundation, Inc.
3
 
4
This file is part of GNU Classpath.
5
 
6
GNU Classpath is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2, or (at your option)
9
any later version.
10
 
11
GNU Classpath is distributed in the hope that it will be useful, but
12
WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with GNU Classpath; see the file COPYING.  If not, write to the
18
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
02110-1301 USA.
20
 
21
Linking this library statically or dynamically with other modules is
22
making a combined work based on this library.  Thus, the terms and
23
conditions of the GNU General Public License cover the whole
24
combination.
25
 
26
As a special exception, the copyright holders of this library give you
27
permission to link this library with independent modules to produce an
28
executable, regardless of the license terms of these independent
29
modules, and to copy and distribute the resulting executable under
30
terms of your choice, provided that you also meet, for each linked
31
independent module, the terms and conditions of the license of that
32
module.  An independent module is a module which is not derived from
33
or based on this library.  If you modify this library, you may extend
34
this exception to your version of the library, but you are not
35
obligated to do so.  If you do not wish to do so, delete this
36
exception statement from your version. */
37
 
38
#include <config.h>
39
#include <jcl.h>
40
 
41
#include <stdio.h>
42
#include <assert.h>
43
#include <errno.h>
44
 
45
#if defined(HAVE_ICONV)
46
#include <iconv.h>
47
#endif
48
 
49
#include "gnu_java_nio_charset_iconv_IconvEncoder.h"
50
 
51
 
52
#if defined(HAVE_ICONV)
53
static void createRawData (JNIEnv * env, jobject obj, void *ptr);
54
static void *getData (JNIEnv * env, jobject obj);
55
 
56
static jfieldID infid = NULL;
57
static jfieldID outfid = NULL;
58
#endif
59
 
60
/* Union used for type punning. */
61
union char_union
62
{
63
  jbyte **jb;
64
  jchar **jc;
65
  char **c;
66
};
67
 
68
JNIEXPORT void JNICALL
69
Java_gnu_java_nio_charset_iconv_IconvEncoder_openIconv (JNIEnv * env UNUSED,
70
                                                        jobject obj UNUSED,
71
                                                        jstring jname UNUSED)
72
{
73
#if defined(HAVE_ICONV)
74
  iconv_t iconv_object;
75
  jclass cls;
76
 
77
  const char *name = JCL_jstring_to_cstring (env, jname);
78
  if (name == NULL)
79
    return;
80
 
81
  /* Cache fieldIDs for use in encode function. */
82
  if (infid == NULL || outfid == NULL)
83
    {
84
      cls = (*env)->GetObjectClass (env, obj);
85
      infid = (*env)->GetFieldID (env, cls, "inremaining", "I");
86
      assert (infid != 0);
87
      outfid = (*env)->GetFieldID (env, cls, "outremaining", "I");
88
      assert (outfid != 0);
89
    }
90
 
91
  /* to "name" from java, native java format depends on endianness */
92
#ifdef WORDS_BIGENDIAN
93
  iconv_object = iconv_open (name, "UTF-16BE");
94
#else
95
  iconv_object = iconv_open (name, "UTF-16LE");
96
#endif
97
 
98
  JCL_free_cstring (env, jname, name);
99
  if ((long) iconv_object == -1L)
100
    {
101
      JCL_ThrowException (env, "java/lang/IllegalArgumentException",
102
                          "Charset not available");
103
      return;
104
    }
105
  createRawData (env, obj, (void *) iconv_object);
106
#else
107
  JCL_ThrowException (env, "java/lang/IllegalArgumentException",
108
                      "iconv not available");
109
#endif
110
}
111
 
112
JNIEXPORT jint JNICALL
113
Java_gnu_java_nio_charset_iconv_IconvEncoder_encode (JNIEnv * env UNUSED,
114
                                                     jobject obj UNUSED,
115
                                                     jcharArray inArr UNUSED,
116
                                                     jbyteArray outArr UNUSED,
117
                                                     jint posIn UNUSED,
118
                                                     jint remIn UNUSED,
119
                                                     jint posOut UNUSED,
120
                                                     jint remOut UNUSED)
121
{
122
#if defined(HAVE_ICONV)
123
  iconv_t iconv_object = getData (env, obj);
124
  size_t retval;
125
  union char_union in, out;
126
  jchar *input, *inputcopy;
127
  jbyte *output, *outputcopy;
128
  size_t lenIn = (size_t) remIn * 2;
129
  size_t lenOut = (size_t) remOut;
130
 
131
  inputcopy = input = (*env)->GetCharArrayElements (env, inArr, 0);
132
  outputcopy = output = (*env)->GetByteArrayElements (env, outArr, 0);
133
 
134
  input += posIn;
135
  output += posOut;
136
 
137
  in.jc = &input;
138
  out.jb = &output;
139
  retval = iconv (iconv_object, (ICONV_CONST char **) in.c, &lenIn,
140
                  out.c, &lenOut);
141
 
142
  /* XXX: Do we need to relase the input array? It's not modified. */
143
  (*env)->ReleaseCharArrayElements (env, inArr, inputcopy, 0);
144
  (*env)->ReleaseByteArrayElements (env, outArr, outputcopy, 0);
145
 
146
  if (retval == (size_t) (-1))
147
    {
148
      if (errno == EILSEQ || errno == EINVAL)
149
        retval = 1;
150
      else
151
        retval = 0;
152
    }
153
  else
154
    retval = 0;
155
 
156
  (*env)->SetIntField (env, obj, infid, (jint) (lenIn >> 1));
157
  (*env)->SetIntField (env, obj, outfid, (jint) lenOut);
158
 
159
  return (jint) retval;
160
#else
161
  return -1;
162
#endif
163
}
164
 
165
JNIEXPORT void JNICALL
166
Java_gnu_java_nio_charset_iconv_IconvEncoder_closeIconv (JNIEnv * env UNUSED,
167
                                                         jobject obj UNUSED)
168
{
169
#if defined(HAVE_ICONV)
170
  iconv_t iconv_object;
171
  iconv_object = getData (env, obj);
172
  iconv_close (iconv_object);
173
#endif
174
}
175
 
176
 
177
#if defined(HAVE_ICONV)
178
static void
179
createRawData (JNIEnv * env, jobject obj, void *ptr)
180
{
181
  jclass cls;
182
  jobject data;
183
  jfieldID data_fid;
184
 
185
  cls = (*env)->GetObjectClass (env, obj);
186
  data_fid = (*env)->GetFieldID (env, cls, "data", "Lgnu/classpath/Pointer;");
187
  assert (data_fid != 0);
188
 
189
  data = JCL_NewRawDataObject (env, ptr);
190
 
191
  (*env)->SetObjectField (env, obj, data_fid, data);
192
}
193
 
194
static void *
195
getData (JNIEnv * env, jobject obj)
196
{
197
  jclass cls;
198
  jfieldID data_fid;
199
  jobject data;
200
 
201
  cls = (*env)->GetObjectClass (env, obj);
202
  data_fid = (*env)->GetFieldID (env, cls, "data", "Lgnu/classpath/Pointer;");
203
  assert (data_fid != 0);
204
  data = (*env)->GetObjectField (env, obj, data_fid);
205
 
206
  return JCL_GetRawData(env, data);
207
}
208
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.