mbed2 zad 5b
Dependencies: LCD_DISCO_F429ZI mbed BSP_DISCO_F429ZI
Dekodowanie/Tokeny/Tokeny.cpp@4:7855d3ab4c15, 2020-06-08 (annotated)
- Committer:
- krzysiek99
- Date:
- Mon Jun 08 21:49:36 2020 +0000
- Revision:
- 4:7855d3ab4c15
- Child:
- 5:cafab5a4d1c9
mbed2 zad 5b
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
krzysiek99 | 4:7855d3ab4c15 | 1 | #include "Tokeny.h" |
krzysiek99 | 4:7855d3ab4c15 | 2 | |
krzysiek99 | 4:7855d3ab4c15 | 3 | enum Result{OK, ERROR}; |
krzysiek99 | 4:7855d3ab4c15 | 4 | |
krzysiek99 | 4:7855d3ab4c15 | 5 | extern unsigned char ucTokenNr; |
krzysiek99 | 4:7855d3ab4c15 | 6 | extern struct Token asToken[MAX_TOKEN_NR]; |
krzysiek99 | 4:7855d3ab4c15 | 7 | extern struct Keyword asKeywordList[MAX_KEYWORD_NR]; |
krzysiek99 | 4:7855d3ab4c15 | 8 | |
krzysiek99 | 4:7855d3ab4c15 | 9 | |
krzysiek99 | 4:7855d3ab4c15 | 10 | void ReplaceCharactersInString(char pcString[], char cOldChar, char cNewChar) |
krzysiek99 | 4:7855d3ab4c15 | 11 | { |
krzysiek99 | 4:7855d3ab4c15 | 12 | char *pcStringCharacters = pcString; |
krzysiek99 | 4:7855d3ab4c15 | 13 | pcStringCharacters = strchr(pcStringCharacters, cOldChar); |
krzysiek99 | 4:7855d3ab4c15 | 14 | while(pcStringCharacters != NULL) |
krzysiek99 | 4:7855d3ab4c15 | 15 | { |
krzysiek99 | 4:7855d3ab4c15 | 16 | *pcStringCharacters = cNewChar; |
krzysiek99 | 4:7855d3ab4c15 | 17 | pcStringCharacters = strchr(pcStringCharacters+1, cOldChar); |
krzysiek99 | 4:7855d3ab4c15 | 18 | } |
krzysiek99 | 4:7855d3ab4c15 | 19 | } |
krzysiek99 | 4:7855d3ab4c15 | 20 | |
krzysiek99 | 4:7855d3ab4c15 | 21 | enum Result eHexStringToUInt(char pcStr[], unsigned int *puiValue) |
krzysiek99 | 4:7855d3ab4c15 | 22 | { |
krzysiek99 | 4:7855d3ab4c15 | 23 | char *pcEnd; |
krzysiek99 | 4:7855d3ab4c15 | 24 | unsigned char ucCharCounter; |
krzysiek99 | 4:7855d3ab4c15 | 25 | if((strncmp(pcStr, "0x", 2) != 0) ||(strlen(pcStr)) != 6) |
krzysiek99 | 4:7855d3ab4c15 | 26 | { |
krzysiek99 | 4:7855d3ab4c15 | 27 | return ERROR; |
krzysiek99 | 4:7855d3ab4c15 | 28 | } |
krzysiek99 | 4:7855d3ab4c15 | 29 | for(ucCharCounter = 2; pcStr[ucCharCounter] != NULL; ucCharCounter++) |
krzysiek99 | 4:7855d3ab4c15 | 30 | { |
krzysiek99 | 4:7855d3ab4c15 | 31 | if((strncmp(pcStr+ucCharCounter, "0", 1) < 0) || (strncmp(pcStr+ucCharCounter, "F", 1) > 0) || ((strncmp(pcStr+ucCharCounter, "9", 1) > 0) && (strncmp(pcStr+ucCharCounter, "A", 1) < 0))) |
krzysiek99 | 4:7855d3ab4c15 | 32 | { |
krzysiek99 | 4:7855d3ab4c15 | 33 | return ERROR; |
krzysiek99 | 4:7855d3ab4c15 | 34 | } |
krzysiek99 | 4:7855d3ab4c15 | 35 | } |
krzysiek99 | 4:7855d3ab4c15 | 36 | *puiValue = strtol(pcStr, &pcEnd, 0); |
krzysiek99 | 4:7855d3ab4c15 | 37 | |
krzysiek99 | 4:7855d3ab4c15 | 38 | return OK; |
krzysiek99 | 4:7855d3ab4c15 | 39 | } |
krzysiek99 | 4:7855d3ab4c15 | 40 | |
krzysiek99 | 4:7855d3ab4c15 | 41 | unsigned char ucFindTokensInString (char *pcString) |
krzysiek99 | 4:7855d3ab4c15 | 42 | { |
krzysiek99 | 4:7855d3ab4c15 | 43 | char *pcTokenStart = pcString; |
krzysiek99 | 4:7855d3ab4c15 | 44 | ucTokenNr = 0; |
krzysiek99 | 4:7855d3ab4c15 | 45 | pcTokenStart = strtok(pcString, " "); |
krzysiek99 | 4:7855d3ab4c15 | 46 | while((pcTokenStart != NULL) && (ucTokenNr < MAX_TOKEN_NR)) |
krzysiek99 | 4:7855d3ab4c15 | 47 | { |
krzysiek99 | 4:7855d3ab4c15 | 48 | asToken[ucTokenNr].uValue.pcString = pcTokenStart; |
krzysiek99 | 4:7855d3ab4c15 | 49 | ucTokenNr++; |
krzysiek99 | 4:7855d3ab4c15 | 50 | pcTokenStart = strchr(pcTokenStart, ' '); |
krzysiek99 | 4:7855d3ab4c15 | 51 | pcTokenStart = strtok(pcTokenStart, " "); |
krzysiek99 | 4:7855d3ab4c15 | 52 | } |
krzysiek99 | 4:7855d3ab4c15 | 53 | |
krzysiek99 | 4:7855d3ab4c15 | 54 | return ucTokenNr; |
krzysiek99 | 4:7855d3ab4c15 | 55 | } |
krzysiek99 | 4:7855d3ab4c15 | 56 | |
krzysiek99 | 4:7855d3ab4c15 | 57 | enum Result eStringToKeyword (char pcStr[], enum KeywordCode *peKeywordCode) |
krzysiek99 | 4:7855d3ab4c15 | 58 | { |
krzysiek99 | 4:7855d3ab4c15 | 59 | unsigned char ucKeywordListCounter; |
krzysiek99 | 4:7855d3ab4c15 | 60 | |
krzysiek99 | 4:7855d3ab4c15 | 61 | for(ucKeywordListCounter = 0; ucKeywordListCounter < MAX_KEYWORD_NR; ucKeywordListCounter++) |
krzysiek99 | 4:7855d3ab4c15 | 62 | { |
krzysiek99 | 4:7855d3ab4c15 | 63 | if(strcmp(pcStr, asKeywordList[ucKeywordListCounter].cString) == 0) |
krzysiek99 | 4:7855d3ab4c15 | 64 | { |
krzysiek99 | 4:7855d3ab4c15 | 65 | *peKeywordCode = asKeywordList[ucKeywordListCounter].eCode; |
krzysiek99 | 4:7855d3ab4c15 | 66 | return OK; |
krzysiek99 | 4:7855d3ab4c15 | 67 | } |
krzysiek99 | 4:7855d3ab4c15 | 68 | } |
krzysiek99 | 4:7855d3ab4c15 | 69 | return ERROR; |
krzysiek99 | 4:7855d3ab4c15 | 70 | } |
krzysiek99 | 4:7855d3ab4c15 | 71 | |
krzysiek99 | 4:7855d3ab4c15 | 72 | void DecodeTokens(void) |
krzysiek99 | 4:7855d3ab4c15 | 73 | { |
krzysiek99 | 4:7855d3ab4c15 | 74 | unsigned char ucTokenListCounter; |
krzysiek99 | 4:7855d3ab4c15 | 75 | struct Token *TokenValue; |
krzysiek99 | 4:7855d3ab4c15 | 76 | |
krzysiek99 | 4:7855d3ab4c15 | 77 | for(ucTokenListCounter = 0; ucTokenListCounter < ucTokenNr; ucTokenListCounter++) |
krzysiek99 | 4:7855d3ab4c15 | 78 | { |
krzysiek99 | 4:7855d3ab4c15 | 79 | TokenValue = &asToken[ucTokenListCounter]; |
krzysiek99 | 4:7855d3ab4c15 | 80 | if( eStringToKeyword(TokenValue->uValue.pcString, &TokenValue->uValue.eKeyword) == OK ) |
krzysiek99 | 4:7855d3ab4c15 | 81 | { |
krzysiek99 | 4:7855d3ab4c15 | 82 | TokenValue->eType = KEYWORD; |
krzysiek99 | 4:7855d3ab4c15 | 83 | } |
krzysiek99 | 4:7855d3ab4c15 | 84 | else if( eHexStringToUInt(TokenValue->uValue.pcString, &TokenValue->uValue.uiNumber) == OK ) |
krzysiek99 | 4:7855d3ab4c15 | 85 | { |
krzysiek99 | 4:7855d3ab4c15 | 86 | TokenValue->eType = NUMBER; |
krzysiek99 | 4:7855d3ab4c15 | 87 | } |
krzysiek99 | 4:7855d3ab4c15 | 88 | else |
krzysiek99 | 4:7855d3ab4c15 | 89 | { |
krzysiek99 | 4:7855d3ab4c15 | 90 | TokenValue->eType = STRING; |
krzysiek99 | 4:7855d3ab4c15 | 91 | } |
krzysiek99 | 4:7855d3ab4c15 | 92 | } |
krzysiek99 | 4:7855d3ab4c15 | 93 | } |
krzysiek99 | 4:7855d3ab4c15 | 94 | |
krzysiek99 | 4:7855d3ab4c15 | 95 | void DecodeMsg(char *pcString) |
krzysiek99 | 4:7855d3ab4c15 | 96 | { |
krzysiek99 | 4:7855d3ab4c15 | 97 | ucFindTokensInString(pcString); |
krzysiek99 | 4:7855d3ab4c15 | 98 | DecodeTokens(); |
krzysiek99 | 4:7855d3ab4c15 | 99 | } |
krzysiek99 | 4:7855d3ab4c15 | 100 | |
krzysiek99 | 4:7855d3ab4c15 | 101 |