SDFileSystem, slightly modified for the ExoController
Dependents: Data-Management-Honka
Fork of SDFileSystem_HelloWorld by
Diff: SDFile.cpp
- Revision:
- 4:99e9c9e0dfb0
- Parent:
- 3:8f5903a77a13
- Child:
- 5:3f554866f226
diff -r 8f5903a77a13 -r 99e9c9e0dfb0 SDFile.cpp --- a/SDFile.cpp Wed Feb 04 23:58:12 2015 +0000 +++ b/SDFile.cpp Sat Feb 14 00:36:44 2015 +0000 @@ -2,9 +2,10 @@ #include "SDFileSystem.h" #include "SDFile.h" #include "errno.h" +#include "initDatabed.h" -SDFile::SDFile(string path, string filename) +SDFile::SDFile(string path, string filename, bool readOnly) { //Creates the necessary directory printf("Creating SDFile object\r\n"); @@ -17,10 +18,14 @@ char *b = new char[fullname.size()+1]; b[fullname.size()] = 0; memcpy(b,fullname.c_str(),fullname.size()); - _fp = fopen((const char*)b, "w+"); - printf("fp is %d\r\n", _fp); - - + if (readOnly) { + //printf("Opening readonly file\r\n"); + //_fp = fopen((const char*)b, "r"); + } else { + printf("Opening writable file\r\n"); + _fp = fopen((const char*)b, "w+"); + } + printf("fp is %d\r\n", _fp); } /** @@ -43,6 +48,15 @@ return array; } +int* SDFile::read_from_start(int length, int *array) { + fseek(_fp, 0, SEEK_SET); + for(int i = 0; i < length; i++) { + fscanf(_fp, "%x, ", &array[i]); + } + + return array; +} + /** * This function writes from an array to the file pointed to by fp * @param length length of data to write @@ -52,10 +66,77 @@ */ void SDFile::write(int length, int *array) { + printf("Seeking START...\r\n"); fseek(_fp, 0, SEEK_SET); + printf("Starting to write\r\n"); for(int i = 0; i < length-1; i++) { + /* This line fails when run on testFile, reason unknown...*/ + printf("%d\r\n", fprintf(_fp, "%04x, ", array[i])); + } + printf("%d\r\n", fprintf(_fp, "%04x\r\n", array[length-1])); +} + +void SDFile::append(int length, int *array) { + fseek(_fp, 0, SEEK_END); + for(int i = 0; i < length-1; i++) { + fprintf(_fp, "%04x, ", array[i]); } fprintf(_fp, "%04x\r\n", array[length-1]); } - \ No newline at end of file +/* +void SDFile::changeAccess(string path, string filename) { + fclose(_fp); + char *a = new char[path.size()+1]; + a[path.size()] = 0; + memcpy(a,path.c_str(),path.size()); + //Calculates the full filename, then creates the file + std::string fullname = path + filename; + + char *b = new char[fullname.size()+1]; + + b[fullname.size()] = 0; + memcpy(b,fullname.c_str(),fullname.size()); + printf("Using name %s\r\n", b); + _fp = fopen((const char *)b, "r+"); + printf("new FP is %d\r\n", _fp); +}*/ + +void SDFile::open_for_read(string path, string filename) { + //Creates the necessary directory + printf("Creating SDFile object\r\n"); + char *a = new char[path.size()+1]; + a[path.size()] = 0; + memcpy(a,path.c_str(),path.size()); + //Calculates the full filename, then creates the file + std::string fullname = path + filename; + + char *b = new char[fullname.size()+1]; + b[fullname.size()] = 0; + memcpy(b,fullname.c_str(),fullname.size()); + _fp = fopen((const char*)b, "r"); + + printf("fp is %d\r\n", _fp); +} + +void SDFile::open_for_write(string path, string filename) { + + //Creates the necessary directory + printf("Creating SDFile object\r\n"); + char *a = new char[path.size()+1]; + a[path.size()] = 0; + memcpy(a,path.c_str(),path.size()); + //Calculates the full filename, then creates the file + std::string fullname = path + filename; + + char *b = new char[fullname.size()+1]; + b[fullname.size()] = 0; + memcpy(b,fullname.c_str(),fullname.size()); + _fp = fopen((const char*)b, "w+"); + + printf("fp is %d\r\n", _fp); +} + +void SDFile::close() { + fclose(_fp); +} \ No newline at end of file