Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions co_sim_io/c/co_sim_io_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
// C Interface for CoSimulation
// see "co_sim_io.hpp"

// System includes
#include <stdio.h>

// Project includes
#include "co_sim_io_c_info.h"
#include "co_sim_io_c_model_part.h"

Expand Down
9 changes: 6 additions & 3 deletions tests/integration_tutorials/c/connect_disconnect_a.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
// Main authors: Pooyan Dadvand
//

// System includes
#include <string.h>

// CoSimulation includes
#include "c/co_sim_io_c.h"

Expand All @@ -29,9 +32,10 @@ int main()

// The connect must be called before any CosimIO function called
CoSimIO_Info connect_info = CoSimIO_Connect(settings);
const char* connection_name = CoSimIO_Info_GetString(connect_info, "connection_name");

char connection_name[BUFSIZ];
strcpy(connection_name, CoSimIO_Info_GetString(connect_info, "connection_name"));
COSIMIO_CHECK_EQUAL(CoSimIO_Info_GetInt(connect_info, "connection_status"), CoSimIO_Connected);
CoSimIO_FreeInfo(connect_info); // Don't forget to free the connect_info

// Now you may call any CoSimIO functions

Expand All @@ -46,7 +50,6 @@ int main()
// Don't forget to release the settings and info
CoSimIO_FreeInfo(settings);
CoSimIO_FreeInfo(disconnect_settings);
CoSimIO_FreeInfo(connect_info);
CoSimIO_FreeInfo(disconnect_info);

return 0;
Expand Down
9 changes: 6 additions & 3 deletions tests/integration_tutorials/c/connect_disconnect_b.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
// Main authors: Pooyan Dadvand
//

// System includes
#include <string.h>

// CoSimulation includes
#include "c/co_sim_io_c.h"

Expand All @@ -29,9 +32,10 @@ int main()

// The connect must be called before any CosimIO function called
CoSimIO_Info connect_info = CoSimIO_Connect(settings);
const char* connection_name = CoSimIO_Info_GetString(connect_info, "connection_name");

char connection_name[BUFSIZ];
strcpy(connection_name, CoSimIO_Info_GetString(connect_info, "connection_name"));
COSIMIO_CHECK_EQUAL(CoSimIO_Info_GetInt(connect_info, "connection_status"), CoSimIO_Connected);
CoSimIO_FreeInfo(connect_info); // Don't forget to free the connect_info

// Now you may call any CoSimIO functions

Expand All @@ -46,7 +50,6 @@ int main()
// Don't forget to release the settings and info
CoSimIO_FreeInfo(settings);
CoSimIO_FreeInfo(disconnect_settings);
CoSimIO_FreeInfo(connect_info);
CoSimIO_FreeInfo(disconnect_info);

return 0;
Expand Down
8 changes: 6 additions & 2 deletions tests/integration_tutorials/c/export_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
// Main authors: Pooyan Dadvand
//

// System includes
#include <string.h>

// CoSimulation includes
#include "c/co_sim_io_c.h"

Expand All @@ -30,8 +33,10 @@ int main()

// Connecting using the connection settings
CoSimIO_Info connect_info = CoSimIO_Connect(connection_settings);
char connection_name[BUFSIZ];
strcpy(connection_name, CoSimIO_Info_GetString(connect_info, "connection_name"));
COSIMIO_CHECK_EQUAL(CoSimIO_Info_GetInt(connect_info, "connection_status"), CoSimIO_Connected);
const char* connection_name = CoSimIO_Info_GetString(connect_info, "connection_name");
CoSimIO_FreeInfo(connect_info); // Don't forget to free the connect_info

// After connecting we may export the data
int data_size = 4;
Expand All @@ -57,7 +62,6 @@ int main()
// Don't forget to release the settings and info
CoSimIO_FreeInfo(connection_settings);
CoSimIO_FreeInfo(disconnect_settings);
CoSimIO_FreeInfo(connect_info); // Don't forget to free the connect_info
CoSimIO_FreeInfo(disconnect_info);

