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-16
Revision:
0:22618cf06f45
Child:
1:5731f31f96be

File content as of revision 0:22618cf06f45:

#include "mbed.h"
#include "TextLCD.h"
#include "SDFileSystem.h"
#include <stdio.h>
#include <string>
#include <stdlib.h>
#include <vector>
using std::string;


//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) {
    
    vector<string> filenames;
    DIR *dp;
    struct dirent *dirp;
    dp = opendir(dir);
  //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(string fileNames[], 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, ".");

        // if the file is a .txt file, then save it to the array
        if (strncmp(fileExtension,"txt", 3) == 0) {   
            fileNames[n] = fileName;
            n++;
        }
    }
    
    return n; //Return the number of txt files that were found in the directory   
}

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

void getNextLine(FILE  *selectedFile) {
    
    char newLine[MAX_LINE_LENGTH];
    fgets(newLine, MAX_LINE_LENGTH, selectedFile);

    lineData.fullLine = newLine;    
    lineData.lineNumber++;    
    
    //Create a copy of the line, so that we don't effect the full line when we're extracting the words
    char cpyLine[MAX_LINE_LENGTH];
    strcpy(cpyLine, newLine);
    
    char *tempWord;
    int numWords = 0;
    tempWord = strtok(cpyLine, " ");
    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);
    /*
    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(5);*/
}