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

Revision:
2:3e7baa3e3fec
Parent:
1:5731f31f96be
Child:
5:e36e0538a903
--- a/TextFile.h	Tue Sep 16 18:27:41 2014 +0000
+++ b/TextFile.h	Thu Sep 18 23:38:11 2014 +0000
@@ -1,3 +1,6 @@
+#ifndef TEXTFILE_HPP
+#define TEXTFILE_HPP
+
 #include "mbed.h"
 #include "TextLCD.h"
 #include "SDFileSystem.h"
@@ -7,10 +10,17 @@
 #include <vector>
 using std::string;
 
+#include <iostream>
+#include <istream>
+#include <ostream>
+#include <iterator>
+#include <sstream>
+
+
 
 //vector<string> filenames; //filenames are stored in a vector string
 
-vector<string> readFileNames(char *);
+vector<string> readFileNames(char []);
 int getFileNamesWithoutExt(string[], vector<string> &);
 string getPreviousLine(istream &);
 
@@ -40,28 +50,29 @@
 /***                      <getFileNamesWithoutExt>                          ***/
 /******************************************************************************/
  // save filename strings to array from vector using an iterator
-int getFileNamesWithoutExt(string fileNames[], vector<string> &filenames) {
+int getFileNamesWithoutExt(vector<string> &textFiles, vector<string> filenames) {
     
     //Cycle through all files listed in the directoy (strings in the vector list)
     int n = 0;
     for(vector<string>::iterator it=filenames.begin(); it < filenames.end(); it++)  
     {
-        //Convert the Vector found String to a char for use in the strtok function
-        char temp[50];  
-        if((*it).size() < 100)
-            strcpy(temp, (*it).c_str());
-            
-        // Retrieve the fileName and fileExtension by splitting the string
-        char *fileName;
-        char *fileExtension;
-        fileName = strtok(temp, ".");
-        fileExtension = strtok(NULL, ".");
+        
+        vector<string> fileName; //filename[0] = filename, filename[1] = extension
+
+        stringstream ss;
+        ss << (*it);
 
-        // if the file is a .txt file, then save it to the array
-        if (strncmp(fileExtension,"txt", 3) == 0) {   
-            fileNames[n] = fileName;
+        //parse the array based on a '.' delimeter, effectively getting the file name and extension
+        string item;
+        while (getline(ss, item, '.')) {
+            fileName.push_back(item);
+        }
+
+        // if the fileName vector has two items (a name and an extension) and is a .txt file, then save it to the array
+        if (fileName.size() == 2 && fileName[1].compare("txt") == 0) {  
+            textFiles.push_back(fileName[0]);
             n++;
-        }
+        }    
     }
     
     return n; //Return the number of txt files that were found in the directory   
@@ -72,45 +83,44 @@
 /******************************************************************************/
 //
 
-void getNextLine(FILE  *selectedFile) {
+void getNextLine(FILE *selectedFile, Line &lineData) {
     
     lineData.lineAddress = ftell(selectedFile);
     
-    char *newLine;
-    newLine = new char[MAX_LINE_LENGTH];
+    //char *newLine;
+    //getline(selectedFile, newLine);
+    char newLine[MAX_LINE_LENGTH];
     
     fgets(newLine, MAX_LINE_LENGTH, selectedFile);
     
-    lineData.fullLine = newLine;    
-    lineData.lineNumber++;    
+    string tempString(newLine);
+    //lineData.fullLine = newLine;    
+    lineData.lineNumber++;       
+    
+    stringstream newLineStrm(newLine);
+    //string tempWord = strtok(cpyLine, " ");
     
-    //Create a copy of the line, so that we don't effect the full line when we're extracting the words
-    char *cpyLine;
-    cpyLine = new char[MAX_LINE_LENGTH];
-    strcpy(cpyLine, newLine);
-
-    char *tempWord;
-    tempWord = new char;
+    istream_iterator<string> it(newLineStrm);
+    istream_iterator<string> end;
+    vector<string> results(it, end);
+    copy(results.begin(), results.end(), lineData.word);
     
-    int numWords = 0;
+    lineData.numWords = results.size(); //Record the number of words in the line
     
-    tempWord = strtok(cpyLine, " ");
+    results.erase(results.begin(), results.end());
+    
+    /*
+    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, " ");
-    }
+    }*/
     
     
     
-    lineData.numWords = numWords; //Record the number of words in the line
-    
     lcd.setAddress(0,1);
     lcd.printf("Current Line#: %d ", lineData.lineNumber);
-
-    delete[] tempWord;
-    delete[] newLine;
-    delete[] cpyLine;
     
     /*
     lcd.cls(); //clear the display   
@@ -126,9 +136,9 @@
 }
 
 
+#endif 
 
 
 
 
 
-