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
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
|
/*
* FileInfoParser.cpp
*
* Created on: 31 Mar 2018
* Author: David
*/
#include "FileInfoParser.h"
#include <Platform/OutputMemory.h>
#include <Platform/RepRap.h>
#include <Platform/Platform.h>
#include <PrintMonitor/PrintMonitor.h>
#include <GCodes/GCodes.h>
#if HAS_MASS_STORAGE || HAS_EMBEDDED_FILES
FileInfoParser::FileInfoParser() noexcept
: parseState(notParsing), fileBeingParsed(nullptr), accumulatedParseTime(0), accumulatedReadTime(0), accumulatedSeekTime(0), fileOverlapLength(0)
{
parsedFileInfo.Init();
parserMutex.Create("FileInfoParser");
}
// This following method needs to be called repeatedly until it returns true - this may take a few runs
GCodeResult FileInfoParser::GetFileInfo(const char *filePath, GCodeFileInfo& info, bool quitEarly) noexcept
{
MutexLocker lock(parserMutex, MAX_FILEINFO_PROCESS_TIME);
if (!lock.IsAcquired())
{
return GCodeResult::notFinished;
}
if (parseState != notParsing && !StringEqualsIgnoreCase(filePath, filenameBeingParsed.c_str()))
{
// We are already parsing a different file
if (millis() - lastFileParseTime < MaxFileParseInterval)
{
return GCodeResult::notFinished; // try again later
}
// Time this client out because it has probably disconnected
fileBeingParsed->Close();
parseState = notParsing;
}
if (parseState == notParsing)
{
// See if we can access the file
// Webserver may call rr_fileinfo for a directory, check this case here
if (MassStorage::DirectoryExists(filePath))
{
info.isValid = false;
return GCodeResult::ok;
}
fileBeingParsed = MassStorage::OpenFile(filePath, OpenMode::read, 0);
if (fileBeingParsed == nullptr)
{
// Something went wrong - we cannot open it
info.isValid = false;
return GCodeResult::error;
}
// File has been opened, let's start now
filenameBeingParsed.copy(filePath);
fileOverlapLength = 0;
// Set up the info struct
parsedFileInfo.Init();
parsedFileInfo.fileSize = fileBeingParsed->Length();
#if HAS_MASS_STORAGE
parsedFileInfo.lastModifiedTime = MassStorage::GetLastModifiedTime(filePath);
#endif
parsedFileInfo.isValid = true;
// Record some debug values here
if (reprap.Debug(modulePrintMonitor))
{
accumulatedReadTime = accumulatedParseTime = 0;
reprap.GetPlatform().MessageF(UsbMessage, "-- Parsing file %s --\n", filePath);
}
// If the file is empty or not a G-Code file, we don't need to parse anything
constexpr const char *GcodeFileExtensions[] = { ".gcode", ".g", ".gco", ".gc", ".nc" };
bool isGcodeFile = false;
for (const char *ext : GcodeFileExtensions)
{
if (StringEndsWithIgnoreCase(filePath, ext))
{
isGcodeFile = true;
break;
}
}
if (fileBeingParsed->Length() == 0 || !isGcodeFile)
{
fileBeingParsed->Close();
parsedFileInfo.incomplete = false;
info = parsedFileInfo;
return GCodeResult::ok;
}
parseState = parsingHeader;
}
// Getting file information take a few runs. Speed it up when we are not printing by calling it several times.
const uint32_t loopStartTime = millis();
do
{
size_t sizeToRead, sizeToScan; // number of bytes we want to read and scan in this go
switch (parseState)
{
case parsingHeader:
{
bool headerInfoComplete = true;
// Read a chunk from the header. On the first run only process GCODE_READ_SIZE bytes, but use overlap next times.
sizeToRead = (size_t)min<FilePosition>(fileBeingParsed->Length() - fileBeingParsed->Position(), GCODE_READ_SIZE);
if (fileOverlapLength > 0)
{
sizeToScan = sizeToRead + fileOverlapLength;
}
else
{
sizeToScan = sizeToRead;
}
uint32_t startTime = millis();
const FilePosition bufferStartFileOffset = fileBeingParsed->Position() - fileOverlapLength;
const int nbytes = fileBeingParsed->Read(&buf[fileOverlapLength], sizeToRead);
if (nbytes != (int)sizeToRead)
{
reprap.GetPlatform().MessageF(WarningMessage, "Failed to read header of G-Code file \"%s\"\n", filePath);
parseState = notParsing;
fileBeingParsed->Close();
info = parsedFileInfo;
return GCodeResult::warning;
}
buf[sizeToScan] = 0;
// Record performance data
uint32_t now = millis();
accumulatedReadTime += now - startTime;
startTime = now;
// Search for filament usage (Cura puts it at the beginning of a G-code file)
if (parsedFileInfo.numFilaments == 0)
{
parsedFileInfo.numFilaments = FindFilamentUsed(buf);
headerInfoComplete &= (parsedFileInfo.numFilaments != 0);
}
// Look for layer height
if (parsedFileInfo.layerHeight == 0.0)
{
headerInfoComplete &= FindLayerHeight(buf);
}
// Look for slicer program
if (parsedFileInfo.generatedBy.IsEmpty())
{
headerInfoComplete &= FindSlicerInfo(buf);
}
// Look for print time
if (parsedFileInfo.printTime == 0)
{
headerInfoComplete &= FindPrintTime(buf);
}
// Look for thumbnail images
headerInfoComplete &= FindThumbnails(buf, bufferStartFileOffset);
// Keep track of the time stats
accumulatedParseTime += millis() - startTime;
// Can we proceed to the footer? Don't scan more than the first 4KB of the file
FilePosition pos = fileBeingParsed->Position();
if (headerInfoComplete || pos >= GCODE_HEADER_SIZE || pos == fileBeingParsed->Length())
{
// Yes - see if we need to output some debug info
if (reprap.Debug(modulePrintMonitor))
{
reprap.GetPlatform().MessageF(UsbMessage, "Header complete, processed %lu bytes, read time %.3fs, parse time %.3fs\n",
fileBeingParsed->Position(), (double)((float)accumulatedReadTime/1000.0), (double)((float)accumulatedParseTime/1000.0));
}
// Go to the last chunk and proceed from there on
const FilePosition seekFromEnd = ((fileBeingParsed->Length() - 1) % GCODE_READ_SIZE) + 1;
nextSeekPos = fileBeingParsed->Length() - seekFromEnd;
accumulatedSeekTime = accumulatedReadTime = accumulatedParseTime = 0;
fileOverlapLength = 0;
parseState = seeking;
}
else
{
// No - copy the last chunk of the buffer for overlapping search
fileOverlapLength = min<size_t>(sizeToRead, GCODE_OVERLAP_SIZE);
memcpy(buf, &buf[sizeToRead - fileOverlapLength], fileOverlapLength);
}
}
break;
case seeking:
// Seeking into a large file can take a long time using the FAT file system, so do it in stages
{
#if HAS_MASS_STORAGE
FilePosition currentPos = fileBeingParsed->Position();
const uint32_t clsize = fileBeingParsed->ClusterSize();
if (currentPos/clsize > nextSeekPos/clsize)
{
// Seeking backwards over a cluster boundary, so in practice the seek will start from the start of the file
currentPos = 0;
}
// Seek at most 512 clusters at a time
const FilePosition maxSeekDistance = 512 * (FilePosition)clsize;
const bool doFullSeek = (nextSeekPos <= currentPos + maxSeekDistance);
const FilePosition thisSeekPos = (doFullSeek) ? nextSeekPos : currentPos + maxSeekDistance;
#elif HAS_EMBEDDED_FILES
const bool doFullSeek = true;
const FilePosition thisSeekPos = nextSeekPos;
#endif
const uint32_t startTime = millis();
if (!fileBeingParsed->Seek(thisSeekPos))
{
reprap.GetPlatform().MessageF(WarningMessage, "Could not seek from end of file \"%s\"\n", filePath);
parseState = notParsing;
fileBeingParsed->Close();
info = parsedFileInfo;
return GCodeResult::warning;
}
accumulatedSeekTime += millis() - startTime;
if (doFullSeek)
{
parseState = parsingFooter;
}
}
break;
case parsingFooter:
{
// Processing the footer. See how many bytes we need to read and if we can reuse the overlap
sizeToRead = (size_t)min<FilePosition>(fileBeingParsed->Length() - nextSeekPos, GCODE_READ_SIZE);
if (fileOverlapLength > 0)
{
memcpy(&buf[sizeToRead], buf, fileOverlapLength);
sizeToScan = sizeToRead + fileOverlapLength;
}
else
{
sizeToScan = sizeToRead;
}
// Read another chunk from the footer
uint32_t startTime = millis();
int nbytes = fileBeingParsed->Read(buf, sizeToRead);
if (nbytes != (int)sizeToRead)
{
reprap.GetPlatform().MessageF(WarningMessage, "Failed to read footer from G-Code file \"%s\"\n", filePath);
parseState = notParsing;
fileBeingParsed->Close();
info = parsedFileInfo;
return GCodeResult::warning;
}
buf[sizeToScan] = 0;
// Record performance data
uint32_t now = millis();
accumulatedReadTime += now - startTime;
startTime = now;
bool footerInfoComplete = true;
// Search for filament used
if (parsedFileInfo.numFilaments == 0)
{
parsedFileInfo.numFilaments = FindFilamentUsed(buf);
if (parsedFileInfo.numFilaments == 0)
{
footerInfoComplete = false;
}
}
// Search for layer height
if (parsedFileInfo.layerHeight == 0.0)
{
if (!FindLayerHeight(buf))
{
footerInfoComplete = false;
}
}
// Search for object height
if (parsedFileInfo.objectHeight == 0.0)
{
if (!FindHeight(buf, sizeToScan))
{
footerInfoComplete = false;
}
}
// Search for number of layers
if (parsedFileInfo.numLayers == 0)
{
// Number of layers should come before the object height
(void)FindNumLayers(buf, sizeToScan);
}
// Look for print time
if (parsedFileInfo.printTime == 0)
{
if (!FindPrintTime(buf) && fileBeingParsed->Length() - nextSeekPos <= GcodeFooterPrintTimeSearchSize)
{
footerInfoComplete = false;
}
}
// Look for simulated print time. It will always be right at the end of the file, so don't look too far back
if (parsedFileInfo.simulatedTime == 0)
{
if (!FindSimulatedTime(buf) && fileBeingParsed->Length() - nextSeekPos <= GcodeFooterPrintTimeSearchSize)
{
footerInfoComplete = false;
}
}
// Keep track of the time stats
accumulatedParseTime += millis() - startTime;
// If we've collected all details, scanned the last 192K of the file or if we cannot go any further, stop here.
if (footerInfoComplete || nextSeekPos == 0 || fileBeingParsed->Length() - nextSeekPos >= GCODE_FOOTER_SIZE)
{
if (reprap.Debug(modulePrintMonitor))
{
reprap.GetPlatform().MessageF(UsbMessage, "Footer complete, processed %lu bytes, read time %.3fs, parse time %.3fs, seek time %.3fs\n",
fileBeingParsed->Length() - fileBeingParsed->Position() + GCODE_READ_SIZE,
(double)((float)accumulatedReadTime/1000.0), (double)((float)accumulatedParseTime/1000.0), (double)((float)accumulatedSeekTime/1000.0));
}
parseState = notParsing;
fileBeingParsed->Close();
if (parsedFileInfo.numLayers == 0 && parsedFileInfo.layerHeight > 0.0 && parsedFileInfo.objectHeight > 0.0)
{
parsedFileInfo.numLayers = lrintf(parsedFileInfo.objectHeight / parsedFileInfo.layerHeight);
}
parsedFileInfo.incomplete = false;
info = parsedFileInfo;
return GCodeResult::ok;
}
// Else go back further
fileOverlapLength = (size_t)min<FilePosition>(sizeToScan, GCODE_OVERLAP_SIZE);
nextSeekPos = (nextSeekPos <= GCODE_READ_SIZE) ? 0 : nextSeekPos - GCODE_READ_SIZE;
parseState = seeking;
}
break;
default: // should not get here
parsedFileInfo.incomplete = false;
fileBeingParsed->Close();
info = parsedFileInfo;
parseState = notParsing;
return GCodeResult::ok;
}
lastFileParseTime = millis();
} while (!reprap.GetPrintMonitor().IsPrinting() && lastFileParseTime - loopStartTime < MAX_FILEINFO_PROCESS_TIME);
if (quitEarly)
{
info = parsedFileInfo; // note that the 'incomplete' flag is still set
fileBeingParsed->Close();
parseState = notParsing;
return GCodeResult::ok;
}
return GCodeResult::notFinished;
}
// Scan the buffer for a G1 Zxxx command. The buffer is null-terminated.
// This parsing algorithm needs to be fast. The old one sometimes took 5 seconds or more to parse about 120K of data.
// To speed up parsing, we now parse forwards from the start of the buffer. This means we can't stop when we have found a G1 Z command,
// we have to look for a later G1 Z command in the buffer. But it is faster in the (common) case that we don't find a match in the buffer at all.
bool FileInfoParser::FindHeight(const char* bufp, size_t len) noexcept
{
bool foundHeight = false;
bool inRelativeMode = false;
for(;;)
{
// Skip to next newline
char c;
while (len >= 6 && (c = *bufp) != '\r' && c != '\n')
{
++bufp;
--len;
}
// Skip the newline and any leading spaces
do
{
++bufp; // skip the newline
--len;
c = *bufp;
} while (len >= 5 && (c == ' ' || c == '\t' || c == '\r' || c == '\n'));
if (len < 5)
{
break; // not enough characters left for a G1 Zx.x command
}
++bufp; // move to 1 character beyond c
--len;
// In theory we should skip N and a line number here if they are present, but no slicers seem to generate line numbers
if (c == 'G')
{
if (inRelativeMode)
{
// We have seen a G91 in this buffer already, so we are only interested in G90 commands that switch back to absolute mode
if (bufp[0] == '9' && bufp[1] == '0' && (bufp[2] < '0' || bufp[2] > '9'))
{
// It's a G90 command so go back to absolute mode
inRelativeMode = false;
}
}
else if (*bufp == '1' || *bufp == '0')
{
// It could be a G0 or G1 command
++bufp;
--len;
if (*bufp < '0' || *bufp > '9')
{
// It is a G0 or G1 command. See if it has a Z parameter.
while (len >= 4)
{
c = *bufp;
if (c == 'Z')
{
const char* zpos = bufp + 1;
// Check special case of this code ending with ";E" or "; E" - ignore such codes
while (len > 2 && *bufp != '\n' && *bufp != '\r' && *bufp != ';')
{
++bufp;
--len;
}
if ((len >= 2 && StringStartsWith(bufp, ";E")) || (len >= 3 && StringStartsWith(bufp, "; E")))
{
// Ignore this G1 Z command
}
else
{
float objectHeight = SafeStrtof(zpos, nullptr);
if (!std::isnan(objectHeight) && !std::isinf(objectHeight))
{
parsedFileInfo.objectHeight = objectHeight;
foundHeight = true;
}
}
break; // carry on looking for a later G1 Z command
}
if (c == ';' || c == '\n' || c == '\r')
{
break; // no Z parameter
}
++bufp;
--len;
}
}
}
else if (bufp[0] == '9' && bufp[1] == '1' && (bufp[2] < '0' || bufp[2] > '9'))
{
// It's a G91 command
inRelativeMode = true;
}
}
else if (c == ';')
{
static const char kisslicerHeightString[] = " END_LAYER_OBJECT z=";
if (len > 31 && StringStartsWithIgnoreCase(bufp, kisslicerHeightString))
{
float objectHeight = SafeStrtof(bufp + sizeof(kisslicerHeightString)/sizeof(char) - 1, nullptr);
if (!std::isnan(objectHeight) && !std::isinf(objectHeight))
{
parsedFileInfo.objectHeight = objectHeight;
return true;
}
}
}
}
return foundHeight;
}
// Scan the buffer for th total number of layers. The buffer is null-terminated.
bool FileInfoParser::FindNumLayers(const char* bufp, size_t len) noexcept
{
static const char* const numLayerStrings[] =
{
"num_layers",
"NUM_LAYERS"
};
if (*bufp != 0)
{
++bufp; // make sure we can look back 1 character after we find a match
for (const char * lhStr : numLayerStrings) // search for each string in turn
{
const char *pos = bufp;
for(;;) // loop until success or strstr returns null
{
pos = strstr(pos, lhStr);
if (pos == nullptr)
{
break; // didn't find this string in the buffer, so try the next string
}
const char c = pos[-1]; // fetch the previous character
pos += strlen(lhStr); // skip the string we matched
if (c == ' ' || c == ';' || c == '\t') // check we are not in the middle of a word
{
while (strchr(" \t=:,", *pos) != nullptr) // skip the possible separators
{
++pos;
}
const unsigned int val = StrToU32(pos);
if (val > 0)
{
parsedFileInfo.numLayers = val;
return true;
}
}
}
}
}
return false;
}
// Scan the buffer for the layer height. The buffer is null-terminated.
bool FileInfoParser::FindLayerHeight(const char *bufp) noexcept
{
static const char* const layerHeightStrings[] =
{
"layer_height", // slic3r
"Layer height", // Cura
"layerHeight", // S3D
"layer_thickness_mm", // Kisslicer
"layerThickness", // Matter Control
"sliceHeight" // kiri:moto
};
if (*bufp != 0)
{
++bufp; // make sure we can look back 1 character after we find a match
for (const char * lhStr : layerHeightStrings) // search for each string in turn
{
const char *pos = bufp;
for(;;) // loop until success or strstr returns null
{
pos = strstr(pos, lhStr);
if (pos == nullptr)
{
break; // didn't find this string in the buffer, so try the next string
}
const char c = pos[-1]; // fetch the previous character
pos += strlen(lhStr); // skip the string we matched
if (c == ' ' || c == ';' || c == '\t') // check we are not in the middle of a word
{
while (strchr(" \t=:,", *pos) != nullptr) // skip the possible separators
{
++pos;
}
const char *tailPtr;
const float val = SafeStrtof(pos, &tailPtr);
if (tailPtr != pos && !std::isnan(val) && !std::isinf(val)) // if we found and converted a number
{
parsedFileInfo.layerHeight = val;
return true;
}
}
}
}
}
return false;
}
bool FileInfoParser::FindSlicerInfo(const char* bufp) noexcept
{
static const char * const GeneratedByStrings[] =
{
"; KISSlicer", // KISSlicer
";Sliced at: ", // Cura (old)
";Fusion version:", // Fusion 360
"generated by ", // slic3r and S3D
";Sliced by ", // ideaMaker
";Generated with ", // Cura (new)
"; Generated by ", // kiri:moto
";GENERATOR.NAME:", // Pathio (the version is separate, we don't include that)
"; Generated with " // Matter Control
};
size_t index = 0;
const char* pos;
do
{
pos = strstr(bufp, GeneratedByStrings[index]);
if (pos != nullptr)
{
break;
}
++index;
} while (index < ARRAY_SIZE(GeneratedByStrings));
if (pos != nullptr)
{
const char* introString = "";
switch (index)
{
default:
pos += strlen(GeneratedByStrings[index]);
break;
case 0: // KISSlicer
pos += 2;
break;
case 1: // Cura (old)
introString = "Cura at ";
pos += strlen(GeneratedByStrings[index]);
break;
case 2: // Fusion 360
pos += 1;
break;
}
parsedFileInfo.generatedBy.copy(introString);
while (*pos >= ' ')
{
parsedFileInfo.generatedBy.cat(*pos++);
}
return true;
}
return false;
}
// Scan the buffer for a 2-part filament used string. Return the number of filament found.
void FileInfoParser::FindFilamentUsedEmbedded(const char* p, const char *s1, const char *s2, unsigned int &filamentsFound) noexcept
{
const size_t maxFilaments = reprap.GetGCodes().GetNumExtruders();
while (filamentsFound < maxFilaments && (p = strstr(p, s1)) != nullptr)
{
p += strlen(s1);
const char *q1, *q2;
uint32_t num = StrToU32(p, &q1);
if (q1 != p && num < maxFilaments && (q2 = strstr(q1, s2)) == q1)
{
p = q1 + strlen(s2);
while(strchr(" :\t", *p) != nullptr)
{
++p; // this allows for " Used: "
}
if (isDigit(*p))
{
float filamentLength = SafeStrtof(p, nullptr);
if (!std::isnan(filamentLength) && !std::isinf(filamentLength))
{
parsedFileInfo.filamentNeeded[filamentsFound] = filamentLength;
++filamentsFound;
}
}
}
}
}
// Scan the buffer for the filament used. The buffer is null-terminated.
// Returns the number of filaments found.
unsigned int FileInfoParser::FindFilamentUsed(const char* bufp) noexcept
{
unsigned int filamentsFound = 0;
// Look for filament usage as generated by Slic3r and Cura
const char* const filamentUsedStr1 = "ilament used"; // comment string used by slic3r and Cura, followed by filament used and "mm"
const char* p = bufp;
while (filamentsFound < MaxFilaments && (p = strstr(p, filamentUsedStr1)) != nullptr)
{
p += strlen(filamentUsedStr1);
while(strchr(" [m]:=\t", *p) != nullptr) // Prusa slicer now uses "; filament used [mm] = 4235.9"
{
++p; // this allows for " = " from default slic3r comment and ": " from default Cura comment
}
while (isDigit(*p) && filamentsFound < MaxFilaments)
{
const char* q;
const float filamentLength = SafeStrtof(p, &q);
p = q;
if (!std::isnan(filamentLength) && !std::isinf(filamentLength))
{
parsedFileInfo.filamentNeeded[filamentsFound] = filamentLength;
if (*p == 'm')
{
++p;
if (*p == 'm')
{
++p;
}
else
{
parsedFileInfo.filamentNeeded[filamentsFound] *= 1000.0; // Cura outputs filament used in metres not mm
}
}
++filamentsFound;
}
while (strchr(", \t", *p) != nullptr)
{
++p;
}
}
}
// Look for filament usage strings generated by Ideamaker, e.g. ";Material#1 Used: 868.0"
FindFilamentUsedEmbedded(bufp, ";Material#", " Used", filamentsFound);
// Look for filament usage strings generated by Fusion 360, e.g. ";Extruder 1 material used: 1811mm"
FindFilamentUsedEmbedded(bufp, ";Extruder ", " material used", filamentsFound);
// Look for filament usage as generated by S3D
if (filamentsFound == 0)
{
const char *filamentLengthStr = "ilament length"; // comment string used by S3D
p = bufp;
while (filamentsFound < MaxFilaments && (p = strstr(p, filamentLengthStr)) != nullptr)
{
p += strlen(filamentLengthStr);
while(strchr(" :=\t", *p) != nullptr)
{
++p;
}
if (isDigit(*p))
{
const float filamentLength = SafeStrtof(p, nullptr);
if (!std::isnan(filamentLength) && !std::isinf(filamentLength))
{
parsedFileInfo.filamentNeeded[filamentsFound] = filamentLength;
++filamentsFound;
}
}
}
}
// Look for filament usage as generated by recent KISSlicer versions
if (filamentsFound == 0)
{
const char *filamentLengthStr = "; Ext ";
p = bufp;
while (filamentsFound < MaxFilaments && (p = strstr(p, filamentLengthStr)) != nullptr)
{
p += strlen(filamentLengthStr);
if (*p == '#')
{
++p; // later KISSlicer versions add a # here
}
while(isdigit(*p))
{
++p;
}
while(strchr(" :=\t", *p) != nullptr)
{
++p;
}
if (isDigit(*p))
{
const float filamentLength = SafeStrtof(p, nullptr);
if (!std::isnan(filamentLength) && !std::isinf(filamentLength))
{
parsedFileInfo.filamentNeeded[filamentsFound] = filamentLength;
++filamentsFound;
}
}
}
}
// Special case: Old KISSlicer and Pathio only generate the filament volume, so we need to calculate the length from it
if (filamentsFound == 0 && reprap.GetPlatform().GetFilamentWidth() > 0.0)
{
const char *filamentVolumeStr = "; Estimated Build Volume: "; // old KISSlicer
float multipler = 1000.0; // volume is in cm^3
p = strstr(bufp, filamentVolumeStr);
if (p == nullptr)
{
filamentVolumeStr = ";EXTRUDER_TRAIN.0.MATERIAL.VOLUME_USED:"; // Pathio
multipler = 1.0; // volume is in mm^3
p = strstr(bufp, filamentVolumeStr);
}
if (p != nullptr)
{
const float filamentCMM = SafeStrtof(p + strlen(filamentVolumeStr), nullptr) * multipler;
if (!std::isnan(filamentCMM) && !std::isinf(filamentCMM))
{
parsedFileInfo.filamentNeeded[filamentsFound++] = filamentCMM / (Pi * fsquare(reprap.GetPlatform().GetFilamentWidth() * 0.5));
}
}
}
return filamentsFound;
}
// Scan the buffer for the estimated print time
bool FileInfoParser::FindPrintTime(const char* bufp) noexcept
{
static const char* const PrintTimeStrings[] =
{
// Note: if a string in this table is a leading or embedded substring of another, the longer one must come first
" estimated printing time (normal mode)", // slic3r PE later versions "; estimated printing time (normal mode) = 2d 1h 5m 24s"
" estimated printing time", // slic3r PE older versions "; estimated printing time = 1h 5m 24s"
";TIME", // Cura ";TIME:38846"
" Build time", // S3D "; Build time: 0 hours 42 minutes"
// also REALvision "; Build time: 2:11:47"
" Build Time", // KISSlicer "; Estimated Build Time: 332.83 minutes"
// also KISSSlicer 2 alpha "; Calculated-during-export Build Time: 130.62 minutes"
";Print Time:", // Ideamaker
";PRINT.TIME:", // Patio
";Print time:", // Fusion 360
"; total print time (s) =" // Matter Control
};
for (const char * ptStr : PrintTimeStrings)
{
const char* pos = strstr(bufp, ptStr);
if (pos != nullptr)
{
pos += strlen(ptStr);
while (strchr(" \t=:", *pos))
{
++pos;
}
const char * const q = pos;
float days = 0.0, hours = 0.0, minutes = 0.0;
float secs = SafeStrtof(pos, &pos);
if (q != pos)
{
while (*pos == ' ')
{
++pos;
}
if (*pos == ':') // special code for REALvision
{
minutes = secs;
secs = SafeStrtof(pos + 1, &pos);
if (*pos == ':')
{
hours = minutes;
minutes = secs;
secs = SafeStrtof(pos + 1, &pos);
// I am assuming that it stops at hours
}
}
else
{
if (*pos == 'd')
{
days = secs;
if (StringStartsWithIgnoreCase(pos, "day")) // not sure if any slicer needs this, but include it j.i.c.
{
pos += 3;
if (*pos == 's')
{
++pos;
}
}
else
{
++pos;
}
secs = SafeStrtof(pos, &pos);
while (*pos == ' ' || *pos == ':')
{
++pos;
}
}
if (*pos == 'h')
{
hours = secs;
if (StringStartsWithIgnoreCase(pos, "hour")) // S3D
{
pos += 4;
if (*pos == 's')
{
++pos;
}
}
else
{
++pos;
}
secs = SafeStrtof(pos, &pos);
while (*pos == ' ' || *pos == ':') // Fusion 360 gives e.g. ";Print time: 40m:36s"
{
++pos;
}
}
if (*pos == 'm')
{
minutes = secs;
if (StringStartsWithIgnoreCase(pos, "minute"))
{
pos += 6;
if (*pos == 's')
{
++pos;
}
}
else if (StringStartsWithIgnoreCase(pos, "min")) // Fusion 360
{
pos += 3;
}
else
{
++pos;
}
secs = SafeStrtof(pos, &pos);
}
}
}
parsedFileInfo.printTime = lrintf(((days * 24.0 + hours) * 60.0 + minutes) * 60.0 + secs);
return true;
}
}
return false;
}
// Scan the buffer for the simulated print time
bool FileInfoParser::FindSimulatedTime(const char* bufp) noexcept
{
const char* pos = strstr(bufp, SimulatedTimeString);
if (pos != nullptr)
{
pos += strlen(SimulatedTimeString);
while (strchr(" \t=:", *pos))
{
++pos;
}
const char * const q = pos;
const uint32_t secs = StrToU32(pos, &pos);
if (q != pos)
{
parsedFileInfo.simulatedTime = secs;
return true;
}
}
return false;
}
// Search for embedded thumbnail images that start in the buffer
// Subsequent calls pass a buffer that overlaps with the previous one, so take care to avoid finding the same one twice.
// The overlap is small enough that we can discount the possibility of finding more than one thumbnail header in the overlap area.
// Return true if we have no room to store further thumbnails, or we are certain that we have found all the thumbnails in the file.
// Thumbnail data is preceded by comment lines of the following form:
// ; thumbnail_QOI begin 32x32 2140
// or
// ; thumbnail begin 32x32 2140
bool FileInfoParser::FindThumbnails(const char *_ecv_array bufp, FilePosition bufferStartFilePosition) noexcept
{
// Find the next free slot in which to store thumbnail data
size_t thumbnailIndex = 0;
for (thumbnailIndex = 0; parsedFileInfo.thumbnails[thumbnailIndex].IsValid(); )
{
++thumbnailIndex;
if (thumbnailIndex == GCodeFileInfo::MaxThumbnails)
{
return true; // no more space to store thumbnail info
}
}
constexpr const char * PngThumbnailBegin = "; thumbnail begin ";
constexpr const char * QoiThumbnailBegin = "; thumbnail_QOI begin ";
constexpr const char * JpegThumbnailBegin = "; thumbnail_JPG begin ";
const char *_ecv_array pos = bufp;
while (true)
{
const char *_ecv_array qoiPos = strstr(pos, QoiThumbnailBegin);
const char *_ecv_array pngPos = strstr(pos, PngThumbnailBegin);
const char *_ecv_array jpegPos = strstr(pos, JpegThumbnailBegin);
GCodeFileInfo::ThumbnailInfo::Format fmt(GCodeFileInfo::ThumbnailInfo::Format::qoi);
if (qoiPos != nullptr && (pngPos == nullptr || qoiPos < pngPos) && (jpegPos == nullptr || qoiPos < jpegPos))
{
// found a QOI thumbnail
pos = qoiPos + strlen(QoiThumbnailBegin);
}
else if (pngPos != nullptr && (jpegPos == nullptr || pngPos < jpegPos))
{
// found a PNG thumbnail
pos = pngPos + strlen(PngThumbnailBegin);
fmt = GCodeFileInfo::ThumbnailInfo::Format::png;
}
else if (jpegPos != nullptr)
{
// found a JPEG thumbnail
pos = jpegPos + strlen(JpegThumbnailBegin);
fmt = GCodeFileInfo::ThumbnailInfo::Format::jpeg;
}
else
{
return false; // no more thumbnails in this buffer, but we have room to save more thumbnail details
}
// Store this thumbnail data
const char *_ecv_array npos;
const uint32_t w = StrToU32(pos, &npos);
if (w >= 16 && w <= 500 && *npos == 'x')
{
pos = npos + 1;
const uint32_t h = StrToU32(pos, &npos);
if (h >= 16 && h <= 500 && *npos == ' ')
{
pos = npos + 1;
const uint32_t size = StrToU32(pos, &npos);
if (size >= 10)
{
pos = npos;
while (*pos == ' ' || *pos == '\r' || *pos == '\n')
{
++pos;
}
if (*pos == ';')
{
const FilePosition offset = bufferStartFilePosition + (pos - bufp);
if (thumbnailIndex == 0 || offset != parsedFileInfo.thumbnails[thumbnailIndex - 1].offset)
{
GCodeFileInfo::ThumbnailInfo& th = parsedFileInfo.thumbnails[thumbnailIndex];
th.width = w;
th.height = h;
th.size = size;
th.format = fmt;
th.offset = offset;
++thumbnailIndex;
if (thumbnailIndex == GCodeFileInfo::MaxThumbnails)
{
return true; // no more space to store thumbnail info
}
}
}
}
}
}
}
}
#endif
// End
|