This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Patch for Review: JvGetStringUTF and JvTempCString


Hi Tom,

This is a followup to this:

http://gcc.gnu.org/ml/java-patches/2003-q3/msg00202.html

>I like JStringToCString, but let's (1) put it somewhere less
>win32-specific, and (2) rename it.  Also it doesn't need to be in its
>own namespace.  Just adding this class, without using it, would be a
>good candidate for a separate patch.  BTW you don't need the
>heapAllocated_ field; you can just check `if (buf_ != stackbuf_)'.

Would it be sacrilegious to put this in cni.h? If not, here
is a patch. If you're okay with this, I'll assume you'll want
a patch for the CNI documentation also.

Tested on (i686-pc-linux-gnu,i686-pc-linux-gnu), (i686-pc-linux-gnu,i686-pc-mingw32)
and (i686-pc-mingw32,i686-pc-mingw32). The testcase is attached.

-- Mohan
http://www.thisiscool.com/
http://www.animalsong.org/

ChangeLog
2003-08-05  Mohan Embar  <gnustuff@thisiscool.com>

	* gcj/cni.h: Added function JvGetStringUTF (puts and null
	terminates a jstring into a character buffer) and class
	JvTempCString (helper class for getting a temporary C string
	from a jstring)

Index: gcj/cni.h
===================================================================
RCS file: /cvsroot/gcc/gcc/libjava/gcj/cni.h,v
retrieving revision 1.10
diff -u -2 -r1.10 cni.h
--- gcj/cni.h	8 Jul 2003 21:27:37 -0000	1.10
+++ gcj/cni.h	5 Aug 2003 16:51:32 -0000
@@ -90,8 +90,56 @@
 } 
 
+extern inline jsize
+JvGetStringUTF (jstring str, char *buf)
+{
+  jsize len = JvGetStringUTFRegion (str, 0, str->length(), buf);
+  buf[len] = '\0';
+  return len;
+}
+
 extern inline jstring
 JvNewStringUTF (const char *bytes)
 {
   return _Jv_NewStringUTF (bytes);
+}
+
+/* Helper class which converts a jstring to a temporary char*. */
+class JvTempCString
+{
+public:
+  JvTempCString(jstring jstr);
+  ~JvTempCString();
+
+// Accessors
+  operator const char*() const  {return buf_;}
+  const char* buf() const       {return buf_;}
+  char* buf()                   {return buf_;}
+
+private:
+  char* buf_;
+  char stackbuf_[500];
+};
+
+inline JvTempCString::JvTempCString (jstring jstr):
+  buf_(0)
+{
+  if (!jstr) return;
+
+  jsize len = JvGetStringUTFLength(jstr);
+  if (len < jsize(sizeof(stackbuf_)-1))
+    {
+      buf_ = stackbuf_;
+    }
+  else
+    {
+      buf_ = (char*) _Jv_Malloc (len+1);
+    }
+  JvGetStringUTF (jstr, buf_);
+}
+
+inline JvTempCString::~JvTempCString ()
+{
+  if (buf_ != stackbuf_)
+    _Jv_Free (buf_);
 }
 

Attachment: CNITest.tar.bz2
Description: application/bzip2


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]