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  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef MBED_TESTFILEHANDLE_H
17 #define MBED_TESTFILEHANDLE_H
18 
19 #include "platform/FileHandle.h"
20 
21 #define POS_IS_VALID(pos) (pos >= 0 && pos < _end)
22 #define NEW_POS_IS_VALID(pos) (pos >= 0 && pos < (int32_t)FILE_SIZE)
23 #define SEEK_POS_IS_VALID(pos) (pos >= 0 && pos <= _end)
24 #define INVALID_POS (-1)
25 
26 template<uint32_t FILE_SIZE>
27 class TestFile : public mbed::FileHandle {
28 public:
29  TestFile(): _pos(0), _end(0) {}
30  ~TestFile() {}
31 
32  enum FunctionName {
33  fnNone, fnRead, fnWrite, fnSeek, fnClose, fnIsatty, fnTruncate
34  };
35 
36  virtual ssize_t read(void *buffer, size_t size)
37  {
38  ssize_t read;
39  _fnCalled = fnRead;
40 
41  for (read = 0; (size_t)read < size; read++) {
42  if (POS_IS_VALID(_pos)) {
43  ((uint8_t *)buffer)[read] = _data[_pos++];
44  } else {
45  break;
46  }
47  } // for
48  return read;
49  }
50 
51  virtual ssize_t write(const void *buffer, size_t size)
52  {
53  ssize_t written;
54  _fnCalled = fnWrite;
55 
56  for (written = 0; (size_t)written < size; written++) {
57  if (NEW_POS_IS_VALID(_pos)) {
58  _data[_pos++] = ((uint8_t *)buffer)[written];
59  } else {
60  if (0 == written) {
61  return -ENOSPC;
62  }
63  break;
64  }
65  if (_end < _pos) {
66  _end++;
67  }
68  } // for
69  return written;
70  }
71 
72  virtual off_t seek(off_t offset, int whence)
73  {
74  _fnCalled = fnSeek;
75  int32_t new_pos = INVALID_POS;
76 
77  switch (whence) {
78  case SEEK_SET:
79  new_pos = offset;
80  break;
81 
82  case SEEK_CUR:
83  new_pos = _pos + offset;
84  break;
85 
86  case SEEK_END:
87  new_pos = _end - offset;
88  break;
89 
90  default:
91  // nothing todo
92  break;
93  }
94 
95  if (SEEK_POS_IS_VALID(new_pos)) {
96  _pos = new_pos;
97  } else {
98  return -EINVAL;
99  }
100 
101  return _pos;
102  }
103 
104  virtual int close()
105  {
106  _fnCalled = fnClose;
107  return 0;
108  }
109 
110  virtual off_t size()
111  {
112  return _end;
113  }
114 
115  virtual int truncate(off_t length)
116  {
117  _fnCalled = fnTruncate;
118  if (!NEW_POS_IS_VALID(length)) {
119  return -EINVAL;
120  }
121  while (_end < length) {
122  _data[_end++] = 0;
123  }
124  _end = length;
125  return 0;
126  }
127 
128 
129  static void resetFunctionCallHistory()
130  {
131  _fnCalled = fnNone;
132  }
133 
134  static bool functionCalled(FunctionName name)
135  {
136  return (name == _fnCalled);
137  }
138 
139  static FunctionName getFunctionCalled()
140  {
141  return _fnCalled;
142  }
143 
144 private:
145 
146  // stores last function call name
147  static FunctionName _fnCalled;
148 
149  // file storage
150  uint8_t _data[FILE_SIZE];
151 
152  int32_t _pos, _end;
153 };
154 
155 template<uint32_t FILE_SIZE>
156 typename TestFile<FILE_SIZE>::FunctionName TestFile<FILE_SIZE>::_fnCalled;
157 
158 
159 #endif // MBED_TESTFILEHANDLE_H
virtual int truncate(off_t length)
Truncate or extend a file.
Definition: TestFile.h:115
virtual ssize_t read(void *buffer, size_t size)
Read the contents of a file into a buffer.
Definition: TestFile.h:36
virtual ssize_t write(const void *buffer, size_t size)
Write the contents of a buffer to a file.
Definition: TestFile.h:51
virtual int close()
Close a file.
Definition: TestFile.h:104
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:72
virtual off_t size()
Get the size of the file.
Definition: TestFile.h:110
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.