MRPT  1.9.9
CStream.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "io-precomp.h" // Precompiled headers
11 
12 #include <mrpt/io/CStream.h>
13 #include <mrpt/core/exceptions.h>
15 #include <iostream>
16 #include <cstdarg>
17 #include <vector>
18 #include <cstring> // strlen()
19 
20 //#include "internal_class_registry.h"
21 
22 using namespace mrpt;
23 using namespace mrpt::io;
24 using namespace std;
25 
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 -------------------------------------------------------------*/
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:262
GLuint buffer
Definition: glext.h:3917
STL namespace.
#define vsnprintf
Definition: zutil.h:203
virtual ~CStream()
Definition: CStream.cpp:26
GLsizei const GLchar ** string
Definition: glext.h:4101
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
#define MRPT_END
Definition: exceptions.h:266
GLuint GLsizei GLsizei * length
Definition: glext.h:4064
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 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020