return 0;
Expand Down
5 changes: 5 additions & 0 deletions tests/integration_tutorials/c/export_import_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
// Main authors: Pooyan Dadvand
//

// System includes
#include <string.h>

// CoSimulation includes
#include "c/co_sim_io_c.h"

Expand All @@ -36,6 +39,8 @@ int main()

// Connecting using the connection settings
CoSimIO_Info connect_info = CoSimIO_Connect(connection_settings);
char connection_name[BUFSIZ];
strcpy(connection_name, CoSimIO_Info_GetString(connect_info, "connection_name"));
COSIMIO_CHECK_EQUAL_INT(CoSimIO_Info_GetInt(connect_info, "connection_status"), CoSimIO_Connected);
CoSimIO_FreeInfo(connect_info); // Don't forget to free the connect_info

Expand Down
5 changes: 5 additions & 0 deletions tests/integration_tutorials/c/export_import_mesh.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
// Main authors: Pooyan Dadvand
//

// System includes
#include <string.h>

// CoSimulation includes
#include "c/co_sim_io_c.h"

Expand All @@ -36,6 +39,8 @@ int main()

// Connecting using the connection settings
CoSimIO_Info connect_info = CoSimIO_Connect(connection_settings);
char connection_name[BUFSIZ];
strcpy(connection_name, CoSimIO_Info_GetString(connect_info, "connection_name"));
COSIMIO_CHECK_EQUAL_INT(CoSimIO_Info_GetInt(connect_info, "connection_status"), CoSimIO_Connected);
CoSimIO_FreeInfo(connect_info); // Don't forget to free the connect_info

Expand Down
8 changes: 6 additions & 2 deletions tests/integration_tutorials/c/export_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
// Main authors: Pooyan Dadvand
//

// System includes
#include <string.h>

// CoSimulation includes
#include "c/co_sim_io_c.h"

Expand All @@ -30,8 +33,10 @@ int main()

// Connecting using the connection settings
CoSimIO_Info connect_info = CoSimIO_Connect(connection_settings);
char connection_name[BUFSIZ];
strcpy(connection_name, CoSimIO_Info_GetString(connect_info, "connection_name"));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In cases like this, why we should copy the connection_name? What was the problem of directly using the pointer to the internal name stored in the info?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it is easier to handle when we have a local variable, then we can safely delete the connect_info

Otherwise we need to keep it around until we disconnect

Not sure what is better, we can also leave it as is and let the user decide what they prefer

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see... I think we should use the strcpy only when we want to keep some string for our own. In cases like the above one, I would just use the pointer to the internal data and put a comment in the tutorial that // p_connection_name is not valid anymore after freeing the info

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

COSIMIO_CHECK_EQUAL(CoSimIO_Info_GetInt(connect_info, "connection_status"), CoSimIO_Connected);
const char* connection_name = CoSimIO_Info_GetString(connect_info, "connection_name");
CoSimIO_FreeInfo(connect_info); // Don't forget to free the connect_info

// Creatinf the info to export
CoSimIO_Info info_to_export=CoSimIO_CreateInfo();
Expand All @@ -57,7 +62,6 @@ int main()
// Don't forget to release the settings and info
CoSimIO_FreeInfo(connection_settings);
CoSimIO_FreeInfo(disconnect_settings);
CoSimIO_FreeInfo(connect_info); // Don't forget to free the connect_info
CoSimIO_FreeInfo(disconnect_info);

return 0;
Expand Down
8 changes: 6 additions & 2 deletions tests/integration_tutorials/c/export_mesh.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
// Main authors: Pooyan Dadvand
//

// System includes
#include <string.h>

// CoSimulation includes
#include "c/co_sim_io_c.h"

Expand All @@ -30,8 +33,10 @@ int main()

// Connecting using the connection settings
CoSimIO_Info connect_info = CoSimIO_Connect(connection_settings);
char connection_name[BUFSIZ];
strcpy(connection_name, CoSimIO_Info_GetString(connect_info, "connection_name"));
COSIMIO_CHECK_EQUAL(CoSimIO_Info_GetInt(connect_info, "connection_status"), CoSimIO_Connected);
const char* connection_name = CoSimIO_Info_GetString(connect_info, "connection_name");
CoSimIO_FreeInfo(connect_info); // Don't forget to free the connect_info

