Changeset 16:1d329bbc0303 in xplra


Ignore:
Timestamp:
01/28/13 19:25:31 (11 years ago)
Author:
István Váradi <ivaradi@…>
Branch:
default
Phase:
public
Message:

Added the functions to handle the array queries

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/client/c/hu/varadiistvan/xplra/XPlane.cc

    r15 r16  
    4949
    5050using std::auto_ptr;
     51using std::string;
    5152
    5253//------------------------------------------------------------------------------
     
    181182//------------------------------------------------------------------------------
    182183
     184size_t XPlane::getArray(const char* name, uint8_t type,
     185                        ssize_t length, size_t offset) throw(Exception)
     186{
     187    checkStream();
     188
     189    stream->writeU8(Protocol::COMMAND_GET_SINGLE);
     190    stream->writeString(name, strlen(name));
     191    stream->writeU8(type);
     192    stream->writeS32(static_cast<int32_t>(length));
     193    stream->writeS32(static_cast<int32_t>(offset));
     194    if (!stream->flush()) checkStream();
     195
     196    uint8_t result = stream->readU8();
     197    length = stream->readS32();
     198    checkStream();
     199    checkResult(result);
     200
     201    return length;
     202}
     203
     204//------------------------------------------------------------------------------
     205
     206size_t XPlane::getFloatArray(const char* name, float* dest,
     207                             size_t length, size_t offset) throw(Exception)
     208{
     209    length = getArray(name, Protocol::TYPE_FLOAT_ARRAY, length, offset);
     210
     211    if (!stream->read(dest, length*sizeof(float))) checkStream();
     212
     213    return length;
     214}
     215
     216//------------------------------------------------------------------------------
     217
     218float* XPlane::getFloatArray(const char* name, size_t& length,
     219                             size_t offset) throw(Exception)
     220{
     221    length = getArray(name, Protocol::TYPE_FLOAT_ARRAY, -1, offset);
     222
     223    auto_ptr<float> data(new float[length]);
     224    if (!stream->read(data.get(), length*sizeof(float))) checkStream();
     225    return data.release();
     226}
     227
     228//------------------------------------------------------------------------------
     229
     230size_t XPlane::getIntArray(const char* name, int32_t* dest,
     231                           size_t length, size_t offset) throw(Exception)
     232{
     233    length = getArray(name, Protocol::TYPE_INT_ARRAY, length, offset);
     234
     235    if (!stream->read(dest, length*sizeof(int32_t))) checkStream();
     236
     237    return length;
     238}
     239
     240//------------------------------------------------------------------------------
     241
     242int32_t* XPlane::getIntArray(const char* name, size_t& length,
     243                             size_t offset) throw(Exception)
     244{
     245    length = getArray(name, Protocol::TYPE_INT_ARRAY, -1, offset);
     246
     247    auto_ptr<int32_t> data(new int32_t[length]);
     248    if (!stream->read(data.get(), length*sizeof(int32_t))) checkStream();
     249    return data.release();
     250}
     251
     252//------------------------------------------------------------------------------
     253
     254size_t XPlane::getByteArray(const char* name, uint8_t* dest,
     255                            size_t length, size_t offset) throw(Exception)
     256{
     257    length = getArray(name, Protocol::TYPE_BYTE_ARRAY, length, offset);
     258
     259    if (!stream->read(dest, length*sizeof(uint8_t))) checkStream();
     260
     261    return length;
     262}
     263
     264//------------------------------------------------------------------------------
     265
     266uint8_t* XPlane::getByteArray(const char* name, size_t& length,
     267                              size_t offset) throw(Exception)
     268{
     269    length = getArray(name, Protocol::TYPE_BYTE_ARRAY, -1, offset);
     270
     271    auto_ptr<uint8_t> data(new uint8_t[length]);
     272    if (!stream->read(data.get(), length*sizeof(uint8_t))) checkStream();
     273    return data.release();
     274}
     275
     276//------------------------------------------------------------------------------
     277
     278string XPlane::getString(const char* name, size_t offset) throw(Exception)
     279{
     280    size_t length = 0;
     281    auto_ptr<uint8_t> data(getByteArray(name, length, offset));
     282    return string(reinterpret_cast<char*>(data.get()));
     283}
     284
     285//------------------------------------------------------------------------------
     286
    183287// Local Variables:
    184288// mode: C++
  • src/client/c/hu/varadiistvan/xplra/XPlane.h

    r15 r16  
    118118    double getDouble(const char* name) throw(Exception);
    119119
     120    /**
     121     * Get a possibly partial array of floats.
     122     *
     123     * @param length the length of the destination buffer
     124     * @param offset the offset from which to get the array
     125     *
     126     * @return the number of values acquired actually
     127     */
     128    size_t getFloatArray(const char* name, float* dest,
     129                         size_t length, size_t offset = 0) throw(Exception);
     130
     131    /**
     132     * Get a possibly partial array of floats. The result array will
     133     * be created with a length needed to hold the returned value.
     134     *
     135     * @param offset the offset from which to get the array
     136     */
     137    float* getFloatArray(const char* name, size_t& length,
     138                         size_t offset = 0) throw(Exception);
     139
     140    /**
     141     * Get a possibly partial array of integers.
     142     *
     143     * @param length the length of the destination buffer
     144     * @param offset the offset from which to get the array
     145     *
     146     * @return the number of values acquired actually
     147     */
     148    size_t getIntArray(const char* name, int32_t* dest,
     149                       size_t length, size_t offset = 0) throw(Exception);
     150
     151    /**
     152     * Get a possibly partial array of integers. The result array will
     153     * be created with a length needed to hold the returned value.
     154     *
     155     * @param offset the offset from which to get the array
     156     */
     157    int32_t* getIntArray(const char* name, size_t& length,
     158                         size_t offset = 0) throw(Exception);
     159
     160    /**
     161     * Get a possibly partial array of bytes.
     162     *
     163     * @param length the length of the destination buffer
     164     * @param offset the offset from which to get the array
     165     *
     166     * @return the number of values acquired actually
     167     */
     168    size_t getByteArray(const char* name, uint8_t* dest,
     169                        size_t length, size_t offset = 0) throw(Exception);
     170
     171    /**
     172     * Get a possibly partial array of bytes. The result array will
     173     * be created with a length needed to hold the returned value.
     174     *
     175     * @param offset the offset from which to get the array
     176     */
     177    uint8_t* getByteArray(const char* name, size_t& lengyh,
     178                          size_t offset = 0) throw(Exception);
     179
     180    /**
     181     * Get a string. A string is a byte array.
     182     */
     183    std::string getString(const char* name, size_t offset = 0) throw(Exception);
     184
    120185private:
    121186    /**
     
    135200     */
    136201    void getScalar(const char* name, uint8_t type) throw(Exception);
     202
     203    /**
     204     * Issue the command to get an array of values of the given
     205     * type. It checks the result and retrieves the number of value
     206     * items available.
     207     *
     208     * @return the number of value items available
     209     */
     210    size_t getArray(const char* name, uint8_t type,
     211                    ssize_t length, size_t offset) throw(Exception);
     212
    137213};
    138214
  • test/basictest.cc

    r15 r16  
    3939using hu::varadiistvan::xplra::ProtocolException;
    4040
     41using std::string;
     42
    4143//------------------------------------------------------------------------------
    4244
     
    7577        printf("The coordinates: %f, %f\n\n", latitude, longitude);
    7678
     79        printf("Querying the aircraft's description as an array of a fixed size...\n");
     80        uint8_t result[260];
     81        size_t length = xplane.getByteArray("sim/aircraft/view/acf_descrip",
     82                                            result, sizeof(result)/sizeof(result[0]));
     83        printf("Got %zu bytes, as a string: '%s'\n\n", length, result);
     84
     85        printf("Querying the aircraft's description as a dynamically created array...\n");
     86        uint8_t* result1 = xplane.getByteArray("sim/aircraft/view/acf_descrip", length);
     87        printf("Got %zu bytes, as a string: '%s'\n\n", length, result1);
     88
     89        printf("Querying the aircraft's description as a string, with an offset of 3...\n");
     90        string result2 = xplane.getString("sim/aircraft/view/acf_descrip", 3);
     91        printf("Got: '%s'\n\n", result2.c_str());
     92
     93        printf("Querying the aircraft's engine types as an array of a fixed size...\n");
     94        int32_t result3[8];
     95        length = xplane.getIntArray("sim/aircraft/prop/acf_en_type",
     96                                    result3, sizeof(result3)/sizeof(result3[0]));
     97        printf("Got %zu values:", length);
     98        for(size_t i = 0; i<length; ++i) {
     99            if (i>0) printf(",");
     100            printf(" %d", result3[i]);
     101        }
     102        printf("\n\n");
     103
     104        printf("Querying the aircraft's engine types as a dynamically created array...\n");
     105        length = 0;
     106        int32_t* result4 = xplane.getIntArray("sim/aircraft/prop/acf_en_type", length);
     107        printf("Got %zu values:", length);
     108        for(size_t i = 0; i<length; ++i) {
     109            if (i>0) printf(",");
     110            printf(" %d", result4[i]);
     111        }
     112        printf("\n\n");
     113
     114        printf("Querying the aircraft's propeller direction as an array of a fixed size...\n");
     115        float result5[8];
     116        length = xplane.getFloatArray("sim/aircraft/prop/acf_prop_dir",
     117                                      result5, sizeof(result5)/sizeof(result5[0]));
     118        printf("Got %zu values:", length);
     119        for(size_t i = 0; i<length; ++i) {
     120            if (i>0) printf(",");
     121            printf(" %f", result5[i]);
     122        }
     123        printf("\n\n");
     124
     125        printf("Querying the aircraft's propeller direction as a dynamically created array...\n");
     126        length = 0;
     127        float* result6 = xplane.getFloatArray("sim/aircraft/prop/acf_prop_dir", length);
     128        printf("Got %zu values:", length);
     129        for(size_t i = 0; i<length; ++i) {
     130            if (i>0) printf(",");
     131            printf(" %f", result6[i]);
     132        }
     133        printf("\n\n");
     134
    77135        return 0;
    78136    } catch(const Exception& exception) {
Note: See TracChangeset for help on using the changeset viewer.