Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FATFileHandle.cpp 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* mbed Microcontroller Library
  2. * Copyright (c) 2006-2012 ARM Limited
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. #include "ff.h"
  23. #include "ffconf.h"
  24. #include "mbed_debug.h"
  25. #include "FATFileHandle.h"
  26. FATFileHandle::FATFileHandle(FIL fh) {
  27. _fh = fh;
  28. }
  29. int FATFileHandle::close() {
  30. int retval = f_close(&_fh);
  31. delete this;
  32. return retval;
  33. }
  34. ssize_t FATFileHandle::write(const void* buffer, size_t length) {
  35. UINT n;
  36. FRESULT res = f_write(&_fh, buffer, length, &n);
  37. if (res) {
  38. debug_if(FFS_DBG, "f_write() failed: %d", res);
  39. return -1;
  40. }
  41. return n;
  42. }
  43. ssize_t FATFileHandle::read(void* buffer, size_t length) {
  44. debug_if(FFS_DBG, "read(%d)\n", length);
  45. UINT n;
  46. FRESULT res = f_read(&_fh, buffer, length, &n);
  47. if (res) {
  48. debug_if(FFS_DBG, "f_read() failed: %d\n", res);
  49. return -1;
  50. }
  51. return n;
  52. }
  53. int FATFileHandle::isatty() {
  54. return 0;
  55. }
  56. off_t FATFileHandle::lseek(off_t position, int whence) {
  57. if (whence == SEEK_END) {
  58. position += _fh.fsize;
  59. } else if(whence==SEEK_CUR) {
  60. position += _fh.fptr;
  61. }
  62. FRESULT res = f_lseek(&_fh, position);
  63. if (res) {
  64. debug_if(FFS_DBG, "lseek failed: %d\n", res);
  65. return -1;
  66. } else {
  67. debug_if(FFS_DBG, "lseek OK, returning %i\n", _fh.fptr);
  68. return _fh.fptr;
  69. }
  70. }
  71. int FATFileHandle::fsync() {
  72. FRESULT res = f_sync(&_fh);
  73. if (res) {
  74. debug_if(FFS_DBG, "f_sync() failed: %d\n", res);
  75. return -1;
  76. }
  77. return 0;
  78. }
  79. off_t FATFileHandle::flen() {
  80. return _fh.fsize;
  81. }