Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6285 in orxonox.OLD for branches/network/src


Ignore:
Timestamp:
Dec 25, 2005, 4:24:50 PM (19 years ago)
Author:
rennerc
Message:

converter: added function which allocates momory

Location:
branches/network/src/lib/network
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/network/src/lib/network/converter.cc

    r6275 r6285  
    453453  return n+length;
    454454}
     455
     456/**
     457 * reads a string out of a byte array and allocates memory for string
     458 * @param a: byte array
     459 * @param s: string
     460 * @param maxLength: max bytes to copy
     461 * @return: the number of read bytes in byte array
     462 */
     463int Converter::byteArrayToStringM( const byte * a, char * & s )
     464{
     465  int length;
     466
     467  int n = Converter::byteArrayToInt( a, &length );
     468
     469  s = new char[n+1];
     470
     471  if ( !s )
     472  {
     473    PRINTF(1)("Could not allocate memory!\n" );
     474    return -1;
     475  }
     476
     477  memcpy( s, a+n, length );
     478  s[length] = '\0';
     479
     480  return n+length;
     481}
     482
  • branches/network/src/lib/network/converter.h

    r6273 r6285  
    3535    static int stringToByteArray(const char* s, byte* a, int length, int maxLength);
    3636    static int byteArrayToString(const byte* a, char* s, int maxLength);
     37    static int byteArrayToStringM(const byte* a, char*& s );
    3738
    3839    //Test
  • branches/network/src/lib/network/synchronizeable.h

    r6277 r6285  
    2929 * with the same argument names!
    3030 *
     31 * SYNCHELP_WRITE_BEGIN()
     32 * SYNCHELP_WRITE_INT(i)
     33 * SYNCHELP_WRITE_FLOAT(f)
     34 * SYNCHELP_WRITE_BYTE(b)
     35 * SYNCHELP_WRITE_STRING(s)
     36 * SYNCHELP_WRITE_N
     37 *
     38 * SYNCHELP_READ_BEGIN()
     39 * SYNCHELP_READ_INT(i)
     40 * SYNCHELP_READ_FLOAT(f)
     41 * SYNCHELP_READ_STRING(s,l) l = size of buffer s
     42 * SYNCHELP_READ_STRINGM(s)  allocates memory for string! you have to free this after
     43 * SYNCHELP_READ_BYTE(b)
     44 *
     45 *
    3146 *
    3247 * Example 1:
     
    4459#define SYNCHELP_WRITE_BEGIN()    int __synchelp_write_i = 0; \
    4560                                  int __synchelp_write_n
    46 #define SYNCHELP_WRITE_RESET() { __synchelp_write_i = 0; __synchelp_write_err = false; }
    4761#define SYNCHELP_WRITE_INT(i) { __synchelp_write_n = \
    4862                                Converter::intToByteArray( i, data+__synchelp_write_i, maxLength-__synchelp_write_i ); \
     
    112126                                    __synchelp_read_i += __synchelp_read_n; \
    113127}
     128#define SYNCHELP_READ_STRINGM(s)    { \
     129                                    __synchelp_read_n = Converter::byteArrayToStringM( data+__synchelp_read_i, s );  \
     130                                    if ( __synchelp_read_n <0 )  \
     131{ \
     132                                      PRINTF(1)("There is not enough data to read string\n");  \
     133                                      return; \
     134} \
     135                                    __synchelp_read_i += __synchelp_read_n; \
     136}
    114137#define SYNCHELP_READ_BYTE(b)      { \
    115138                                    if ( length-__synchelp_read_i < 1 ) \
Note: See TracChangeset for help on using the changeset viewer.