-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calvin Command Reader.ipf
executable file
·705 lines (648 loc) · 20.3 KB
/
Calvin Command Reader.ipf
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
#pragma TextEncoding = "UTF-8"
#pragma rtGlobals=3 // Use modern global access method and strict wave access.
//-----------------------------------------------------------------------------------
//
// Calvin — Online Instruction in Acid-Base Chemistry
// © 2020, Melissa A. Hines
// Melissa.Hines@cornell.edu
//
//-----------------------------------------------------------------------------------
Function InitCommandReader()
DFREF savedDF= GetDataFolderDFR()
NewDataFolder/o root:MAH
SetDataFolder root:MAH
make/n=0/o/t validCommands
wave/t validCommands
InsertCommand("Make_New_25ml_Volumetric_Flask")
InsertCommand("Make_New_50ml_Volumetric_Flask")
InsertCommand("Fill_Volumetric_Flask_with_H2O")
InsertCommand("Make_New_Test_Tube")
InsertCommand("Make_New_100ml_Beaker")
InsertCommand("Fill_Empty_100ml_Beaker_with_Standardized_Base")
InsertCommand("Fill_Empty_100ml_Beaker_with_Standardized_Acid")
InsertCommand("Fill_Empty_100ml_Beaker_with_Standardized_KSCN")
InsertCommand("Fill_Empty_100ml_Beaker_with_Standardized_Ferric_Nitrate")
InsertCommand("Fill_Empty_100ml_Beaker_with_Standardized_Crystal_Violet")
InsertCommand("Fill_Empty_100ml_Beaker_with_H2O")
InsertCommand("Fill_Empty_100ml_Beaker_with_Standardized_Buffer")
InsertCommand("Fill_Empty_100ml_Beaker_with_Unknown_Buffer")
InsertCommand("Transfer_Soln_with_5ml_Pipette")
InsertCommand("Transfer_Soln_with_10ml_Pipette")
InsertCommand("Transfer_Soln_with_20ml_Pipette")
InsertCommand("Transfer_Soln_with_Graduated_Cylinder")
InsertCommand("Fill_50ml_Buret")
InsertCommand("Add_Soln_from_Buret")
InsertCommand("Read_Buret_Volume")
InsertCommand("Add_Solid_Acid_to_Vessel")
InsertCommand("Add_One_Drop_of_Indicator")
InsertCommand("Measure_pH")
InsertCommand("Observe_Color")
InsertCommand("Observe_Color_Range")
InsertCommand("Titrate_Beaker_from_Buret_until_Color_Change")
InsertCommand("Take_Spectrum")
InsertCommand("Set_Spectrometer_Temperature")
InsertCommand("Measure_Absorbance_Every_2_Min")
InsertCommand("Clean_and_Dry")
InsertCommand("Verbose_Reporting_On")
InsertCommand("Verbose_Reporting_Off")
InsertCommand("Observe_Volume")
InsertCommand("Set_Group_Name")
InsertCommand("Set_TA_Name")
SetDataFolder savedDF
End
Function InsertCommand(cmd)
string cmd
wave/t validCommands = root:MAH:validCommands
InsertPoints 0, 1, validCommands
validCommands[0] = cmd
End
Function StartWatchingFolder()
Variable numTicks = 2 * 60 // Run every 2 seconds (120 ticks)
variable/g root:MAH:gFilesProcessed = 0, root:MAH:gCalvinWorking = 0
CtrlNamedBackground WatchFolder, period=numTicks, proc=ExamineFolder
CtrlNamedBackground WatchFolder, start
End
Function StopWatchingFolder()
CtrlNamedBackground WatchFolder, stop
End
Function ExamineFolder(s)
STRUCT WMBackgroundStruct &s
NVAR calvinWorking = root:MAH:gCalvinWorking
if(calvinWorking == 0)
doExamineFolder()
endif
return 0
End
Function doExamineFolder()
string theList, nextFile, tempStr
variable numToProcess = 0, err
NVAR filesProcessed = root:MAH:gFilesProcessed
NVAR calvinWorking = root:MAH:gCalvinWorking
calvinWorking = 1
theList = IndexedFile(watchedFolder, -1, "????")
theList = RemoveFromList(".DS_Store", theList, ";", 0)
numToProcess = ItemsInList(theList, ";")
if(numToProcess == 0)
calvinWorking = 0
return 0
endif
do
nextFile = StringFromList(numToProcess - 1, theList)
GetFileFolderInfo/P=watchedFolder/Z/Q nextFile // Does the file exist
if(V_flag != 0) // No, it does not. Assume there is a file with one or more ";" in the filename
do
numToProcess -= 1
tempStr = nextFile
nextFile = StringFromList(numToProcess - 1, theList) + ";" + tempStr
GetFileFolderInfo/P=watchedFolder/Z/Q nextFile
while((V_flag != 0) && numToProcess > -0.5)
endif
if(V_flag == 0)
err = RunCommandFile(nextFile)
if(err > -9)
DeleteFile/P=watchedFolder nextFile
numToProcess -= 1
filesProcessed += 1
print "Processed file " + nextFile + ". Total files processed = " + num2istr(filesProcessed)
endif
else
print "There is an unexpected error in StartWatchingFolder."
endif
while(numToProcess > 0)
calvinWorking = 0
End
Function RunCommandFile(fileName)
string fileName
string buffer, cmd, igorCmd, outputFilename, theNote, cwd, cloudPath,tempPath
variable refNum, len, dotPosition
NVAR gVerboseReporting = root:MAH:gVerboseReporting, gTANumber = root:MAH:gTAnumber
NVAR gGroupNumber = root:MAH:gGroupNumber, gMaxCommands = root:MAH:gMaxCommands
SVAR gCmdFileName = root:MAH:gCmdFileName, gRunTime = root:MAH:gRunTime
// Strip extension from fileName
dotPosition = StrSearch(fileName, ".", 0)
if(dotPosition > -1)
gCmdFileName = fileName[0, dotPosition - 1]
else
gCmdFileName = fileName
endif
NewExperiments() // Closes any open files
// Occassionally Box drops the connection if we try to process the file on Box. To stop this, first copy
// the file to disk, then process.
PathInfo tempFolder
CopyFile/D/P=watchedFolder/O/Z filename as S_path
if(V_flag != 0)
Print "Couldn't copy " + filename + " from remote server."
return -10
endif
Open/P=tempFolder/R refNum as fileName
string fullPath = S_fileName
variable ii = 0, numCommands = 0
do
FReadLine refNum, buffer
if(strlen(buffer) == 0)
break
endif
if (CmpStr(buffer[len-1],"\r") != 0) // Last line has no CR ?
buffer += "\r"
endif
ii += 1
// Echo lines starting with / to the Notebook, but don't process
if(strsearch(buffer, "/", 0) != 0)
cmd = CleanCommandLine(buffer)
if(strlen(cmd) > 0) // Got a command
igorCmd = RemovePrompts(cmd)
igorCmd = ValidatedIgorCommand(igorCmd)
if(strlen(igorCmd) > 0)
if(gVerboseReporting)
WriteCommandToLog(cmd)
endif
Execute/Z igorCmd
numCommands += 1
else
WriteSimpleErrorToLog(kBadCommand, cmd)
endif
endif
else
if(gVerboseReporting)
buffer = ReplaceString("\r", buffer, "")
buffer += "\r"
WriteCommentToLog(buffer)
endif
endif
while(ii < 500 && numCommands < gMaxCommands)
// Close the command file and remove local copy
Close refNum
DeleteFile/P=tempFolder/Z fileName
if(numCommands == gMaxCommands)
WriteSimpleErrorToLog(kTooManyCommands, num2istr(gMaxCommands))
endif
if(numCommands < 1) // The file was probably not text, and nothing useful came out
return -1
endif
// Save the experiment
wave/t Group_Names = root:MAH:Group_Names
SVAR gBadGroupName = root:MAH:gBadGroupName, gBadTAName = root:MAH:gBadTAName
if(gGroupNumber < 0)
outputFileName = gBadGroupName + "_" + gCmdFileName + "_" + gRunTime
else
outputFileName = Group_Names[gGroupNumber] + "_" + gCmdFileName + "_" + gRunTime
endif
outputFileName = StripBadCharacters(outputFileName)
theNote = "Calvin command file: " + fileName + "\r"
theNote += "Output file: " + outputFilename + "\r"
Notebook Experiment_Log selection={startOfFile, startOfFile}
Notebook Experiment_Log fStyle = 1, fSize = 13
Notebook Experiment_Log ruler=headerRuler
Notebook Experiment_Log textRGB = (0, 0, 0)
Notebook Experiment_Log text=theNote
SaveNotebook/P=tempFolder/O/S=4 Experiment_Log as "Experiment_Log.rtf"
PathInfo tempFolder
string newFolder = ParseFilePath(1, S_path, ":", 1,0) + outputFileName
CopyFolder/i=0/o S_path as newFolder
// Zip the folder, then delete the unzipped
PathInfo tempFolder
cwd = ParseFilePath(9, ParseFilePath(1, S_path, ":", 1, 0), "*", 0, 0)
cmd = "cd '" + cwd + "';zip -r -X '" + outputFileName + ".zip' '" + outputFileName + "'"
ExecuteUnixShellCommand(cmd)
cmd = "cd '" + cwd + "';rm -rf '" + outputFileName + "'"
ExecuteUnixShellCommand(cmd)
// Copy the zipped file to Box
wave/t TA_Names = root:MAH:TA_Names
pathInfo cloudFolder
if(gTAnumber < 0)
cloudPath = ParseFilePath(9, S_path, ":", 1,0) + "_Lost&Found" + "/"
else
cloudPath = ParseFilePath(9, S_path, ":", 1,0) + TA_Names[gTAnumber] + "/"
endif
if(gGroupNumber < 0)
cloudPath += "_Lost&Found" + "/"
else
cloudPath += Group_names[gGroupNumber] + "/"
endif
// Check to make sure no one renamed the folder
if(CheckPath(cloudPath) < 0)
print "The path " + cloudPath + " does not exist."
print "ERROR! ERROR! ERROR! ERROR! ERROR! ERROR! ERROR! ERROR!"
print "ERROR! ERROR! ERROR! ERROR! ERROR! ERROR! ERROR! ERROR!"
print "ERROR! ERROR! ERROR! ERROR! ERROR! ERROR! ERROR! ERROR!"
else
cmd = "cd '" + cwd + "';mv '" + outputFileName + ".zip' '" + cloudPath + "'"
ExecuteUnixShellCommand(cmd)
endif
return 1
End
Function/S StripBadCharacters(fileName)
string fileName
fileName = ReplaceString(":", fileName, "_")
fileName = ReplaceString("/", fileName, "_")
fileName = ReplaceString("\\", fileName, "_")
fileName = ReplaceString(" ", fileName, "_")
fileName = ReplaceString("?", fileName, "_")
fileName = ReplaceString("%", fileName, "_")
fileName = ReplaceString("*", fileName, "_")
fileName = ReplaceString("|", fileName, "_")
fileName = ReplaceString("\"", fileName, "_")
fileName = ReplaceString("<", fileName, "_")
fileName = ReplaceString(">", fileName, "_")
fileName = ReplaceString("'", fileName, "") // Added to fix problems with Beer's Law
return fileName
End
Function MakeAllTAFolders()
variable ii, jj
string cloudPath, theTApath, theGroupPath, cmd
wave/t TA_Names = root:MAH:TA_Names, Group_Names = root:MAH:Group_Names
pathInfo cloudFolder
cloudPath = ParseFilePath(9, S_path, ":", 1,0)
for(ii = 0; ii < numpnts(TA_Names); ii += 1)
theTApath = cloudPath + TA_Names[ii] + "/"
cmd = "mkdir -p '" + theTApath + "'"
ExecuteUnixShellCommand(cmd)
for(jj = 0; jj < numpnts(Group_Names); jj += 1)
theGroupPath = theTApath + Group_Names[jj] + "/"
cmd = "mkdir -p '" + theGroupPath + "'"
ExecuteUnixShellCommand(cmd)
endfor
theGroupPath = theTApath + "_Lost&Found" + "/"
cmd = "mkdir -p '" + theGroupPath + "'"
ExecuteUnixShellCommand(cmd)
endfor
theTApath = cloudPath + "_Lost&Found" + "/"
cmd = "mkdir -p '" + theTApath + "'"
ExecuteUnixShellCommand(cmd)
for(jj = 0; jj < numpnts(Group_Names); jj += 1)
theGroupPath = theTApath + Group_Names[jj] + "/"
cmd = "mkdir -p '" + theGroupPath + "'"
ExecuteUnixShellCommand(cmd)
endfor
theGroupPath = theTApath + "_Lost&Found" + "/"
cmd = "mkdir -p '" + theGroupPath + "'"
ExecuteUnixShellCommand(cmd)
End
Function/S ExecuteUnixShellCommand(unixCmd)
String unixCmd
// Paths must be POSIX paths (using /).
// Paths containing spaces or other nonstandard characters
// must be single-quoted.
String igorCmd
sprintf igorCmd, "do shell script \"%s\"", unixCmd
// Print igorCmd // For debugging only
ExecuteScriptText/UNQ/Z igorCmd
if(V_flag != 0)
print "Command: " + igorCmd
print "EUSC Error: " + S_value
print "ERROR! ERROR! ERROR! ERROR! ERROR! ERROR! ERROR! ERROR!"
print "ERROR! ERROR! ERROR! ERROR! ERROR! ERROR! ERROR! ERROR!"
print "ERROR! ERROR! ERROR! ERROR! ERROR! ERROR! ERROR! ERROR!"
endif
// Print S_value // For debugging only
return S_value
End
Function CheckPath(unixCmd)
String unixCmd
// Paths must be POSIX paths (using /).
// Paths containing spaces or other nonstandard characters
// must be single-quoted.
String igorCmd
sprintf igorCmd, "do shell script \"ls '%s'\"", unixCmd
ExecuteScriptText/UNQ/Z igorCmd
if(V_flag != 0)
print "Command: " + igorCmd
print "Check Path Error: " + S_value
print "ERROR! ERROR! ERROR! ERROR! ERROR! ERROR! ERROR! ERROR!"
print "ERROR! ERROR! ERROR! ERROR! ERROR! ERROR! ERROR! ERROR!"
print "ERROR! ERROR! ERROR! ERROR! ERROR! ERROR! ERROR! ERROR!"
return -1
else
return 1
endif
End
Function/S CleanCommandLine(buffer)
string buffer
variable openParenPos, closeParenPos, ii
string cmd, doubleQuote = "\"", tab = "\t"
wave/t validCommands = root:MAH:validCommands
buffer = TrimString(buffer, 1) // Remove leading and trailing whitespace
if(strlen(buffer) < 1)
// WriteBlankLineToLog()
return ""
endif
closeParenPos = strsearch(buffer,")",0)
openParenPos = strsearch(buffer, "(", 0)
if((closeParenPos < 0) || (openParenPos < 0))
WriteSimpleErrorToLog(kBadCommand, buffer)
return ""
endif
cmd = buffer[0, openParenPos - 1]
for(ii = 0; ii < numpnts(validCommands);ii = ii + 1)
if(CmpStr(cmd, validCommands[ii], 0) == 0)
break
endif
endfor
if(ii == numpnts(validCommands))
WriteSimpleErrorToLog(kBadCommand, cmd)
return ""
endif
// Truncate everything after the closing paren
cmd = buffer[0, closeParenPos]
// Get rid of curly quotes and other troublemakers
cmd = ReplaceString("“", cmd, doubleQuote)
cmd = ReplaceString("”", cmd, doubleQuote)
cmd = ReplaceString("‘", cmd, doubleQuote)
cmd = ReplaceString("’", cmd, doubleQuote)
cmd = ReplaceString("'", cmd, doubleQuote)
cmd = ReplaceString("—", cmd, "-")
cmd = ReplaceString("bromcresol_green", cmd, "bromocresol_green")
// Get rid of any spaces before semicolons
if(strsearch(cmd, " :", 0) > 0)
do
cmd = ReplaceString(" :", cmd, ":")
while(strsearch(cmd, " :", 0) > 0)
endif
// Add a CR
cmd += "\r"
return cmd
End
Function/S RemovePrompts(cmd)
string cmd
string igorCmd
igorCmd = ReplaceString("indicator:", cmd, "")
igorCmd = ReplaceString("newName:", igorCmd, "")
igorCmd = ReplaceString("buffer:", igorCmd, "")
igorCmd = ReplaceString("grams:", igorCmd, "")
igorCmd = ReplaceString("name:", igorCmd, "")
igorCmd = ReplaceString("into:", igorCmd, "")
igorCmd = ReplaceString("from:", igorCmd, "")
igorCmd = ReplaceString("acid:", igorCmd, "")
igorCmd = ReplaceString("degC:", igorCmd, "")
igorCmd = ReplaceString("min:", igorCmd, "")
igorCmd = ReplaceString("nm:", igorCmd, "")
igorCmd = ReplaceString("of:", igorCmd, "")
igorCmd = ReplaceString("to:", igorCmd, "")
igorCmd = ReplaceString("mL:", igorCmd, "")
igorCmd = ReplaceString("at:", igorCmd, "")
return igorCmd
End
Function/S ValidatedIgorCommand(line)
string line
variable openParenPos, closeParenPos, err = 0
string cmd, args, cleanArgs
NVAR gSolidAcidsEnabled = root:MAH:gSolidAcidsEnabled
NVAR gBuffersEnabled = root:MAH:gBuffersEnabled
closeParenPos = strsearch(line,")",0)
openParenPos = strsearch(line, "(", 0)
cmd = line[0, openParenPos - 1]
args = line[openParenPos + 1, closeParenPos - 1]
args = RemoveSpacesInStrings(args)
// Check if experiment is running today
if(!gSolidAcidsEnabled)
if(CmpStr(cmd, "Add_Solid_Acid_to_Vessel", 0) == 0)
WriteSimpleErrorToLog(kNoSolidAcids, "")
return ""
endif
endif
if(!gBuffersEnabled)
if(CmpStr(cmd, "Fill_Empty_100ml_Beaker_with_Standardized_Buffer", 0) == 0)
WriteSimpleErrorToLog(kNoBuffers, "")
return ""
endif
if(CmpStr(cmd, "Fill_Empty_100ml_Beaker_with_Unknown_Buffer", 0) == 0)
WriteSimpleErrorToLog(kNoBuffers, "")
return ""
endif
endif
strswitch(cmd)
case "Read_Buret_Volume":
case "Verbose_Reporting_On":
case "Verbose_Reporting_Off":
err = (NumArgs(args) == 0) ? 0 : -1
cleanArgs = ""
break
case "Make_New_25ml_Volumetric_Flask":
case "Make_New_50ml_Volumetric_Flask":
case "Fill_Volumetric_Flask_with_H2O":
case "Make_New_Test_Tube":
case "Make_New_100ml_Beaker":
case "Fill_Empty_100ml_Beaker_with_Standardized_Base":
case "Fill_Empty_100ml_Beaker_with_Standardized_Acid":
case "Fill_Empty_100ml_Beaker_with_Standardized_KSCN":
case "Fill_Empty_100ml_Beaker_with_Standardized_Ferric_Nitrate":
case "Fill_Empty_100ml_Beaker_with_Standardized_Crystal_Violet":
case "Fill_Empty_100ml_Beaker_with_Standardized_Buffer":
case "Fill_Empty_100ml_Beaker_with_H2O":
case "Fill_50ml_Buret":
case "Measure_pH":
case "Observe_Color":
case "Take_Spectrum":
case "Titrate_Beaker_from_Buret_until_Color_Change":
case "Clean_and_Dry":
case "Observe_Volume":
case "Set_Group_Name":
case "Set_TA_Name":
if(NumArgs(args) == 1)
cleanArgs = ValidateStr(args)
err = strlen(cleanArgs) == 0 ? -1 : 0
else
err = -1
endif
break
case "Transfer_Soln_with_5ml_Pipette":
case "Transfer_Soln_with_10ml_Pipette":
case "Transfer_Soln_with_20ml_Pipette":
case "Fill_Empty_100ml_Beaker_with_Unknown_Buffer":
case "Add_One_Drop_of_Indicator":
if(NumArgs(args) == 2)
cleanArgs = ValidateStrStr(args)
err = strlen(cleanArgs) == 0 ? -1 : 0
else
err = -1
endif
break
case "Transfer_Soln_with_Graduated_Cylinder":
case "Add_Solid_Acid_to_Vessel":
if(NumArgs(args) == 3)
cleanArgs = ValidateNumStrStr(args)
err = strlen(cleanArgs) == 0 ? -1 : 0
else
err = -1
endif
break
case "Add_Soln_from_Buret":
if(NumArgs(args) == 2)
cleanArgs = ValidateNumStr(args)
err = strlen(cleanArgs) == 0 ? -1 : 0
else
err = -1
endif
break
case "Set_Spectrometer_Temperature":
if(NumArgs(args) == 1)
cleanArgs = ValidateNum(args)
err = strlen(cleanArgs) == 0 ? -1 : 0
else
err = -1
endif
break
case "Measure_Absorbance_Every_2_Min":
if(NumArgs(args) == 3)
cleanArgs = ValidateStrNumNum(args)
err = strlen(cleanArgs) == 0 ? -1 : 0
else
err = -1
endif
break
case "Observe_Color_Range":
if(NumArgs(args) == 2)
cleanArgs = ValidateStrNum(args)
err = strlen(cleanArgs) == 0 ? -1 : 0
else
err = -1
endif
break
endswitch
if(err < 0)
return ""
else
return cmd + "(" + cleanArgs + ")\r"
endif
End
Function/S ValidateStr(arg)
string arg
string str1
arg = ReplaceString(",", arg, " ")
arg = ReplaceString("\"", arg, " ")
sscanf arg, "%s", str1
if(V_flag == 1 && !CheckIfNum(str1))
if(CheckIfValidStr(str1))
return "\"" + str1 + "\""
endif
endif
return ""
End
Function CheckIfValidStr(arg) // returns 1 if valid, 0 if not
// Only allow strings that start with a letter, then contain letters, numbers, and _
// This is not so much for Igor compatibility as for filename and Box compatibility
// Don't allow strings that conflict with Igor commands
string arg
if(GrepString(arg,"([A-Za-z]+[A-Za-z0-9_]+)") ? 1 : 0)
if(exists(arg) > 1)
WriteSimpleErrorToLog(kConflictingString, arg)
return 0
else
return 1
endif
else
return 0
endif
End
Function CheckIfNum(arg) // 1 if number, 0 if not
string arg
variable num
sscanf arg, "%f", num
return V_flag
End
Function/S RemoveSpacesInStrings(arg)
string arg
variable ii, endString, inString
endString = strlen(arg)
if(endString < 1)
return ""
else
inString = 0
ii = 0
do
if(CmpStr(arg[ii], "\"") == 0)
inString = (inString == 0) ? 1 : 0
elseif(inString > 0 && CmpStr(arg[ii], " ") == 0)
arg = arg[0, ii-1] + "_" + arg[ii+1,endString - 1]
endif
ii += 1
while(ii < endString)
return arg
endif
End
Function/S ValidateStrStr(arg)
string arg
string str1, str2
arg = ReplaceString(",", arg, " ")
arg = ReplaceString("\"", arg, " ")
sscanf arg, "%s %s", str1, str2
if(V_flag == 2 && !CheckIfNum(str1) && !CheckIfNum(str2))
if(CheckIfValidStr(str1) && CheckIfValidStr(str2))
return "\"" + str1 + "\", \"" + str2 + "\""
endif
endif
return ""
End
Function/S ValidateNumStrStr(arg)
string arg
string str1, str2
variable num
arg = ReplaceString(",", arg, " ")
arg = ReplaceString("\"", arg, " ")
sscanf arg, "%f %s %s", num, str1, str2
if(V_flag == 3 && !CheckIfNum(str1) && !CheckIfNum(str2))
if(CheckIfValidStr(str1) && CheckIfValidStr(str2))
return num2str(num) + ", \"" + str1 + "\", \"" + str2 + "\""
endif
endif
return ""
End
Function/S ValidateNumStr(arg)
string arg
string str1
variable num
arg = ReplaceString(",", arg, " ")
arg = ReplaceString("\"", arg, " ")
sscanf arg, "%f %s", num, str1
if(V_flag == 2 && !CheckIfNum(str1))
if(CheckIfValidStr(str1))
return num2str(num) + ", \"" + str1 + "\""
endif
endif
return ""
End
Function/S ValidateNum(arg)
string arg
variable num
sscanf arg, "%f", num
if(V_flag == 1)
return num2str(num)
endif
return ""
End
Function/S ValidateStrNumNum(arg)
string arg
string str
variable num1, num2
arg = ReplaceString(",", arg, " ")
arg = ReplaceString("\"", arg, " ")
sscanf arg, "%s %f %f", str, num1, num2
if(V_flag == 3 && !CheckIfNum(str))
if(CheckIfValidStr(str))
return "\"" + str + "\", " + num2str(num1) + ", " + num2str(num2)
endif
endif
return ""
End
Function/S ValidateStrNum(arg)
string arg
string str1
variable num
arg = ReplaceString(",", arg, " ")
arg = ReplaceString("\"", arg, " ")
sscanf arg, "%s %f", str1, num
if(V_flag == 2 && !CheckIfNum(str1))
if(CheckIfValidStr(str1))
return "\"" + str1 + "\", " + num2str(num)
endif
endif
return ""
End
Function NumArgs(arg)
string arg
string str1, str2, str3, str4
arg = ReplaceString(",", arg, " ")
arg = ReplaceString("\"", arg, " ")
sscanf arg, "%s %s %s %s", str1, str2, str3, str4
return V_flag
End