SDFileSystem, slightly modified for the ExoController
Dependents: Data-Management-Honka
Fork of SDFileSystem_HelloWorld by
SDFile.cpp@6:0fdda2533593, 2015-02-20 (annotated)
- Committer:
- mzling
- Date:
- Fri Feb 20 20:10:14 2015 +0000
- Revision:
- 6:0fdda2533593
- Parent:
- 5:3f554866f226
- Child:
- 7:51c9391e6c4c
Superficial changes to print statements.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mzling | 2:ec4d7e5fa68e | 1 | #include "mbed.h" |
mzling | 2:ec4d7e5fa68e | 2 | #include "SDFileSystem.h" |
mzling | 2:ec4d7e5fa68e | 3 | #include "SDFile.h" |
mzling | 2:ec4d7e5fa68e | 4 | #include "errno.h" |
mzling | 4:99e9c9e0dfb0 | 5 | #include "initDatabed.h" |
mzling | 2:ec4d7e5fa68e | 6 | |
mzling | 2:ec4d7e5fa68e | 7 | |
mzling | 4:99e9c9e0dfb0 | 8 | SDFile::SDFile(string path, string filename, bool readOnly) |
mzling | 3:8f5903a77a13 | 9 | { |
mzling | 2:ec4d7e5fa68e | 10 | //Creates the necessary directory |
mzling | 5:3f554866f226 | 11 | //printf("Creating SDFile object\r\n"); |
mzling | 3:8f5903a77a13 | 12 | char *a = new char[path.size()+1]; |
mzling | 3:8f5903a77a13 | 13 | a[path.size()] = 0; |
mzling | 2:ec4d7e5fa68e | 14 | memcpy(a,path.c_str(),path.size()); |
mzling | 2:ec4d7e5fa68e | 15 | //Calculates the full filename, then creates the file |
mzling | 2:ec4d7e5fa68e | 16 | std::string fullname = path + filename; |
mzling | 2:ec4d7e5fa68e | 17 | |
mzling | 3:8f5903a77a13 | 18 | char *b = new char[fullname.size()+1]; |
mzling | 3:8f5903a77a13 | 19 | b[fullname.size()] = 0; |
mzling | 3:8f5903a77a13 | 20 | memcpy(b,fullname.c_str(),fullname.size()); |
mzling | 4:99e9c9e0dfb0 | 21 | if (readOnly) { |
mzling | 4:99e9c9e0dfb0 | 22 | //printf("Opening readonly file\r\n"); |
mzling | 4:99e9c9e0dfb0 | 23 | //_fp = fopen((const char*)b, "r"); |
mzling | 4:99e9c9e0dfb0 | 24 | } else { |
mzling | 5:3f554866f226 | 25 | //printf("Opening writable file\r\n"); |
mzling | 4:99e9c9e0dfb0 | 26 | _fp = fopen((const char*)b, "w+"); |
mzling | 4:99e9c9e0dfb0 | 27 | } |
mzling | 6:0fdda2533593 | 28 | printf("fp is %d\r\n", _fp); |
mzling | 2:ec4d7e5fa68e | 29 | } |
mzling | 2:ec4d7e5fa68e | 30 | |
mzling | 3:8f5903a77a13 | 31 | /** |
mzling | 3:8f5903a77a13 | 32 | * This function reads bytes from the SDFile object. |
mzling | 3:8f5903a77a13 | 33 | * @author Michael Ling |
mzling | 3:8f5903a77a13 | 34 | * @param length The number of bytes to read |
mzling | 3:8f5903a77a13 | 35 | * @param array The int array the bytes are copied to |
mzling | 3:8f5903a77a13 | 36 | * @date 2/2/2015 |
mzling | 3:8f5903a77a13 | 37 | */ |
mzling | 3:8f5903a77a13 | 38 | int* SDFile::read(int length, int *array) |
mzling | 3:8f5903a77a13 | 39 | { |
mzling | 2:ec4d7e5fa68e | 40 | |
mzling | 2:ec4d7e5fa68e | 41 | //shift to the end of the file and go back accounting for the commas, spaces, \n, and \r (6 places per data) |
mzling | 3:8f5903a77a13 | 42 | fseek(_fp, -6*length, SEEK_END); |
mzling | 2:ec4d7e5fa68e | 43 | //cycle through the length of the vector and read the values. |
mzling | 3:8f5903a77a13 | 44 | for(int i = 0; i < length; i++) { |
mzling | 3:8f5903a77a13 | 45 | fscanf(_fp, "%x, ", &array[i]); |
mzling | 2:ec4d7e5fa68e | 46 | } |
mzling | 2:ec4d7e5fa68e | 47 | |
mzling | 2:ec4d7e5fa68e | 48 | return array; |
mzling | 2:ec4d7e5fa68e | 49 | } |
mzling | 2:ec4d7e5fa68e | 50 | |
mzling | 4:99e9c9e0dfb0 | 51 | int* SDFile::read_from_start(int length, int *array) { |
mzling | 4:99e9c9e0dfb0 | 52 | fseek(_fp, 0, SEEK_SET); |
mzling | 4:99e9c9e0dfb0 | 53 | for(int i = 0; i < length; i++) { |
mzling | 4:99e9c9e0dfb0 | 54 | fscanf(_fp, "%x, ", &array[i]); |
mzling | 4:99e9c9e0dfb0 | 55 | } |
mzling | 4:99e9c9e0dfb0 | 56 | |
mzling | 4:99e9c9e0dfb0 | 57 | return array; |
mzling | 4:99e9c9e0dfb0 | 58 | } |
mzling | 4:99e9c9e0dfb0 | 59 | |
mzling | 3:8f5903a77a13 | 60 | /** |
mzling | 3:8f5903a77a13 | 61 | * This function writes from an array to the file pointed to by fp |
mzling | 3:8f5903a77a13 | 62 | * @param length length of data to write |
mzling | 3:8f5903a77a13 | 63 | * @param array array to draw written data from |
mzling | 3:8f5903a77a13 | 64 | * @author Michael Ling |
mzling | 3:8f5903a77a13 | 65 | * @date 2/2/2015 |
mzling | 3:8f5903a77a13 | 66 | */ |
mzling | 3:8f5903a77a13 | 67 | void SDFile::write(int length, int *array) |
mzling | 3:8f5903a77a13 | 68 | { |
mzling | 5:3f554866f226 | 69 | //printf("Seeking START...\r\n"); |
mzling | 3:8f5903a77a13 | 70 | fseek(_fp, 0, SEEK_SET); |
mzling | 5:3f554866f226 | 71 | //printf("Starting to write\r\n"); |
mzling | 3:8f5903a77a13 | 72 | for(int i = 0; i < length-1; i++) { |
mzling | 4:99e9c9e0dfb0 | 73 | /* This line fails when run on testFile, reason unknown...*/ |
mzling | 5:3f554866f226 | 74 | //printf("%d\r\n", fprintf(_fp, "%04x, ", array[i])); |
mzling | 5:3f554866f226 | 75 | fprintf(_fp, "%04x, ", array[i]); |
mzling | 4:99e9c9e0dfb0 | 76 | } |
mzling | 5:3f554866f226 | 77 | // printf("%d\r\n", fprintf(_fp, "%04x\r\n", array[length-1])); |
mzling | 5:3f554866f226 | 78 | fprintf(_fp, "%04x\r\n", array[length-1]); |
mzling | 4:99e9c9e0dfb0 | 79 | } |
mzling | 4:99e9c9e0dfb0 | 80 | |
mzling | 4:99e9c9e0dfb0 | 81 | void SDFile::append(int length, int *array) { |
mzling | 4:99e9c9e0dfb0 | 82 | fseek(_fp, 0, SEEK_END); |
mzling | 4:99e9c9e0dfb0 | 83 | for(int i = 0; i < length-1; i++) { |
mzling | 4:99e9c9e0dfb0 | 84 | |
mzling | 3:8f5903a77a13 | 85 | fprintf(_fp, "%04x, ", array[i]); |
mzling | 2:ec4d7e5fa68e | 86 | } |
mzling | 3:8f5903a77a13 | 87 | fprintf(_fp, "%04x\r\n", array[length-1]); |
mzling | 2:ec4d7e5fa68e | 88 | } |
mzling | 4:99e9c9e0dfb0 | 89 | /* |
mzling | 4:99e9c9e0dfb0 | 90 | void SDFile::changeAccess(string path, string filename) { |
mzling | 4:99e9c9e0dfb0 | 91 | fclose(_fp); |
mzling | 4:99e9c9e0dfb0 | 92 | char *a = new char[path.size()+1]; |
mzling | 4:99e9c9e0dfb0 | 93 | a[path.size()] = 0; |
mzling | 4:99e9c9e0dfb0 | 94 | memcpy(a,path.c_str(),path.size()); |
mzling | 4:99e9c9e0dfb0 | 95 | //Calculates the full filename, then creates the file |
mzling | 4:99e9c9e0dfb0 | 96 | std::string fullname = path + filename; |
mzling | 4:99e9c9e0dfb0 | 97 | |
mzling | 4:99e9c9e0dfb0 | 98 | char *b = new char[fullname.size()+1]; |
mzling | 4:99e9c9e0dfb0 | 99 | |
mzling | 4:99e9c9e0dfb0 | 100 | b[fullname.size()] = 0; |
mzling | 4:99e9c9e0dfb0 | 101 | memcpy(b,fullname.c_str(),fullname.size()); |
mzling | 4:99e9c9e0dfb0 | 102 | printf("Using name %s\r\n", b); |
mzling | 4:99e9c9e0dfb0 | 103 | _fp = fopen((const char *)b, "r+"); |
mzling | 4:99e9c9e0dfb0 | 104 | printf("new FP is %d\r\n", _fp); |
mzling | 4:99e9c9e0dfb0 | 105 | }*/ |
mzling | 4:99e9c9e0dfb0 | 106 | |
mzling | 4:99e9c9e0dfb0 | 107 | void SDFile::open_for_read(string path, string filename) { |
mzling | 4:99e9c9e0dfb0 | 108 | //Creates the necessary directory |
mzling | 5:3f554866f226 | 109 | //printf("Trying to open for read\r\n"); |
mzling | 4:99e9c9e0dfb0 | 110 | char *a = new char[path.size()+1]; |
mzling | 4:99e9c9e0dfb0 | 111 | a[path.size()] = 0; |
mzling | 4:99e9c9e0dfb0 | 112 | memcpy(a,path.c_str(),path.size()); |
mzling | 4:99e9c9e0dfb0 | 113 | //Calculates the full filename, then creates the file |
mzling | 4:99e9c9e0dfb0 | 114 | std::string fullname = path + filename; |
mzling | 4:99e9c9e0dfb0 | 115 | |
mzling | 4:99e9c9e0dfb0 | 116 | char *b = new char[fullname.size()+1]; |
mzling | 4:99e9c9e0dfb0 | 117 | b[fullname.size()] = 0; |
mzling | 4:99e9c9e0dfb0 | 118 | memcpy(b,fullname.c_str(),fullname.size()); |
mzling | 4:99e9c9e0dfb0 | 119 | _fp = fopen((const char*)b, "r"); |
mzling | 4:99e9c9e0dfb0 | 120 | |
mzling | 5:3f554866f226 | 121 | //printf("fp is %d\r\n", _fp); |
mzling | 4:99e9c9e0dfb0 | 122 | } |
mzling | 4:99e9c9e0dfb0 | 123 | |
mzling | 4:99e9c9e0dfb0 | 124 | void SDFile::open_for_write(string path, string filename) { |
mzling | 4:99e9c9e0dfb0 | 125 | |
mzling | 4:99e9c9e0dfb0 | 126 | //Creates the necessary directory |
mzling | 5:3f554866f226 | 127 | |
mzling | 4:99e9c9e0dfb0 | 128 | char *a = new char[path.size()+1]; |
mzling | 4:99e9c9e0dfb0 | 129 | a[path.size()] = 0; |
mzling | 4:99e9c9e0dfb0 | 130 | memcpy(a,path.c_str(),path.size()); |
mzling | 4:99e9c9e0dfb0 | 131 | //Calculates the full filename, then creates the file |
mzling | 4:99e9c9e0dfb0 | 132 | std::string fullname = path + filename; |
mzling | 4:99e9c9e0dfb0 | 133 | |
mzling | 4:99e9c9e0dfb0 | 134 | char *b = new char[fullname.size()+1]; |
mzling | 4:99e9c9e0dfb0 | 135 | b[fullname.size()] = 0; |
mzling | 4:99e9c9e0dfb0 | 136 | memcpy(b,fullname.c_str(),fullname.size()); |
mzling | 4:99e9c9e0dfb0 | 137 | _fp = fopen((const char*)b, "w+"); |
mzling | 4:99e9c9e0dfb0 | 138 | |
mzling | 5:3f554866f226 | 139 | //printf("fp is %d\r\n", _fp); |
mzling | 4:99e9c9e0dfb0 | 140 | } |
mzling | 4:99e9c9e0dfb0 | 141 | |
mzling | 4:99e9c9e0dfb0 | 142 | void SDFile::close() { |
mzling | 4:99e9c9e0dfb0 | 143 | fclose(_fp); |
mzling | 4:99e9c9e0dfb0 | 144 | } |