Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7230 in orxonox.OLD for trunk/src/lib/network/converter.cc


Ignore:
Timestamp:
Mar 21, 2006, 3:20:36 PM (18 years ago)
Author:
bensch
Message:

orxonox/trunk: merged the std::branche back

File:
1 edited

Legend:

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

    r6959 r7230  
    159159  if ( length< INTSIZE )
    160160  {
    161     PRINTF(1)("Byte Array to small\n");
     161    PRINTF(1)("Byte Array to small : %d\n", length);
    162162    return 0;
    163163  }
     
    457457 * @return: the used number of bytes in byte array
    458458 */
    459 int Converter::stringToByteArray( const char * s, byte * a, int length, int maxLength )
    460 {
    461   if ( length+INTSIZE > maxLength )
    462   {
    463     PRINTF(1)("Byte array is too small (%d) to store %d bytes\n", maxLength, length+INTSIZE);
     459int Converter::stringToByteArray( const std::string & s, byte * a, int maxLength )
     460{
     461  if ( s.length()+INTSIZE > maxLength )
     462  {
     463    PRINTF(1)("Byte array is too small (%d) to store %d bytes\n", maxLength, s.length()+INTSIZE);
    464464    return -1;
    465465  }
    466466
    467   int n = Converter::intToByteArray( length, a, maxLength );
    468 
    469   memcpy( a+INTSIZE, s, length );
    470 
    471   return length + INTSIZE;
     467  int n = Converter::intToByteArray( s.length(), a, maxLength );
     468
     469  memcpy( a+INTSIZE, s.c_str(), s.length() );
     470
     471  return s.length() + INTSIZE;
    472472}
    473473
     
    479479 * @return: the number of read bytes in byte array
    480480 */
    481 int Converter::byteArrayToString( const byte * a, char * s, int maxLength )
     481int Converter::byteArrayToString( const byte * a, std::string&s, int maxLength )
    482482{
    483483  int length;
     
    488488  if ( length+1 > maxLength )
    489489  {
    490     PRINTF(1)("There is not enough space in string (%d) to store %d bytes\n", maxLength, length+1 );
    491     strncpy(s,"",maxLength);
     490    PRINTF(1)("something went wrong length > remaining bytes in buffer\n" );
     491    s = "";
    492492    return -1;
    493493  }
    494494
    495   memcpy( s, a+n, length );
    496   s[length] = '\0';
     495  s[0] = '\0';
     496  s.append( (char*)a+n, length );
    497497
    498498  return n+length;
    499499}
    500500
     501#if 0
    501502/**
    502503 * reads a string out of a byte array and allocates memory for string
     
    525526  return n+length;
    526527}
    527 
     528#endif
    528529
    529530
Note: See TracChangeset for help on using the changeset viewer.