Main MRPT website > C++ reference for 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 }
mrpt::io::CStream::~CStream
virtual ~CStream()
Definition: CStream.cpp:26
exceptions.h
mrpt::io
Definition: img/CImage.h:22
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
length
GLuint GLsizei GLsizei * length
Definition: glext.h:4064
MRPT_START
#define MRPT_START
Definition: exceptions.h:262
CStream.h
reverse_bytes.h
buffer
GLuint buffer
Definition: glext.h:3917
vsnprintf
#define vsnprintf
Definition: zutil.h:203
MRPT_END
#define MRPT_END
Definition: exceptions.h:266
string
GLsizei const GLchar ** string
Definition: glext.h:4101
mrpt::io::CStream::printf
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
mrpt::io::CStream::getline
bool getline(std::string &out_str)
Reads from the stream until a ' ' character is found ('\r' characters are ignored).
Definition: CStream.cpp:69
io-precomp.h



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST