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

TextFile.h

Committer:
mehatfie
Date:
2014-09-23
Revision:
6:d1594fd2ec5a
Parent:
5:e36e0538a903
Child:
9:5a0c4c6e39c7

File content as of revision 6:d1594fd2ec5a:

#ifndef TEXTFILE_HPP
#define TEXTFILE_HPP

#include "mbed.h"
#include "TextLCD.h"
#include "SDFileSystem.h"
#include <stdio.h>
#include <string>
#include <stdlib.h>
#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 []);
int getFileNamesWithoutExt(string[], vector<string> &);
string getPreviousLine(istream &);



/******************************************************************************/
/***                           <readFileNames>                              ***/
/******************************************************************************/
// 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));
    }
    closedir(dp);
    return filenames;
}



/******************************************************************************/
/***                      <getFileNamesWithoutExt>                          ***/
/******************************************************************************/
 // save filename strings to array from vector using an iterator
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++)  
    {
        
        vector<string> fileName; //filename[0] = filename, filename[1] = extension

        stringstream ss;
        ss << (*it);

        //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   
}

/******************************************************************************/
/***                              <getNextLine>                             ***/
/******************************************************************************/
//

void getNextLine(FILE *selectedFile, LineData &lineData) {
    
    lineData.lineAddress = ftell(selectedFile);
    
    //char *newLine;
    //getline(selectedFile, newLine);
    char newLine[MAX_LINE_LENGTH];
    
    fgets(newLine, MAX_LINE_LENGTH, selectedFile);
    
    string tempString(newLine);
    //lineData.fullLine = newLine;    
    lineData.lineNumber++;       
    
    stringstream newLineStrm(newLine);
    //string tempWord = strtok(cpyLine, " ");
    
    istream_iterator<string> it(newLineStrm);
    istream_iterator<string> end;
    vector<string> results(it, end);
    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());
    
    /*
    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, " ");
    }*/
    
    
    
    lcd.setAddress(0,2);
    lcd.printf("Current Line#: %d ", lineData.lineNumber);
    
    lcd.setAddress(0,3);
    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]);
    
    /*
    lcd.cls(); //clear the display   
    lcd.setAddress(0,0);
    lcd.printf("wrd1: %s", lineData.word[0]);
    lcd.setAddress(0,1);
    lcd.printf("wrd2: %s", lineData.word[1]);
    lcd.setAddress(0,2);
    lcd.printf("wrd3: %s", lineData.word[2]);
    lcd.setAddress(0,3);
    lcd.printf("wrd4: %s", lineData.word[3]);
    wait(2);*/
}


#endif