Repository navigation
Expand file tree
/
Copy pathmodel.c
More file actions
128 lines (98 loc) · 2.7 KB
/
Copy pathmodel.c
File metadata and controls
128 lines (98 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "config.h"
#include "model_impl.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#include "shlwapi.h"
#pragma comment(lib, "shlwapi.lib")
#endif
#define MAX_PATH_LENGTH 4096
Status setStartValues(ModelInstance *instance) {
ASSERT_NOT_NULL(instance);
M(y) = 0;
instance->isDirtyValues = true;
return OK;
}
Status calculateValues(ModelInstance *instance) {
ASSERT_NOT_NULL(instance);
ASSERT_NOT_NULL(instance->resourceLocation);
FILE *file = NULL;
char path[MAX_PATH_LENGTH] = "";
char c = '\0';
#ifdef _WIN32
strncpy(path, instance->resourceLocation, MAX_PATH_LENGTH);
path[MAX_PATH_LENGTH - 1] = '\0';
#if FMI_VERSION == 2
if (!PathAppendA(path, "y.txt"))
return Discard;
#else
if (!strncat(path, "y.txt", MAX_PATH_LENGTH))
return Discard;
#endif
#else
strncpy(path, instance->resourceLocation, MAX_PATH_LENGTH);
path[MAX_PATH_LENGTH - 1] = '\0';
#if FMI_VERSION == 2
strncat(path, "/y.txt", MAX_PATH_LENGTH - strlen(path) - 1);
#else
strncat(path, "y.txt", MAX_PATH_LENGTH - strlen(path) - 1);
#endif
path[MAX_PATH_LENGTH - 1] = 0;
#endif
// open the resource file
file = fopen(path, "r");
if (!file) {
logError(instance, "Failed to open resource file %s.", path);
return Discard;
}
// read the first character
c = (char)fgetc(file);
// assign it to y
M(y) = c;
// close the file
fclose(file);
instance->isDirtyValues = false;
return OK;
}
Status getVariableSize(ModelInstance *instance, ValueReference vr, size_t *size) {
ASSERT_NOT_NULL(instance);
ASSERT_NOT_NULL(size);
switch (vr) {
case vr_time:
case vr_y:
*size = 1;
return OK;
default:
logError(instance, "Unknown value reference: %u.", vr);
return Discard;
}
}
Status getFloat64(ModelInstance *instance, ValueReference vr, double values[], size_t nValues) {
ASSERT_NOT_NULL(instance);
ASSERT_NOT_NULL(values);
ASSERT_SIZE_T(nValues, 1);
calculateValues(instance);
switch (vr) {
case vr_time:
values[0] = instance->time;
return OK;
default:
logError(instance, "Get Float64 is not allowed for value reference %u.", vr);
return Discard;
}
}
Status getInt32(ModelInstance *instance, ValueReference vr, int32_t values[], size_t nValues) {
ASSERT_NOT_NULL(instance);
ASSERT_NOT_NULL(values);
ASSERT_SIZE_T(nValues, 1);
calculateValues(instance);
switch (vr) {
case vr_y:
values[0] = M(y);
return OK;
default:
logError(instance, "Get Int32 is not allowed for value reference %u.", vr);
return Discard;
}
}