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.

84 lines
3.2KB

  1. /* Arduino SdFat Library
  2. * Copyright (C) 2012 by William Greiman
  3. *
  4. * This file is part of the Arduino SdFat Library
  5. *
  6. * This Library is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This Library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with the Arduino SdFat Library. If not, see
  18. * <http://www.gnu.org/licenses/>.
  19. */
  20. #include <SdFile.h>
  21. /** Create a file object and open it in the current working directory.
  22. *
  23. * \param[in] path A path with a valid 8.3 DOS name for a file to be opened.
  24. *
  25. * \param[in] oflag Values for \a oflag are constructed by a bitwise-inclusive
  26. * OR of open flags. see SdBaseFile::open(SdBaseFile*, const char*, uint8_t).
  27. */
  28. SdFile::SdFile(const char* path, uint8_t oflag) : SdBaseFile(path, oflag) {
  29. }
  30. //------------------------------------------------------------------------------
  31. /** Write data to an open file.
  32. *
  33. * \note Data is moved to the cache but may not be written to the
  34. * storage device until sync() is called.
  35. *
  36. * \param[in] buf Pointer to the location of the data to be written.
  37. *
  38. * \param[in] nbyte Number of bytes to write.
  39. *
  40. * \return For success write() returns the number of bytes written, always
  41. * \a nbyte. If an error occurs, write() returns -1. Possible errors
  42. * include write() is called before a file has been opened, write is called
  43. * for a read-only file, device is full, a corrupt file system or an I/O error.
  44. *
  45. */
  46. int SdFile::write(const void* buf, size_t nbyte) {
  47. return SdBaseFile::write(buf, nbyte);
  48. }
  49. //------------------------------------------------------------------------------
  50. /** Write a byte to a file. Required by the Arduino Print class.
  51. * \param[in] b the byte to be written.
  52. * Use getWriteError to check for errors.
  53. * \return 1 for success and 0 for failure.
  54. */
  55. size_t SdFile::write(uint8_t b) {
  56. return SdBaseFile::write(&b, 1) == 1 ? 1 : 0;
  57. }
  58. //------------------------------------------------------------------------------
  59. /** Write a string to a file. Used by the Arduino Print class.
  60. * \param[in] str Pointer to the string.
  61. * Use getWriteError to check for errors.
  62. * \return count of characters written for success or -1 for failure.
  63. */
  64. int SdFile::write(const char* str) {
  65. return SdBaseFile::write(str, strlen(str));
  66. }
  67. //------------------------------------------------------------------------------
  68. /** Write a PROGMEM string to a file.
  69. * \param[in] str Pointer to the PROGMEM string.
  70. * Use getWriteError to check for errors.
  71. */
  72. void SdFile::write_P(PGM_P str) {
  73. for (uint8_t c; (c = pgm_read_byte(str)); str++) write(c);
  74. }
  75. //------------------------------------------------------------------------------
  76. /** Write a PROGMEM string followed by CR/LF to a file.
  77. * \param[in] str Pointer to the PROGMEM string.
  78. * Use getWriteError to check for errors.
  79. */
  80. void SdFile::writeln_P(PGM_P str) {
  81. write_P(str);
  82. write_P(PSTR("\r\n"));
  83. }