Component Test's Software to work with "Universal Controller Box" - Software is an interpreter or "compiler" for programs to be done with a .txt file and read off of the SD Card
Dependencies: BridgeDriver FrontPanelButtons MCP23017 SDFileSystem TextLCD mbed
Diff: TextFile.h
- Revision:
- 9:5a0c4c6e39c7
- Parent:
- 6:d1594fd2ec5a
- Child:
- 11:bc9cd2869f95
--- a/TextFile.h Wed Sep 24 01:46:02 2014 +0000
+++ b/TextFile.h Wed Sep 24 22:23:00 2014 +0000
@@ -32,26 +32,22 @@
// read file names into vector of strings
vector<string> readFileNames(char *dir) {
- lcd.setAddress(0,1);
- lcd.printf("TEST1");
-
vector<string> filenames;
DIR *dp;
struct dirent *dirp;
- lcd.setAddress(0,2);
- lcd.printf("TEST2");
-
+
dp = opendir(dir);
- lcd.setAddress(0,3);
- lcd.printf("TEST3");
-
- //read all directory and file names in current directory into filename vector
- while((dirp = readdir(dp)) != NULL) {
- filenames.push_back(string(dirp->d_name));
+ //if no directory was found, don't try and get the fileNames and return an empty vector of 0
+ if (dp != NULL){
+
+ //read all directory and file names in current directory into filename vector
+ while((dirp = readdir(dp)) != NULL) {
+ filenames.push_back(string(dirp->d_name));
+ }
+ closedir(dp);
}
- closedir(dp);
return filenames;
}
@@ -94,47 +90,48 @@
/******************************************************************************/
//
-void getNextLine(FILE *selectedFile, LineData &lineData) {
+int getNextLine(FILE *selectedFile, LineData &lineData) {
lineData.lineAddress = ftell(selectedFile);
+ if (lineData.lineAddress == -1L){
+ ErrorOut("Unable to get address of line, SD Card Removed?", lineData.lineNumber + 1);
+ return -1;
+ }
+
//char *newLine;
//getline(selectedFile, newLine);
char newLine[MAX_LINE_LENGTH];
fgets(newLine, MAX_LINE_LENGTH, selectedFile);
- string tempString(newLine);
- //lineData.fullLine = newLine;
- lineData.lineNumber++;
+ if (newLine == NULL){
+ ErrorOut("Unable to get the next line, SD Card Removed?", lineData.lineNumber + 1);
+ return -1;
+ }
- stringstream newLineStrm(newLine);
- //string tempWord = strtok(cpyLine, " ");
+ lineData.lineNumber++; // new line successfully found, increase lineNumber
+ //pull out each individual word (separated by any white space), and place it in the vector
+ stringstream newLineStrm(newLine);
istream_iterator<string> it(newLineStrm);
istream_iterator<string> end;
vector<string> results(it, end);
+
+ //copy the vector of results into the array of words
copy(results.begin(), results.end(), lineData.word);
lineData.numWords = results.size(); //Record the number of words in the line
- results.erase(results.begin(), results.end());
+ results.erase(results.begin(), results.end()); //remove the results vector from memory
- /*
- int numWords = 0;
- while (tempWord != NULL){
- strcpy(lineData.word[numWords], tempWord); //Record the word in the lineData array
- numWords++; //add the number of words first, since we've already got one word, this helps for the way the while loop works too
- tempWord = strtok(NULL, " ");
- }*/
-
-
-
+ //Update the Current Line Number
lcd.setAddress(0,2);
lcd.printf("Current Line#: %d ", lineData.lineNumber);
+ //Update the cmd/dvc label for the line so that the user has an idea of what's going on
lcd.setAddress(0,3);
- lcd.printf(" "); // Clear the Line using Spaces (Emptyness) - Note one line is 20 Characters
+ lcd.printf(" "); // Clear the Line using Spaces (Emptyness) - Note one line is 20 Characters
lcd.setAddress(0,3);
lcd.printf("cmd/dvc: %s", lineData.word[0]);