[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" |
---|
[9406] | 19 | #include <cassert> |
---|
[7444] | 20 | |
---|
| 21 | |
---|
| 22 | /** |
---|
| 23 | * standard constructor |
---|
| 24 | * @todo this constructor is not jet implemented - do it |
---|
| 25 | */ |
---|
| 26 | SynchronizeableString::SynchronizeableString( std::string * ptrIn, std::string * ptrOut, std::string name, int permission, int priority) : SynchronizeableVar( ptrIn, ptrOut, name, 0, permission, priority ) |
---|
| 27 | { |
---|
| 28 | this->vPtrIn = ptrIn; |
---|
| 29 | this->vPtrOut = ptrOut; |
---|
| 30 | } |
---|
| 31 | |
---|
| 32 | |
---|
| 33 | /** |
---|
| 34 | * standard deconstructor |
---|
| 35 | */ |
---|
| 36 | SynchronizeableString::~SynchronizeableString () |
---|
| 37 | { |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | /** |
---|
| 41 | * write var data to byte buffer |
---|
| 42 | * @param buf pointer to write to |
---|
| 43 | * @param maxLength writeToBuf will not write more than maxLength bytes |
---|
| 44 | * @return number bytes written |
---|
| 45 | */ |
---|
| 46 | int SynchronizeableString::writeToBuf( byte * buf, int maxLength ) |
---|
| 47 | { |
---|
[7459] | 48 | int res = Converter::stringToByteArray( *vPtrIn, buf, maxLength ); |
---|
[9406] | 49 | |
---|
[7459] | 50 | assert( res > 0 ); |
---|
[8147] | 51 | assert( res == vPtrIn->length()+INTSIZE ); |
---|
[9406] | 52 | |
---|
[7459] | 53 | return res; |
---|
[7444] | 54 | } |
---|
| 55 | |
---|
[9406] | 56 | int SynchronizeableString::getSize() |
---|
| 57 | { |
---|
| 58 | return vPtrIn->length()+INTSIZE; |
---|
| 59 | } |
---|
| 60 | |
---|
[7444] | 61 | /** |
---|
| 62 | * read var data from byte buffer |
---|
| 63 | * @param buf pointer to read from |
---|
| 64 | * @param maxLength readFromBuf will not read more than maxLength bytes |
---|
| 65 | * @return number bytes read |
---|
| 66 | */ |
---|
| 67 | int SynchronizeableString::readFromBuf( byte * buf, int maxLength ) |
---|
| 68 | { |
---|
[7631] | 69 | std::string oldVal = *vPtrOut; |
---|
[9406] | 70 | |
---|
[7459] | 71 | int res = Converter::byteArrayToString( buf, *vPtrOut, maxLength ); |
---|
[9406] | 72 | |
---|
[7631] | 73 | setHasChanged( oldVal != *vPtrOut ); |
---|
[9406] | 74 | |
---|
[7804] | 75 | if ( res < 0 ) |
---|
| 76 | { |
---|
| 77 | printf("%s | res (%d) < 0\n", this->getName().c_str(), res); |
---|
| 78 | } |
---|
[7459] | 79 | assert( res > 0 ); |
---|
[9406] | 80 | |
---|
[7459] | 81 | return res; |
---|
| 82 | } |
---|
[7578] | 83 | |
---|
| 84 | /** |
---|
| 85 | * print out variable value |
---|
| 86 | */ |
---|
| 87 | void SynchronizeableString::debug( ) |
---|
| 88 | { |
---|
| 89 | printf( "SYNCHRONIZEABLE_VAR: %s IN: '%s' OUT: '%s'\n", name.c_str(), vPtrIn->c_str(), vPtrOut->c_str() ); |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | |
---|
[7631] | 93 | /** |
---|
| 94 | * reads actual size from buffer. this is used when size is not constant |
---|
| 95 | * @param buf pointer to data |
---|
| 96 | * @param maxLength maximal size of data |
---|
| 97 | * @return same as readFromBuf would return |
---|
| 98 | */ |
---|
| 99 | int SynchronizeableString::getSizeFromBuf( byte * buf, int maxLength ) |
---|
| 100 | { |
---|
| 101 | std::string t; |
---|
[9406] | 102 | |
---|
[7631] | 103 | int res = Converter::byteArrayToString( buf, t, maxLength ); |
---|
[9406] | 104 | |
---|
[7631] | 105 | assert( res > 0 ); |
---|
[9406] | 106 | |
---|
[7631] | 107 | return res; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | |
---|