cni converting String args[]
Andi Vajda
andi@osafoundation.org
Fri Dec 10 17:33:00 GMT 2004
I don't see in your code where you allocate the memory for the strings to be
contained in char *arguments[argc] and where you add the terminating '\0' to
these buffers. JvGetStringURFRegion expects a pre-allocated buffer to receive
the characters from the java string. Also, the length of that pre-allocated
buffer should be computed with JvGetStringUTFLength() + 1 as the number of
unicode chars in your java string, as returned by the length() method, is not
usually the number of utf-8 bytes needed to represent it (unless you stick to
strict ascii).
Andi..
On Fri, 10 Dec 2004, Little-John wrote:
> * littlejohn@paranoici.org (littlejohn@paranoici.org) ha scritto:
>> * Andrew Haley (aph@redhat.com) ha scritto:
>>> > arguments[i] = (char *) elements(argsElements[i]->getBytes());
>>>
>>> If you want chars, you'll need an encoding -- elements returns a
>>> jchar*. Try JvGetStringUTFRegion.
>>
>> ok, I'll let you know
>
> jint A::init(JArray< ::java::lang::String *> *args) {
>
> int argc = 0;
> char* arguments[argc];
>
> if (args != NULL) // It's a very bad day otherwise
> {
> argc = args->length;
> arguments[argc];
>
> /*
> * convert the JArray<String*>* in char**
> * so in a way suitable for VLC_Init
> */
> ::java::lang::String** argsElements = elements(args);
>
> for (int i = 0; i < argc; i++) {
> ::java::lang::System::out->println(argsElements[i]->length());
> JvGetStringUTFRegion(argsElements[i], 0,
> argsElements[i]->length(), arguments[i]);
> printf("library: %s\n", arguments[i]);
> }
> }
> return 0;
> }
>
> does not work. If i issue the command:
>
> % A --help --helpp
>
> i get
> 6 (the length of --help)
> library: --help (the printf)
> 7 (the length of --helpp)
> Exception in thread "main" java.lang.NullPointerException
>
> debugging with gdb, I see a strange thing. Maybe related to the
> gc. On the second iteration, just after the JvGetStringUTFRegion the
> arguments array is:
>
> (gdb) print arguments
> $1 = {0x0, 0x0}
>
> I can understand why.
>
> lj
>
More information about the Java
mailing list