
zoba
Dependencies: LCD_DISCO_F429ZI mbed BSP_DISCO_F429ZI
TokensHandling.cpp@1:4fd32c2e7975, 2020-06-09 (annotated)
- Committer:
- bolko
- Date:
- Tue Jun 09 11:20:57 2020 +0000
- Revision:
- 1:4fd32c2e7975
- Parent:
- 0:befccd954577
filip;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
filipksiezyc | 0:befccd954577 | 1 | #include "TokensHandling.h" |
filipksiezyc | 0:befccd954577 | 2 | #include "mbed.h" |
filipksiezyc | 0:befccd954577 | 3 | |
filipksiezyc | 0:befccd954577 | 4 | #define MAX_KEYWORD_NR 4 |
filipksiezyc | 0:befccd954577 | 5 | |
filipksiezyc | 0:befccd954577 | 6 | enum Result {OK, ERR}; |
filipksiezyc | 0:befccd954577 | 7 | |
filipksiezyc | 0:befccd954577 | 8 | struct Keyword{ |
filipksiezyc | 0:befccd954577 | 9 | enum KeywordCode eCode; |
filipksiezyc | 0:befccd954577 | 10 | char cString[MAX_KEYWORD_STRING_LTH]; |
filipksiezyc | 0:befccd954577 | 11 | }; |
filipksiezyc | 0:befccd954577 | 12 | |
filipksiezyc | 0:befccd954577 | 13 | struct Keyword asKeywordList[MAX_KEYWORD_NR]={ |
filipksiezyc | 0:befccd954577 | 14 | {CALLIB, "callib"}, |
filipksiezyc | 0:befccd954577 | 15 | {GOTO, "goto"}, |
filipksiezyc | 0:befccd954577 | 16 | {ID, "id"}, |
filipksiezyc | 0:befccd954577 | 17 | {STEP, "step"}, |
filipksiezyc | 0:befccd954577 | 18 | }; |
filipksiezyc | 0:befccd954577 | 19 | |
bolko | 1:4fd32c2e7975 | 20 | void TokensHandler::ReplaceCharactersInString(char pcString[],char cOldChar,char cNewChar){ |
bolko | 1:4fd32c2e7975 | 21 | char *pString = pcString; |
bolko | 1:4fd32c2e7975 | 22 | while((pString = strchr(pString,cOldChar)) != NULL){ |
bolko | 1:4fd32c2e7975 | 23 | *pString++ = cNewChar; |
bolko | 1:4fd32c2e7975 | 24 | } |
filipksiezyc | 0:befccd954577 | 25 | } |
filipksiezyc | 0:befccd954577 | 26 | |
filipksiezyc | 0:befccd954577 | 27 | |
filipksiezyc | 0:befccd954577 | 28 | unsigned char TokensHandler::ucFindTokensInString(char *pcString){ |
filipksiezyc | 0:befccd954577 | 29 | char *pcTokenString = pcString; |
filipksiezyc | 0:befccd954577 | 30 | pcTokenString = strtok(pcTokenString, " "); |
filipksiezyc | 0:befccd954577 | 31 | |
filipksiezyc | 0:befccd954577 | 32 | for(ucTokenNr=0;((pcTokenString != NULL) && (ucTokenNr < MAX_TOKEN_NR));ucTokenNr++) |
filipksiezyc | 0:befccd954577 | 33 | { |
filipksiezyc | 0:befccd954577 | 34 | asToken[ucTokenNr].uValue.pcString = pcTokenString; |
filipksiezyc | 0:befccd954577 | 35 | pcTokenString = strtok(NULL, " "); |
filipksiezyc | 0:befccd954577 | 36 | } |
filipksiezyc | 0:befccd954577 | 37 | return ucTokenNr; |
filipksiezyc | 0:befccd954577 | 38 | } |
filipksiezyc | 0:befccd954577 | 39 | |
filipksiezyc | 0:befccd954577 | 40 | |
filipksiezyc | 0:befccd954577 | 41 | |
filipksiezyc | 0:befccd954577 | 42 | enum Result TokensHandler::eStringToKeyword (char pcStr[],enum KeywordCode *peKeywordCode){ |
filipksiezyc | 0:befccd954577 | 43 | unsigned char ucKeywordCounter; |
filipksiezyc | 0:befccd954577 | 44 | |
filipksiezyc | 0:befccd954577 | 45 | for(ucKeywordCounter=0; ucKeywordCounter< MAX_KEYWORD_NR; ucKeywordCounter++){ |
filipksiezyc | 0:befccd954577 | 46 | |
filipksiezyc | 0:befccd954577 | 47 | if( (strcmp(pcStr, asKeywordList[ucKeywordCounter].cString))==0){ |
filipksiezyc | 0:befccd954577 | 48 | *peKeywordCode = asKeywordList[ucKeywordCounter].eCode; |
filipksiezyc | 0:befccd954577 | 49 | return OK; |
filipksiezyc | 0:befccd954577 | 50 | } |
filipksiezyc | 0:befccd954577 | 51 | } |
filipksiezyc | 0:befccd954577 | 52 | return ERR; |
filipksiezyc | 0:befccd954577 | 53 | } |
filipksiezyc | 0:befccd954577 | 54 | |
filipksiezyc | 0:befccd954577 | 55 | void TokensHandler::DecodeTokens(void){ |
filipksiezyc | 0:befccd954577 | 56 | unsigned char ucTokenCounter; |
filipksiezyc | 0:befccd954577 | 57 | struct Token *psCurrentToken; |
filipksiezyc | 0:befccd954577 | 58 | unsigned int uiTokenValue; |
filipksiezyc | 0:befccd954577 | 59 | enum KeywordCode eTokenCode; |
filipksiezyc | 0:befccd954577 | 60 | |
filipksiezyc | 0:befccd954577 | 61 | for(ucTokenCounter= 0; ucTokenCounter< ucTokenNr; ucTokenCounter++){ |
filipksiezyc | 0:befccd954577 | 62 | psCurrentToken= &asToken[ucTokenCounter]; |
bolko | 1:4fd32c2e7975 | 63 | if(OK== eStringToKeyword(psCurrentToken->uValue.pcString, &eTokenCode)){ |
bolko | 1:4fd32c2e7975 | 64 | psCurrentToken->eType=KEYWORD; |
bolko | 1:4fd32c2e7975 | 65 | psCurrentToken->uValue.eKeyword=eTokenCode; |
bolko | 1:4fd32c2e7975 | 66 | } |
bolko | 1:4fd32c2e7975 | 67 | else if(sscanf(asToken[ucTokenCounter].uValue.pcString, "%x", &uiTokenValue) == 1 ){ |
filipksiezyc | 0:befccd954577 | 68 | psCurrentToken->eType= NUMBER; |
filipksiezyc | 0:befccd954577 | 69 | psCurrentToken->uValue.uiNumber= uiTokenValue; |
filipksiezyc | 0:befccd954577 | 70 | } |
filipksiezyc | 0:befccd954577 | 71 | else{ |
filipksiezyc | 0:befccd954577 | 72 | psCurrentToken->eType=STRING; |
filipksiezyc | 0:befccd954577 | 73 | } |
filipksiezyc | 0:befccd954577 | 74 | } |
filipksiezyc | 0:befccd954577 | 75 | } |
filipksiezyc | 0:befccd954577 | 76 | |
filipksiezyc | 0:befccd954577 | 77 | void TokensHandler::DecodeMsg(char *pcString){ |
filipksiezyc | 0:befccd954577 | 78 | ucTokenNr= ucFindTokensInString(pcString); |
filipksiezyc | 0:befccd954577 | 79 | ReplaceCharactersInString(pcString, ' ', NULL); |
filipksiezyc | 0:befccd954577 | 80 | DecodeTokens(); |
filipksiezyc | 0:befccd954577 | 81 | } |
filipksiezyc | 0:befccd954577 | 82 | |
filipksiezyc | 0:befccd954577 | 83 | TokensHandler::TokensHandler(){ |
filipksiezyc | 0:befccd954577 | 84 | ucTokenNr=0; |
filipksiezyc | 0:befccd954577 | 85 | } |
filipksiezyc | 0:befccd954577 | 86 |