[7444] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | ### File Specific: |
---|
| 12 | main-programmer: Christoph Renner |
---|
| 13 | co-programmer: ... |
---|
| 14 | */ |
---|
| 15 | |
---|
| 16 | |
---|
| 17 | #include "synchronizeable_string.h" |
---|
[7459] | 18 | #include "converter.h" |
---|
[7444] | 19 | |
---|
| 20 | |
---|
| 21 | /** |
---|
| 22 | * standard constructor |
---|
| 23 | * @todo this constructor is not jet implemented - do it |
---|
| 24 | */ |
---|
| 25 | SynchronizeableString::SynchronizeableString( std::string * ptrIn, std::string * ptrOut, std::string name, int permission, int priority) : SynchronizeableVar( ptrIn, ptrOut, name, 0, permission, priority ) |
---|
| 26 | { |
---|
| 27 | this->vPtrIn = ptrIn; |
---|
| 28 | this->vPtrOut = ptrOut; |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | |
---|
| 32 | /** |
---|
| 33 | * standard deconstructor |
---|
| 34 | */ |
---|
| 35 | SynchronizeableString::~SynchronizeableString () |
---|
| 36 | { |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | /** |
---|
| 40 | * write var data to byte buffer |
---|
| 41 | * @param buf pointer to write to |
---|
| 42 | * @param maxLength writeToBuf will not write more than maxLength bytes |
---|
| 43 | * @return number bytes written |
---|
| 44 | */ |
---|
| 45 | int SynchronizeableString::writeToBuf( byte * buf, int maxLength ) |
---|
| 46 | { |
---|
[7459] | 47 | int res = Converter::stringToByteArray( *vPtrIn, buf, maxLength ); |
---|
| 48 | |
---|
| 49 | assert( res > 0 ); |
---|
[8147] | 50 | assert( res == vPtrIn->length()+INTSIZE ); |
---|
[7459] | 51 | |
---|
| 52 | return res; |
---|
[7444] | 53 | } |
---|
| 54 | |
---|
| 55 | /** |
---|
| 56 | * read var data from byte buffer |
---|
| 57 | * @param buf pointer to read from |
---|
| 58 | * @param maxLength readFromBuf will not read more than maxLength bytes |
---|
| 59 | * @return number bytes read |
---|
| 60 | */ |
---|
| 61 | int SynchronizeableString::readFromBuf( byte * buf, int maxLength ) |
---|
| 62 | { |
---|
[7631] | 63 | std::string oldVal = *vPtrOut; |
---|
| 64 | |
---|
[7459] | 65 | int res = Converter::byteArrayToString( buf, *vPtrOut, maxLength ); |
---|
| 66 | |
---|
[7631] | 67 | setHasChanged( oldVal != *vPtrOut ); |
---|
| 68 | |
---|
[7804] | 69 | if ( res < 0 ) |
---|
| 70 | { |
---|
| 71 | printf("%s | res (%d) < 0\n", this->getName().c_str(), res); |
---|
| 72 | } |
---|
[7459] | 73 | assert( res > 0 ); |
---|
| 74 | |
---|
| 75 | return res; |
---|
| 76 | } |
---|
[7578] | 77 | |
---|
| 78 | /** |
---|
| 79 | * print out variable value |
---|
| 80 | */ |
---|
| 81 | void SynchronizeableString::debug( ) |
---|
| 82 | { |
---|
| 83 | printf( "SYNCHRONIZEABLE_VAR: %s IN: '%s' OUT: '%s'\n", name.c_str(), vPtrIn->c_str(), vPtrOut->c_str() ); |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | |
---|
[7631] | 87 | /** |
---|
| 88 | * reads actual size from buffer. this is used when size is not constant |
---|
| 89 | * @param buf pointer to data |
---|
| 90 | * @param maxLength maximal size of data |
---|
| 91 | * @return same as readFromBuf would return |
---|
| 92 | */ |
---|
| 93 | int SynchronizeableString::getSizeFromBuf( byte * buf, int maxLength ) |
---|
| 94 | { |
---|
| 95 | std::string t; |
---|
| 96 | |
---|
| 97 | int res = Converter::byteArrayToString( buf, t, maxLength ); |
---|
| 98 | |
---|
| 99 | assert( res > 0 ); |
---|
| 100 | |
---|
| 101 | return res; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | |
---|