Mistake on this page?
Report an issue in GitHub or email us
Stream.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2013 ARM Limited
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #ifndef MBED_STREAM_H
18 #define MBED_STREAM_H
19 
20 #include "platform/platform.h"
21 #include "platform/FileLike.h"
22 #include "platform/FileHandle.h"
23 #include "platform/NonCopyable.h"
24 #include "mbed_toolchain.h"
25 #include <cstdio>
26 #include <cstdarg>
27 
28 namespace mbed {
29 /** \addtogroup platform */
30 /** @{*/
31 /**
32  * \defgroup platform_Stream Stream class
33  * @{
34  */
35 
36 extern void mbed_set_unbuffered_stream(std::FILE *_file);
37 
38 /** File stream
39  *
40  * @note Synchronization level: Set by subclass
41  */
42 class Stream : public FileLike, private NonCopyable<Stream> {
43 
44 public:
45  Stream(const char *name = NULL);
46  virtual ~Stream();
47 
48  int putc(int c);
49  int puts(const char *s);
50  int getc();
51  char *gets(char *s, int size);
52  int printf(const char *format, ...) MBED_PRINTF_METHOD(1, 2);
53  int scanf(const char *format, ...) MBED_SCANF_METHOD(1, 2);
54  int vprintf(const char *format, std::va_list args) MBED_PRINTF_METHOD(1, 0);
55  int vscanf(const char *format, std::va_list args) MBED_SCANF_METHOD(1, 0);
56 
57  operator std::FILE *()
58  {
59  return _file;
60  }
61 
62 protected:
63  virtual int close();
64  virtual ssize_t write(const void *buffer, size_t length);
65  virtual ssize_t read(void *buffer, size_t length);
66  virtual off_t seek(off_t offset, int whence);
67  virtual off_t tell();
68  virtual void rewind();
69  virtual int isatty();
70  virtual int sync();
71  virtual off_t size();
72 
73  virtual int _putc(int c) = 0;
74  virtual int _getc() = 0;
75 
76  std::FILE *_file;
77 
78  /** Acquire exclusive access to this object.
79  */
80  virtual void lock()
81  {
82  // Stub
83  }
84 
85  /** Release exclusive access to this object.
86  */
87  virtual void unlock()
88  {
89  // Stub
90  }
91 };
92 /**@}*/
93 
94 /**@}*/
95 } // namespace mbed
96 
97 #endif
98 
virtual off_t seek(off_t offset, int whence)
Move the file position to a given offset from from a given location.
File stream.
Definition: Stream.h:42
virtual ssize_t write(const void *buffer, size_t length)
Write the contents of a buffer to a file.
Prevents generation of copy constructor and copy assignment operator in derived classes.
Definition: NonCopyable.h:168
virtual int close()
Close a file.
virtual off_t tell()
Get the file position of the file.
virtual void rewind()
Rewind the file position to the beginning of the file.
virtual int isatty()
Check if the file in an interactive terminal device.
virtual int sync()
Flush any buffers associated with the file.
Class FileLike.
Definition: FileLike.h:39
virtual void unlock()
Release exclusive access to this object.
Definition: Stream.h:87
virtual ssize_t read(void *buffer, size_t length)
Read the contents of a file into a buffer.
virtual void lock()
Acquire exclusive access to this object.
Definition: Stream.h:80
virtual off_t size()
Get the size of the file.
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.