daily pastebin goal
1%
SHARE
TWEET

Untitled

a guest Jan 16th, 2019 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Finite state machine for parsing XML tags of interest
  2. void RSSReader::runStateMachine(char ch) {
  3.  
  4.   switch (state) {
  5.     case INIT:
  6.       // Make buffers empty
  7.       memset(titleBuffer, 0, sizeof(titleBuffer));
  8.       memset(descBuffer, 0, sizeof(descBuffer));
  9.       memset(dateBuffer, 0, sizeof(dateBuffer));
  10.  
  11.       // Next state
  12.       state = CHK_START;
  13.       break;
  14.  
  15.     case CHK_START:
  16.       if (ch == '<') {
  17.         state = TEST_FIRST_CHAR;
  18.       }
  19.       break;
  20.  
  21.     case TEST_FIRST_CHAR:
  22.       if (ch == date[0]) {
  23.         dateIndex = 1;
  24.         state = CHK_DATE;
  25.       }
  26.       else if (ch == title[0]) {
  27.         titleIndex = 1;
  28.         state = CHK_TITLE;
  29.       } else if (ch == desc[0]) {
  30.         descIndex = 1;
  31.         state = CHK_DESC;
  32.       } else  {
  33.         state = CHK_START;
  34.       }
  35.       break;
  36.  
  37.     case CHK_DATE:
  38.       if (ch != date[dateIndex]) {
  39.         state = CHK_START;
  40.         break;
  41.       }
  42.       dateIndex++;
  43.       if (ch == '>') {
  44.         dateIndex = 0;
  45.         state = GET_DATE;
  46.       }
  47.       break;
  48.  
  49.     case CHK_TITLE:
  50.       if (ch != title[titleIndex]) {
  51.         state = CHK_START;
  52.         break;
  53.       }
  54.       titleIndex++;
  55.       if (ch == '>') {
  56.         titleIndex = 0;
  57.         state = GET_TITLE;
  58.       }
  59.       break;
  60.  
  61.     case CHK_DESC:
  62.       if (ch != desc[descIndex]) {
  63.         state = CHK_START;
  64.         break;
  65.       }
  66.       descIndex++;
  67.       if (ch == '>') {
  68.         descIndex = 0;
  69.         state = GET_DESC;
  70.       }
  71.       break;
  72.  
  73.     case GET_TITLE:
  74.       if (ch != '<') {
  75.         titleBuffer[titleIndex++] = ch;
  76.       } else  {
  77.         titleBuffer[titleIndex++] = '\0';
  78.         if ((strlen(titleBuffer) != 0) && (titleCallback != NULL)) {
  79.           titleCallback(titleBuffer);
  80.         }
  81.         state = CHK_START;
  82.       }
  83.       break;
  84.  
  85.     case GET_DESC:
  86.       if ((ch != '<') && (ch != '&')) {
  87.         descBuffer[descIndex++] = ch;
  88.       } else  {
  89.         descBuffer[descIndex++] = '\0';
  90.         if ((strlen(descBuffer) != 0) && (descCallback != NULL)) {
  91.           descCallback(descBuffer);
  92.         }
  93.         state = CHK_START;
  94.       }
  95.       break;
  96.  
  97.     case GET_DATE:
  98.       if (ch != '<') {
  99.         dateBuffer[dateIndex++] = ch;
  100.       } else  {
  101.         // Just return the date not the time
  102.         dateBuffer[17] = '\0';
  103.         if ((strlen(dateBuffer) != 0) && (dateCallback != NULL)) {
  104.           dateCallback(dateBuffer);
  105.         }
  106.         state = CHK_START;
  107.       }
  108.       break;
  109.   }
  110. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top