glibtojava.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006  SRI International
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2.1 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public
00015  * License along with this library; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00017  *
00018  * SRI International: 333 Ravenswood Ave, Menlo Park, CA 94025
00019  */
00020 
00021 #include <string.h>
00022 #include "glibtojava.h"
00023 
00024 GByteArray* glibtojava_writeJavaChar(GByteArray* buf, gchar theChar)
00025 {
00026   // each char is two bytes; the first byte is 0x00, the second is the char
00027   char toWrite[2];
00028   toWrite[0] = '\0';
00029   toWrite[1] = theChar;
00030   buf = g_byte_array_append(buf, (guint8*)toWrite, 2);
00031   return buf;
00032 }
00033 
00034 GByteArray* glibtojava_writeJavaChars(GByteArray* buf, size_t bufSize, gchar* theCharString)
00035 {
00036   size_t i;
00037 
00038   for(i = 0; i < bufSize; ++i) {
00039     buf = glibtojava_writeJavaChar(buf, theCharString[i]);
00040   }
00041   return buf;
00042 }
00043 
00044 GByteArray* glibtojava_writeJavaBytes(GByteArray* buf, size_t bufSize, guint8* bytes)
00045 {
00046   buf = g_byte_array_append(buf, bytes, bufSize);
00047   return buf;
00048 }
00049 
00050 GByteArray* glibtojava_writeJavaInt(GByteArray* buf, gint32 theInt)
00051 {
00052   guint8 toWrite[4];
00053 
00054   toWrite[0] = (0xff & (theInt >> 24));
00055   toWrite[1] = (0xff & (theInt >> 16));
00056   toWrite[2] = (0xff & (theInt >> 8));
00057   toWrite[3] = (0xff & theInt);
00058   buf = g_byte_array_append(buf, toWrite, 4);
00059   return buf;
00060 }
00061 
00062 GByteArray* glibtojava_writeJavaLong(GByteArray* buf, gint64 theLong)
00063 {
00064   guint8 toWrite[8];
00065 
00066   toWrite[0] = (0xff & (theLong >> 56));
00067   toWrite[1] = (0xff & (theLong >> 48));
00068   toWrite[2] = (0xff & (theLong >> 40));
00069   toWrite[3] = (0xff & (theLong >> 32));
00070   toWrite[4] = (0xff & (theLong >> 24));
00071   toWrite[5] = (0xff & (theLong >> 16));
00072   toWrite[6] = (0xff & (theLong >> 8));
00073   toWrite[7] = (0xff & theLong);
00074 
00075   buf = g_byte_array_append(buf, toWrite, 8);
00076   return buf;
00077 }
00078 
00079 GByteArray* glibtojava_writeJavaDouble(GByteArray* buf, gdouble theDouble)
00080 {
00081   guint64 bits;
00082   guint8 toWrite[8];
00083 
00084   memcpy(&bits, &theDouble, sizeof(gdouble));
00085   bits = GUINT64_TO_BE(bits);
00086   memcpy(toWrite, &bits, sizeof(guint64));
00087   buf = g_byte_array_append(buf, toWrite, 8);
00088   return buf;
00089 }
00090 

Generated on Wed May 23 17:20:10 2007 using doxygen 1.5.2