enabled specifying numeric config values using all strtof capabilities
[clinton/Smoothieware.git] / src / libs / StreamOutput.h
CommitLineData
df27a6a3 1/*
38d375e7
AW
2 This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl).
3 Smoothie is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
4 Smoothie is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
df27a6a3 5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
38d375e7
AW
6*/
7
25a19381
AW
8#ifndef STREAMOUTPUT_H
9#define STREAMOUTPUT_H
10
99b9ed8a 11#include <cstdarg>
d0ef6382 12#include <cstring>
9a993543 13#include <stdio.h>
d0ef6382 14
93694d6b
AW
15// This is a base class for all StreamOutput objects.
16// StreamOutputs are basically "things you can sent strings to". They are passed along with gcodes for example so modules can answer to those gcodes.
17// They are usually associated with a command source, but can also be a NullStreamOutput if we just want to ignore whatever is sent
18
d0ef6382 19class NullStreamOutput;
99b9ed8a 20
04095259 21class StreamOutput {
25a19381 22 public:
99b9ed8a 23 StreamOutput(){}
04095259
JM
24 virtual ~StreamOutput(){}
25
4748cbf1 26 virtual int printf(const char* format, ...) __attribute__ ((format(printf, 2, 3))) {
3bde4e0d 27 char b[64];
44c00d06 28 char *buffer;
99b9ed8a
MM
29 // Make the message
30 va_list args;
31 va_start(args, format);
32
3bde4e0d 33 int size = vsnprintf(b, 64, format, args)
99b9ed8a
MM
34 + 1; // we add one to take into account space for the terminating \0
35
3bde4e0d
MM
36 if (size < 64)
37 buffer = b;
38 else
39 {
40 buffer = new char[size];
41 vsnprintf(buffer, size, format, args);
42 }
99b9ed8a
MM
43 va_end(args);
44
45 puts(buffer);
46
3bde4e0d
MM
47 if (buffer != b)
48 delete[] buffer;
49
99b9ed8a
MM
50 return size - 1;
51 }
d0ef6382 52 virtual int _putc(int c) { return 1; }
99b9ed8a 53 virtual int _getc(void) { return 0; }
d0ef6382
MM
54 virtual int puts(const char* str) = 0;
55
56 static NullStreamOutput NullStream;
57};
58
59class NullStreamOutput : public StreamOutput {
60 public:
61 int puts(const char* str) { return strlen(str); }
25a19381
AW
62};
63
25a19381 64#endif