-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileContentsServerDriver.cpp
More file actions
318 lines (277 loc) · 8.87 KB
/
Copy pathFileContentsServerDriver.cpp
File metadata and controls
318 lines (277 loc) · 8.87 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <math.h>
#include <exception>
#include <iostream>
#include <map>
#include <list>
#include <vector>
#include <iomanip>
#include <fstream>
#include <sys/timeb.h>
#include <sstream>
#include <epicsTypes.h>
#include <epicsTime.h>
#include <epicsThread.h>
#include <epicsString.h>
#include <epicsTimer.h>
#include <epicsMutex.h>
#include <epicsEvent.h>
#include <shareLib.h>
#include <iocsh.h>
#include <macLib.h>
#include <epicsGuard.h>
#include "pcrecpp.h"
#include "envDefs.h"
#include "errlog.h"
#include "utilities.h"
#include "FileContentsServerDriver.h"
#include <epicsExport.h>
static const char *driverName = "FileContentsServerDriver";
FileContentsServerDriver::FileContentsServerDriver(const char *portName, const char *fileDir, const char *inputFile, const char *outputFile)
: asynPortDriver(portName,
0, /* maxAddr */
asynInt32Mask | asynInt32ArrayMask | asynFloat64Mask | asynFloat64ArrayMask | asynOctetMask | asynDrvUserMask, /* Interface mask */
asynInt32Mask | asynInt32ArrayMask | asynFloat64Mask | asynFloat64ArrayMask | asynOctetMask, /* Interrupt mask */
ASYN_CANBLOCK, /* asynFlags. This driver can block but it is not multi-device */
1, /* Autoconnect */
0, /* Default priority */
0), /* Default stack size*/
m_fileDir(fileDir)
{
const char *functionName = "FileContentsServerDriver";
createParam(P_inputFileNameString, asynParamOctet, &P_inputFileName);
createParam(P_outputFileNameString, asynParamOctet, &P_outputFileName);
createParam(P_fileContentsString, asynParamOctet, &P_fileContents);
createParam(P_saveFileString, asynParamInt32, &P_saveFile);
createParam(P_resetString, asynParamInt32, &P_reset);
createParam(P_fileDirString, asynParamOctet, &P_fileDir);
createParam(P_logString, asynParamOctet, &P_log);
createParam(P_unsavedChangesString, asynParamInt32, &P_unsavedChanges);
setStringParam(P_fileDir, fileDir);
setStringParam(P_inputFileName, inputFile);
// Read the original contents in first.
readFile();
if (outputFile && *outputFile)
{
setStringParam(P_outputFileName, outputFile);
// Save the file so that the output file has the contents of the input file initially.
saveFile();
}
else
{
// If the output file is not specified, default to saving output to the input file.
setStringParam(P_outputFileName, inputFile);
}
const int defaultSaveFile = 0;
setIntegerParam(P_saveFile, defaultSaveFile);
const int defaultReset = 0;
setIntegerParam(P_reset, defaultReset);
const int defaultUnsavedChanges = 0;
setIntegerParam(P_unsavedChanges, defaultUnsavedChanges);
setStringParam(P_log, "");
logMessage("Editor initialized");
callParamCallbacks();
}
asynStatus FileContentsServerDriver::saveFile()
{
logMessage("Triggering save");
std::string buffer;
// Get the value of the parameter we just set
getStringParam(P_fileContents, buffer);
std::string fileName;
getStringParam(P_outputFileName, fileName);
// join the file name with the file directory
std::string fullFileName = m_fileDir + "/" + fileName;
// Write the contents of buffer to a file
try
{
std::ofstream outfile(fullFileName, std::ios::trunc | std::ios::binary); // Open in write mode
if (outfile.is_open())
{
outfile << buffer;
outfile.close();
logMessage("File saved successfully");
setIntegerParam(P_saveFile, 0);
setIntegerParam(P_unsavedChanges, 0);
}
else
{
logMessage(std::string("Failed to open file (") + fullFileName + ") for writing");
return asynError;
}
}
catch (const std::exception &e)
{
logMessage(e.what());
return asynError;
}
callParamCallbacks();
return asynSuccess;
}
// Override the method to handle writes to parameters
asynStatus FileContentsServerDriver::writeInt32(asynUser *pasynUser, epicsInt32 value)
{
int hasChanged = 0;
getIntegerParam(P_unsavedChanges, &hasChanged);
if (pasynUser->reason == P_saveFile)
{
if (value == 1 && hasChanged == 1)
{
return saveFile();
}
return asynSuccess;
}
else if (pasynUser->reason == P_reset)
{
if (value == 1)
{
logMessage("Reloading from disk");
setStringParam(P_fileContents, m_original_lines_array);
saveFile();
logMessage("Reset successful.");
setIntegerParam(P_reset, 0);
setIntegerParam(P_unsavedChanges, 0);
callParamCallbacks();
}
return asynSuccess;
}
else
{
return asynPortDriver::writeInt32(pasynUser, value);
}
}
void FileContentsServerDriver::logMessage(const std::string &message)
{
// add the time to the front of the log message
// current time
// Get the current time
time_t now = time(0);
tm *ltm = localtime(&now);
// Format the time into a string (e.g., "2024-08-15 12:34:56")
char time_str[20];
strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", ltm);
// Create the log message
std::string log_message = std::string(time_str) + ": " + message;
std::cout << log_message << std::endl;
setStringParam(P_log, log_message);
callParamCallbacks();
}
void FileContentsServerDriver::readFile()
{
std::cout << "Calling readFile" << std::endl;
std::ifstream f;
std::string fileName;
getStringParam(P_inputFileName, fileName);
std::string fullFileName = m_fileDir + "/" + fileName;
logMessage(std::string("Reading file ") + fullFileName);
try
{
f.open(fullFileName, std::ios::binary);
if (!f.is_open())
{
m_original_lines_array = "";
setStringParam(P_fileContents, "");
if (errno == ENOENT) // File not found
{
logMessage(std::string("File not found: ") + fullFileName);
throw std::runtime_error("File not found");
}
else // Other errors, e.g., permission denied
{
logMessage(std::string("Unable to open file: ") + fullFileName + ", error " + std::string(strerror(errno)));
throw std::runtime_error(std::string("Unable to open file: ") + std::string(strerror(errno)));
}
}
int i = 0;
std::stringstream buffer;
buffer << f.rdbuf();
setStringParam(P_fileContents, buffer.str());
m_original_lines_array = buffer.str();
setIntegerParam(P_unsavedChanges, 0);
logMessage("File read successfully");
callParamCallbacks();
}
catch (const std::exception &e)
{
std::cerr << "Exception caught: " << e.what() << std::endl;
logMessage(e.what());
}
callParamCallbacks();
}
asynStatus FileContentsServerDriver::writeOctet(asynUser *pasynUser, const char *value, size_t maxChars, size_t *nActual)
{
int function = pasynUser->reason; // Function to call
const char *functionName = "writeOctet";
const char *paramName = NULL;
getParamName(function, ¶mName);
std::cout << "writeOctet " << paramName << std::endl;
if (function == P_fileContents)
{
setStringParam(P_fileContents, value);
if (m_original_lines_array != value)
{
setIntegerParam(P_unsavedChanges, 1);
}
else
{
setIntegerParam(P_unsavedChanges, 0);
}
}
else
{
return asynPortDriver::writeOctet(pasynUser, value, maxChars, nActual);
}
// Set nActual to the length of the string that was written
*nActual = strlen(value);
// Update the parameter
callParamCallbacks();
return asynSuccess;
}
extern "C"
{
/// \param[in] portName @copydoc initArg0
/// \param[in] fileDir @copydoc initArg1
int FileContentsServerConfigure(const char *portName, const char *fileDir, const char *inputFile, const char *outputFile)
{
try
{
FileContentsServerDriver *driver = new FileContentsServerDriver(portName, fileDir, inputFile, outputFile);
if (driver == NULL)
{
errlogSevPrintf(errlogMajor, "FileContentsServerConfigure failed (NULL)\n");
return (asynError);
}
else
{
return (asynSuccess);
}
}
catch (const std::exception &ex)
{
errlogSevPrintf(errlogMajor, "FileContentsServerConfigure failed: %s\n", ex.what());
return (asynError);
}
}
// EPICS iocsh shell commands
static const iocshArg initArg0 = {"portName", iocshArgString}; ///< The name of the asyn driver port we will create
static const iocshArg initArg1 = {"fileDir", iocshArgString}; ///< The directory to open/save files into
static const iocshArg initArg2 = {"inputFile", iocshArgString}; ///< input file name to read and initially serve contents of
static const iocshArg initArg3 = {"outputFile", iocshArgString}; ///< file name to save output to when a user hits save. If not set this defaults to inputFile.
static const iocshArg *const initArgs[] = {&initArg0,
&initArg1,
&initArg2,
&initArg3};
static const iocshFuncDef initFuncDef = {"FileContentsServerConfigure", sizeof(initArgs) / sizeof(iocshArg *), initArgs};
static void initCallFunc(const iocshArgBuf *args)
{
FileContentsServerConfigure(args[0].sval, args[1].sval, args[2].sval, args[3].sval);
}
static void FileContentsServerRegister(void)
{
iocshRegister(&initFuncDef, initCallFunc);
}
epicsExportRegistrar(FileContentsServerRegister);
}