MRPT  2.0.1
CStream.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "io-precomp.h" // Precompiled headers
11 
12 #include <mrpt/core/exceptions.h>
14 #include <mrpt/io/CStream.h>
15 #include <cstdarg>
16 #include <cstring> // strlen()
17 #include <iostream>
18 #include <vector>
19 
20 //#include "internal_class_registry.h"
21 
22 using namespace mrpt;
23 using namespace mrpt::io;
24 using namespace std;
25 
26 CStream::~CStream() = default;
27 /*---------------------------------------------------------------
28  Writes an elemental data type to stream.
29  ---------------------------------------------------------------*/
30 int CStream::printf(const char* fmt, ...)
31 {
33 
34  if (!fmt) throw std::runtime_error("fmt in CStream::printf cannot be NULL");
35 
36  int result = -1, length = 1024;
37  vector<char> buffer;
38  while (result == -1)
39  {
40  buffer.resize(length + 10);
41 
42  va_list args; // This must be done WITHIN the loop
43  va_start(args, fmt);
44 #if defined(_MSC_VER)
45  result = ::vsnprintf_s(&buffer[0], length, _TRUNCATE, fmt, args);
46 #else
47  result = ::vsnprintf(&buffer[0], length, fmt, args);
48 #endif
49  va_end(args);
50 
51  // Truncated?
52  if (result >= length) result = -1;
53  length *= 2;
54  }
55 
56  size_t l = strlen(&buffer[0]);
57  this->Write(&buffer[0], (int)l);
58 
59  return result;
60 
61  MRPT_END
62 }
63 
64 /*-------------------------------------------------------------
65 Reads from the stream until a '\n' character is found ('\r' characters are
66 ignored).
67 return false on EOF or any other read error.
68 -------------------------------------------------------------*/
69 bool CStream::getline(std::string& out_str)
70 {
71  out_str.clear();
72  try
73  {
74  for (;;)
75  {
76  size_t N = out_str.size();
77  out_str.resize(N + 1);
78  if (!Read(&out_str[N], 1)) return false;
79 
80  // New char read:
81  if (out_str[N] == '\r')
82  {
83  out_str.resize(N); // Ignore.
84  }
85  else if (out_str[N] == '\n')
86  {
87  out_str.resize(N); // End of line!
88  return true; // Ok.
89  }
90  }
91  }
92  catch (...)
93  { // Any read error:
94  return false;
95  }
96 }
virtual int printf(const char *fmt,...) MRPT_printf_format_check(2
Writes a string to the stream in a textual form.
Definition: CStream.cpp:30
#define MRPT_START
Definition: exceptions.h:241
STL namespace.
virtual ~CStream()
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
int vsnprintf(char *buf, size_t bufSize, const char *format, va_list args) noexcept
An OS-independent version of vsnprintf (Notice the bufSize param, which may be ignored in some compil...
Definition: os.cpp:230
#define MRPT_END
Definition: exceptions.h:245
bool getline(std::string &out_str)
Reads from the stream until a &#39; &#39; character is found (&#39;&#39; characters are ignored). ...
Definition: CStream.cpp:69



Page generated by Doxygen 1.8.14 for MRPT 2.0.1 Git: 0fef1a6d7 Fri Apr 3 23:00:21 2020 +0200 at vie abr 3 23:20:28 CEST 2020