Changeset 24:838d1d1b619e in xplra


Ignore:
Timestamp:
02/07/13 16:16:04 (12 years ago)
Author:
István Váradi <ivaradi@…>
Branch:
default
Phase:
public
Message:

Added the array reading functions

Files:
3 edited

Legend:

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

    r23 r24  
    414414//------------------------------------------------------------------------------
    415415
     416extern "C" ssize_t xplra_get_float_array(float* dest, size_t length, size_t offset,
     417                                         int connectionID, const char* name)
     418{
     419    Connection* connection = Slot::getConnection(connectionID);
     420    if (connection==0) return -1;
     421    try {
     422        return connection->getFloatArray(name, dest, length, offset);
     423    } catch (...) {
     424        connection->handleException();
     425        return -1;
     426    }
     427}
     428
     429/*----------------------------------------------------------------------------*/
     430
     431extern "C" float* xplra_get_float_array_new(size_t* length, size_t offset,
     432                                            int connectionID, const char* name)
     433{
     434    Connection* connection = Slot::getConnection(connectionID);
     435    if (connection==0) return 0;
     436    try {
     437        return connection->getFloatArray(name, *length, offset);
     438    } catch (...) {
     439        connection->handleException();
     440        return 0;
     441    }
     442}
     443
     444//------------------------------------------------------------------------------
     445
     446extern "C" ssize_t xplra_get_int_array(int32_t* dest, size_t length, size_t offset,
     447                                       int connectionID, const char* name)
     448{
     449    Connection* connection = Slot::getConnection(connectionID);
     450    if (connection==0) return -1;
     451    try {
     452        return connection->getIntArray(name, dest, length, offset);
     453    } catch (...) {
     454        connection->handleException();
     455        return -1;
     456    }
     457}
     458
     459/*----------------------------------------------------------------------------*/
     460
     461extern "C" int32_t* xplra_get_int_array_new(size_t* length, size_t offset,
     462                                            int connectionID, const char* name)
     463{
     464    Connection* connection = Slot::getConnection(connectionID);
     465    if (connection==0) return 0;
     466    try {
     467        return connection->getIntArray(name, *length, offset);
     468    } catch (...) {
     469        connection->handleException();
     470        return 0;
     471    }
     472}
     473
     474//------------------------------------------------------------------------------
     475
     476extern "C" ssize_t xplra_get_byte_array(void* dest, size_t length, size_t offset,
     477                                        int connectionID, const char* name)
     478{
     479    Connection* connection = Slot::getConnection(connectionID);
     480    if (connection==0) return -1;
     481    try {
     482        return connection->getByteArray(name, reinterpret_cast<uint8_t*>(dest),
     483                                        length, offset);
     484    } catch (...) {
     485        connection->handleException();
     486        return -1;
     487    }
     488}
     489
     490/*----------------------------------------------------------------------------*/
     491
     492extern "C" uint8_t* xplra_get_byte_array_new(size_t* length, size_t offset,
     493                                             int connectionID, const char* name)
     494{
     495    Connection* connection = Slot::getConnection(connectionID);
     496    if (connection==0) return 0;
     497    try {
     498        return connection->getByteArray(name, *length, offset);
     499    } catch (...) {
     500        connection->handleException();
     501        return 0;
     502    }
     503}
     504
     505//------------------------------------------------------------------------------
     506
    416507extern "C" int xplra_disconnect(int connectionID)
    417508{
  • src/client/c/hu/varadiistvan/xplra/xplra.h

    r23 r24  
    3232/*----------------------------------------------------------------------------*/
    3333
     34#include <stdlib.h>
     35#include <inttypes.h>
     36
     37/*----------------------------------------------------------------------------*/
     38
    3439#ifdef __cplusplus
    3540extern "C" {
     
    150155
    151156/**
     157 * Get an array of floats into a buffer.
     158 *
     159 * @param dest the array into which to get the data
     160 * @param length the length of the destination buffer
     161 * @param offset the offset from which to query the array
     162 *
     163 * @return the actual number of elements returned in case of success,
     164 * -1 on error
     165 */
     166ssize_t xplra_get_float_array(float* dest, size_t length, size_t offset,
     167                              int connectionID, const char* name);
     168
     169/*----------------------------------------------------------------------------*/
     170
     171/**
     172 * Get an array of floats into a newly allocated buffer.
     173 *
     174 * @param length pointer to a variable containing the length to
     175 * query. On return it will be set to the actual length, which can be
     176 * less than or equal to the input value.
     177 * @param offset the offset from which to query the array
     178 *
     179 * @return the new array on success, 0 on error
     180 */
     181float* xplra_get_float_array_new(size_t* length, size_t offset,
     182                                 int connectionID, const char* name);
     183
     184/*----------------------------------------------------------------------------*/
     185
     186/**
     187 * Get an array of integers into a buffer.
     188 *
     189 * @param dest the array into which to get the data
     190 * @param length the length of the destination buffer
     191 * @param offset the offset from which to query the array
     192 *
     193 * @return the actual number of elements returned in case of success,
     194 * -1 on error
     195 */
     196ssize_t xplra_get_int_array(int32_t* dest, size_t length, size_t offset,
     197                            int connectionID, const char* name);
     198
     199/*----------------------------------------------------------------------------*/
     200
     201/**
     202 * Get an array of integers into a newly allocated buffer.
     203 *
     204 * @param length pointer to a variable containing the length to
     205 * query. On return it will be set to the actual length, which can be
     206 * less than or equal to the input value.
     207 * @param offset the offset from which to query the array
     208 *
     209 * @return the new array on success, 0 on error
     210 */
     211int32_t* xplra_get_int_array_new(size_t* length, size_t offset,
     212                                 int connectionID, const char* name);
     213
     214/*----------------------------------------------------------------------------*/
     215
     216/**
     217 * Get an array of bytes into a buffer.
     218 *
     219 * @param dest the array into which to get the data
     220 * @param length the length of the destination buffer
     221 * @param offset the offset from which to query the array
     222 *
     223 * @return the actual number of elements returned in case of success,
     224 * -1 on error
     225 */
     226ssize_t xplra_get_byte_array(void* dest, size_t length, size_t offset,
     227                             int connectionID, const char* name);
     228
     229/*----------------------------------------------------------------------------*/
     230
     231/**
     232 * Get an array of bytes into a newly allocated buffer.
     233 *
     234 * @param length pointer to a variable containing the length to
     235 * query. On return it will be set to the actual length, which can be
     236 * less than or equal to the input value.
     237 * @param offset the offset from which to query the array
     238 *
     239 * @return the new array on success, 0 on error
     240 */
     241uint8_t* xplra_get_byte_array_new(size_t* length, size_t offset,
     242                                  int connectionID, const char* name);
     243
     244/*----------------------------------------------------------------------------*/
     245
     246/**
    152247 * Destroy the connection with the given ID.
    153248 *
  • test/basicctest.c

    r23 r24  
    4242    float spoolTime = 0.0;
    4343    double latitude = 0.0, longitude = 0.0;
     44    ssize_t length, i;
     45    size_t length1, j;
     46    uint8_t result[260];
     47    uint8_t* result1 = 0;
     48    int32_t result3[8];
     49    int32_t* result4 = 0;
     50    float result5[8];
     51    float* result6 = 0;
    4452
    4553    printf("Connection to X-Plane...\n");
     
    100108    printf("The coordinates: %f, %f\n\n", latitude, longitude);
    101109
     110    printf("Querying the aircraft's description as an array of a fixed size...\n");
     111    length = xplra_get_byte_array(result, sizeof(result)/sizeof(result[0]),
     112                                  0, connectionID,
     113                                  "sim/aircraft/view/acf_descrip");
     114    if (length<0) goto error;
     115    printf("Got %zu bytes, as a string: '%s'\n\n", length, result);
     116
     117    printf("Querying the aircraft's description as a dynamically created array with an offset of 3...\n");
     118    length1 = 0;
     119    result1 = xplra_get_byte_array_new(&length1, 3, connectionID,
     120                                       "sim/aircraft/view/acf_descrip");
     121    if (result1==0) goto error;
     122    printf("Got %zu bytes, as a string: '%s'\n\n", length1, result1);
     123
     124    printf("Querying the aircraft's engine types as an array of a fixed size...\n");
     125    length = xplra_get_int_array(result3, sizeof(result3)/sizeof(result3[0]), 0,
     126                                 connectionID, "sim/aircraft/prop/acf_en_type");
     127
     128    if (length<0) goto error;
     129    printf("Got %zd values:", length);
     130    for(i = 0; i<length; ++i) {
     131        if (i>0) printf(",");
     132        printf(" %d", result3[i]);
     133    }
     134    printf("\n\n");
     135
     136    printf("Querying the aircraft's engine types as a dynamically created array...\n");
     137    length1 = 0;
     138    result4 = xplra_get_int_array_new(&length1, 0, connectionID,
     139                                      "sim/aircraft/prop/acf_en_type");
     140    printf("Got %zu values:", length1);
     141    for(j = 0; j<length1; ++j) {
     142        if (j>0) printf(",");
     143        printf(" %d", result4[j]);
     144    }
     145    printf("\n\n");
     146
     147    printf("Querying the aircraft's propeller direction as an array of a fixed size...\n");
     148    length = xplra_get_float_array(result5, sizeof(result5)/sizeof(result5[0]),
     149                                   0, connectionID,
     150                                   "sim/aircraft/prop/acf_prop_dir");
     151    printf("Got %zd values:", length);
     152    for(i = 0; i<length; ++i) {
     153        if (i>0) printf(",");
     154        printf(" %f", result5[i]);
     155    }
     156    printf("\n\n");
     157
     158    printf("Querying the aircraft's propeller direction as a dynamically created array...\n");
     159    length1 = 0;
     160    result6 = xplra_get_float_array_new(&length1, 0, connectionID,
     161                                        "sim/aircraft/prop/acf_prop_dir");
     162    printf("Got %zu values:", length1);
     163    for(j = 0; j<length1; ++j) {
     164        if (j>0) printf(",");
     165        printf(" %f", result6[j]);
     166    }
     167    printf("\n\n");
     168
    102169    goto cleanup;
    103170error:
     
    116183    // try {
    117184
    118     //     printf("Querying the aircraft's description as an array of a fixed size...\n");
    119     //     uint8_t result[260];
    120     //     size_t length = xplane.getByteArray("sim/aircraft/view/acf_descrip",
    121     //                                         result, sizeof(result)/sizeof(result[0]));
    122     //     printf("Got %zu bytes, as a string: '%s'\n\n", length, result);
    123 
    124     //     printf("Querying the aircraft's description as a dynamically created array...\n");
    125     //     uint8_t* result1 = xplane.getByteArray("sim/aircraft/view/acf_descrip", length);
    126     //     printf("Got %zu bytes, as a string: '%s'\n\n", length, result1);
    127 
    128     //     printf("Querying the aircraft's description as a string, with an offset of 3...\n");
    129     //     string result2 = xplane.getString("sim/aircraft/view/acf_descrip", 3);
    130     //     printf("Got: '%s'\n\n", result2.c_str());
    131 
    132     //     printf("Querying the aircraft's engine types as an array of a fixed size...\n");
    133     //     int32_t result3[8];
    134     //     length = xplane.getIntArray("sim/aircraft/prop/acf_en_type",
    135     //                                 result3, sizeof(result3)/sizeof(result3[0]));
    136     //     printf("Got %zu values:", length);
    137     //     for(size_t i = 0; i<length; ++i) {
    138     //         if (i>0) printf(",");
    139     //         printf(" %d", result3[i]);
    140     //     }
    141     //     printf("\n\n");
    142 
    143     //     printf("Querying the aircraft's engine types as a dynamically created array...\n");
    144     //     length = 0;
    145     //     int32_t* result4 = xplane.getIntArray("sim/aircraft/prop/acf_en_type", length);
    146     //     printf("Got %zu values:", length);
    147     //     for(size_t i = 0; i<length; ++i) {
    148     //         if (i>0) printf(",");
    149     //         printf(" %d", result4[i]);
    150     //     }
    151     //     printf("\n\n");
    152 
    153     //     printf("Querying the aircraft's propeller direction as an array of a fixed size...\n");
    154     //     float result5[8];
    155     //     length = xplane.getFloatArray("sim/aircraft/prop/acf_prop_dir",
    156     //                                   result5, sizeof(result5)/sizeof(result5[0]));
    157     //     printf("Got %zu values:", length);
    158     //     for(size_t i = 0; i<length; ++i) {
    159     //         if (i>0) printf(",");
    160     //         printf(" %f", result5[i]);
    161     //     }
    162     //     printf("\n\n");
    163 
    164     //     printf("Querying the aircraft's propeller direction as a dynamically created array...\n");
    165     //     length = 0;
    166     //     float* result6 = xplane.getFloatArray("sim/aircraft/prop/acf_prop_dir", length);
    167     //     printf("Got %zu values:", length);
    168     //     for(size_t i = 0; i<length; ++i) {
    169     //         if (i>0) printf(",");
    170     //         printf(" %f", result6[i]);
    171     //     }
    172     //     printf("\n\n");
    173185
    174186    //     printf("Setting the number of the engines to %d...\n", numEngines + 1);
Note: See TracChangeset for help on using the changeset viewer.