-
Notifications
You must be signed in to change notification settings - Fork 0
/
mql.c
171 lines (109 loc) · 2.94 KB
/
mql.c
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "mql.h"
#define LINEBUFFERSIZE 65536
#define DEFAULT_OUTPUT_SIZE 5242880
#define DEFAULT_ERROR_SIZE 1048576
int MQL_output_size = DEFAULT_OUTPUT_SIZE;
int MQL_error_size = DEFAULT_ERROR_SIZE;
int current_session;
int have_output;
int have_error;
int MQL_Open(char *path, char *exe, char *bootstrap) {
int session;
session = mqlOpen2(path, exe, bootstrap);
return session;
}
int MQL_Close(int session) {
return mqlClose2(session);
}
int MQL_OpenLog(char *path) {
return mqlOpenLog(path);
}
int MQL_CloseLog(void) {
return mqlCloseLog();
}
int MQL_Execute(int session, char *mql)
{
int rc;
have_output = 0;
have_error = 0;
MQL_SetBuffers(MQL_output_size, MQL_error_size);
current_session = session;
rc = mqlExecute2(session, mql);
if(rc != MQLERROR) {
rc = mqlCallback2(session,
outputCallback, NULL,
errorCallback, NULL);
}
return rc;
}
int outputCallback(void *outData)
{
char buffer[LINEBUFFERSIZE] = "";
int rc = 0;
if(mqlOutput2(current_session, buffer, LINEBUFFERSIZE)) {
if(MQL_output_size < (strlen(MQL_output) + LINEBUFFERSIZE)) {
printf("ERROR: MQL output buffer too small - %d\n",
MQL_output_size);
return MQLERROR;
}
if(! have_output) {
strcpy(MQL_output, buffer);
have_output = 1;
}
else {
strcat(MQL_output, buffer);
}
}
return MQLOK;
}
int errorCallback(void *errData)
{
char buffer[LINEBUFFERSIZE] = "";
if(mqlError2(current_session, buffer, LINEBUFFERSIZE)) {
if(MQL_error_size < (strlen(MQL_error) + LINEBUFFERSIZE)) {
printf("ERROR: MQL error buffer too small - %d\n",
MQL_error_size);
return MQLERROR;
}
if(! have_error) {
strcpy(MQL_error, buffer);
have_error = 1;
}
else {
strcat(MQL_error, buffer);
}
}
return MQLOK;
}
void MQL_SetBuffers(int output_size, int error_size)
{
if(! MQL_output || (MQL_output_size != output_size)) {
MQL_output_size = output_size;
if(MQL_output)
free(MQL_output);
MQL_output = (char *)malloc(sizeof(char) * MQL_output_size);
}
if(! MQL_error || (MQL_error_size != error_size)) {
MQL_error_size = error_size;
if(MQL_error)
free(MQL_error);
MQL_error = (char *)malloc(sizeof(char) * MQL_error_size);
}
MQL_ClearBuffers();
}
void MQL_ClearBuffers(void)
{
memset(MQL_output, 0, MQL_output_size);
memset(MQL_error, 0, MQL_error_size);
}
void MQL_SetBufferSize(int output_size, int error_size)
{
if(output_size > 0)
MQL_output_size = output_size;
else
MQL_output_size = DEFAULT_OUTPUT_SIZE;
if(error_size > 0)
MQL_error_size = error_size;
else
MQL_error_size = DEFAULT_ERROR_SIZE;
}