int number_of_nodes=6;
double nodal_coordinates[] = {
Expand Down Expand Up @@ -77,7 +82,6 @@ int main()
// Don't forget to release the settings and info
CoSimIO_FreeInfo(connection_settings);
CoSimIO_FreeInfo(disconnect_settings);
CoSimIO_FreeInfo(connect_info); // Don't forget to free the connect_info
CoSimIO_FreeInfo(disconnect_info);


Expand Down
8 changes: 6 additions & 2 deletions tests/integration_tutorials/c/import_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
// Main authors: Pooyan Dadvand
//

// System includes
#include <string.h>

// CoSimulation includes
#include "c/co_sim_io_c.h"

Expand All @@ -30,8 +33,10 @@ int main()

// Connecting using the connection settings
CoSimIO_Info connect_info = CoSimIO_Connect(connection_settings);
char connection_name[BUFSIZ];
strcpy(connection_name, CoSimIO_Info_GetString(connect_info, "connection_name"));
COSIMIO_CHECK_EQUAL(CoSimIO_Info_GetInt(connect_info, "connection_status"), CoSimIO_Connected);
const char* connection_name = CoSimIO_Info_GetString(connect_info, "connection_name");
CoSimIO_FreeInfo(connect_info); // Don't forget to free the connect_info

// After conneting we may import the data
double* data;
Expand Down Expand Up @@ -60,7 +65,6 @@ int main()
// Don't forget to release the settings and info
CoSimIO_FreeInfo(connection_settings);
CoSimIO_FreeInfo(disconnect_settings);
CoSimIO_FreeInfo(connect_info); // Don't forget to free the connect_info
CoSimIO_FreeInfo(disconnect_info);

return 0;
Expand Down
25 changes: 13 additions & 12 deletions tests/integration_tutorials/c/import_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@
#include <math.h>
#include <string.h>

// CoSimulation includes
// Project includes
#include "c/co_sim_io_c.h"

#define COSIMIO_CHECK_INT_EQUAL(a, b) \
#define COSIMIO_CHECK_EQUAL_INT(a, b) \
if (a != b) { \
printf("in line %d: %d is not equal to %d", __LINE__, a, b); \
return 1; \
}

#define COSIMIO_CHECK_DOUBLE_EQUAL(a, b) \
#define COSIMIO_CHECK_EQUAL_DOUBLE(a, b) \
if (fabs(a-b)>1e-15) { \
printf("in line %d: %f is not equal to %f", __LINE__, a, b); \
return 1; \
}

#define COSIMIO_CHECK_STRING_EQUAL(a, b) \
#define COSIMIO_CHECK_EQUAL_STRING(a, b) \
if (strcmp(a,b)) { \
printf("in line %d: %s is not equal to %s", __LINE__, a, b); \
return 1; \
Expand All @@ -47,8 +47,10 @@ int main()

// Connecting using the connection settings
CoSimIO_Info connect_info = CoSimIO_Connect(connection_settings);
COSIMIO_CHECK_INT_EQUAL(CoSimIO_Info_GetInt(connect_info, "connection_status"), CoSimIO_Connected);
const char* connection_name = CoSimIO_Info_GetString(connect_info, "connection_name");
char connection_name[BUFSIZ];
strcpy(connection_name, CoSimIO_Info_GetString(connect_info, "connection_name"));
COSIMIO_CHECK_EQUAL_INT(CoSimIO_Info_GetInt(connect_info, "connection_status"), CoSimIO_Connected);
CoSimIO_FreeInfo(connect_info); // Don't forget to free the connect_info

// Creating the info for export
CoSimIO_Info export_info=CoSimIO_CreateInfo();
Expand All @@ -59,10 +61,10 @@ int main()
CoSimIO_Info imported_info = CoSimIO_ImportInfo(export_info);

// check imported Info
COSIMIO_CHECK_STRING_EQUAL(CoSimIO_Info_GetString(imported_info, "id"), "convergence_information");
COSIMIO_CHECK_INT_EQUAL(CoSimIO_Info_GetBool(imported_info, "is_converged"), 1);
COSIMIO_CHECK_DOUBLE_EQUAL(CoSimIO_Info_GetDouble(imported_info, "tol"), 0.23);
COSIMIO_CHECK_INT_EQUAL(CoSimIO_Info_GetInt(imported_info, "echo_level"), 2);
COSIMIO_CHECK_EQUAL_STRING(CoSimIO_Info_GetString(imported_info, "id"), "convergence_information");
COSIMIO_CHECK_EQUAL_INT(CoSimIO_Info_GetBool(imported_info, "is_converged"), 1);
COSIMIO_CHECK_EQUAL_DOUBLE(CoSimIO_Info_GetDouble(imported_info, "tol"), 0.23);
COSIMIO_CHECK_EQUAL_INT(CoSimIO_Info_GetInt(imported_info, "echo_level"), 2);

// Freeing the export_info and imported_info
CoSimIO_FreeInfo(export_info);
Expand All @@ -72,12 +74,11 @@ int main()
CoSimIO_Info disconnect_settings=CoSimIO_CreateInfo();
CoSimIO_Info_SetString(disconnect_settings, "connection_name", connection_name);
CoSimIO_Info disconnect_info = CoSimIO_Disconnect(disconnect_settings); // disconnect afterwards
COSIMIO_CHECK_INT_EQUAL(CoSimIO_Info_GetInt(disconnect_info, "connection_status"), CoSimIO_Disconnected);
COSIMIO_CHECK_EQUAL_INT(CoSimIO_Info_GetInt(disconnect_info, "connection_status"), CoSimIO_Disconnected);

// Don't forget to release the settings and info
CoSimIO_FreeInfo(connection_settings);
CoSimIO_FreeInfo(disconnect_settings);
CoSimIO_FreeInfo(connect_info); // Don't forget to free the connect_info
CoSimIO_FreeInfo(disconnect_info);

return 0;
Expand Down
8 changes: 6 additions & 2 deletions tests/integration_tutorials/c/import_mesh.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
// Main authors: Philipp Bucher (https://github.com/philbucher)
//

// System includes
#include <string.h>

// CoSimulation includes
#include "c/co_sim_io_c.h"

Expand All @@ -36,8 +39,10 @@ int main()

// Connecting using the connection settings
CoSimIO_Info connect_info = CoSimIO_Connect(connection_settings);
char connection_name[BUFSIZ];
strcpy(connection_name, CoSimIO_Info_GetString(connect_info, "connection_name"));
COSIMIO_CHECK_EQUAL_INT(CoSimIO_Info_GetInt(connect_info, "connection_status"), CoSimIO_Connected);
const char* connection_name = CoSimIO_Info_GetString(connect_info, "connection_name");
CoSimIO_FreeInfo(connect_info); // Don't forget to free the connect_info

// After conneting we may import the mesh
double* nodal_coordinates;
Expand Down Expand Up @@ -106,7 +111,6 @@ int main()
// Don't forget to release the settings and info
CoSimIO_FreeInfo(connection_settings);
CoSimIO_FreeInfo(disconnect_settings);
CoSimIO_FreeInfo(connect_info); // Don't forget to free the connect_info
CoSimIO_FreeInfo(disconnect_info);

return 0;
Expand Down
5 changes: 5 additions & 0 deletions tests/integration_tutorials/c/mapping.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
// Main authors: Pooyan Dadvand
//

// System includes
#include <string.h>

// CoSimulation includes
#include "c/co_sim_io_c.h"

Expand All @@ -30,6 +33,8 @@ int main()

// Connecting using the connection settings
CoSimIO_Info connect_info = CoSimIO_Connect(connection_settings);
char connection_name[BUFSIZ];
strcpy(connection_name, CoSimIO_Info_GetString(connect_info, "connection_name"));
COSIMIO_CHECK_EQUAL(CoSimIO_Info_GetInt(connect_info, "connection_status"), CoSimIO_Connected);
CoSimIO_FreeInfo(connect_info); // Don't forget to free the connect_info

Expand Down