File System mit einen Argument

Dependencies:   mbed

Committer:
schoeni_91
Date:
Mon Nov 14 17:07:21 2016 +0000
Revision:
0:6791a518728e
File System mit einen Argument

Who changed what in which revision?

UserRevisionLine numberNew contents of line
schoeni_91 0:6791a518728e 1 #include "parser.h"
schoeni_91 0:6791a518728e 2 #include <cctype>
schoeni_91 0:6791a518728e 3
schoeni_91 0:6791a518728e 4 // functions
schoeni_91 0:6791a518728e 5 char* normalizeStr(char* theArr) {
schoeni_91 0:6791a518728e 6 int i=0;
schoeni_91 0:6791a518728e 7 string myStr, bestStr;
schoeni_91 0:6791a518728e 8 myStr.assign(theArr);
schoeni_91 0:6791a518728e 9 while (myStr[i]) { // replace all white spaces into spaces (0x20)
schoeni_91 0:6791a518728e 10 if (isspace(myStr[i]))
schoeni_91 0:6791a518728e 11 myStr[i]=' ';
schoeni_91 0:6791a518728e 12 i++;
schoeni_91 0:6791a518728e 13 }
schoeni_91 0:6791a518728e 14 myStr.erase(0, myStr.find_first_not_of(" ")); // remove leading spaces
schoeni_91 0:6791a518728e 15 myStr.erase(myStr.find_last_not_of(" ")+1); // remove trailing spaces
schoeni_91 0:6791a518728e 16 strcpy(theArr, myStr.c_str());
schoeni_91 0:6791a518728e 17 int x = 0;
schoeni_91 0:6791a518728e 18 for (i=0; i<=myStr.length(); i++) {
schoeni_91 0:6791a518728e 19 theArr[x] = myStr[i];
schoeni_91 0:6791a518728e 20 if (x>=1) {
schoeni_91 0:6791a518728e 21 if ((theArr[x] == ' ') && (theArr[x-1] == ' ')) // reduce several spaces to one space
schoeni_91 0:6791a518728e 22 x--;
schoeni_91 0:6791a518728e 23 if ((theArr[x] == ';') && (theArr[x-1] == ' ')) // delete every space before ';'
schoeni_91 0:6791a518728e 24 theArr[--x] = ';';
schoeni_91 0:6791a518728e 25 }
schoeni_91 0:6791a518728e 26 x++;
schoeni_91 0:6791a518728e 27 if (theArr[x-1] == ';')
schoeni_91 0:6791a518728e 28 break; // ignore everything after ';'
schoeni_91 0:6791a518728e 29 }
schoeni_91 0:6791a518728e 30 theArr[x] = '\0';
schoeni_91 0:6791a518728e 31 if (x>1) {
schoeni_91 0:6791a518728e 32 if ((theArr[0] == '/') && (theArr[1] == '/'))
schoeni_91 0:6791a518728e 33 theArr[0] = 0; // ignore comments
schoeni_91 0:6791a518728e 34 }
schoeni_91 0:6791a518728e 35 return theArr;
schoeni_91 0:6791a518728e 36 }
schoeni_91 0:6791a518728e 37
schoeni_91 0:6791a518728e 38 bool getArg(int16_t myCom[], string myItem) { // keyword specific argument handling
schoeni_91 0:6791a518728e 39 bool done=true;
schoeni_91 0:6791a518728e 40 switch (myCom[0]) {
schoeni_91 0:6791a518728e 41 case 1: // LEDS
schoeni_91 0:6791a518728e 42 if (myItem[0] == 'E') myCom[1]=0x0F;
schoeni_91 0:6791a518728e 43 else if (myItem[0] == 'A') myCom[1]=0;
schoeni_91 0:6791a518728e 44 else myCom[1] = (int16_t)std::strtol(myItem.c_str(), NULL, 10);
schoeni_91 0:6791a518728e 45 break;
schoeni_91 0:6791a518728e 46 case 2: // WAIT
schoeni_91 0:6791a518728e 47 case 3: // REPEAT
schoeni_91 0:6791a518728e 48 case 4: // GOTO
schoeni_91 0:6791a518728e 49 myCom[1] = (int16_t)std::strtol(myItem.c_str(), NULL, 10);
schoeni_91 0:6791a518728e 50 break;
schoeni_91 0:6791a518728e 51
schoeni_91 0:6791a518728e 52 case 5: //END
schoeni_91 0:6791a518728e 53 break;
schoeni_91 0:6791a518728e 54 case 6: //NOP
schoeni_91 0:6791a518728e 55 break;
schoeni_91 0:6791a518728e 56 case 7:
schoeni_91 0:6791a518728e 57 myCom[1] = (int16_t)std::strtol(myItem.c_str(), NULL, 10);//TONE
schoeni_91 0:6791a518728e 58 break;
schoeni_91 0:6791a518728e 59
schoeni_91 0:6791a518728e 60
schoeni_91 0:6791a518728e 61 default: myCom[1]= myCom[0]*-1; done=false; break;
schoeni_91 0:6791a518728e 62 }
schoeni_91 0:6791a518728e 63 // printf("... getArg: myItem=<%s>; %s %d\r\n", myItem.c_str(), keywords[myCom[0]].c_str(), myCom[1]);
schoeni_91 0:6791a518728e 64 return done;
schoeni_91 0:6791a518728e 65 }
schoeni_91 0:6791a518728e 66
schoeni_91 0:6791a518728e 67 bool parseLine(string line, int16_t command[]) { // parse a normalized command line
schoeni_91 0:6791a518728e 68 bool done = false;
schoeni_91 0:6791a518728e 69 int i=0;
schoeni_91 0:6791a518728e 70
schoeni_91 0:6791a518728e 71 size_t pos = line.find_first_of(" ;"); // find first ' ' or ';' in string
schoeni_91 0:6791a518728e 72 string theItem = line.substr(0, pos);
schoeni_91 0:6791a518728e 73 for (i=1; i <= validKeyWords; i++) {
schoeni_91 0:6791a518728e 74 if (keywords[i].compare(theItem)==0) { // compare keyword with keywords in list (array)
schoeni_91 0:6791a518728e 75 command[0] = i; // store no representation of the found keyword in command[0]
schoeni_91 0:6791a518728e 76 if (line[pos] == ';') {
schoeni_91 0:6791a518728e 77 done = true;
schoeni_91 0:6791a518728e 78 command[1] = -101;
schoeni_91 0:6791a518728e 79 }
schoeni_91 0:6791a518728e 80 else {
schoeni_91 0:6791a518728e 81 line.erase(0, line.find_first_of(" ")+1); // remove keyword from string
schoeni_91 0:6791a518728e 82 pos = line.find_first_of(";\r"); // error tolerant: if line is not completed with ';'; --> '\r\n' .. end of line
schoeni_91 0:6791a518728e 83 if (pos >0) {
schoeni_91 0:6791a518728e 84 theItem = line.substr(0, pos);
schoeni_91 0:6791a518728e 85 done = getArg(command, theItem); // handling of a second arument depending on the keyword
schoeni_91 0:6791a518728e 86 }
schoeni_91 0:6791a518728e 87 else {
schoeni_91 0:6791a518728e 88 command[1] = -102;
schoeni_91 0:6791a518728e 89 done = false;
schoeni_91 0:6791a518728e 90 }
schoeni_91 0:6791a518728e 91 }
schoeni_91 0:6791a518728e 92 break; // keyword found
schoeni_91 0:6791a518728e 93 }
schoeni_91 0:6791a518728e 94 } // for
schoeni_91 0:6791a518728e 95 return done;
schoeni_91 0:6791a518728e 96 }