SdFile.cpp

Thu, 07 Jul 2016 12:23:34 +0200

author
mbayer
date
Thu, 07 Jul 2016 12:23:34 +0200
changeset 2
b373b0288715
parent 0
2c8ba1964db7
permissions
-rw-r--r--

added missing sanguino files

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

mercurial