diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/GCodes/GCodeBuffer/StringParser.cpp | 51 | ||||
-rw-r--r-- | src/Storage/FileData.h | 14 | ||||
-rw-r--r-- | src/Storage/FileStore.cpp | 2 |
3 files changed, 57 insertions, 10 deletions
diff --git a/src/GCodes/GCodeBuffer/StringParser.cpp b/src/GCodes/GCodeBuffer/StringParser.cpp index 4876f3d6..9da35b71 100644 --- a/src/GCodes/GCodeBuffer/StringParser.cpp +++ b/src/GCodes/GCodeBuffer/StringParser.cpp @@ -306,7 +306,7 @@ bool StringParser::CheckMetaCommand(const StringRef& reply) THROWS(GCodeExceptio { if (overflowed) { - throw GCodeException(gb.GetLineNumber(), ARRAY_SIZE(gb.buffer) - 1, "GCode command too long"); + throw GCodeException(gb.GetLineNumber(), ARRAY_SIZE(gb.buffer) + commandIndent - 1, "GCode command too long"); } const bool doingFile = gb.IsDoingFile(); @@ -742,14 +742,41 @@ void StringParser::ProcessAbortCommand(const StringRef& reply) noexcept reprap.GetGCodes().AbortPrint(gb); } -void StringParser::ProcessEchoCommand(const StringRef& reply) +void StringParser::ProcessEchoCommand(const StringRef& reply) THROWS(GCodeException) { + SkipWhiteSpace(); + FileData outputFile; + + if (gb.buffer[readPointer] == '>') + { + // Redirect the line to file + ++readPointer; + OpenMode openMode; + if (gb.buffer[readPointer] == '>') + { + openMode = OpenMode::append; + ++readPointer; + } + else + { + openMode = OpenMode::write; + } + String<MaxFilenameLength> filename; + GetQuotedString(filename.GetRef(), false); + FileStore * const f = reprap.GetPlatform().OpenSysFile(filename.c_str(), openMode); + if (f == nullptr) + { + throw GCodeException(gb.GetLineNumber(), readPointer + commandIndent, "Failed to create or open file"); + } + outputFile.Set(f); + } + while (true) { SkipWhiteSpace(); if (gb.buffer[readPointer] == 0) { - return; + break; } ExpressionParser parser(gb, gb.buffer + readPointer, gb.buffer + ARRAY_SIZE(gb.buffer), commandIndent + readPointer); const ExpressionValue val = parser.Parse(); @@ -769,10 +796,22 @@ void StringParser::ProcessEchoCommand(const StringRef& reply) throw ConstructParseException("expected ','"); } } + + if (outputFile.IsLive()) + { + reply.cat('\n'); + const bool ok = outputFile.Write(reply.c_str()); + outputFile.Close(); + reply.Clear(); + if (!ok) + { + throw GCodeException(gb.GetLineNumber(), -1, "Failed to write to redirect file"); + } + } } // Evaluate the condition that should follow 'if' or 'while' -bool StringParser::EvaluateCondition() +bool StringParser::EvaluateCondition() THROWS(GCodeException) { ExpressionParser parser(gb, gb.buffer + readPointer, gb.buffer + ARRAY_SIZE(gb.buffer), commandIndent + readPointer); const bool b = parser.ParseBoolean(); @@ -1222,7 +1261,7 @@ void StringParser::CheckArrayLength(size_t actualLength, size_t maxLength) THROW } } -// Get and copy a quoted string returning true if successful +// Get and copy a quoted string returning true if successful, leaving the read pointer just after the string void StringParser::GetQuotedString(const StringRef& str, bool allowEmpty) THROWS(GCodeException) { if (readPointer <= 0) @@ -1256,7 +1295,7 @@ void StringParser::GetQuotedString(const StringRef& str, bool allowEmpty) THROWS } } -// Given that the current character is double-quote, fetch the quoted string +// Given that the current character is double-quote, fetch the quoted string, leaving the read pointer just after the string void StringParser::InternalGetQuotedString(const StringRef& str) THROWS(GCodeException) { str.Clear(); diff --git a/src/Storage/FileData.h b/src/Storage/FileData.h index 63a38e01..a10c1c0b 100644 --- a/src/Storage/FileData.h +++ b/src/Storage/FileData.h @@ -23,6 +23,8 @@ public: FileData() noexcept : f(nullptr) {} + ~FileData() { (void)Close(); } + FileData(const FileData& other) noexcept { f = other.f; @@ -32,6 +34,15 @@ public: } } + FileData(FileData&& other) noexcept + { + f = other.f; + other.f = nullptr; + } + + // Make sure we don't assign these objects + FileData& operator=(const FileData&) = delete; + // Set this to refer to a newly-opened file void Set(FileStore* pfile) noexcept { @@ -125,9 +136,6 @@ private: { f = nullptr; } - - // Private assignment operator to prevent us assigning these objects - FileData& operator=(const FileData&) noexcept; }; #endif diff --git a/src/Storage/FileStore.cpp b/src/Storage/FileStore.cpp index 617c26ff..a37eec73 100644 --- a/src/Storage/FileStore.cpp +++ b/src/Storage/FileStore.cpp @@ -128,7 +128,7 @@ bool FileStore::Open(const char* filePath, OpenMode mode, uint32_t preAllocSize) { const FRESULT openReturn = f_open(&file, filePath, (mode == OpenMode::write || mode == OpenMode::writeWithCrc) ? FA_CREATE_ALWAYS | FA_WRITE - : (mode == OpenMode::append) ? FA_READ | FA_WRITE | FA_OPEN_ALWAYS + : (mode == OpenMode::append) ? FA_READ | FA_WRITE | FA_OPEN_ALWAYS | FA_OPEN_APPEND : FA_OPEN_EXISTING | FA_READ); if (openReturn == FR_OK) { |