Mistake on this page?
Report an issue in GitHub or email us
TestFile.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2018 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_TESTFILEHANDLE_H
18 #define MBED_TESTFILEHANDLE_H
19 
20 #include "platform/FileHandle.h"
21 
22 #define POS_IS_VALID(pos) (pos >= 0 && pos < _end)
23 #define NEW_POS_IS_VALID(pos) (pos >= 0 && pos < (int32_t)FILE_SIZE)
24 #define SEEK_POS_IS_VALID(pos) (pos >= 0 && pos <= _end)
25 #define INVALID_POS (-1)
26 
27 template<uint32_t FILE_SIZE>
28 class TestFile : public mbed::FileHandle {
29 public:
30  TestFile(): _pos(0), _end(0) {}
31  ~TestFile() {}
32 
33  enum FunctionName {
34  fnNone, fnRead, fnWrite, fnSeek, fnClose, fnIsatty, fnTruncate
35  };
36 
37  virtual ssize_t read(void *buffer, size_t size)
38  {
39  ssize_t read;
40  _fnCalled = fnRead;
41 
42  for (read = 0; (size_t)read < size; read++) {
43  if (POS_IS_VALID(_pos)) {
44  ((uint8_t *)buffer)[read] = _data[_pos++];
45  } else {
46  break;
47  }
48  } // for
49  return read;
50  }
51 
52  virtual ssize_t write(const void *buffer, size_t size)
53  {
54  ssize_t written;
55  _fnCalled = fnWrite;
56 
57  for (written = 0; (size_t)written < size; written++) {
58  if (NEW_POS_IS_VALID(_pos)) {
59  _data[_pos++] = ((uint8_t *)buffer)[written];
60  } else {
61  if (0 == written) {
62  return -ENOSPC;
63  }
64  break;
65  }
66  if (_end < _pos) {
67  _end++;
68  }
69  } // for
70  return written;
71  }
72 
73  virtual off_t seek(off_t offset, int whence)
74  {
75  _fnCalled = fnSeek;
76  int32_t new_pos = INVALID_POS;
77 
78  switch (whence) {
79  case SEEK_SET:
80  new_pos = offset;
81  break;
82 
83  case SEEK_CUR:
84  new_pos = _pos + offset;
85  break;
86 
87  case SEEK_END:
88  new_pos = _end - offset;
89  break;
90 
91  default:
92  // nothing todo
93  break;
94  }
95 
96  if (SEEK_POS_IS_VALID(new_pos)) {
97  _pos = new_pos;
98  } else {
99  return -EINVAL;
100  }
101 
102  return _pos;
103  }
104 
105  virtual int close()
106  {
107  _fnCalled = fnClose;
108  return 0;
109  }
110 
111  virtual off_t size()
112  {
113  return _end;
114  }
115 
116  virtual int truncate(off_t length)
117  {
118  _fnCalled = fnTruncate;
119  if (!NEW_POS_IS_VALID(length)) {
120  return -EINVAL;
121  }
122  while (_end < length) {
123  _data[_end++] = 0;
124  }
125  _end = length;
126  return 0;
127  }
128 
129 
130  static void resetFunctionCallHistory()
131  {
132  _fnCalled = fnNone;
133  }
134 
135  static bool functionCalled(FunctionName name)
136  {
137  return (name == _fnCalled);
138  }
139 
140  static FunctionName getFunctionCalled()
141  {
142  return _fnCalled;
143  }
144 
145 private:
146 
147  // stores last function call name
148  static FunctionName _fnCalled;
149 
150  // file storage
151  uint8_t _data[FILE_SIZE];
152 
153  int32_t _pos, _end;
154 };
155 
156 template<uint32_t FILE_SIZE>
157 typename TestFile<FILE_SIZE>::FunctionName TestFile<FILE_SIZE>::_fnCalled;
158 
159 
160 #endif // MBED_TESTFILEHANDLE_H
virtual int truncate(off_t length)
Truncate or extend a file.
Definition: TestFile.h:116
virtual ssize_t read(void *buffer, size_t size)
Read the contents of a file into a buffer.
Definition: TestFile.h:37
virtual ssize_t write(const void *buffer, size_t size)
Write the contents of a buffer to a file.
Definition: TestFile.h:52
virtual int close()
Close a file.
Definition: TestFile.h:105
Class FileHandle.
Definition: FileHandle.h:46
virtual off_t seek(off_t offset, int whence)
Move the file position to a given offset from from a given location.
Definition: TestFile.h:73
virtual off_t size()
Get the size of the file.
Definition: TestFile.h:111
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.